internalJSONNode.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #ifndef INTERNAL_JSONNODE_H
  2. #define INTERNAL_JSONNODE_H
  3. #include "JSONDebug.h"
  4. #include "JSONChildren.h"
  5. #include "JSONMemory.h"
  6. #include "JSONGlobals.h"
  7. #ifdef JSON_DEBUG
  8. #include <climits> //to check int value
  9. #endif
  10. #include "JSONSharedString.h"
  11. #ifdef JSON_LESS_MEMORY
  12. #ifdef __GNUC__
  13. #pragma pack(push, 1)
  14. #elif _MSC_VER
  15. #pragma pack(push, internalJSONNode_pack, 1)
  16. #endif
  17. #endif
  18. /*
  19. This class is the work horse of libjson, it handles all of the
  20. functinality of JSONNode. This object is reference counted for
  21. speed and memory reasons.
  22. If JSON_REF_COUNT is not on, this internal structure still has an important
  23. purpose, as it can be passed around by JSONNoders that are flagged as temporary
  24. */
  25. class JSONNode; //forward declaration
  26. #ifndef JSON_LIBRARY
  27. #define DECL_SET_INTEGER(type) void Set(type) json_nothrow json_write_priority; void Set(unsigned type) json_nothrow json_write_priority;
  28. #define DECL_CAST_OP(type) operator type() const json_nothrow; operator unsigned type() const json_nothrow;
  29. #endif
  30. #ifdef JSON_MUTEX_CALLBACKS
  31. #define initializeMutex(x) ,mylock(x)
  32. #else
  33. #define initializeMutex(x)
  34. #endif
  35. #if defined(JSON_PREPARSE) || !defined(JSON_READ_PRIORITY)
  36. #define SetFetched(b) (void)0
  37. #define Fetch() (void)0
  38. #define initializeFetch(x)
  39. #else
  40. #define initializeFetch(x) ,fetched(x)
  41. #endif
  42. #ifdef JSON_REF_COUNT
  43. #define initializeRefCount(x) ,refcount(x)
  44. #else
  45. #define initializeRefCount(x)
  46. #endif
  47. #ifdef JSON_COMMENTS
  48. #define initializeComment(x) ,_comment(x)
  49. #else
  50. #define initializeComment(x)
  51. #endif
  52. #ifdef JSON_LESS_MEMORY
  53. #define CHILDREN _value.Children
  54. #define DELETE_CHILDREN()\
  55. if (isContainer()){\
  56. jsonChildren::deleteChildren(CHILDREN);\
  57. }
  58. #define CHILDREN_TO_NULL() (void)0
  59. #define initializeChildren(x)
  60. #else
  61. #define CHILDREN Children
  62. #define DELETE_CHILDREN()\
  63. if (CHILDREN != 0) jsonChildren::deleteChildren(CHILDREN);
  64. #define CHILDREN_TO_NULL() CHILDREN = 0
  65. #define makeNotContainer() (void)0
  66. #define makeContainer() if (!CHILDREN) CHILDREN = jsonChildren::newChildren()
  67. #define initializeChildren(x) ,CHILDREN(x)
  68. #endif
  69. class internalJSONNode {
  70. public:
  71. LIBJSON_OBJECT(internalJSONNode);
  72. internalJSONNode(char mytype = JSON_NULL) json_nothrow json_hot;
  73. #ifdef JSON_READ_PRIORITY
  74. internalJSONNode(const json_string & unparsed) json_nothrow json_hot;
  75. internalJSONNode(const json_string & name_t, const json_string & value_t) json_nothrow json_read_priority;
  76. #endif
  77. internalJSONNode(const internalJSONNode & orig) json_nothrow json_hot;
  78. internalJSONNode & operator = (const internalJSONNode &) json_nothrow json_hot;
  79. ~internalJSONNode(void) json_nothrow json_hot;
  80. static internalJSONNode * newInternal(char mytype = JSON_NULL) json_hot;
  81. #ifdef JSON_READ_PRIORITY
  82. static internalJSONNode * newInternal(const json_string & unparsed) json_hot;
  83. static internalJSONNode * newInternal(const json_string & name_t, const json_string & value_t) json_hot;
  84. #endif
  85. static internalJSONNode * newInternal(const internalJSONNode & orig) json_hot; //not copyable, only by this class
  86. static void deleteInternal(internalJSONNode * ptr) json_nothrow json_hot;
  87. json_index_t size(void) const json_nothrow json_read_priority;
  88. bool empty(void) const json_nothrow;
  89. unsigned char type(void) const json_nothrow json_read_priority;
  90. json_string name(void) const json_nothrow json_read_priority;
  91. void setname(const json_string & newname) json_nothrow json_write_priority;
  92. #ifdef JSON_COMMENTS
  93. void setcomment(const json_string & comment) json_nothrow;
  94. json_string getcomment(void) const json_nothrow;
  95. #endif
  96. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  97. void preparse(void) json_nothrow;
  98. #endif
  99. #ifdef JSON_LIBRARY
  100. void push_back(JSONNode * node) json_nothrow;
  101. #else
  102. void push_back(const JSONNode & node) json_nothrow;
  103. #endif
  104. void reserve(json_index_t siz) json_nothrow;
  105. void push_front(const JSONNode & node) json_nothrow;
  106. JSONNode * pop_back(json_index_t pos) json_nothrow;
  107. JSONNode * pop_back(const json_string & name_t) json_nothrow;
  108. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  109. JSONNode * pop_back_nocase(const json_string & name_t) json_nothrow;
  110. #endif
  111. JSONNode * at(json_index_t pos) json_nothrow;
  112. //These return ** because pop_back needs them
  113. JSONNode ** at(const json_string & name_t) json_nothrow;
  114. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  115. JSONNode ** at_nocase(const json_string & name_t) json_nothrow;
  116. #endif
  117. void Set(const json_string & val) json_nothrow json_write_priority;
  118. #ifdef JSON_LIBRARY
  119. void Set(json_number val) json_nothrow json_write_priority;
  120. void Set(json_int_t val) json_nothrow json_write_priority;
  121. operator json_int_t() const json_nothrow;
  122. operator json_number() const json_nothrow;
  123. #else
  124. DECL_SET_INTEGER(char)
  125. DECL_SET_INTEGER(short)
  126. DECL_SET_INTEGER(int)
  127. DECL_SET_INTEGER(long)
  128. #ifndef JSON_ISO_STRICT
  129. DECL_SET_INTEGER(long long)
  130. void Set(long double val) json_nothrow json_write_priority;
  131. #endif
  132. void Set(float val) json_nothrow json_write_priority;
  133. void Set(double val) json_nothrow json_write_priority;
  134. DECL_CAST_OP(char)
  135. DECL_CAST_OP(short)
  136. DECL_CAST_OP(int)
  137. DECL_CAST_OP(long)
  138. #ifndef JSON_ISO_STRICT
  139. DECL_CAST_OP(long long)
  140. operator long double() const json_nothrow;
  141. #endif
  142. operator float() const json_nothrow;
  143. operator double() const json_nothrow;
  144. #endif
  145. operator json_string()const json_nothrow;
  146. operator bool() const json_nothrow;
  147. void Set(bool val) json_nothrow;
  148. bool IsEqualTo(const json_string & val) const json_nothrow;
  149. bool IsEqualTo(bool val) const json_nothrow;
  150. bool IsEqualTo(const internalJSONNode * val) const json_nothrow;
  151. template<typename T>
  152. bool IsEqualToNum(T val) const json_nothrow;
  153. internalJSONNode * incRef(void) json_nothrow;
  154. #ifdef JSON_REF_COUNT
  155. void decRef(void) json_nothrow json_hot;
  156. bool hasNoReferences(void) json_nothrow json_hot;
  157. #endif
  158. internalJSONNode * makeUnique(void) json_nothrow json_hot;
  159. JSONNode ** begin(void) const json_nothrow;
  160. JSONNode ** end(void) const json_nothrow;
  161. bool Fetched(void) const json_nothrow json_hot;
  162. #ifdef JSON_MUTEX_CALLBACKS
  163. void _set_mutex(void * mutex, bool unset = true) json_nothrow json_cold;
  164. void _unset_mutex(void) json_nothrow json_cold;
  165. #endif
  166. #ifdef JSON_WRITE_PRIORITY
  167. void DumpRawString(json_string & output) const json_nothrow json_write_priority;
  168. void WriteName(bool formatted, bool arrayChild, json_string & output) const json_nothrow json_write_priority;
  169. #ifdef JSON_ARRAY_SIZE_ON_ONE_LINE
  170. void WriteChildrenOneLine(unsigned int indent, json_string & output) const json_nothrow json_write_priority;
  171. #endif
  172. void WriteChildren(unsigned int indent, json_string & output) const json_nothrow json_write_priority;
  173. void WriteComment(unsigned int indent, json_string & output) const json_nothrow json_write_priority;
  174. void Write(unsigned int indent, bool arrayChild, json_string & output) const json_nothrow json_write_priority;
  175. #endif
  176. inline bool isContainer(void) const json_nothrow {
  177. return (_type == JSON_NODE || _type == JSON_ARRAY);
  178. }
  179. inline bool isNotContainer(void) const json_nothrow {
  180. return (_type != JSON_NODE && _type != JSON_ARRAY);
  181. }
  182. #ifdef JSON_LESS_MEMORY
  183. inline void makeNotContainer(void){
  184. if (isContainer()){
  185. jsonChildren::deleteChildren(CHILDREN);
  186. }
  187. }
  188. inline void makeContainer(void){
  189. if (isNotContainer()){
  190. CHILDREN = jsonChildren::newChildren();
  191. }
  192. }
  193. #endif
  194. void Nullify(void) const json_nothrow;
  195. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  196. void SetFetched(bool val) const json_nothrow json_hot;
  197. void Fetch(void) const json_nothrow json_hot; //it's const because it doesn't change the VALUE of the function
  198. #endif
  199. #ifdef JSON_READ_PRIORITY
  200. void FetchString(void) const json_nothrow json_read_priority;
  201. void FetchNode(void) const json_nothrow json_read_priority;
  202. void FetchArray(void) const json_nothrow json_read_priority;
  203. #endif
  204. void FetchNumber(void) const json_nothrow json_read_priority;
  205. #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
  206. static bool AreEqualNoCase(const json_char * ch_one, const json_char * ch_two) json_nothrow json_read_priority;
  207. #endif
  208. inline void clearname(void) json_nothrow {
  209. clearString(_name);
  210. }
  211. #ifdef JSON_DEBUG
  212. #ifndef JSON_LIBRARY
  213. JSONNode Dump(size_t & totalmemory) const json_nothrow;
  214. JSONNode DumpMutex(void) const json_nothrow;
  215. #endif
  216. #endif
  217. mutable unsigned char _type BITS(3);
  218. json_string _name;
  219. mutable bool _name_encoded BITS(1); //must be above name due to initialization list order
  220. mutable json_string _string; //these are both mutable because the string can change when it's fetched
  221. mutable bool _string_encoded BITS(1);
  222. //the value of the json
  223. union value_union_t {
  224. bool _bool BITS(1);
  225. json_number _number;
  226. #ifdef JSON_LESS_MEMORY
  227. jsonChildren * Children;
  228. #endif
  229. };
  230. mutable value_union_t _value; //internal structure changes depending on type
  231. #ifdef JSON_MUTEX_CALLBACKS
  232. void * mylock;
  233. #endif
  234. #ifdef JSON_REF_COUNT
  235. size_t refcount PACKED(20);
  236. #endif
  237. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  238. mutable bool fetched BITS(1);
  239. #endif
  240. #ifdef JSON_COMMENTS
  241. json_string _comment;
  242. #endif
  243. #ifndef JSON_LESS_MEMORY
  244. jsonChildren * CHILDREN;
  245. #endif
  246. };
  247. inline internalJSONNode::internalJSONNode(char mytype) json_nothrow : _type(mytype), _name(), _name_encoded(), _string(), _string_encoded(), _value()
  248. initializeMutex(0)
  249. initializeRefCount(1)
  250. initializeFetch(true)
  251. initializeComment(json_global(EMPTY_JSON_STRING))
  252. initializeChildren((_type == JSON_NODE || _type == JSON_ARRAY) ? jsonChildren::newChildren() : 0){
  253. LIBJSON_CTOR;
  254. #ifdef JSON_LESS_MEMORY
  255. //if not less memory, its in the initialization list
  256. if (isContainer()){
  257. CHILDREN = jsonChildren::newChildren();
  258. }
  259. #endif
  260. }
  261. inline internalJSONNode * internalJSONNode::incRef(void) json_nothrow {
  262. #ifdef JSON_REF_COUNT
  263. ++refcount;
  264. return this;
  265. #else
  266. return makeUnique();
  267. #endif
  268. }
  269. inline json_index_t internalJSONNode::size(void) const json_nothrow {
  270. if (isNotContainer()) return 0;
  271. Fetch();
  272. return CHILDREN -> size();
  273. }
  274. inline bool internalJSONNode::empty(void) const json_nothrow {
  275. if (isNotContainer()) return true;
  276. Fetch();
  277. return CHILDREN -> empty();
  278. }
  279. inline unsigned char internalJSONNode::type(void) const json_nothrow {
  280. return _type;
  281. }
  282. inline json_string internalJSONNode::name(void) const json_nothrow {
  283. return _name;
  284. }
  285. inline void internalJSONNode::setname(const json_string & newname) json_nothrow {
  286. #ifdef JSON_LESS_MEMORY
  287. JSON_ASSERT(newname.capacity() == newname.length(), JSON_TEXT("name object too large"));
  288. #endif
  289. _name = newname;
  290. _name_encoded = true;
  291. }
  292. #ifdef JSON_COMMENTS
  293. inline void internalJSONNode::setcomment(const json_string & comment) json_nothrow {
  294. _comment = comment;
  295. }
  296. inline json_string internalJSONNode::getcomment(void) const json_nothrow {
  297. return _comment;
  298. }
  299. #endif
  300. inline bool internalJSONNode::IsEqualTo(const json_string & val) const json_nothrow {
  301. if (type() != JSON_STRING) return false;
  302. Fetch();
  303. return _string == val;
  304. }
  305. inline bool internalJSONNode::IsEqualTo(bool val) const json_nothrow {
  306. if (type() != JSON_BOOL) return false;
  307. Fetch();
  308. return val == _value._bool;
  309. }
  310. template<typename T>
  311. inline bool internalJSONNode::IsEqualToNum(T val) const json_nothrow {
  312. if (type() != JSON_NUMBER) return false;
  313. Fetch();
  314. return (json_number)val == _value._number;
  315. }
  316. #ifdef JSON_REF_COUNT
  317. inline void internalJSONNode::decRef(void) json_nothrow {
  318. JSON_ASSERT(refcount != 0, JSON_TEXT("decRef on a 0 refcount internal"));
  319. --refcount;
  320. }
  321. inline bool internalJSONNode::hasNoReferences(void) json_nothrow {
  322. return refcount == 0;
  323. }
  324. #endif
  325. inline internalJSONNode * internalJSONNode::makeUnique(void) json_nothrow {
  326. #ifdef JSON_REF_COUNT
  327. if (refcount > 1){
  328. decRef();
  329. return newInternal(*this);
  330. }
  331. JSON_ASSERT(refcount == 1, JSON_TEXT("makeUnique on a 0 refcount internal"));
  332. return this;
  333. #else
  334. return newInternal(*this);
  335. #endif
  336. }
  337. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  338. inline void internalJSONNode::SetFetched(bool val) const json_nothrow {
  339. fetched = val;
  340. }
  341. #endif
  342. inline bool internalJSONNode::Fetched(void) const json_nothrow {
  343. #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
  344. return fetched;
  345. #else
  346. return true;
  347. #endif
  348. }
  349. inline JSONNode ** internalJSONNode::begin(void) const json_nothrow {
  350. JSON_ASSERT_SAFE(isContainer(), json_global(ERROR_NON_CONTAINER) + JSON_TEXT("begin"), return 0;);
  351. Fetch();
  352. return CHILDREN -> begin();
  353. }
  354. inline JSONNode ** internalJSONNode::end(void) const json_nothrow {
  355. JSON_ASSERT_SAFE(isContainer(), json_global(ERROR_NON_CONTAINER) + JSON_TEXT("end"), return 0;);
  356. Fetch();
  357. return CHILDREN -> end();
  358. }
  359. inline JSONNode * internalJSONNode::at(json_index_t pos) json_nothrow {
  360. JSON_ASSERT_SAFE(isContainer(), JSON_TEXT("calling at on non-container type"), return 0;);
  361. Fetch();
  362. return (*CHILDREN)[pos];
  363. }
  364. #if defined(JSON_LESS_MEMORY) && defined(__GNUC__)
  365. inline void internalJSONNode::reserve(json_index_t __attribute__((unused)) siz) json_nothrow
  366. #else
  367. inline void internalJSONNode::reserve(json_index_t siz) json_nothrow
  368. #endif
  369. {
  370. JSON_ASSERT_SAFE(isContainer(), json_global(ERROR_NON_CONTAINER) + JSON_TEXT("reserve"), return;);
  371. Fetch();
  372. jsonChildren::reserve2(CHILDREN, siz);
  373. }
  374. /*
  375. cast operators
  376. */
  377. #ifndef JSON_LIBRARY
  378. #ifdef JSON_ISO_STRICT
  379. #define BASE_CONVERT_TYPE long
  380. #else
  381. #define BASE_CONVERT_TYPE long long
  382. #endif
  383. #define IMP_SMALLER_INT_CAST_OP(_type, type_max, type_min)\
  384. inline internalJSONNode::operator _type() const json_nothrow {\
  385. JSON_ASSERT(_value._number > type_min, _string + json_global(ERROR_LOWER_RANGE) + JSON_TEXT(#_type));\
  386. JSON_ASSERT(_value._number < type_max, _string + json_global(ERROR_UPPER_RANGE) + JSON_TEXT(#_type));\
  387. JSON_ASSERT(_value._number == (json_number)((_type)(_value._number)), json_string(JSON_TEXT("(")) + json_string(JSON_TEXT(#_type)) + json_string(JSON_TEXT(") will truncate ")) + _string);\
  388. return (_type)static_cast<BASE_CONVERT_TYPE>(*this);\
  389. }
  390. IMP_SMALLER_INT_CAST_OP(char, CHAR_MAX, CHAR_MIN)
  391. IMP_SMALLER_INT_CAST_OP(unsigned char, UCHAR_MAX, 0)
  392. IMP_SMALLER_INT_CAST_OP(short, SHRT_MAX, SHRT_MIN)
  393. IMP_SMALLER_INT_CAST_OP(unsigned short, USHRT_MAX, 0)
  394. IMP_SMALLER_INT_CAST_OP(int, INT_MAX, INT_MIN)
  395. IMP_SMALLER_INT_CAST_OP(unsigned int, UINT_MAX, 0)
  396. #ifndef JSON_ISO_STRICT
  397. IMP_SMALLER_INT_CAST_OP(long, LONG_MAX, LONG_MIN)
  398. IMP_SMALLER_INT_CAST_OP(unsigned long, ULONG_MAX, 0)
  399. #endif
  400. #endif
  401. inline internalJSONNode::operator json_string() const json_nothrow {
  402. Fetch();
  403. return _string;
  404. }
  405. #ifndef JSON_LIBRARY
  406. #ifndef JSON_ISO_STRICT
  407. inline internalJSONNode::operator float() const json_nothrow {
  408. return static_cast<float>(static_cast<long double>(*this));
  409. }
  410. inline internalJSONNode::operator double() const json_nothrow {
  411. return static_cast<double>(static_cast<long double>(*this));
  412. }
  413. #else
  414. inline internalJSONNode::operator float() const json_nothrow {
  415. return static_cast<float>(static_cast<double>(*this));
  416. }
  417. #endif
  418. #endif
  419. #ifdef JSON_LESS_MEMORY
  420. #ifdef __GNUC__
  421. #pragma pack(pop)
  422. #elif _MSC_VER
  423. #pragma pack(pop, internalJSONNode_pack,)
  424. #endif
  425. #endif
  426. #endif