OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
WideToUTF8.h
Go to the documentation of this file.
1 
13 // Copyright 2015 Sensics, Inc.
14 //
15 // Licensed under the Apache License, Version 2.0 (the "License");
16 // you may not use this file except in compliance with the License.
17 // You may obtain a copy of the License at
18 //
19 // http://www.apache.org/licenses/LICENSE-2.0
20 //
21 // Unless required by applicable law or agreed to in writing, software
22 // distributed under the License is distributed on an "AS IS" BASIS,
23 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 // See the License for the specific language governing permissions and
25 // limitations under the License.
26 
27 #ifndef INCLUDED_WideToUTF8_h_GUID_A2C0AD0B_9DEB_4E84_5A3A_8517ED03AA32
28 #define INCLUDED_WideToUTF8_h_GUID_A2C0AD0B_9DEB_4E84_5A3A_8517ED03AA32
29 
30 #ifndef OSVR_HAVE_CODECVT
31 /* #undef OSVR_HAVE_CODECVT */
32 #endif
33 
34 #ifdef _WIN32
35 #define WIN32_LEAN_AND_MEAN
36 #include <tchar.h>
37 
38 #ifdef OSVR_HAVE_CODECVT
39 #include <locale>
40 #include <codecvt>
41 #else // !OSVR_HAVE_CODECVT
42 #include <boost/locale/encoding_utf.hpp>
43 #endif // OSVR_HAVE_CODECVT
44 
45 #include <string>
46 #include <type_traits>
47 
48 namespace osvr {
49 namespace util {
50 
51  template <typename T> inline std::string wideToUTF8String(T input) {
52 #ifdef OSVR_HAVE_CODECVT
53  std::wstring_convert<std::codecvt_utf8<wchar_t> > strCvt;
54  return strCvt.to_bytes(input);
55 #else // !OSVR_HAVE_CODECVT
56  return boost::locale::conv::utf_to_utf<char>(input);
57 #endif // OSVR_HAVE_CODECVT
58  }
59 
60  template <typename T> inline std::wstring utf8ToWideString(T input) {
61 #ifdef OSVR_HAVE_CODECVT
62  std::wstring_convert<std::codecvt_utf8<wchar_t> > strCvt;
63  return strCvt.from_bytes(input);
64 #else // !OSVR_HAVE_CODECVT
65  return boost::locale::conv::utf_to_utf<wchar_t>(input);
66 #endif // OSVR_HAVE_CODECVT
67  }
68 
69 #ifdef _UNICODE
70  inline std::string tcharToUTF8String(TCHAR buf[]) {
71  static_assert(std::is_same<TCHAR, wchar_t>::value,
72  "This code path relies on TCHAR being a wchar_t!");
73  return wideToUTF8String(buf);
74  }
75 #else // !_UNICODE
76  inline std::string tcharToUTF8String(TCHAR buf[]) {
77  static_assert(std::is_same<TCHAR, char>::value,
78  "This code path relies on TCHAR being a single byte!");
79  return std::string(buf);
80  }
81 #endif // _UNICODE
82 
83 } // namespace util
84 } // namespace osvr
85 
86 #endif // _WIN32
87 
88 #endif // INCLUDED_WideToUTF8_h_GUID_A2C0AD0B_9DEB_4E84_5A3A_8517ED03AA32