123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- #pragma once
- #include <vector>
- #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 <DString> * arString) const;
- int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector <DString> * arString) const;
- int GetArray (PCTSTR Key, std::vector <WString> * arString) const;
- int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector <WString> * 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 <DString> * arString);
- int GetArray (PCTSTR Key, std::vector <WString> * 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 <void *> (ToString ().To <UINT_PTR> ());
- }
- 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
- };
- };
|