ResDataObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #pragma once
  2. #ifdef RESDATAOBJECT_EXPORTS
  3. #define RESDATAOBJECT_API
  4. #define RESDATAOBJECT_C_API extern "C"
  5. #else
  6. #define RESDATAOBJECT_API
  7. #define RESDATAOBJECT_C_API extern "C"
  8. #endif
  9. #include <utility>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <sstream>
  14. #include <assert.h>
  15. #include <cctype> // 对于 std::tolower
  16. #include <memory>
  17. using namespace std;
  18. using DWORD = unsigned long; // Linux 兼容的 DWORD 类型
  19. //help note
  20. //数据对象是JSON格式的树状结构
  21. //对象有俩个成员,m_value 还有 vector<pair<string,数据对象>>
  22. //它俩当中必须有一个是有效的,先检查vector,若空 m_value有效.
  23. //JSON定义中数组的定义不要用,因为无法转换到XML对象.
  24. //XML定义中不要用属性,因为无法转换到JSON对象.
  25. //JSON定义为主,XML支持为辅.
  26. //ini定义只有俩层.
  27. //不要在key定义中使用 【.】 ,因为Boost会把【.】看成是模块的切换点.
  28. //get 用法
  29. //["key"],[idx],[xx].get<double>("key"),[xx].get<double>();
  30. //add 用法:不解释
  31. //update 用法:key,val,idx的形式,没有匹配值的情况用add,有值的情况update
  32. class ResDataObject;
  33. class RESDATAOBJECT_API ResDataObjectExption
  34. {
  35. std::unique_ptr<std::string> m_ExpContext;
  36. public:
  37. ResDataObjectExption(const ResDataObjectExption &tValue);
  38. ResDataObjectExption(const char *pExp);
  39. ~ResDataObjectExption(void);
  40. ResDataObjectExption& operator = (const ResDataObjectExption &tValue);
  41. const char* what() const;
  42. };
  43. //缺省的值和字符串以外不要用
  44. class RESDATAOBJECT_API EDATAOBJECT {
  45. std::unique_ptr<std::string> m_value;
  46. public:
  47. EDATAOBJECT() : m_value(std::make_unique<std::string>()) {}
  48. ~EDATAOBJECT() = default;
  49. //set
  50. EDATAOBJECT& operator = (const char* pVal)
  51. {
  52. (*m_value) = (pVal);
  53. return (*this);
  54. };
  55. EDATAOBJECT& operator = (const float tValue)
  56. {
  57. unsigned int temp,*pF ;
  58. std::stringstream strm;
  59. //strm.precision(numeric_limits<float>::digits10);
  60. assert(sizeof(float) == sizeof(unsigned int));
  61. pF = (unsigned int*)&tValue;
  62. temp = (*pF);
  63. strm << temp;
  64. (*this) = strm.str().c_str();
  65. return (*this);
  66. };
  67. EDATAOBJECT& operator = (const double tValue)
  68. {
  69. unsigned long long temp,*pF ;
  70. std::stringstream strm;
  71. //strm.precision(numeric_limits<double>::digits10);
  72. assert(sizeof(double) == sizeof(unsigned long long));
  73. pF = (unsigned long long*)&tValue;
  74. temp = (*pF);
  75. strm << temp;
  76. (*this) = strm.str().c_str();
  77. return (*this);
  78. };
  79. template<typename T> EDATAOBJECT& operator = (const T tValue)
  80. {
  81. std::stringstream strm;
  82. strm << tValue;
  83. (*this) = strm.str().c_str();
  84. return (*this);
  85. };
  86. //get
  87. operator float()
  88. {
  89. try {
  90. return std::stof(*m_value);
  91. }
  92. catch (...) {
  93. return 0.0f;
  94. }
  95. };
  96. operator double()
  97. {
  98. try {
  99. return std::stod(*m_value);
  100. }
  101. catch (...) {
  102. return 0.0;
  103. }
  104. };
  105. operator const char*()
  106. {
  107. return m_value->c_str();
  108. }
  109. template<typename T> operator T()
  110. {
  111. T ret1 = 0;
  112. std::stringstream strm;
  113. strm << m_value->c_str();
  114. strm >> ret1;
  115. return ret1;
  116. };
  117. };
  118. class RESDATAOBJECT_API ResDataObject_Pair
  119. {
  120. public:
  121. std::unique_ptr<std::string> first;
  122. std::unique_ptr<ResDataObject> second;
  123. ResDataObject_Pair();
  124. ~ResDataObject_Pair() = default;
  125. ResDataObject_Pair(const ResDataObject_Pair& other);
  126. ResDataObject_Pair(ResDataObject_Pair&& other) noexcept = default;
  127. ResDataObject_Pair& operator=(const ResDataObject_Pair& other);
  128. ResDataObject_Pair& operator=(ResDataObject_Pair&& other) noexcept = default;
  129. };
  130. class RESDATAOBJECT_API ResDataObject
  131. {
  132. protected:
  133. std::unique_ptr<std::string> m_encode;
  134. std::unique_ptr<std::string> m_value;
  135. std::unique_ptr<std::vector<ResDataObject_Pair>> m_vec;
  136. //std::string *m_encode;//serialize
  137. //std::string *m_value;
  138. //vector<ResDataObject_Pair*> *m_vec;
  139. public:
  140. ResDataObject(void);
  141. ResDataObject(const ResDataObject &tValue);
  142. ResDataObject(ResDataObject&& tValue) noexcept = default;
  143. ~ResDataObject(void);
  144. ResDataObject& operator=(ResDataObject&& tValue) noexcept = default;
  145. bool DumpJsonFile(const char *pfileName);
  146. //open close
  147. bool loadFile(const char *pfileName);//支持xml,json,ini扩展名
  148. bool SaveFile( const char *pfileName );//支持xml,json,ini扩展名
  149. //serialize
  150. const char *encode();
  151. bool decode(const char *pdata);
  152. //commons
  153. void clear();
  154. size_t size();
  155. bool IsObject();
  156. void MakeKeyLower();
  157. size_t GetKeyCount(const char *pKey);
  158. //erase
  159. bool eraseAllOf(const char *pKey);
  160. bool eraseOneOf(const char *pKey,size_t idx = 0);
  161. //set
  162. ResDataObject& operator = (const ResDataObject &tValue);
  163. //add
  164. ResDataObject& operator += (const ResDataObject &tValue);
  165. ResDataObject& operator = (const bool tValue);
  166. ResDataObject& operator = (const char tValue);
  167. ResDataObject& operator = (const unsigned char tValue);
  168. ResDataObject& operator = (const short tValue);
  169. ResDataObject& operator = (const unsigned short tValue);
  170. ResDataObject& operator = (const int tValue);
  171. ResDataObject& operator = (const unsigned int tValue);
  172. ResDataObject& operator = (const long tValue);
  173. ResDataObject& operator = (const unsigned long tValue);
  174. ResDataObject& operator = (const long long tValue);
  175. ResDataObject& operator = (const unsigned long long tValue);
  176. ResDataObject& operator = (const float tValue);
  177. ResDataObject& operator = (const double tValue);
  178. ResDataObject& operator = (const char* pVal);
  179. //get
  180. ResDataObject &operator [](size_t idx);
  181. ResDataObject &operator [](int idx);
  182. ResDataObject &operator [](const char *pKey);
  183. bool SetKey(size_t idx,const char *pKey);
  184. const char* GetKey(size_t idx);
  185. const char* GetKey(int idx);
  186. //get
  187. operator bool();
  188. operator char();
  189. operator unsigned char();
  190. operator short();
  191. operator unsigned short();
  192. operator int();
  193. operator unsigned int();
  194. operator long();
  195. operator unsigned long();
  196. operator long long();
  197. operator unsigned long long();
  198. operator float();
  199. operator double();
  200. operator const char*();
  201. //same name in the row
  202. int GetFirstOf(const char *pKey);
  203. int GetNextOf(const char *pKey,int PrevIdx);
  204. //add
  205. bool add(const char* pKey,bool tValue);
  206. bool add(const char* pKey,char tValue);
  207. bool add(const char* pKey,unsigned char tValue);
  208. bool add(const char* pKey,short tValue);
  209. bool add(const char* pKey,unsigned short tValue);
  210. bool add(const char* pKey,int tValue);
  211. bool add(const char* pKey,unsigned int tValue);
  212. bool add(const char* pKey,long tValue);
  213. bool add(const char* pKey,unsigned long tValue);
  214. bool add(const char* pKey,long long tValue);
  215. bool add(const char* pKey,unsigned long long tValue);
  216. bool add(const char* pKey,float tValue);
  217. bool add(const char* pKey,double tValue);
  218. bool add(const char* pKey,const char* pValue);
  219. bool add(const char* pKey,ResDataObject &dataobj);
  220. //bool add(const char* pKey,ResDataObject dataobj);
  221. bool update(const char* pKey,bool tValue);
  222. bool update(const char* pKey,char tValue);
  223. bool update(const char* pKey,unsigned char tValue);
  224. bool update(const char* pKey,short tValue);
  225. bool update(const char* pKey,unsigned short tValue);
  226. bool update(const char* pKey,int tValue);
  227. bool update(const char* pKey,unsigned int tValue);
  228. bool update(const char* pKey,long tValue);
  229. bool update(const char* pKey,unsigned long tValue);
  230. bool update(const char* pKey,long long tValue);
  231. bool update(const char* pKey,unsigned long long tValue);
  232. bool update(const char* pKey,float tValue);
  233. bool update(const char* pKey,double tValue);
  234. bool update(const char* pKey,const char* pValue);
  235. bool update(const char* pKey,ResDataObject &dataobj);
  236. //bool add(const char* pKey,ResDataObject dataobj);
  237. bool operator == (const char* pVal);
  238. bool operator == (const ResDataObject &Obj);
  239. };
  240. RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res);
  241. class RESDATAOBJECT_API ExJsonDataObject
  242. {
  243. std::unique_ptr<std::string> m_pKey;
  244. std::unique_ptr<std::string> m_ValString;
  245. std::unique_ptr<ResDataObject> m_pTargetObject;
  246. public:
  247. ExJsonDataObject();
  248. virtual ~ExJsonDataObject() = default;
  249. ExJsonDataObject(const ExJsonDataObject &tValue);
  250. //base
  251. void SetKey(const char *pKey);
  252. const char *GetKey();
  253. //基于对象
  254. virtual ResDataObject &GetResDataObject();
  255. virtual bool SetResDataObject(ResDataObject &obj);
  256. //基于字符串
  257. //virtual const char *GetVal();
  258. //virtual bool SetVal(const char* pValString);
  259. //基于下标读取
  260. ResDataObject &operator [](const char *pKey);
  261. ExJsonDataObject& operator = (const ExJsonDataObject &tValue);
  262. };
  263. template<typename T> class BaseJsonDataObject
  264. {
  265. protected:
  266. T m_Data;
  267. std::string m_KeyString;
  268. std::string m_ValString;
  269. public:
  270. BaseJsonDataObject() = default;
  271. virtual ~BaseJsonDataObject() = default;
  272. virtual void GetResDataObject(ResDataObject &obj)
  273. {
  274. obj.add(GetKey(), GetVal());
  275. };
  276. virtual bool SetResDataObject(ResDataObject &obj)
  277. {
  278. return SetVal((const char *)obj);
  279. };
  280. virtual const char *encode()
  281. {
  282. ResDataObject obj;
  283. if(obj.add(GetKey(),GetVal()) == false)
  284. {
  285. return NULL;
  286. }
  287. (m_ValString) = obj.encode();
  288. return m_ValString.c_str();
  289. };
  290. virtual bool Decode(const char *pString)
  291. {
  292. bool ret = true;
  293. ResDataObject obj;
  294. try {
  295. if(obj.decode(pString) == false)
  296. {
  297. return false;
  298. }
  299. ret = SetVal(obj[m_KeyString.c_str()]);
  300. }
  301. catch(...)
  302. {
  303. ret = false;
  304. }
  305. return ret;
  306. };
  307. virtual const char *GetKey()
  308. {
  309. return m_KeyString.c_str();
  310. };
  311. virtual const char *GetVal()
  312. {
  313. std::stringstream strm;
  314. strm << (m_Data);
  315. strm >> (m_ValString);
  316. return m_ValString.c_str();
  317. };
  318. virtual void SetKey(const char* pKeyString)
  319. {
  320. (m_KeyString) = pKeyString;
  321. };
  322. virtual bool SetVal(const char* pValString)
  323. {
  324. try {
  325. if constexpr (std::is_same_v<T, float>) {
  326. m_Data = std::stof(pValString);
  327. }
  328. else if constexpr (std::is_same_v<T, double>) {
  329. m_Data = std::stod(pValString);
  330. }
  331. else if constexpr (std::is_integral_v<T>) {
  332. std::stringstream strm(pValString);
  333. strm >> m_Data;
  334. }
  335. else {
  336. std::stringstream strm(pValString);
  337. strm >> m_Data;
  338. }
  339. return true;
  340. }
  341. catch (...)
  342. {
  343. return false;
  344. }
  345. };
  346. virtual bool AddVal(const char* pValString)
  347. {
  348. return false;
  349. };
  350. virtual bool DelVal(const char* pValString)
  351. {
  352. return false;
  353. };
  354. //BaseJsonDataObject& operator = (BaseJsonDataObject &tValue)
  355. //{
  356. // if (this != &tValue)
  357. // {
  358. // (*m_pKeyString) = (*tValue.m_pKeyString);
  359. // (*m_pValString) = (*tValue.m_pValString);
  360. // (*m_pData) = (*tValue.m_pData);
  361. // }
  362. // return (*this);
  363. //}
  364. BaseJsonDataObject& operator = (T Val)
  365. {
  366. (m_Data) = Val;
  367. return (*this);
  368. };
  369. operator T()
  370. {
  371. return (m_Data);
  372. };
  373. };
  374. //Ccos协议包中的数据区对象
  375. //数据区用此对象进行操作,
  376. //数据区的数据保存在CcosPacket中,而ResRawObject是引用这个数据,并非拷贝!!!
  377. //关系为 CcosPacket 1->n ResRawObject,注意CcosPacket被删除的时候,不要继续使用ResRawObject!!!
  378. //线程安全,非进程安全,多线程抢占自己加锁!!
  379. //class RESDATAOBJECT_API ResRawObject
  380. //{
  381. // size_t m_BuffLen;
  382. // size_t m_DataLen;
  383. // char *m_pszDataPosition;
  384. //
  385. //public:
  386. // ResRawObject();
  387. // ResRawObject(char *pData, size_t BuffSize,size_t DataSize);
  388. // virtual ~ResRawObject();
  389. //
  390. // //init op
  391. // void ClearData();
  392. //
  393. // //Get operation
  394. //
  395. // //尽量用在读取,不要用在更新上,因为数据是一个缓存,前面更新的话,将有可能破坏后面数据区
  396. // ResRawObject &operator [](CCOSKEYTYPE key);
  397. // operator bool();
  398. // operator char();
  399. // operator unsigned char();
  400. // operator short();
  401. // operator unsigned short();
  402. // operator int();
  403. // operator unsigned int();
  404. // operator long();
  405. // operator unsigned long();
  406. // operator long long();
  407. // operator unsigned long long();
  408. // operator float();
  409. // operator double();
  410. // operator const char*();
  411. //
  412. // //Set operation
  413. //
  414. // //固定长度资源的添加,12BIT的KEY是不变,3bit的内容由参数来决定
  415. // bool add(CCOSKEYTYPE Key, bool tValue);
  416. // bool add(CCOSKEYTYPE Key, char tValue);
  417. // bool add(CCOSKEYTYPE Key, unsigned char tValue);
  418. // bool add(CCOSKEYTYPE Key, short tValue);
  419. // bool add(CCOSKEYTYPE Key, unsigned short tValue);
  420. // bool add(CCOSKEYTYPE Key, int tValue);
  421. // bool add(CCOSKEYTYPE Key, unsigned int tValue);
  422. // bool add(CCOSKEYTYPE Key, long tValue);
  423. // bool add(CCOSKEYTYPE Key, unsigned long tValue);
  424. // bool add(CCOSKEYTYPE Key, long long tValue);
  425. // bool add(CCOSKEYTYPE Key, unsigned long long tValue);
  426. // bool add(CCOSKEYTYPE Key, float tValue);
  427. // bool add(CCOSKEYTYPE Key, double tValue);
  428. // bool add(CCOSKEYTYPE Key, size_t KeySize, const char *pData, size_t datasize);//Key的上限<size的上限,失败返回
  429. //
  430. // //可变长度资源的添加,12BIT的KEY+高位1是不变,3bit的内容由LenSize来决定
  431. // bool addFlexible(CCOSKEYTYPE Key, size_t KeySize, const char* pData, size_t datasize);//Flexible key
  432. //
  433. //
  434. //
  435. //};