123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #include "String.StringView.hpp"
- #include "String.DString.hpp"
- #include "String.WString.hpp"
- #pragma once
- namespace ECOM::Utility::Convert
- {
- template <typename> constexpr bool dependent_false = false;
- template <typename T>
- T To (const char * str)
- {
- static_assert (dependent_false <T>);
- }
- template <typename T>
- T To (const wchar_t * str)
- {
- static_assert (dependent_false <T>);
- }
- template <typename T>
- T To (const eSTR::DString &)
- {
- static_assert (dependent_false <T>);
- }
- template <typename T>
- T To (const std::string &)
- {
- static_assert (dependent_false <T>);
- }
- //< for DString
- // 把未知长度的字符串转换成 DString
- template <>
- inline eSTR::DString To <eSTR::DString> (const char * str)
- {
- return eSTR::DString { CV_DString (str) };
- }
- template <>
- inline eSTR::DString To <eSTR::DString> (const eSTR::DString & str)
- {
- return str;
- }
- template <>
- inline eSTR::DString To <eSTR::DString> (const std::string & str)
- {
- return eSTR::DString {str };
- }
- template <>
- inline eSTR::DString To <eSTR::DString> (const wchar_t * str)
- {
- return eSTR::WString::From ({str}).ToDString ();
- }
- //>
- template <typename T>
- T To (const eSTR::WString &)
- {
- static_assert (dependent_false <T>);
- }
- //< for WString
- // 把未知长度的字符串转换成 WString
- template <>
- inline eSTR::WString To <eSTR::WString> (const char * str)
- {
- eSTR::DString ds { str };
- return eSTR::WString (ds);
- }
- // 把未知长度的字符串转换成 WString
- template <>
- inline eSTR::WString To <eSTR::WString> (const wchar_t * str)
- {
- return eSTR::WString::From ({ str });
- }
- template <>
- inline eSTR::WString To <eSTR::WString> (const eSTR::DString & str)
- {
- return eSTR::WString { str };
- }
- template <>
- inline eSTR::WString To <eSTR::WString> (const eSTR::WString & str)
- {
- return str;
- }
- //>
- #if 0 // 可用, 但是容易引起误解, 因此注释掉
- // 如果不加参数, 默认就是 DString/WString
- inline eSTR::DString To (const char * str)
- {
- return To <eSTR::DString> (str);
- }
- inline eSTR::WString To (const wchar_t * str)
- {
- return To <eSTR::WString> (str);
- }
- #endif
- //< for int
- // 把未知长度的字符串转换成 int
- template <>
- inline int To <int> (const char * str)
- {
- return CV_DString (str).Int ();
- }
- template <>
- inline int To <int> (const eSTR::DString & str)
- {
- return str.Int ();
- }
- template <>
- inline int To <int> (const std::string & str)
- {
- return CV_DString (str).Int ();
- }
- template <>
- inline int To <int> (const wchar_t * str)
- {
- return CV_WString (str).Int ();
- }
- //>
- //< for long
- // 把未知长度的字符串转换成 long
- template <>
- inline long To <long> (const char * str)
- {
- return CV_DString (str).Long ();
- }
- template <>
- inline long To <long> (const eSTR::DString & str)
- {
- return str.Long ();
- }
- template <>
- inline long To <long> (const std::string & str)
- {
- return CV_DString (str).Long ();
- }
- template <>
- inline long To <long> (const wchar_t * str)
- {
- return CV_WString (str).Long ();
- }
- //>
- //< for double
- // 把未知长度的字符串转换成 long
- template <>
- inline double To <double> (const char * str)
- {
- return CV_DString (str).Double ();
- }
- template <>
- inline double To <double> (const eSTR::DString & str)
- {
- return str.Double ();
- }
- template <>
- inline double To <double> (const std::string & str)
- {
- return CV_DString (str).Double ();
- }
- template <>
- inline double To <double> (const wchar_t * str)
- {
- return CV_WString (str).Double ();
- }
- //>
- inline eSTR::DString & operator << (eSTR::DString & to, const char * from)
- {
- to << CV_String (from);
- return to;
- }
- inline eSTR::DString & operator << (eSTR::DString & to, const wchar_t * from)
- {
- eSTR::WString tmp { CV_WString (from) };
- to += tmp.ToDString ();
- return to;
- }
- inline eSTR::WString & operator << (eSTR::WString & to, const char * from)
- {
- to << CV_String (from);
- return to;
- }
- inline eSTR::WString & operator << (eSTR::WString & to, const wchar_t * from)
- {
- to << CV_WString (from);
- return to;
- }
- inline eSTR::DString operator + (const eSTR::DString & S1, const char * S2)
- {
- auto rc { S1 };
- rc << S2;
- return rc;
- }
- inline eSTR::WString operator + (const eSTR::WString & S1, const wchar_t * S2)
- {
- auto rc { S1 };
- rc << S2;
- return rc;
- }
- inline eSTR::WString operator + (const eSTR::WString & S1, const char * S2)
- {
- auto rc { S1 };
- rc << S2;
- return rc;
- }
- inline eSTR::DString operator + (const char * S1, const eSTR::DString & S2)
- {
- eSTR::DString rc { S1 };
- rc << S2;
- return rc;
- }
- inline eSTR::WString operator + (const wchar_t * S1, const eSTR::WString & S2)
- {
- eSTR::WString rc;
- rc << S1;
- rc << S2;
- return rc;
- }
- inline eSTR::WString operator + (const char * S1, const eSTR::WString & S2)
- {
- eSTR::WString rc;
- rc << S1;
- rc << S2;
- return rc;
- }
- class From
- {
- public:
- From () = delete;
- From (const char * S) : bPtr { S }, wPtr { nullptr } { assert (bPtr); }
- From (const wchar_t * S) : wPtr { S }, bPtr { nullptr } { assert (wPtr); }
- ~From () = default;
- private:
- const char * bPtr;
- const wchar_t * wPtr;
- public:
- inline operator eSTR::DString () const
- {
- if (bPtr)
- return eSTR::DString { bPtr };
- else
- {
- assert (wPtr);
- return eSTR::WString { CV_WString (wPtr) }.ToDString ();
- }
- }
- inline operator eSTR::WString () const
- {
- if (bPtr)
- return eSTR::WString { eSTR::DStringView (bPtr) };
- else
- {
- assert (wPtr);
- return eSTR::WString { CV_WString (wPtr) };
- }
- }
- };
- }
- // namespace CVT = ECOM::Utility::Convert;
|