ListStruct_useless.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #pragma once
  2. #ifndef LIB_LISTSTRUCT_EXPORTS
  3. #ifdef WIN_64BIT
  4. #ifdef _DEBUG
  5. #pragma comment(lib, "libListStructX64D.lib")
  6. #else
  7. #pragma comment(lib, "libListStructX64.lib")
  8. #endif
  9. #else
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "libListStructD.lib")
  12. #else
  13. #pragma comment(lib, "libListStruct.lib")
  14. #endif
  15. #endif
  16. #endif
  17. #include <utility>
  18. #include <string>
  19. #include <vector>
  20. #include <algorithm>
  21. #include <sstream>
  22. using namespace std;
  23. //help note
  24. //数据对象是JSON格式的树状结构
  25. //对象有俩个成员,m_value 还有 vector<pair<string,数据对象>>
  26. //它俩当中必须有一个是有效的,先检查vector,若空 m_value有效.
  27. //JSON定义中数组的定义不要用,因为无法转换到XML对象.
  28. //XML定义中不要用属性,因为无法转换到JSON对象.
  29. //JSON定义为主,XML支持为辅.
  30. //ini定义只有俩层.
  31. //不要在key定义中使用 【.】 ,因为Boost会把【.】看成是模块的切换点.
  32. //get 用法
  33. //["key"],[idx],[xx].get<double>("key"),[xx].get<double>();
  34. //add 用法:不解释
  35. //update 用法:key,val,idx的形式,没有匹配值的情况用add,有值的情况update
  36. class ResDataObjectExption : public std::exception
  37. {
  38. std::wstring m_ExpContext;
  39. public:
  40. ResDataObjectExption(const wchar_t *pExp);
  41. ~ResDataObjectExption(void);
  42. const wchar_t *what();
  43. };
  44. //缺省的值和字符串以外不要用
  45. class EDATAOBJECT {
  46. protected:
  47. std::wstring m_value;
  48. public:
  49. EDATAOBJECT();
  50. ~EDATAOBJECT();
  51. //set
  52. EDATAOBJECT& operator = (const char* pVal);
  53. EDATAOBJECT& operator = (const wchar_t* pVal);
  54. template<typename T> EDATAOBJECT& operator = (const T tValue)
  55. {
  56. std::wstringstream strm;
  57. strm << tValue;
  58. (*this) = strm.str().c_str();
  59. return (*this);
  60. };
  61. //get
  62. operator char();
  63. operator unsigned char();
  64. operator const wchar_t*();
  65. template<typename T> operator T()
  66. {
  67. T ret1 = 0;
  68. std::wstringstream strm;
  69. strm << (const wchar_t*)(*this);
  70. strm >> ret1;
  71. return ret1;
  72. };
  73. };
  74. class ResDataObject
  75. {
  76. protected:
  77. std::wstring m_encode;//serialize
  78. std::wstring m_value;
  79. vector<pair<wstring,ResDataObject>> m_vec;
  80. public:
  81. ResDataObject(void);
  82. ~ResDataObject(void);
  83. //open close
  84. template<class T> bool loadFile(const T *pfileName);//支持xml,json,ini扩展名
  85. template<class T> bool SaveFile( const T *pfileName );//支持xml,json,ini扩展名
  86. //serialize
  87. const wchar_t *encode();
  88. bool decode(const wchar_t *pdata);
  89. //commons
  90. void clear();
  91. size_t size();
  92. bool IsObject();
  93. //erase
  94. template<typename T> bool eraseAllOf(const T *pKey);
  95. template<typename T> bool eraseOneOf(const T *pKey,size_t idx = 0);
  96. //set
  97. ResDataObject& operator = (const char tValue);
  98. ResDataObject& operator = (const unsigned char tValue);
  99. template<typename T> ResDataObject& operator = (const T tValue)
  100. {
  101. std::wstringstream strm;
  102. strm << tValue;
  103. (*this) = strm.str().c_str();
  104. return (*this);
  105. };
  106. ResDataObject& operator = (const char* pVal);
  107. ResDataObject& operator = (const wchar_t* pVal);
  108. //get
  109. ResDataObject &operator [](size_t idx);
  110. template<class T> ResDataObject &operator [](T idx)
  111. {
  112. return (*this)[(size_t)idx];
  113. };
  114. ResDataObject &operator [](const char *pKey);
  115. ResDataObject &operator [](const wchar_t *pKey);
  116. bool SetKey(size_t idx,const char *pKey);
  117. bool SetKey(size_t idx,const wchar_t *pKey);
  118. const wchar_t* GetKey(size_t idx);
  119. template<typename T> const wchar_t* GetKey(T idx)
  120. {
  121. return GetKey((size_t)idx);
  122. };
  123. //get
  124. operator char();
  125. operator unsigned char();
  126. operator const wchar_t*();
  127. template<typename T> operator T()
  128. {
  129. T ret1 = 0;
  130. std::wstringstream strm;
  131. strm << (const wchar_t*)(*this);
  132. strm >> ret1;
  133. return ret1;
  134. };
  135. //same name in the row
  136. int GetFirstOf(const char *pKey);
  137. int GetFirstOf(const wchar_t *pKey);
  138. int GetNextOf(const char *pKey,int PrevIdx);
  139. int GetNextOf(const wchar_t *pKey,int PrevIdx);
  140. //add
  141. template<typename T0> bool add(const T0* pKey,ResDataObject &dataobj);
  142. template<typename T0,typename T> bool add(const T0* pKey,const T tValue)
  143. {
  144. ResDataObject val1;
  145. val1 = tValue;
  146. return add(pKey,val1);
  147. return true;
  148. };
  149. };
  150. class libListInitial {
  151. public:
  152. libListInitial();
  153. ~libListInitial();
  154. bool testList();
  155. //void PrintObject(ResDataObject &obj,int level1);
  156. };