ResDataObject.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #pragma once
  2. #ifndef RESDATAOBJECT_EXPORTS
  3. #ifdef _WIN64
  4. #ifdef _DEBUG
  5. #pragma comment(lib, "ResDataObjectX64D.lib")
  6. #else
  7. #pragma comment(lib, "ResDataObjectX64.lib")
  8. #endif
  9. #else
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "ResDataObjectD.lib")
  12. #else
  13. #pragma comment(lib, "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. //open close
  164. bool loadFile(const char *pfileName);//支持xml,json,ini扩展名
  165. bool SaveFile( const char *pfileName );//支持xml,json,ini扩展名
  166. //serialize
  167. const char *encode();
  168. bool decode(const char *pdata);
  169. //commons
  170. void clear();
  171. size_t size();
  172. bool IsObject();
  173. void MakeKeyLower();
  174. size_t GetKeyCount(const char *pKey);
  175. //erase
  176. bool eraseAllOf(const char *pKey);
  177. bool eraseOneOf(const char *pKey,size_t idx = 0);
  178. //set
  179. ResDataObject& operator = (const ResDataObject &tValue);
  180. //add
  181. ResDataObject& operator += (const ResDataObject &tValue);
  182. ResDataObject& operator = (const bool tValue);
  183. ResDataObject& operator = (const char tValue);
  184. ResDataObject& operator = (const unsigned char tValue);
  185. ResDataObject& operator = (const short tValue);
  186. ResDataObject& operator = (const unsigned short tValue);
  187. ResDataObject& operator = (const int tValue);
  188. ResDataObject& operator = (const unsigned int tValue);
  189. ResDataObject& operator = (const long tValue);
  190. ResDataObject& operator = (const unsigned long tValue);
  191. ResDataObject& operator = (const long long tValue);
  192. ResDataObject& operator = (const unsigned long long tValue);
  193. ResDataObject& operator = (const float tValue);
  194. ResDataObject& operator = (const double tValue);
  195. ResDataObject& operator = (const char* pVal);
  196. //get
  197. ResDataObject &operator [](size_t idx);
  198. ResDataObject &operator [](int idx);
  199. ResDataObject &operator [](const char *pKey);
  200. bool SetKey(size_t idx,const char *pKey);
  201. const char* GetKey(size_t idx);
  202. const char* GetKey(int idx);
  203. //get
  204. operator bool();
  205. operator char();
  206. operator unsigned char();
  207. operator short();
  208. operator unsigned short();
  209. operator int();
  210. operator unsigned int();
  211. operator long();
  212. operator unsigned long();
  213. operator long long();
  214. operator unsigned long long();
  215. operator float();
  216. operator double();
  217. operator const char*();
  218. //same name in the row
  219. int GetFirstOf(const char *pKey);
  220. int GetNextOf(const char *pKey,int PrevIdx);
  221. //add
  222. bool add(const char* pKey,bool tValue);
  223. bool add(const char* pKey,char tValue);
  224. bool add(const char* pKey,unsigned char tValue);
  225. bool add(const char* pKey,short tValue);
  226. bool add(const char* pKey,unsigned short tValue);
  227. bool add(const char* pKey,int tValue);
  228. bool add(const char* pKey,unsigned int tValue);
  229. bool add(const char* pKey,long tValue);
  230. bool add(const char* pKey,unsigned long tValue);
  231. bool add(const char* pKey,long long tValue);
  232. bool add(const char* pKey,unsigned long long tValue);
  233. bool add(const char* pKey,float tValue);
  234. bool add(const char* pKey,double tValue);
  235. bool add(const char* pKey,const char* pValue);
  236. bool add(const char* pKey,ResDataObject &dataobj);
  237. //bool add(const char* pKey,ResDataObject dataobj);
  238. bool update(const char* pKey,bool tValue);
  239. bool update(const char* pKey,char tValue);
  240. bool update(const char* pKey,unsigned char tValue);
  241. bool update(const char* pKey,short tValue);
  242. bool update(const char* pKey,unsigned short tValue);
  243. bool update(const char* pKey,int tValue);
  244. bool update(const char* pKey,unsigned int tValue);
  245. bool update(const char* pKey,long tValue);
  246. bool update(const char* pKey,unsigned long tValue);
  247. bool update(const char* pKey,long long tValue);
  248. bool update(const char* pKey,unsigned long long tValue);
  249. bool update(const char* pKey,float tValue);
  250. bool update(const char* pKey,double tValue);
  251. bool update(const char* pKey,const char* pValue);
  252. bool update(const char* pKey,ResDataObject &dataobj);
  253. //bool add(const char* pKey,ResDataObject dataobj);
  254. bool operator == (const char* pVal);
  255. bool operator == (const ResDataObject &Obj);
  256. };
  257. RESDATAOBJECT_C_API bool TryGetValue(ResDataObject &obj, const char *pKey, ResDataObject &res);
  258. class RESDATAOBJECT_API ExJsonDataObject
  259. {
  260. string *m_pKey;
  261. string *m_ValString;
  262. ResDataObject * m_pTargetObject;
  263. public:
  264. ExJsonDataObject();
  265. virtual ~ExJsonDataObject();
  266. ExJsonDataObject(const ExJsonDataObject &tValue);
  267. //base
  268. void SetKey(const char *pKey);
  269. const char *GetKey();
  270. //基于对象
  271. virtual ResDataObject &GetResDataObject();
  272. virtual bool SetResDataObject(ResDataObject &obj);
  273. //基于字符串
  274. //virtual const char *GetVal();
  275. //virtual bool SetVal(const char* pValString);
  276. //基于下标读取
  277. ResDataObject &operator [](const char *pKey);
  278. ExJsonDataObject& operator = (const ExJsonDataObject &tValue);
  279. };
  280. template<typename T> class BaseJsonDataObject
  281. {
  282. protected:
  283. T m_Data;
  284. std::string m_KeyString;
  285. std::string m_ValString;
  286. public:
  287. BaseJsonDataObject()
  288. {
  289. };
  290. //BaseJsonDataObject(BaseJsonDataObject &tValue)
  291. //{
  292. // m_pKeyString = new string();
  293. // m_pValString = new string();
  294. // m_pData = new T;
  295. // (*m_pKeyString) = (*tValue.m_pKeyString);
  296. // (*m_pValString) = (*tValue.m_pValString);
  297. // (*m_pData) = (*tValue.m_pData);
  298. //};
  299. ~BaseJsonDataObject()
  300. {
  301. };
  302. virtual void GetResDataObject(ResDataObject &obj)
  303. {
  304. obj.add(GetKey(), GetVal());
  305. };
  306. virtual bool SetResDataObject(ResDataObject &obj)
  307. {
  308. return SetVal((const char *)obj);
  309. };
  310. virtual const char *encode()
  311. {
  312. ResDataObject obj;
  313. if(obj.add(GetKey(),GetVal()) == false)
  314. {
  315. return NULL;
  316. }
  317. (m_ValString) = obj.encode();
  318. return m_ValString.c_str();
  319. };
  320. virtual bool Decode(const char *pString)
  321. {
  322. bool ret = true;
  323. ResDataObject obj;
  324. try {
  325. if(obj.decode(pString) == false)
  326. {
  327. return false;
  328. }
  329. ret = SetVal(obj[m_KeyString.c_str()]);
  330. }
  331. catch(...)
  332. {
  333. ret = false;
  334. }
  335. return ret;
  336. };
  337. virtual const char *GetKey()
  338. {
  339. return m_KeyString.c_str();
  340. };
  341. virtual const char *GetVal()
  342. {
  343. std::stringstream strm;
  344. strm << (m_Data);
  345. strm >> (m_ValString);
  346. return m_ValString.c_str();
  347. };
  348. virtual void SetKey(const char* pKeyString)
  349. {
  350. (m_KeyString) = pKeyString;
  351. };
  352. virtual bool SetVal(const char* pValString)
  353. {
  354. #pragma warning( push )
  355. #pragma warning( disable : 4800 4244 )
  356. bool ret = true;
  357. try {
  358. std::stringstream strm;
  359. std::string typenameT = typeid(T).name();
  360. transform(typenameT.begin(), typenameT.end(), typenameT.begin(), tolower);
  361. if(typenameT == "float")
  362. {
  363. assert(sizeof(float) == sizeof(unsigned int));
  364. unsigned int ret1 = 0;
  365. strm << (pValString);
  366. strm >> ret1;
  367. float *pF = (float*)&ret1;
  368. (m_Data) = (*pF);
  369. }
  370. else if(typenameT == "double")
  371. {
  372. assert(sizeof(double) == sizeof(unsigned long long));
  373. unsigned long long ret1 = 0;
  374. strm << (pValString);
  375. strm >> ret1;
  376. double *pF = (double*)&ret1;
  377. (m_Data) = (*pF);
  378. }
  379. else
  380. {
  381. strm << (pValString);
  382. strm >> (m_Data);
  383. }
  384. return true;
  385. }
  386. catch(...)
  387. {
  388. ret = false;
  389. }
  390. return ret;
  391. #pragma warning( pop )
  392. };
  393. virtual bool AddVal(const char* pValString)
  394. {
  395. return false;
  396. };
  397. virtual bool DelVal(const char* pValString)
  398. {
  399. return false;
  400. };
  401. //BaseJsonDataObject& operator = (BaseJsonDataObject &tValue)
  402. //{
  403. // if (this != &tValue)
  404. // {
  405. // (*m_pKeyString) = (*tValue.m_pKeyString);
  406. // (*m_pValString) = (*tValue.m_pValString);
  407. // (*m_pData) = (*tValue.m_pData);
  408. // }
  409. // return (*this);
  410. //}
  411. BaseJsonDataObject& operator = (T Val)
  412. {
  413. (m_Data) = Val;
  414. return (*this);
  415. };
  416. operator T()
  417. {
  418. return (m_Data);
  419. };
  420. };
  421. //Dios协议包中的数据区对象
  422. //数据区用此对象进行操作,
  423. //数据区的数据保存在DiosPacket中,而ResRawObject是引用这个数据,并非拷贝!!!
  424. //关系为 DiosPacket 1->n ResRawObject,注意DiosPacket被删除的时候,不要继续使用ResRawObject!!!
  425. //线程安全,非进程安全,多线程抢占自己加锁!!
  426. //class RESDATAOBJECT_API ResRawObject
  427. //{
  428. // size_t m_BuffLen;
  429. // size_t m_DataLen;
  430. // char *m_pszDataPosition;
  431. //
  432. //public:
  433. // ResRawObject();
  434. // ResRawObject(char *pData, size_t BuffSize,size_t DataSize);
  435. // virtual ~ResRawObject();
  436. //
  437. // //init op
  438. // void ClearData();
  439. //
  440. // //Get operation
  441. //
  442. // //尽量用在读取,不要用在更新上,因为数据是一个缓存,前面更新的话,将有可能破坏后面数据区
  443. // ResRawObject &operator [](DIOSKEYTYPE key);
  444. // operator bool();
  445. // operator char();
  446. // operator unsigned char();
  447. // operator short();
  448. // operator unsigned short();
  449. // operator int();
  450. // operator unsigned int();
  451. // operator long();
  452. // operator unsigned long();
  453. // operator long long();
  454. // operator unsigned long long();
  455. // operator float();
  456. // operator double();
  457. // operator const char*();
  458. //
  459. // //Set operation
  460. //
  461. // //固定长度资源的添加,12BIT的KEY是不变,3bit的内容由参数来决定
  462. // bool add(DIOSKEYTYPE Key, bool tValue);
  463. // bool add(DIOSKEYTYPE Key, char tValue);
  464. // bool add(DIOSKEYTYPE Key, unsigned char tValue);
  465. // bool add(DIOSKEYTYPE Key, short tValue);
  466. // bool add(DIOSKEYTYPE Key, unsigned short tValue);
  467. // bool add(DIOSKEYTYPE Key, int tValue);
  468. // bool add(DIOSKEYTYPE Key, unsigned int tValue);
  469. // bool add(DIOSKEYTYPE Key, long tValue);
  470. // bool add(DIOSKEYTYPE Key, unsigned long tValue);
  471. // bool add(DIOSKEYTYPE Key, long long tValue);
  472. // bool add(DIOSKEYTYPE Key, unsigned long long tValue);
  473. // bool add(DIOSKEYTYPE Key, float tValue);
  474. // bool add(DIOSKEYTYPE Key, double tValue);
  475. // bool add(DIOSKEYTYPE Key, size_t KeySize, const char *pData, size_t datasize);//Key的上限<size的上限,失败返回
  476. //
  477. // //可变长度资源的添加,12BIT的KEY+高位1是不变,3bit的内容由LenSize来决定
  478. // bool addFlexible(DIOSKEYTYPE Key, size_t KeySize, const char* pData, size_t datasize);//Flexible key
  479. //
  480. //
  481. //
  482. //};