123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #pragma once
- #include <vector>
- class DString;
- class WString;
- //-----------------------------------------------------------------------------
- // FastJsonEncoder
- //-----------------------------------------------------------------------------
- template <typename T>
- class FastJsonEncoder
- {
- protected:
- typedef const char * tPCTSTR;
- public:
- FastJsonEncoder ();
- FastJsonEncoder (const FastJsonEncoder & Encoder);
- FastJsonEncoder (FastJsonEncoder && Encoder);
- virtual ~FastJsonEncoder ();
- public:
- void Set (tPCTSTR Key, LPCSTR strValue);
- void Set (tPCTSTR Key, PCWSTR strValue);
- void Set (tPCTSTR Key, const DString & Value);
- void Set (tPCTSTR Key, const WString & Value);
- void Set (tPCTSTR Key, __int64 Value);
- void Set (tPCTSTR Key, UINT_PTR Value);
- void Set (tPCTSTR Key, DWORD Value);
- void Set (tPCTSTR Key, int Value);
- void Set (tPCTSTR Key, bool Value);
- void Set (tPCTSTR Key, void * ptr);
- void Set (tPCTSTR Key, float Value);
- void Set (tPCTSTR Key, double Value);
- void Set (tPCTSTR Key, const std::vector <T> & arValue);
- void Set (tPCTSTR Key, const std::vector <int> & arValue);
- void Set (tPCTSTR Key, const std::vector <FastJsonEncoder> & arValue);
- public:
- // 直接把数组转换成 JSON, 格式为 [ {...}, {...} ]
- static T ToStringOf (const std::vector <FastJsonEncoder> & arValue);
- public:
- bool Recode (LPCSTR String);
- bool Recode (PCWSTR String);
- public:
- T ToString () const;
- T ToStringOfElement () const;
- operator T () const { return ToString (); }
- void operator = (FastJsonEncoder && Encoder);
- void operator = (const FastJsonEncoder & Encoder);
- void CopyTo (FastJsonEncoder & toEncoder) const;
- void Swap (FastJsonEncoder & toEncoder);
- public:
- void Empty ();
- public:
- FastJsonEncoder & Encode (tPCTSTR Key, const T & value)
- {
- Set (Key, value);
- return (*this);
- }
- template <typename TT>
- FastJsonEncoder & Encode (tPCTSTR Key, TT value)
- {
- Set (Key, value);
- return (*this);
- }
- protected:
- std::vector <T> m_arMessage;
- protected:
- class Proxy
- {
- public:
- inline Proxy (FastJsonEncoder & Encoder, tPCTSTR Key) :
- m_Encoder (Encoder),
- m_Key (Key)
- {
- }
- inline Proxy & operator = (LPCSTR Value)
- {
- static_assert (std::is_base_of <DString, T>::value, "T must be derived from DString");
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (PCWSTR Value)
- {
- static_assert (std::is_base_of <WString, T>::value, "T must be derived from WString");
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (const DString & Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (const WString & Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (__int64 Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (UINT_PTR Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (DWORD Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (int Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (bool Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (void * ptr)
- {
- m_Encoder.Set (m_Key, ptr);
- return (*this);
- }
- inline Proxy & operator = (float Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (double Value)
- {
- m_Encoder.Set (m_Key, Value);
- return (*this);
- }
- inline Proxy & operator = (std::vector <T> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- inline Proxy & operator = (std::vector <int> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- inline Proxy & operator = (std::vector <FastJsonEncoder> & arValue)
- {
- m_Encoder.Set (m_Key, arValue);
- return (*this);
- }
- protected:
- tPCTSTR m_Key;
- FastJsonEncoder & m_Encoder;
- };
- public:
- Proxy operator [] (tPCTSTR Key)
- {
- return Proxy (*this, Key);
- }
- Proxy operator () (tPCTSTR Key)
- {
- return Proxy (*this, Key);
- }
- };
|