RCFProto
 All Classes Functions Typedefs
Tchar.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_UTIL_TCHAR_HPP
20 #define INCLUDE_UTIL_TCHAR_HPP
21 
22 #include <string>
23 #include <vector>
24 
25 #if (defined(UNICODE) || defined(_UNICODE)) && !defined(BOOST_WINDOWS)
26 #error UNICODE and _UNICODE should only be defined for Windows builds.
27 #endif
28 
29 namespace RCF {
30 
31 #ifndef BOOST_NO_STD_WSTRING
32 
33 #ifdef _MSC_VER
34 #pragma warning( push )
35 #pragma warning( disable : 4996 ) // warning C4996: 'ctime' was declared deprecated
36 #endif
37 
38  inline std::wstring stringToWstring(const std::string &s)
39  {
40  std::wstring ws;
41  if (!s.empty())
42  {
43  const char *sz = s.c_str();
44  std::size_t szlen = s.length();
45  std::vector<wchar_t> vec(szlen);
46  wchar_t *wsz = &vec[0];
47  std::size_t wszlen = mbstowcs(wsz, sz, szlen);
48  if (wszlen == std::size_t(-1)) throw std::runtime_error("mbstowcs() failed");
49  ws.assign(wsz, wszlen);
50  }
51  return ws;
52  }
53 
54  inline std::string wstringToString(const std::wstring &ws)
55  {
56  std::string s;
57  if (!ws.empty())
58  {
59  const wchar_t *wsz = ws.c_str();
60  std::size_t wszlen = ws.length();
61  std::vector<char> vec(4*wszlen);
62  char *sz = &vec[0];
63  std::size_t szlen = wcstombs(sz, wsz, wszlen);
64  if (szlen == std::size_t(-1)) throw std::runtime_error("wcstombs() failed");
65  s.assign(sz, szlen);
66  }
67  return s;
68  }
69 
70 #ifdef _MSC_VER
71 #pragma warning( pop )
72 #endif
73 
74 #endif
75 
76 
77 #if !defined(BOOST_WINDOWS) || defined(UNICODE)
78 
79  #define RCF_T(x) L ## x
80  typedef std::wstring tstring;
81  inline tstring toTstring(std::string s) { return stringToWstring(s); }
82  inline tstring toTstring(std::wstring s) { return s; }
83  inline std::string toAstring(tstring s) { return wstringToString(s); }
84  inline std::wstring toWstring(tstring s) { return s; }
85 
86 #else
87 
88  #define RCF_T(x) x
89  typedef std::string tstring;
90  inline tstring toTstring(std::string s) { return s; }
91  inline tstring toTstring(std::wstring ws) { return wstringToString(ws); }
92  inline std::string toAstring(tstring s) { return s; }
93  inline std::wstring toWstring(tstring s) { return stringToWstring(s); }
94 
95 #endif
96 
97 } // namespace RCF
98 
99 #endif // ! INCLUDE_UTIL_TCHAR_HPP