JSONStream.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef LIBJSON_GUARD_STREAM_H
  2. #define LIBJSON_GUARD_STREAM_H
  3. #include "JSONDebug.h"
  4. #ifdef JSON_STREAM
  5. #ifdef JSON_LESS_MEMORY
  6. #ifdef __GNUC__
  7. #pragma pack(push, 1)
  8. #elif _MSC_VER
  9. #pragma pack(push, JSONStream_pack, 1)
  10. #endif
  11. #endif
  12. #ifdef JSON_MEMORY_CALLBACKS
  13. #include "JSONMemory.h"
  14. #endif
  15. #ifndef JSON_LIBRARY
  16. class JSONNode; //foreward declaration
  17. typedef void (*json_stream_callback_t)(JSONNode &, void *);
  18. #endif
  19. class JSONStream {
  20. public:
  21. LIBJSON_OBJECT(JSONStream);
  22. JSONStream(json_stream_callback_t call_p, json_stream_e_callback_t call_e = NULL, void * callbackIdentifier = JSONSTREAM_SELF) json_nothrow;
  23. JSONStream(const JSONStream & orig) json_nothrow;
  24. JSONStream & operator =(const JSONStream & orig) json_nothrow;
  25. ~JSONStream(void) json_nothrow { LIBJSON_DTOR; }
  26. #ifdef JSON_LIBRARY
  27. JSONStream & operator << (const json_char * str) json_nothrow;
  28. #else
  29. JSONStream & operator << (const json_string & str) json_nothrow;
  30. #endif
  31. static void deleteJSONStream(JSONStream * stream) json_nothrow {
  32. #ifdef JSON_MEMORY_CALLBACKS
  33. stream -> ~JSONStream();
  34. libjson_free<JSONStream>(stream);
  35. #else
  36. delete stream;
  37. #endif
  38. }
  39. static JSONStream * newJSONStream(json_stream_callback_t callback, json_stream_e_callback_t call_e, void * callbackIdentifier) json_nothrow {
  40. #ifdef JSON_MEMORY_CALLBACKS
  41. return new(json_malloc<JSONStream>(1)) JSONStream(callback, call_e, callbackIdentifier);
  42. #else
  43. return new JSONStream(callback, call_e, callbackIdentifier);
  44. #endif
  45. }
  46. inline void reset() json_nothrow {
  47. state = true;
  48. buffer.clear();
  49. }
  50. JSON_PRIVATE
  51. inline void * getIdentifier(void) json_nothrow {
  52. if (callback_identifier == JSONSTREAM_SELF){
  53. return (void*)this;
  54. }
  55. return callback_identifier;
  56. }
  57. #if (JSON_READ_PRIORITY == HIGH) && (!(defined(JSON_LESS_MEMORY)))
  58. template<json_char ch>
  59. static size_t FindNextRelevant(const json_string & value_t, const size_t pos) json_nothrow json_read_priority;
  60. #else
  61. static size_t FindNextRelevant(json_char ch, const json_string & value_t, const size_t pos) json_nothrow json_read_priority;
  62. #endif
  63. void parse(void) json_nothrow;
  64. json_string buffer;
  65. json_stream_callback_t call;
  66. json_stream_e_callback_t err_call;
  67. void * callback_identifier;
  68. bool state BITS(1);
  69. };
  70. #ifdef JSON_LESS_MEMORY
  71. #ifdef __GNUC__
  72. #pragma pack(pop)
  73. #elif _MSC_VER
  74. #pragma pack(pop, JSONStream_pack)
  75. #endif
  76. #endif
  77. #endif
  78. #endif