archiver.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef ARCHIVER_H_
  2. #define ARCHIVER_H_
  3. #include <cstddef>
  4. #include <string>
  5. /**
  6. \class Archiver
  7. \brief Archiver concept
  8. Archiver can be a reader or writer for serialization or deserialization respectively.
  9. class Archiver {
  10. public:
  11. /// \returns true if the archiver is in normal state. false if it has errors.
  12. operator bool() const;
  13. /// Starts an object
  14. Archiver& StartObject();
  15. /// After calling StartObject(), assign a member with a name
  16. Archiver& Member(const char* name);
  17. /// After calling StartObject(), check if a member presents
  18. bool HasMember(const char* name) const;
  19. /// Ends an object
  20. Archiver& EndObject();
  21. /// Starts an array
  22. /// \param size If Archiver::IsReader is true, the size of array is written.
  23. Archiver& StartArray(size_t* size = 0);
  24. /// Ends an array
  25. Archiver& EndArray();
  26. /// Read/Write primitive types.
  27. Archiver& operator&(bool& b);
  28. Archiver& operator&(unsigned& u);
  29. Archiver& operator&(int& i);
  30. Archiver& operator&(double& d);
  31. Archiver& operator&(std::string& s);
  32. /// Write primitive types.
  33. Archiver& SetNull();
  34. //! Whether it is a reader.
  35. static const bool IsReader;
  36. //! Whether it is a writer.
  37. static const bool IsWriter;
  38. };
  39. */
  40. /// Represents a JSON reader which implements Archiver concept.
  41. class JsonReader {
  42. public:
  43. /// Constructor.
  44. /**
  45. \param json A non-const source json string for in-situ parsing.
  46. \note in-situ means the source JSON string will be modified after parsing.
  47. */
  48. JsonReader(const char* json);
  49. /// Destructor.
  50. ~JsonReader();
  51. // Archive concept
  52. operator bool() const { return !mError; }
  53. JsonReader& StartObject();
  54. JsonReader& Member(const char* name);
  55. bool HasMember(const char* name) const;
  56. JsonReader& EndObject();
  57. JsonReader& StartArray(size_t* size = 0);
  58. JsonReader& EndArray();
  59. JsonReader& operator&(bool& b);
  60. JsonReader& operator&(unsigned& u);
  61. JsonReader& operator&(int& i);
  62. JsonReader& operator&(double& d);
  63. JsonReader& operator&(std::string& s);
  64. JsonReader& SetNull();
  65. static const bool IsReader = true;
  66. static const bool IsWriter = !IsReader;
  67. private:
  68. JsonReader(const JsonReader&);
  69. JsonReader& operator=(const JsonReader&);
  70. void Next();
  71. // PIMPL
  72. void* mDocument; ///< DOM result of parsing.
  73. void* mStack; ///< Stack for iterating the DOM
  74. bool mError; ///< Whether an error has occurred.
  75. };
  76. class JsonWriter {
  77. public:
  78. /// Constructor.
  79. JsonWriter();
  80. /// Destructor.
  81. ~JsonWriter();
  82. /// Obtains the serialized JSON string.
  83. const char* GetString() const;
  84. // Archive concept
  85. operator bool() const { return true; }
  86. JsonWriter& StartObject();
  87. JsonWriter& Member(const char* name);
  88. bool HasMember(const char* name) const;
  89. JsonWriter& EndObject();
  90. JsonWriter& StartArray(size_t* size = 0);
  91. JsonWriter& EndArray();
  92. JsonWriter& operator&(bool& b);
  93. JsonWriter& operator&(unsigned& u);
  94. JsonWriter& operator&(int& i);
  95. JsonWriter& operator&(double& d);
  96. JsonWriter& operator&(std::string& s);
  97. JsonWriter& SetNull();
  98. static const bool IsReader = false;
  99. static const bool IsWriter = !IsReader;
  100. private:
  101. JsonWriter(const JsonWriter&);
  102. JsonWriter& operator=(const JsonWriter&);
  103. // PIMPL idiom
  104. void* mWriter; ///< JSON writer.
  105. void* mStream; ///< Stream buffer.
  106. };
  107. #endif // ARCHIVER_H__