MessageDecoder.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #pragma once
  2. #include <vector>
  3. #include "DString.hpp"
  4. #include "WString.hpp"
  5. //#include "BSTTree.hpp"
  6. namespace ECOM
  7. {
  8. namespace Utility
  9. {
  10. class BSTTree;
  11. }
  12. }
  13. #ifndef _ServiceBus_DLL
  14. #define _ServiceBus_API _declspec(dllimport)
  15. #else
  16. #define _ServiceBus_API _declspec(dllexport)
  17. #endif
  18. namespace ECOM
  19. {
  20. namespace ServiceBus
  21. {
  22. #if 0
  23. class _ServiceBus_API MessageDecoder
  24. {
  25. public:
  26. MessageDecoder (PCTSTR Message);
  27. MessageDecoder (const DString & Message);
  28. MessageDecoder (const WString & Message) : MessageDecoder (Message.ToDString (CP_UTF8))
  29. {
  30. }
  31. virtual ~MessageDecoder ();
  32. DString GetString (PCTSTR Key) const;
  33. DString Get (PCTSTR Key, PCTSTR IfNotFound) const;
  34. __int64 Get (PCTSTR Key, __int64 IfNotFound = 0) const;
  35. unsigned __int64 Get (PCTSTR Key, unsigned __int64 IfNotFound = 0) const;
  36. int Get (PCTSTR Key, int IfNotFound = 0) const;
  37. unsigned int Get (PCTSTR Key, unsigned int IfNotFound = 0) const;
  38. bool Get (PCTSTR Key, bool IfNotFound = false) const;
  39. float Get (PCTSTR Key, float IfNotFound = false) const;
  40. double Get (PCTSTR Key, double IfNotFound = false) const;
  41. int GetArray (PCTSTR Key, std::vector <DString> * arString) const;
  42. int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector <DString> * arString) const;
  43. int GetArray (PCTSTR Key, std::vector <WString> * arString) const;
  44. int GetArray (PCTSTR KeyArray, PCTSTR KeyMember, std::vector <WString> * arString) const;
  45. protected:
  46. DString m_Message; // 保留一个 Copy, 调试用
  47. Json::Value * m_JsonRoot;
  48. public:
  49. class _ServiceBus_API KVMap
  50. {
  51. public:
  52. KVMap (const KVMap & KVMap);
  53. KVMap (const MessageDecoder & Decoder);
  54. KVMap (const MessageDecoder & Decoder, PCTSTR Key);
  55. KVMap (const KVMap & KVMap, PCTSTR Key);
  56. KVMap (const KVMap & KVMap, int Index);
  57. public:
  58. DString ToString () const;
  59. WString ToWString (UINT CodePage = CP_UTF8) const;
  60. DString ForceToString () const;
  61. DString ToStyledString () const;
  62. DString To (PCTSTR IfNotFound) const;
  63. __int64 To (__int64 IfNotFound = 0) const;
  64. unsigned __int64 To (unsigned __int64 IfNotFound = 0) const;
  65. int To (int IfNotFound = 0) const;
  66. unsigned int To (unsigned int IfNotFound = 0) const;
  67. long To (long IfNotFound = 0) const;
  68. bool To (bool IfNotFound = false) const;
  69. float To (float IfNotFound = 0) const;
  70. double To (double IfNotFound = 0) const;
  71. inline operator DString () const
  72. {
  73. return ToString ();
  74. }
  75. inline operator WString () const
  76. {
  77. return ToWString ();
  78. }
  79. inline operator void * () const
  80. {
  81. return (void *) To ((UINT_PTR) 0);
  82. }
  83. inline operator DWORD () const
  84. {
  85. return To (0);
  86. }
  87. inline operator __int64 () const
  88. {
  89. return To (__int64 (0));
  90. }
  91. inline operator unsigned __int64 () const
  92. {
  93. return To (unsigned __int64 (0));
  94. }
  95. inline operator int () const
  96. {
  97. return To (0);
  98. }
  99. inline operator unsigned int () const
  100. {
  101. return To (unsigned int(0));
  102. }
  103. inline operator long () const
  104. {
  105. return To (0);
  106. }
  107. inline operator bool () const
  108. {
  109. return To (false);
  110. }
  111. inline operator float () const
  112. {
  113. return To (0.0f);
  114. }
  115. inline operator double () const
  116. {
  117. return To (0.0);
  118. }
  119. public:
  120. KVMap operator [] (int Index); // 数组, 用下标来访问
  121. int Count () const; // 获取数组的数量
  122. public:
  123. inline KVMap operator [] (PCTSTR Key)
  124. {
  125. return KVMap (*this, Key);
  126. }
  127. inline KVMap operator () (PCTSTR Key)
  128. {
  129. return KVMap (*this, Key);
  130. }
  131. protected:
  132. Json::Value * m_JsonValue;
  133. friend MessageDecoder;
  134. };
  135. class KVMap_String : public KVMap
  136. {
  137. public:
  138. inline KVMap_String (MessageDecoder & Decoder, PCTSTR Key, PCTSTR DefValue) :
  139. KVMap (Decoder, Key),
  140. m_DefValue (DefValue)
  141. {
  142. }
  143. inline operator DString () const
  144. {
  145. DString out = To (m_DefValue);
  146. return out;
  147. }
  148. protected:
  149. PCTSTR m_DefValue;
  150. };
  151. class KVMap_uint : public KVMap
  152. {
  153. public:
  154. inline KVMap_uint (MessageDecoder & Decoder, PCTSTR Key, unsigned int DefValue) :
  155. KVMap (Decoder, Key),
  156. m_DefValue (DefValue)
  157. {
  158. }
  159. inline operator unsigned int () const
  160. {
  161. unsigned int out = To (m_DefValue);
  162. return out;
  163. }
  164. protected:
  165. unsigned int m_DefValue;
  166. };
  167. class KVMap_int : public KVMap
  168. {
  169. public:
  170. inline KVMap_int (MessageDecoder & Decoder, PCTSTR Key, int DefValue) :
  171. KVMap (Decoder, Key),
  172. m_DefValue (DefValue)
  173. {
  174. }
  175. inline operator int () const
  176. {
  177. int out = To (m_DefValue);
  178. return out;
  179. }
  180. inline operator unsigned int () const
  181. {
  182. unsigned int out = To (m_DefValue);
  183. return out;
  184. }
  185. inline operator unsigned short () const
  186. {
  187. unsigned short out = To (m_DefValue);
  188. return out;
  189. }
  190. protected:
  191. int m_DefValue;
  192. };
  193. class KVMap_uint64 : public KVMap
  194. {
  195. public:
  196. inline KVMap_uint64 (MessageDecoder & Decoder, PCTSTR Key, unsigned __int64 DefValue) :
  197. KVMap (Decoder, Key),
  198. m_DefValue (DefValue)
  199. {
  200. }
  201. inline operator unsigned __int64 () const
  202. {
  203. unsigned __int64 out = To (m_DefValue);
  204. return out;
  205. }
  206. protected:
  207. unsigned __int64 m_DefValue;
  208. };
  209. class KVMap_int64 : public KVMap
  210. {
  211. public:
  212. inline KVMap_int64 (MessageDecoder & Decoder, PCTSTR Key, __int64 DefValue) :
  213. KVMap (Decoder, Key),
  214. m_DefValue (DefValue)
  215. {
  216. }
  217. inline operator __int64 () const
  218. {
  219. __int64 out = To (m_DefValue);
  220. return out;
  221. }
  222. inline operator unsigned __int64 () const
  223. {
  224. unsigned __int64 out = To (m_DefValue);
  225. return out;
  226. }
  227. inline operator int () const
  228. {
  229. int out = (int) To (m_DefValue);
  230. return out;
  231. }
  232. inline operator unsigned int () const
  233. {
  234. unsigned int out = (unsigned int) To (m_DefValue);
  235. return out;
  236. }
  237. inline operator unsigned short () const
  238. {
  239. unsigned short out = (unsigned short) To (m_DefValue);
  240. return out;
  241. }
  242. protected:
  243. __int64 m_DefValue;
  244. };
  245. class KVMap_bool : public KVMap
  246. {
  247. public:
  248. inline KVMap_bool (MessageDecoder & Decoder, PCTSTR Key, bool DefValue) :
  249. KVMap (Decoder, Key),
  250. m_DefValue (DefValue)
  251. {
  252. }
  253. inline operator bool () const
  254. {
  255. bool out = To (m_DefValue);
  256. return out;
  257. }
  258. protected:
  259. bool m_DefValue;
  260. };
  261. class KVMap_float : public KVMap
  262. {
  263. public:
  264. inline KVMap_float (MessageDecoder & Decoder, PCTSTR Key, float DefValue) :
  265. KVMap (Decoder, Key),
  266. m_DefValue (DefValue)
  267. {
  268. }
  269. inline operator float () const
  270. {
  271. float out = To (m_DefValue);
  272. return out;
  273. }
  274. protected:
  275. float m_DefValue;
  276. };
  277. class KVMap_double : public KVMap
  278. {
  279. public:
  280. inline KVMap_double (MessageDecoder & Decoder, PCTSTR Key, double DefValue) :
  281. KVMap (Decoder, Key),
  282. m_DefValue (DefValue)
  283. {
  284. }
  285. inline operator double () const
  286. {
  287. double out = To (m_DefValue);
  288. return out;
  289. }
  290. protected:
  291. double m_DefValue;
  292. };
  293. public:
  294. KVMap operator [] (PCTSTR Key)
  295. {
  296. return KVMap (*this, Key);
  297. }
  298. KVMap operator () (PCTSTR Key)
  299. {
  300. return KVMap (*this, Key);
  301. }
  302. KVMap_String operator () (PCTSTR Key, PCTSTR DefValue)
  303. {
  304. return KVMap_String (*this, Key, DefValue);
  305. }
  306. KVMap_int64 operator () (PCTSTR Key, __int64 DefValue)
  307. {
  308. return KVMap_int64 (*this, Key, DefValue);
  309. }
  310. KVMap_uint64 operator () (PCTSTR Key, unsigned __int64 DefValue)
  311. {
  312. return KVMap_uint64 (*this, Key, DefValue);
  313. }
  314. KVMap_int operator () (PCTSTR Key, int DefValue)
  315. {
  316. return KVMap_int (*this, Key, DefValue);
  317. }
  318. KVMap_uint operator () (PCTSTR Key, unsigned int DefValue)
  319. {
  320. return KVMap_uint (*this, Key, DefValue);
  321. }
  322. KVMap_bool operator () (PCTSTR Key, bool DefValue)
  323. {
  324. return KVMap_bool (*this, Key, DefValue);
  325. }
  326. KVMap_float operator () (PCTSTR Key, float DefValue)
  327. {
  328. return KVMap_float (*this, Key, DefValue);
  329. }
  330. KVMap_double operator () (PCTSTR Key, double DefValue)
  331. {
  332. return KVMap_double (*this, Key, DefValue);
  333. }
  334. class _ServiceBus_API Iterator
  335. {
  336. public:
  337. Iterator (const MessageDecoder & Decoder);
  338. Iterator (const KVMap & Map);
  339. virtual ~Iterator ();
  340. inline DString Key () const { return m_Key; }
  341. inline operator bool () const
  342. {
  343. return !IsEnd ();
  344. }
  345. inline KVMap operator () (void)
  346. {
  347. return Current ();
  348. }
  349. bool IsEnd (void) const;
  350. KVMap Current (void);
  351. void Next (void);
  352. void Prev (void);
  353. inline void operator ++ () // ++Iter; prevfix
  354. {
  355. Next ();
  356. }
  357. inline void operator ++ (int) // Iter ++, postfix
  358. {
  359. Next ();
  360. }
  361. inline void operator -- () // --Iter; prevfix
  362. {
  363. Prev ();
  364. }
  365. inline void operator -- (int) // Iter++, postfix
  366. {
  367. Prev ();
  368. }
  369. protected:
  370. DString m_Key;
  371. void * m_JsonIter;
  372. void * m_JsonIterEnd;
  373. };
  374. };
  375. #else
  376. class _ServiceBus_API MessageDecoder
  377. {
  378. public:
  379. MessageDecoder (PCTSTR Message);
  380. MessageDecoder (const DString & Message);
  381. MessageDecoder (const WString & Message);
  382. virtual ~MessageDecoder ();
  383. DString Get (const std::string & path);
  384. DString Get (const DString & path);
  385. DString Get (PCTSTR path);
  386. DString Get (PCTSTR path, PCTSTR Def);
  387. bool Get (PCTSTR path, bool Def);
  388. int Get (PCTSTR path, int Def);
  389. int GetArray (PCTSTR Key, std::vector <DString> * arString);
  390. int GetArray (PCTSTR Key, std::vector <WString> * arString);
  391. protected:
  392. DString m_Message; // 保留一个 Copy, 调试用
  393. ECOM::Utility::BSTTree * m_JsonRoot;
  394. protected:
  395. class _ServiceBus_API Proxy
  396. {
  397. public:
  398. Proxy (const MessageDecoder & Decoder, PCTSTR Key);
  399. ~Proxy ();
  400. public:
  401. DString ToString () const;
  402. WString ToWString (UINT CodePage = CP_UTF8) const;
  403. inline operator DString () const
  404. {
  405. return ToString ();
  406. }
  407. inline operator WString () const
  408. {
  409. return ToWString ();
  410. }
  411. inline operator void * () const
  412. {
  413. return reinterpret_cast <void *> (ToString ().To <UINT_PTR> ());
  414. }
  415. inline operator DWORD () const
  416. {
  417. return ToString ().Int ();
  418. }
  419. inline operator __int64 () const
  420. {
  421. return ToString ().Int64 ();
  422. }
  423. inline operator unsigned __int64 () const
  424. {
  425. return ToString ().Int64 ();
  426. }
  427. inline operator int () const
  428. {
  429. return ToString ().Int ();
  430. }
  431. inline operator unsigned int () const
  432. {
  433. return ToString ().Int ();
  434. }
  435. inline operator long () const
  436. {
  437. return ToString ().Int ();
  438. }
  439. inline operator bool () const
  440. {
  441. return ToString ().Bool ();
  442. }
  443. inline operator float () const
  444. {
  445. return (float) ToString ().Double ();
  446. }
  447. inline operator double () const
  448. {
  449. return ToString ().Double ();
  450. }
  451. protected:
  452. ECOM::Utility::BSTTree * m_JsonRoot;
  453. friend MessageDecoder;
  454. };
  455. #if 1
  456. // 函数操作符重载, 下标操作符重载
  457. public:
  458. Proxy operator [] (PCTSTR Key)
  459. {
  460. return Proxy (*this, Key);
  461. }
  462. Proxy operator () (PCTSTR Key)
  463. {
  464. return Proxy (*this, Key);
  465. }
  466. #else
  467. public:
  468. DString operator [] (PCTSTR Key)
  469. {
  470. return Get (Key);
  471. }
  472. DString operator () (PCTSTR Key)
  473. {
  474. return Get (Key);
  475. }
  476. #endif
  477. // 带默认值的函数操作符重载
  478. public:
  479. DString operator () (const std::string & path, PCTSTR Def);
  480. bool operator () (const std::string & path, bool Def);
  481. int operator () (const std::string & path, int Def);
  482. unsigned int operator () (const std::string & path, unsigned int Def);
  483. long operator () (const std::string & path, long Def);
  484. float operator () (const std::string & path, float Def);
  485. double operator () (const std::string & path, double Def);
  486. };
  487. #endif
  488. };
  489. };