resdataobject.h 14 KB

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