wchar_t.hpp 7.4 KB

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