Utility.Convert.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "String.StringView.hpp"
  2. #include "String.DString.hpp"
  3. #include "String.WString.hpp"
  4. #pragma once
  5. namespace ECOM::Utility::Convert
  6. {
  7. template <typename> constexpr bool dependent_false = false;
  8. template <typename T>
  9. T To (const char * str)
  10. {
  11. static_assert (dependent_false <T>);
  12. }
  13. template <typename T>
  14. T To (const wchar_t * str)
  15. {
  16. static_assert (dependent_false <T>);
  17. }
  18. template <typename T>
  19. T To (const eSTR::DString &)
  20. {
  21. static_assert (dependent_false <T>);
  22. }
  23. template <typename T>
  24. T To (const std::string &)
  25. {
  26. static_assert (dependent_false <T>);
  27. }
  28. //< for DString
  29. // 把未知长度的字符串转换成 DString
  30. template <>
  31. inline eSTR::DString To <eSTR::DString> (const char * str)
  32. {
  33. return eSTR::DString { CV_DString (str) };
  34. }
  35. template <>
  36. inline eSTR::DString To <eSTR::DString> (const eSTR::DString & str)
  37. {
  38. return str;
  39. }
  40. template <>
  41. inline eSTR::DString To <eSTR::DString> (const std::string & str)
  42. {
  43. return eSTR::DString {str };
  44. }
  45. template <>
  46. inline eSTR::DString To <eSTR::DString> (const wchar_t * str)
  47. {
  48. return eSTR::WString::From ({str}).ToDString ();
  49. }
  50. //>
  51. template <typename T>
  52. T To (const eSTR::WString &)
  53. {
  54. static_assert (dependent_false <T>);
  55. }
  56. //< for WString
  57. // 把未知长度的字符串转换成 WString
  58. template <>
  59. inline eSTR::WString To <eSTR::WString> (const char * str)
  60. {
  61. eSTR::DString ds { str };
  62. return eSTR::WString (ds);
  63. }
  64. // 把未知长度的字符串转换成 WString
  65. template <>
  66. inline eSTR::WString To <eSTR::WString> (const wchar_t * str)
  67. {
  68. return eSTR::WString::From ({ str });
  69. }
  70. template <>
  71. inline eSTR::WString To <eSTR::WString> (const eSTR::DString & str)
  72. {
  73. return eSTR::WString { str };
  74. }
  75. template <>
  76. inline eSTR::WString To <eSTR::WString> (const eSTR::WString & str)
  77. {
  78. return str;
  79. }
  80. //>
  81. #if 0 // 可用, 但是容易引起误解, 因此注释掉
  82. // 如果不加参数, 默认就是 DString/WString
  83. inline eSTR::DString To (const char * str)
  84. {
  85. return To <eSTR::DString> (str);
  86. }
  87. inline eSTR::WString To (const wchar_t * str)
  88. {
  89. return To <eSTR::WString> (str);
  90. }
  91. #endif
  92. //< for int
  93. // 把未知长度的字符串转换成 int
  94. template <>
  95. inline int To <int> (const char * str)
  96. {
  97. return CV_DString (str).Int ();
  98. }
  99. template <>
  100. inline int To <int> (const eSTR::DString & str)
  101. {
  102. return str.Int ();
  103. }
  104. template <>
  105. inline int To <int> (const std::string & str)
  106. {
  107. return CV_DString (str).Int ();
  108. }
  109. template <>
  110. inline int To <int> (const wchar_t * str)
  111. {
  112. return CV_WString (str).Int ();
  113. }
  114. //>
  115. //< for long
  116. // 把未知长度的字符串转换成 long
  117. template <>
  118. inline long To <long> (const char * str)
  119. {
  120. return CV_DString (str).Long ();
  121. }
  122. template <>
  123. inline long To <long> (const eSTR::DString & str)
  124. {
  125. return str.Long ();
  126. }
  127. template <>
  128. inline long To <long> (const std::string & str)
  129. {
  130. return CV_DString (str).Long ();
  131. }
  132. template <>
  133. inline long To <long> (const wchar_t * str)
  134. {
  135. return CV_WString (str).Long ();
  136. }
  137. //>
  138. //< for double
  139. // 把未知长度的字符串转换成 long
  140. template <>
  141. inline double To <double> (const char * str)
  142. {
  143. return CV_DString (str).Double ();
  144. }
  145. template <>
  146. inline double To <double> (const eSTR::DString & str)
  147. {
  148. return str.Double ();
  149. }
  150. template <>
  151. inline double To <double> (const std::string & str)
  152. {
  153. return CV_DString (str).Double ();
  154. }
  155. template <>
  156. inline double To <double> (const wchar_t * str)
  157. {
  158. return CV_WString (str).Double ();
  159. }
  160. //>
  161. inline eSTR::DString & operator << (eSTR::DString & to, const char * from)
  162. {
  163. to << CV_String (from);
  164. return to;
  165. }
  166. inline eSTR::DString & operator << (eSTR::DString & to, const wchar_t * from)
  167. {
  168. eSTR::WString tmp { CV_WString (from) };
  169. to += tmp.ToDString ();
  170. return to;
  171. }
  172. inline eSTR::WString & operator << (eSTR::WString & to, const char * from)
  173. {
  174. to << CV_String (from);
  175. return to;
  176. }
  177. inline eSTR::WString & operator << (eSTR::WString & to, const wchar_t * from)
  178. {
  179. to << CV_WString (from);
  180. return to;
  181. }
  182. inline eSTR::DString operator + (const eSTR::DString & S1, const char * S2)
  183. {
  184. auto rc { S1 };
  185. rc << S2;
  186. return rc;
  187. }
  188. inline eSTR::WString operator + (const eSTR::WString & S1, const wchar_t * S2)
  189. {
  190. auto rc { S1 };
  191. rc << S2;
  192. return rc;
  193. }
  194. inline eSTR::WString operator + (const eSTR::WString & S1, const char * S2)
  195. {
  196. auto rc { S1 };
  197. rc << S2;
  198. return rc;
  199. }
  200. inline eSTR::DString operator + (const char * S1, const eSTR::DString & S2)
  201. {
  202. eSTR::DString rc { S1 };
  203. rc << S2;
  204. return rc;
  205. }
  206. inline eSTR::WString operator + (const wchar_t * S1, const eSTR::WString & S2)
  207. {
  208. eSTR::WString rc;
  209. rc << S1;
  210. rc << S2;
  211. return rc;
  212. }
  213. inline eSTR::WString operator + (const char * S1, const eSTR::WString & S2)
  214. {
  215. eSTR::WString rc;
  216. rc << S1;
  217. rc << S2;
  218. return rc;
  219. }
  220. class From
  221. {
  222. public:
  223. From () = delete;
  224. From (const char * S) : bPtr { S }, wPtr { nullptr } { assert (bPtr); }
  225. From (const wchar_t * S) : wPtr { S }, bPtr { nullptr } { assert (wPtr); }
  226. ~From () = default;
  227. private:
  228. const char * bPtr;
  229. const wchar_t * wPtr;
  230. public:
  231. inline operator eSTR::DString () const
  232. {
  233. if (bPtr)
  234. return eSTR::DString { bPtr };
  235. else
  236. {
  237. assert (wPtr);
  238. return eSTR::WString { CV_WString (wPtr) }.ToDString ();
  239. }
  240. }
  241. inline operator eSTR::WString () const
  242. {
  243. if (bPtr)
  244. return eSTR::WString { eSTR::DStringView (bPtr) };
  245. else
  246. {
  247. assert (wPtr);
  248. return eSTR::WString { CV_WString (wPtr) };
  249. }
  250. }
  251. };
  252. }
  253. // namespace CVT = ECOM::Utility::Convert;