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