#pragma once #include #include "DString.hpp" #include "WString.hpp" //#include "BSTTree.hpp" namespace ECOM { namespace Utility { class BSTTree; } } #ifndef _ServiceBus_DLL #define _ServiceBus_API _declspec(dllimport) #else #define _ServiceBus_API _declspec(dllexport) #endif namespace ECOM { namespace ServiceBus { #if 0 class _ServiceBus_API MessageDecoder { public: MessageDecoder (PCTSTR Message); MessageDecoder (const DString & Message); MessageDecoder (const WString & Message) : MessageDecoder (Message.ToDString (CP_UTF8)) { } virtual ~MessageDecoder (); DString GetString (PCTSTR Key) const; DString Get (PCTSTR Key, PCTSTR IfNotFound) const; __int64 Get (PCTSTR Key, __int64 IfNotFound = 0) const; unsigned __int64 Get (PCTSTR Key, unsigned __int64 IfNotFound = 0) const; int Get (PCTSTR Key, int IfNotFound = 0) const; unsigned int Get (PCTSTR Key, unsigned int IfNotFound = 0) const; bool Get (PCTSTR Key, bool IfNotFound = false) const; float Get (PCTSTR Key, float IfNotFound = false) const; double Get (PCTSTR Key, double IfNotFound = false) const; int GetArray (PCTSTR Key, std::vector * arString) const; int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector * arString) const; int GetArray (PCTSTR Key, std::vector * arString) const; int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector * arString) const; protected: DString m_Message; // 保留一个 Copy, 调试用 Json::Value * m_JsonRoot; public: class _ServiceBus_API KVMap { public: KVMap (const KVMap & KVMap); KVMap (const MessageDecoder & Decoder); KVMap (const MessageDecoder & Decoder, PCTSTR Key); KVMap (const KVMap & KVMap, PCTSTR Key); KVMap (const KVMap & KVMap, int Index); public: DString ToString () const; WString ToWString (UINT CodePage = CP_UTF8) const; DString ForceToString () const; DString ToStyledString () const; DString To (PCTSTR IfNotFound) const; __int64 To (__int64 IfNotFound = 0) const; unsigned __int64 To (unsigned __int64 IfNotFound = 0) const; int To (int IfNotFound = 0) const; unsigned int To (unsigned int IfNotFound = 0) const; long To (long IfNotFound = 0) const; bool To (bool IfNotFound = false) const; float To (float IfNotFound = 0) const; double To (double IfNotFound = 0) const; inline operator DString () const { return ToString (); } inline operator WString () const { return ToWString (); } inline operator void * () const { return (void *) To ((UINT_PTR) 0); } inline operator DWORD () const { return To (0); } inline operator __int64 () const { return To (__int64 (0)); } inline operator unsigned __int64 () const { return To (unsigned __int64 (0)); } inline operator int () const { return To (0); } inline operator unsigned int () const { return To (unsigned int(0)); } inline operator long () const { return To (0); } inline operator bool () const { return To (false); } inline operator float () const { return To (0.0f); } inline operator double () const { return To (0.0); } public: KVMap operator [] (int Index); // 数组, 用下标来访问 int Count () const; // 获取数组的数量 public: inline KVMap operator [] (PCTSTR Key) { return KVMap (*this, Key); } inline KVMap operator () (PCTSTR Key) { return KVMap (*this, Key); } protected: Json::Value * m_JsonValue; friend MessageDecoder; }; class KVMap_String : public KVMap { public: inline KVMap_String (MessageDecoder & Decoder, PCTSTR Key, PCTSTR DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator DString () const { DString out = To (m_DefValue); return out; } protected: PCTSTR m_DefValue; }; class KVMap_uint : public KVMap { public: inline KVMap_uint (MessageDecoder & Decoder, PCTSTR Key, unsigned int DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator unsigned int () const { unsigned int out = To (m_DefValue); return out; } protected: unsigned int m_DefValue; }; class KVMap_int : public KVMap { public: inline KVMap_int (MessageDecoder & Decoder, PCTSTR Key, int DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator int () const { int out = To (m_DefValue); return out; } inline operator unsigned int () const { unsigned int out = To (m_DefValue); return out; } inline operator unsigned short () const { unsigned short out = To (m_DefValue); return out; } protected: int m_DefValue; }; class KVMap_uint64 : public KVMap { public: inline KVMap_uint64 (MessageDecoder & Decoder, PCTSTR Key, unsigned __int64 DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator unsigned __int64 () const { unsigned __int64 out = To (m_DefValue); return out; } protected: unsigned __int64 m_DefValue; }; class KVMap_int64 : public KVMap { public: inline KVMap_int64 (MessageDecoder & Decoder, PCTSTR Key, __int64 DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator __int64 () const { __int64 out = To (m_DefValue); return out; } inline operator unsigned __int64 () const { unsigned __int64 out = To (m_DefValue); return out; } inline operator int () const { int out = (int) To (m_DefValue); return out; } inline operator unsigned int () const { unsigned int out = (unsigned int) To (m_DefValue); return out; } inline operator unsigned short () const { unsigned short out = (unsigned short) To (m_DefValue); return out; } protected: __int64 m_DefValue; }; class KVMap_bool : public KVMap { public: inline KVMap_bool (MessageDecoder & Decoder, PCTSTR Key, bool DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator bool () const { bool out = To (m_DefValue); return out; } protected: bool m_DefValue; }; class KVMap_float : public KVMap { public: inline KVMap_float (MessageDecoder & Decoder, PCTSTR Key, float DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator float () const { float out = To (m_DefValue); return out; } protected: float m_DefValue; }; class KVMap_double : public KVMap { public: inline KVMap_double (MessageDecoder & Decoder, PCTSTR Key, double DefValue) : KVMap (Decoder, Key), m_DefValue (DefValue) { } inline operator double () const { double out = To (m_DefValue); return out; } protected: double m_DefValue; }; public: KVMap operator [] (PCTSTR Key) { return KVMap (*this, Key); } KVMap operator () (PCTSTR Key) { return KVMap (*this, Key); } KVMap_String operator () (PCTSTR Key, PCTSTR DefValue) { return KVMap_String (*this, Key, DefValue); } KVMap_int64 operator () (PCTSTR Key, __int64 DefValue) { return KVMap_int64 (*this, Key, DefValue); } KVMap_uint64 operator () (PCTSTR Key, unsigned __int64 DefValue) { return KVMap_uint64 (*this, Key, DefValue); } KVMap_int operator () (PCTSTR Key, int DefValue) { return KVMap_int (*this, Key, DefValue); } KVMap_uint operator () (PCTSTR Key, unsigned int DefValue) { return KVMap_uint (*this, Key, DefValue); } KVMap_bool operator () (PCTSTR Key, bool DefValue) { return KVMap_bool (*this, Key, DefValue); } KVMap_float operator () (PCTSTR Key, float DefValue) { return KVMap_float (*this, Key, DefValue); } KVMap_double operator () (PCTSTR Key, double DefValue) { return KVMap_double (*this, Key, DefValue); } class _ServiceBus_API Iterator { public: Iterator (const MessageDecoder & Decoder); Iterator (const KVMap & Map); virtual ~Iterator (); inline DString Key () const { return m_Key; } inline operator bool () const { return !IsEnd (); } inline KVMap operator () (void) { return Current (); } bool IsEnd (void) const; KVMap Current (void); void Next (void); void Prev (void); inline void operator ++ () // ++Iter; prevfix { Next (); } inline void operator ++ (int) // Iter ++, postfix { Next (); } inline void operator -- () // --Iter; prevfix { Prev (); } inline void operator -- (int) // Iter++, postfix { Prev (); } protected: DString m_Key; void * m_JsonIter; void * m_JsonIterEnd; }; }; #else class _ServiceBus_API MessageDecoder { public: MessageDecoder (PCTSTR Message); MessageDecoder (const DString & Message); MessageDecoder (const WString & Message); virtual ~MessageDecoder (); DString Get (const std::string & path); DString Get (const DString & path); DString Get (PCTSTR path); DString Get (PCTSTR path, PCTSTR Def); bool Get (PCTSTR path, bool Def); int Get (PCTSTR path, int Def); int GetArray (PCTSTR Key, std::vector * arString); int GetArray (PCTSTR Key, std::vector * arString); protected: DString m_Message; // 保留一个 Copy, 调试用 ECOM::Utility::BSTTree * m_JsonRoot; protected: class _ServiceBus_API Proxy { public: Proxy (const MessageDecoder & Decoder, PCTSTR Key); ~Proxy (); public: DString ToString () const; WString ToWString (UINT CodePage = CP_UTF8) const; inline operator DString () const { return ToString (); } inline operator WString () const { return ToWString (); } inline operator void * () const { return reinterpret_cast (ToString ().To ()); } inline operator DWORD () const { return ToString ().Int (); } inline operator __int64 () const { return ToString ().Int64 (); } inline operator unsigned __int64 () const { return ToString ().Int64 (); } inline operator int () const { return ToString ().Int (); } inline operator unsigned int () const { return ToString ().Int (); } inline operator long () const { return ToString ().Int (); } inline operator bool () const { return ToString ().Bool (); } inline operator float () const { return (float) ToString ().Double (); } inline operator double () const { return ToString ().Double (); } protected: ECOM::Utility::BSTTree * m_JsonRoot; friend MessageDecoder; }; #if 1 // 函数操作符重载, 下标操作符重载 public: Proxy operator [] (PCTSTR Key) { return Proxy (*this, Key); } Proxy operator () (PCTSTR Key) { return Proxy (*this, Key); } #else public: DString operator [] (PCTSTR Key) { return Get (Key); } DString operator () (PCTSTR Key) { return Get (Key); } #endif // 带默认值的函数操作符重载 public: DString operator () (const std::string & path, PCTSTR Def); bool operator () (const std::string & path, bool Def); int operator () (const std::string & path, int Def); unsigned int operator () (const std::string & path, unsigned int Def); long operator () (const std::string & path, long Def); float operator () (const std::string & path, float Def); double operator () (const std::string & path, double Def); }; #endif }; };