wchar_t.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_DETAIL_TRAITS_WCHAR_T_HPP_
  6. #define BOOST_PROCESS_DETAIL_TRAITS_WCHAR_T_HPP_
  7. #include <boost/process/detail/traits/decl.hpp>
  8. #include <boost/process/detail/traits/cmd_or_exe.hpp>
  9. #include <boost/process/detail/traits/env.hpp>
  10. #include <boost/process/locale.hpp>
  11. namespace boost { namespace process { namespace detail {
  12. //template
  13. template<typename T> struct is_wchar_t : std::false_type {};
  14. template<> struct is_wchar_t<boost::filesystem::path> : std::is_same<typename boost::filesystem::path::value_type, wchar_t>
  15. {
  16. };
  17. template<> struct is_wchar_t<const wchar_t* > : std::true_type {};
  18. template<> struct is_wchar_t<wchar_t* > : std::true_type {};
  19. template<std::size_t Size> struct is_wchar_t<const wchar_t [Size]> : std::true_type {};
  20. template<std::size_t Size> struct is_wchar_t<const wchar_t (&)[Size]> : std::true_type {};
  21. template<> struct is_wchar_t<std::wstring> : std::true_type {};
  22. template<> struct is_wchar_t<std::vector<std::wstring>> : std::true_type {};
  23. template<> struct is_wchar_t<std::initializer_list<std::wstring>> : std::true_type {};
  24. template<> struct is_wchar_t<std::vector<wchar_t *>> : std::true_type {};
  25. template<> struct is_wchar_t<std::initializer_list<wchar_t *>> : std::true_type {};
  26. template<typename Char, typename T>
  27. struct char_converter
  28. {
  29. static T& conv(T & in)
  30. {
  31. return in;
  32. }
  33. static T&& conv(T&& in)
  34. {
  35. return std::move(in);
  36. }
  37. static const T& conv(const T & in)
  38. {
  39. return in;
  40. }
  41. };
  42. template<typename Char, typename T>
  43. using char_converter_t = char_converter<Char,
  44. typename std::remove_cv<typename std::remove_reference<T>::type>::type>;
  45. template<>
  46. struct char_converter<char, const wchar_t*>
  47. {
  48. static std::string conv(const wchar_t* in)
  49. {
  50. std::size_t size = 0;
  51. while (in[size] != L'\0') size++;
  52. return ::boost::process::detail::convert(in, in + size);
  53. }
  54. };
  55. template<>
  56. struct char_converter<char, wchar_t*>
  57. {
  58. static std::string conv(wchar_t* in)
  59. {
  60. std::size_t size = 0;
  61. while (in[size] != L'\0') size++;
  62. return ::boost::process::detail::convert(in, in + size);
  63. }
  64. };
  65. template<std::size_t Size>
  66. struct char_converter<char, wchar_t[Size]>
  67. {
  68. static std::string conv(const wchar_t(&in)[Size])
  69. {
  70. return ::boost::process::detail::convert(in, in + Size -1);
  71. }
  72. };
  73. template<>
  74. struct char_converter<wchar_t, const char*>
  75. {
  76. static std::wstring conv(const char* in)
  77. {
  78. std::size_t size = 0;
  79. while (in[size] != '\0') size++;
  80. return ::boost::process::detail::convert(in, in + size);
  81. }
  82. };
  83. template<>
  84. struct char_converter<wchar_t, char*>
  85. {
  86. static std::wstring conv(char* in)
  87. {
  88. std::size_t size = 0;
  89. while (in[size] != '\0') size++;
  90. return ::boost::process::detail::convert(in, in + size);
  91. }
  92. };
  93. template<std::size_t Size>
  94. struct char_converter<wchar_t, char[Size]>
  95. {
  96. static std::wstring conv(const char(&in)[Size])
  97. {
  98. return ::boost::process::detail::convert(in, in + Size -1);
  99. }
  100. };
  101. //all the containers.
  102. template<>
  103. struct char_converter<wchar_t, std::string>
  104. {
  105. static std::wstring conv(const std::string & in)
  106. {
  107. return ::boost::process::detail::convert(in);
  108. }
  109. };
  110. template<>
  111. struct char_converter<char, std::wstring>
  112. {
  113. static std::string conv(const std::wstring & in)
  114. {
  115. return ::boost::process::detail::convert(in);
  116. }
  117. };
  118. template<>
  119. struct char_converter<wchar_t, std::vector<std::string>>
  120. {
  121. static std::vector<std::wstring> conv(const std::vector<std::string> & in)
  122. {
  123. std::vector<std::wstring> ret(in.size());
  124. std::transform(in.begin(), in.end(), ret.begin(),
  125. [](const std::string & st)
  126. {
  127. return convert(st);
  128. });
  129. return ret;
  130. }
  131. };
  132. template<>
  133. struct char_converter<wchar_t, std::initializer_list<std::string>>
  134. {
  135. static std::vector<std::wstring> conv(const std::initializer_list<std::string> & in)
  136. {
  137. std::vector<std::wstring> ret(in.size());
  138. std::transform(in.begin(), in.end(), ret.begin(),
  139. [](const std::string & st)
  140. {
  141. return convert(st);
  142. });
  143. return ret;
  144. }
  145. };
  146. template<>
  147. struct char_converter<wchar_t, std::vector<char* >>
  148. {
  149. static std::vector<std::wstring> conv(const std::vector<char* > & in)
  150. {
  151. std::vector<std::wstring> ret(in.size());
  152. std::transform(in.begin(), in.end(), ret.begin(),
  153. [](const char* st)
  154. {
  155. std::size_t sz = 0;
  156. while (st[sz] != '\0') sz++;
  157. return convert(st, st + sz);
  158. });
  159. return ret;
  160. }
  161. };
  162. template<>
  163. struct char_converter<wchar_t, std::initializer_list<char *>>
  164. {
  165. static std::vector<std::wstring> conv(const std::initializer_list<char * > & in)
  166. {
  167. std::vector<std::wstring> ret(in.size());
  168. std::transform(in.begin(), in.end(), ret.begin(),
  169. [](const char* st)
  170. {
  171. std::size_t sz = 0;
  172. while (st[sz] != '\0') sz++;
  173. return convert(st, st + sz);
  174. });
  175. return ret;
  176. }
  177. };
  178. template<>
  179. struct char_converter<char, std::vector<std::wstring>>
  180. {
  181. static std::vector<std::string> conv(const std::vector<std::wstring> & in)
  182. {
  183. std::vector<std::string> ret(in.size());
  184. std::transform(in.begin(), in.end(), ret.begin(),
  185. [](const std::wstring & st)
  186. {
  187. return convert(st);
  188. });
  189. return ret;
  190. }
  191. };
  192. template<>
  193. struct char_converter<char, std::initializer_list<std::wstring>>
  194. {
  195. static std::vector<std::string> conv(const std::initializer_list<std::wstring> & in)
  196. {
  197. std::vector<std::string> ret(in.size());
  198. std::transform(in.begin(), in.end(), ret.begin(),
  199. [](const std::wstring & st)
  200. {
  201. return convert(st);
  202. });
  203. return ret;
  204. }
  205. };
  206. template<>
  207. struct char_converter<char, std::vector<wchar_t* >>
  208. {
  209. static std::vector<std::string> conv(const std::vector<wchar_t* > & in)
  210. {
  211. std::vector<std::string> ret(in.size());
  212. std::transform(in.begin(), in.end(), ret.begin(),
  213. [](const wchar_t* st)
  214. {
  215. std::size_t sz = 0;
  216. while (st[sz] != L'\0') sz++;
  217. return convert(st, st + sz);
  218. });
  219. return ret;
  220. }
  221. };
  222. template<>
  223. struct char_converter<char, std::initializer_list<wchar_t * >>
  224. {
  225. static std::vector<std::string> conv(const std::initializer_list<wchar_t *> & in)
  226. {
  227. std::vector<std::string> ret(in.size());
  228. std::transform(in.begin(), in.end(), ret.begin(),
  229. [](const wchar_t* st)
  230. {
  231. std::size_t sz = 0;
  232. while (st[sz] != L'\0') sz++;
  233. return convert(st, st + sz);
  234. });
  235. return ret;
  236. }
  237. };
  238. }}}
  239. #endif /* BOOST_PROCESS_DETAIL_TRAITS_WCHAR_T_HPP_ */