25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.2 KiB

  1. #pragma once
  2. #ifndef JOBJECT_H
  3. #define JOBJECT_H
  4. #include <iostream>
  5. #include <vector>
  6. #ifdef WIN32
  7. #ifdef XJSON_EXPORTS
  8. #define XJSON_API __declspec(dllexport)
  9. #define XJSON_TEMPLATE __declspec(dllexport)
  10. #else
  11. #define XJSON_API __declspec(dllimport)
  12. #define XJSON_TEMPLATE
  13. #endif
  14. #define CALLBACK __stdcall
  15. #else
  16. #define XJSON_API
  17. #define XJSON_TEMPLATE
  18. #define CALLBACK
  19. #endif
  20. typedef void* PJSON_DATA;
  21. typedef void* PJSON_STRING;
  22. class XJSON_API JObject
  23. {
  24. public:
  25. JObject(JObject *pParent = NULL, const char *szName = "");
  26. virtual ~JObject(void);
  27. public:
  28. virtual int Parse(const char *szJson);
  29. virtual int Parse(PJSON_DATA pParent, int index = -1);
  30. virtual const char *ToString();
  31. virtual const char* Name(){return name;};
  32. virtual void SetName(const char*nm);
  33. virtual int ToInt();
  34. virtual bool ToBool();
  35. virtual int SetValue(const char *szValue);
  36. virtual int SetValue(bool bValue);
  37. virtual int SetValue(int nValue);
  38. virtual JObject *NewChild();
  39. virtual int AddChild(JObject *pNew);
  40. virtual int DelChild(const char *szName);
  41. virtual char *GetChannelName(int nChannelNo, const char* szStrName, char *pOutChannelName);
  42. virtual int GetBoolOfObjs(const char *szKeys, int nDefValue = 0);
  43. void JSON_SetParent(PJSON_DATA pParent);
  44. int JSON_AddToArray(JObject *pChild);
  45. int JSON_DelFromArray(int nIndex);
  46. protected:
  47. virtual void Clear();
  48. protected:
  49. char *name;
  50. PJSON_DATA _pJSData;
  51. PJSON_DATA _pRoot;
  52. std::vector<JObject *> _items;
  53. PJSON_STRING _sJsonReslt;
  54. };
  55. class XJSON_API JIntObj : public JObject
  56. {
  57. public:
  58. JIntObj(JObject *pParent = NULL, const char *szName = "");
  59. virtual ~JIntObj(void);
  60. public:
  61. int Value();
  62. virtual void operator=(const int nValue);
  63. };
  64. class XJSON_API JStrObj : public JObject
  65. {
  66. public:
  67. JStrObj(JObject *pParent = NULL, const char *szName = "");
  68. virtual ~JStrObj(void);
  69. public:
  70. const char *Value();
  71. virtual void operator=(const char *szValue);
  72. protected:
  73. void *_sValue;
  74. };
  75. class XJSON_API JIntHex : public JStrObj
  76. {
  77. public:
  78. JIntHex(JObject *pParent = NULL, const char *szName = "");
  79. virtual ~JIntHex(void);
  80. int SetValue(bool bValue);
  81. int SetValue(int nValue);
  82. int ToInt();
  83. public:
  84. int Value();
  85. virtual void operator=(int);
  86. };
  87. class XJSON_API JBoolObj : public JObject
  88. {
  89. public:
  90. JBoolObj(JObject *pParent = NULL, const char *szName = "");
  91. virtual ~JBoolObj(void);
  92. public:
  93. bool Value();
  94. virtual void operator=(const bool bValue);
  95. };
  96. class XJSON_API JDoubleObj : public JObject
  97. {
  98. public:
  99. JDoubleObj(JObject *pParent = NULL, const char *szName = "");
  100. virtual ~JDoubleObj(void);
  101. public:
  102. double Value();
  103. virtual void operator=(const double nValue);
  104. };
  105. template <class T>
  106. class XJSON_TEMPLATE JObjArray : public JObject
  107. {
  108. public:
  109. JObjArray(JObject *pParent = NULL, const char *szName = ""):
  110. JObject(pParent, szName)
  111. {
  112. };
  113. virtual ~JObjArray(void){};
  114. public:
  115. T &operator[] (const int index)
  116. {
  117. return *_arrayItems[index];
  118. };
  119. void Clear()
  120. {
  121. typename std::vector<T*>::iterator iter = _arrayItems.begin();
  122. while ( iter != _arrayItems.end() )
  123. {
  124. if( *iter )
  125. {
  126. delete *iter;
  127. }
  128. iter++;
  129. }
  130. _arrayItems.clear();
  131. _items.clear();
  132. JObject::Clear();
  133. };
  134. JObject *NewChild()
  135. {
  136. T *pNew = new T();
  137. _arrayItems.push_back(pNew);
  138. return (JObject *)pNew;
  139. };
  140. virtual int AddChild(JObject *pChild)
  141. {
  142. JSON_AddToArray(pChild);
  143. pChild->JSON_SetParent(NULL);
  144. _arrayItems.push_back((T*)pChild);
  145. return 0;
  146. };
  147. virtual int DelChild(int nIndex)
  148. {
  149. if (nIndex < 0 || nIndex >= _arrayItems.size())
  150. {
  151. return -1;
  152. }
  153. JSON_DelFromArray(nIndex);
  154. delete _arrayItems[nIndex];
  155. _arrayItems.erase(_arrayItems.begin() + nIndex);
  156. return 0;
  157. }
  158. int Size()
  159. {
  160. return (int)_arrayItems.size();
  161. };
  162. protected:
  163. std::vector<T*> _arrayItems;
  164. };
  165. template <class T>
  166. class XJSON_TEMPLATE JObjArrayObject : public JObject
  167. {
  168. public:
  169. JObjArrayObject(JObject *pParent = NULL, const char *szName = ""):
  170. JObject(NULL, ""),
  171. objs(this, szName)
  172. {
  173. };
  174. virtual ~JObjArrayObject(void){};
  175. virtual int AddChild(JObject *pNew)
  176. {
  177. return objs.AddChild(pNew);
  178. };
  179. public:
  180. JObjArray<T> objs;
  181. };
  182. #endif //JOBJECT_H