ResDataObject.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. // 添加移动构造函数
  127. ResDataObject_Pair(ResDataObject_Pair&& other) noexcept
  128. : first(std::move(other.first))
  129. , second(std::move(other.second))
  130. {
  131. // 移动后重置源对象,确保其成员非空
  132. if (!other.first) {
  133. other.first = std::make_unique<std::string>();
  134. }
  135. if (!other.second) {
  136. other.second = std::make_unique<ResDataObject>();
  137. }
  138. }
  139. ResDataObject_Pair& operator=(const ResDataObject_Pair& other);
  140. ResDataObject_Pair& operator=(ResDataObject_Pair&& other) noexcept;
  141. };
  142. class RESDATAOBJECT_API ResDataObject
  143. {
  144. protected:
  145. std::unique_ptr<std::string> m_encode;
  146. std::unique_ptr<std::string> m_value;
  147. std::unique_ptr<std::vector<ResDataObject_Pair>> m_vec;
  148. //std::string *m_encode;//serialize
  149. //std::string *m_value;
  150. //vector<ResDataObject_Pair*> *m_vec;
  151. public:
  152. ResDataObject(void);
  153. ResDataObject(const ResDataObject &tValue);
  154. ResDataObject(ResDataObject&& tValue) noexcept;
  155. ~ResDataObject(void);
  156. ResDataObject& operator=(ResDataObject&& tValue) noexcept;
  157. bool DumpJsonFile(const char *pfileName);
  158. //open close
  159. bool loadFile(const char *pfileName);//支持xml,json,ini扩展名
  160. bool SaveFile( const char *pfileName );//支持xml,json,ini扩展名
  161. //serialize
  162. const char *encode();
  163. bool decode(const char *pdata);
  164. //commons
  165. void clear();
  166. size_t size();
  167. bool IsObject();
  168. void MakeKeyLower();
  169. size_t GetKeyCount(const char *pKey);
  170. //erase
  171. bool eraseAllOf(const char *pKey);
  172. bool eraseOneOf(const char *pKey,size_t idx = 0);
  173. //set
  174. ResDataObject& operator = (const ResDataObject &tValue);
  175. //add
  176. ResDataObject& operator += (const ResDataObject &tValue);
  177. ResDataObject& operator = (const bool tValue);
  178. ResDataObject& operator = (const char tValue);
  179. ResDataObject& operator = (const unsigned char tValue);
  180. ResDataObject& operator = (const short tValue);
  181. ResDataObject& operator = (const unsigned short tValue);
  182. ResDataObject& operator = (const int tValue);
  183. ResDataObject& operator = (const unsigned int tValue);
  184. ResDataObject& operator = (const long tValue);
  185. ResDataObject& operator = (const unsigned long tValue);
  186. ResDataObject& operator = (const long long tValue);
  187. ResDataObject& operator = (const unsigned long long tValue);
  188. ResDataObject& operator = (const float tValue);
  189. ResDataObject& operator = (const double tValue);
  190. ResDataObject& operator = (const char* pVal);
  191. //get
  192. ResDataObject &operator [](size_t idx);
  193. ResDataObject &operator [](int idx);
  194. ResDataObject &operator [](const char *pKey);
  195. bool SetKey(size_t idx,const char *pKey);
  196. const char* GetKey(size_t idx);
  197. const char* GetKey(int idx);
  198. //get
  199. operator bool();
  200. operator char();
  201. operator unsigned char();
  202. operator short();
  203. operator unsigned short();
  204. operator int();
  205. operator unsigned int();
  206. operator long();
  207. operator unsigned long();
  208. operator long long();
  209. operator unsigned long long();
  210. operator float();
  211. operator double();
  212. operator const char*();
  213. //same name in the row
  214. int GetFirstOf(const char *pKey);
  215. int GetNextOf(const char *pKey,int PrevIdx);
  216. //add
  217. bool add(const char* pKey,bool tValue);
  218. bool add(const char* pKey,char tValue);
  219. bool add(const char* pKey,unsigned char tValue);
  220. bool add(const char* pKey,short tValue);
  221. bool add(const char* pKey,unsigned short tValue);
  222. bool add(const char* pKey,int tValue);
  223. bool add(const char* pKey,unsigned int tValue);
  224. bool add(const char* pKey,long tValue);
  225. bool add(const char* pKey,unsigned long tValue);
  226. bool add(const char* pKey,long long tValue);
  227. bool add(const char* pKey,unsigned long long tValue);
  228. bool add(const char* pKey,float tValue);
  229. bool add(const char* pKey,double tValue);
  230. bool add(const char* pKey,const char* pValue);
  231. bool add(const char* pKey,ResDataObject &dataobj);
  232. //bool add(const char* pKey,ResDataObject dataobj);
  233. bool update(const char* pKey,bool tValue);
  234. bool update(const char* pKey,char tValue);
  235. bool update(const char* pKey,unsigned char tValue);
  236. bool update(const char* pKey,short tValue);
  237. bool update(const char* pKey,unsigned short tValue);
  238. bool update(const char* pKey,int tValue);
  239. bool update(const char* pKey,unsigned int tValue);
  240. bool update(const char* pKey,long tValue);
  241. bool update(const char* pKey,unsigned long tValue);
  242. bool update(const char* pKey,long long tValue);
  243. bool update(const char* pKey,unsigned long long tValue);
  244. bool update(const char* pKey,float tValue);
  245. bool update(const char* pKey,double tValue);
  246. bool update(const char* pKey,const char* pValue);
  247. bool update(const char* pKey,ResDataObject &dataobj);
  248. //bool add(const char* pKey,ResDataObject dataobj);
  249. bool operator == (const char* pVal);
  250. bool operator == (const ResDataObject &Obj);
  251. };
  252. RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res);
  253. class RESDATAOBJECT_API ExJsonDataObject
  254. {
  255. std::unique_ptr<std::string> m_pKey;
  256. std::unique_ptr<std::string> m_ValString;
  257. std::unique_ptr<ResDataObject> m_pTargetObject;
  258. public:
  259. ExJsonDataObject();
  260. virtual ~ExJsonDataObject() = default;
  261. ExJsonDataObject(const ExJsonDataObject &tValue);
  262. //base
  263. void SetKey(const char *pKey);
  264. const char *GetKey();
  265. //基于对象
  266. virtual ResDataObject &GetResDataObject();
  267. virtual bool SetResDataObject(ResDataObject &obj);
  268. //基于字符串
  269. //virtual const char *GetVal();
  270. //virtual bool SetVal(const char* pValString);
  271. //基于下标读取
  272. ResDataObject &operator [](const char *pKey);
  273. ExJsonDataObject& operator = (const ExJsonDataObject &tValue);
  274. };
  275. template<typename T> class BaseJsonDataObject
  276. {
  277. protected:
  278. T m_Data;
  279. std::string m_KeyString;
  280. std::string m_ValString;
  281. public:
  282. BaseJsonDataObject() = default;
  283. virtual ~BaseJsonDataObject() = default;
  284. virtual void GetResDataObject(ResDataObject &obj)
  285. {
  286. obj.add(GetKey(), GetVal());
  287. };
  288. virtual bool SetResDataObject(ResDataObject &obj)
  289. {
  290. return SetVal((const char *)obj);
  291. };
  292. virtual const char *encode()
  293. {
  294. ResDataObject obj;
  295. if(obj.add(GetKey(),GetVal()) == false)
  296. {
  297. return NULL;
  298. }
  299. (m_ValString) = obj.encode();
  300. return m_ValString.c_str();
  301. };
  302. virtual bool Decode(const char *pString)
  303. {
  304. bool ret = true;
  305. ResDataObject obj;
  306. try {
  307. if(obj.decode(pString) == false)
  308. {
  309. return false;
  310. }
  311. ret = SetVal(obj[m_KeyString.c_str()]);
  312. }
  313. catch(...)
  314. {
  315. ret = false;
  316. }
  317. return ret;
  318. };
  319. virtual const char *GetKey()
  320. {
  321. return m_KeyString.c_str();
  322. };
  323. virtual const char *GetVal()
  324. {
  325. std::stringstream strm;
  326. strm << (m_Data);
  327. strm >> (m_ValString);
  328. return m_ValString.c_str();
  329. };
  330. virtual void SetKey(const char* pKeyString)
  331. {
  332. (m_KeyString) = pKeyString;
  333. };
  334. virtual bool SetVal(const char* pValString)
  335. {
  336. try {
  337. if constexpr (std::is_same_v<T, float>) {
  338. m_Data = std::stof(pValString);
  339. }
  340. else if constexpr (std::is_same_v<T, double>) {
  341. m_Data = std::stod(pValString);
  342. }
  343. else if constexpr (std::is_integral_v<T>) {
  344. std::stringstream strm(pValString);
  345. strm >> m_Data;
  346. }
  347. else {
  348. std::stringstream strm(pValString);
  349. strm >> m_Data;
  350. }
  351. return true;
  352. }
  353. catch (...)
  354. {
  355. return false;
  356. }
  357. };
  358. virtual bool AddVal(const char* pValString)
  359. {
  360. return false;
  361. };
  362. virtual bool DelVal(const char* pValString)
  363. {
  364. return false;
  365. };
  366. //BaseJsonDataObject& operator = (BaseJsonDataObject &tValue)
  367. //{
  368. // if (this != &tValue)
  369. // {
  370. // (*m_pKeyString) = (*tValue.m_pKeyString);
  371. // (*m_pValString) = (*tValue.m_pValString);
  372. // (*m_pData) = (*tValue.m_pData);
  373. // }
  374. // return (*this);
  375. //}
  376. BaseJsonDataObject& operator = (T Val)
  377. {
  378. (m_Data) = Val;
  379. return (*this);
  380. };
  381. operator T()
  382. {
  383. return (m_Data);
  384. };
  385. };
  386. //Ccos协议包中的数据区对象
  387. //数据区用此对象进行操作,
  388. //数据区的数据保存在CcosPacket中,而ResRawObject是引用这个数据,并非拷贝!!!
  389. //关系为 CcosPacket 1->n ResRawObject,注意CcosPacket被删除的时候,不要继续使用ResRawObject!!!
  390. //线程安全,非进程安全,多线程抢占自己加锁!!
  391. //class RESDATAOBJECT_API ResRawObject
  392. //{
  393. // size_t m_BuffLen;
  394. // size_t m_DataLen;
  395. // char *m_pszDataPosition;
  396. //
  397. //public:
  398. // ResRawObject();
  399. // ResRawObject(char *pData, size_t BuffSize,size_t DataSize);
  400. // virtual ~ResRawObject();
  401. //
  402. // //init op
  403. // void ClearData();
  404. //
  405. // //Get operation
  406. //
  407. // //尽量用在读取,不要用在更新上,因为数据是一个缓存,前面更新的话,将有可能破坏后面数据区
  408. // ResRawObject &operator [](CCOSKEYTYPE key);
  409. // operator bool();
  410. // operator char();
  411. // operator unsigned char();
  412. // operator short();
  413. // operator unsigned short();
  414. // operator int();
  415. // operator unsigned int();
  416. // operator long();
  417. // operator unsigned long();
  418. // operator long long();
  419. // operator unsigned long long();
  420. // operator float();
  421. // operator double();
  422. // operator const char*();
  423. //
  424. // //Set operation
  425. //
  426. // //固定长度资源的添加,12BIT的KEY是不变,3bit的内容由参数来决定
  427. // bool add(CCOSKEYTYPE Key, bool tValue);
  428. // bool add(CCOSKEYTYPE Key, char tValue);
  429. // bool add(CCOSKEYTYPE Key, unsigned char tValue);
  430. // bool add(CCOSKEYTYPE Key, short tValue);
  431. // bool add(CCOSKEYTYPE Key, unsigned short tValue);
  432. // bool add(CCOSKEYTYPE Key, int tValue);
  433. // bool add(CCOSKEYTYPE Key, unsigned int tValue);
  434. // bool add(CCOSKEYTYPE Key, long tValue);
  435. // bool add(CCOSKEYTYPE Key, unsigned long tValue);
  436. // bool add(CCOSKEYTYPE Key, long long tValue);
  437. // bool add(CCOSKEYTYPE Key, unsigned long long tValue);
  438. // bool add(CCOSKEYTYPE Key, float tValue);
  439. // bool add(CCOSKEYTYPE Key, double tValue);
  440. // bool add(CCOSKEYTYPE Key, size_t KeySize, const char *pData, size_t datasize);//Key的上限<size的上限,失败返回
  441. //
  442. // //可变长度资源的添加,12BIT的KEY+高位1是不变,3bit的内容由LenSize来决定
  443. // bool addFlexible(CCOSKEYTYPE Key, size_t KeySize, const char* pData, size_t datasize);//Flexible key
  444. //
  445. //
  446. //
  447. //};