#include "String.StringView.hpp" #include "String.DString.hpp" #include "String.WString.hpp" #pragma once namespace ECOM::Utility::Convert { template constexpr bool dependent_false = false; template T To (const char * str) { static_assert (dependent_false ); } template T To (const wchar_t * str) { static_assert (dependent_false ); } template T To (const eSTR::DString &) { static_assert (dependent_false ); } template T To (const std::string &) { static_assert (dependent_false ); } //< for DString // 把未知长度的字符串转换成 DString template <> inline eSTR::DString To (const char * str) { return eSTR::DString { CV_DString (str) }; } template <> inline eSTR::DString To (const eSTR::DString & str) { return str; } template <> inline eSTR::DString To (const std::string & str) { return eSTR::DString {str }; } template <> inline eSTR::DString To (const wchar_t * str) { return eSTR::WString::From ({str}).ToDString (); } //> template T To (const eSTR::WString &) { static_assert (dependent_false ); } //< for WString // 把未知长度的字符串转换成 WString template <> inline eSTR::WString To (const char * str) { eSTR::DString ds { str }; return eSTR::WString (ds); } // 把未知长度的字符串转换成 WString template <> inline eSTR::WString To (const wchar_t * str) { return eSTR::WString::From ({ str }); } template <> inline eSTR::WString To (const eSTR::DString & str) { return eSTR::WString { str }; } template <> inline eSTR::WString To (const eSTR::WString & str) { return str; } //> #if 0 // 可用, 但是容易引起误解, 因此注释掉 // 如果不加参数, 默认就是 DString/WString inline eSTR::DString To (const char * str) { return To (str); } inline eSTR::WString To (const wchar_t * str) { return To (str); } #endif //< for int // 把未知长度的字符串转换成 int template <> inline int To (const char * str) { return CV_DString (str).Int (); } template <> inline int To (const eSTR::DString & str) { return str.Int (); } template <> inline int To (const std::string & str) { return CV_DString (str).Int (); } template <> inline int To (const wchar_t * str) { return CV_WString (str).Int (); } //> //< for long // 把未知长度的字符串转换成 long template <> inline long To (const char * str) { return CV_DString (str).Long (); } template <> inline long To (const eSTR::DString & str) { return str.Long (); } template <> inline long To (const std::string & str) { return CV_DString (str).Long (); } template <> inline long To (const wchar_t * str) { return CV_WString (str).Long (); } //> //< for double // 把未知长度的字符串转换成 long template <> inline double To (const char * str) { return CV_DString (str).Double (); } template <> inline double To (const eSTR::DString & str) { return str.Double (); } template <> inline double To (const std::string & str) { return CV_DString (str).Double (); } template <> inline double To (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;