11 # define maybe_expect(expr,value) __builtin_expect ((expr),(value))
12 # define is_const(expr) __builtin_constant_p ((expr))
14 # define maybe_expect(expr,value) (expr)
15 # define is_const(expr) 0
18 #define expect_false(expr) maybe_expect ((expr) != 0, 0)
19 #define expect_true(expr) maybe_expect ((expr) != 0, 1)
22 # define CURFUNC __PRETTY_FUNCTION__
23 #elif defined(__BORLANDC__)
24 # define CURFUNC __FUNC__
26 # define CURFUNC __FUNCTION__
32 class ValueConstIterator;
49 explicit StaticString (char const *czstring)
54 operator char const *() const
59 char const *c_str() const
70 friend class ValueIteratorBase;
72 typedef std::vector<std::string> Members;
73 typedef ValueIterator iterator;
74 typedef ValueConstIterator const_iterator;
75 typedef unsigned ArrayIndex;
77 static const Value null;
78 static const int minInt;
79 static const int maxInt;
80 static const unsigned maxUInt;
86 enum DuplicationPolicy
93 CZString (char const *cstr, DuplicationPolicy allocate);
94 CZString (const CZString &other);
96 CZString &operator = (const CZString &other);
97 bool operator < (const CZString &other) const;
98 bool operator == (const CZString &other) const;
100 char const *c_str () const;
101 bool isStaticString () const;
103 void swap (CZString &other);
109 typedef std::map<CZString, Value> ObjectValues;
112 Value (ValueType type = nullValue);
114 Value (unsigned value);
115 Value (double value);
116 Value (char const *value);
117 Value (const StaticString &value);
118 Value (const std::string &value);
120 Value (const Value &other);
123 Value &operator = (const Value &other);
125 void swap (Value &other);
127 ValueType type () const;
129 bool operator < (const Value &other) const;
130 bool operator <= (const Value &other) const;
131 bool operator >= (const Value &other) const;
132 bool operator > (const Value &other) const;
134 bool operator == (const Value &other) const;
135 bool operator != (const Value &other) const;
137 operator char const * () const;
138 operator std::string () const;
139 operator int () const;
140 operator unsigned () const;
141 operator double () const;
142 operator bool () const;
144 bool isNull () const;
145 bool isBool () const;
147 bool isUInt () const;
148 bool isIntegral () const;
149 bool isDouble () const;
150 bool isNumeric () const;
151 bool isString () const;
152 bool isArray () const;
153 bool isObject () const;
155 bool isConvertibleTo (ValueType other) const;
157 unsigned size () const;
161 bool operator ! () const;
165 void resize (unsigned size);
167 Value &operator [] (int index);
168 Value &operator [] (unsigned index);
169 const Value &operator [] (int index) const;
170 const Value &operator [] (unsigned index) const;
171 Value get (int index, const Value &defaultValue) const;
172 Value get (unsigned index, const Value &defaultValue) const;
173 bool isValidIndex (int index) const;
174 bool isValidIndex (unsigned index) const;
176 Value &append (const Value &value);
178 Value &operator [] (char const *key);
179 const Value &operator [] (char const *key) const;
180 Value &operator [] (const std::string &key);
181 const Value &operator [] (const std::string &key) const;
182 Value &operator [] (const StaticString &key);
183 Value get (char const *key, const Value &defaultValue) const;
184 Value get (const std::string &key, const Value &defaultValue) const;
185 Value removeMember (char const* key);
186 Value removeMember (const std::string &key);
188 bool isMember (char const *key) const;
189 bool isMember (const std::string &key) const;
191 Members getMemberNames () const;
193 const_iterator begin () const;
194 const_iterator end () const;
200 Value &resolveReference (char const *key, bool isStatic);
202 struct MemberNamesTransform
204 typedef char const *result_type;
205 char const *operator () (const CZString &name) const
207 return name.c_str ();
229 enum { unknown = (unsigned)-1 };
231 virtual ~ValueAllocator ();
233 virtual char *makeMemberName (char const *memberName) = 0;
234 virtual void releaseMemberName (char *memberName) = 0;
235 virtual char *duplicateStringValue (char const *value,
236 unsigned length = unknown) = 0;
237 virtual void releaseStringValue (char *value) = 0;
240 class ValueIteratorBase
243 typedef unsigned size_t;
244 typedef int difference_type;
245 typedef ValueIteratorBase SelfType;
247 ValueIteratorBase ();
248 explicit ValueIteratorBase (const Value::ObjectValues::iterator ¤t);
250 bool operator == (const SelfType &other) const
252 return isEqual (other);
255 bool operator != (const SelfType &other) const
257 return !isEqual (other);
260 difference_type operator - (const SelfType &other) const
262 return computeDistance (other);
267 unsigned index () const;
269 char const *memberName () const;
272 Value &deref () const;
278 difference_type computeDistance (const SelfType &other) const;
280 bool isEqual (const SelfType &other) const;
282 void copy (const SelfType &other);
285 Value::ObjectValues::iterator current_;
288 class ValueConstIterator : public ValueIteratorBase
292 typedef unsigned size_t;
293 typedef int difference_type;
294 typedef const Value &reference;
295 typedef const Value *pointer;
296 typedef ValueConstIterator SelfType;
298 ValueConstIterator ();
300 explicit ValueConstIterator (const Value::ObjectValues::iterator ¤t);
302 SelfType &operator = (const ValueIteratorBase &other);
304 SelfType operator ++ (int)
306 SelfType temp (*this);
311 SelfType operator -- (int)
313 SelfType temp (*this);
318 SelfType &operator -- ()
324 SelfType &operator ++ ()
330 reference operator * () const
337 class ValueIterator : public ValueIteratorBase
341 typedef unsigned size_t;
342 typedef int difference_type;
343 typedef Value &reference;
344 typedef Value *pointer;
345 typedef ValueIterator SelfType;
348 ValueIterator (const ValueConstIterator &other);
349 ValueIterator (const ValueIterator &other);
352 explicit ValueIterator (const Value::ObjectValues::iterator ¤t);
356 SelfType &operator = (const SelfType &other);
358 SelfType operator ++ (int)
360 SelfType temp (*this);
365 SelfType operator -- (int)
367 SelfType temp (*this);
372 SelfType &operator -- ()
378 SelfType &operator ++ ()
384 reference operator * () const
398 typedef char const *Location;
402 bool parse (const std::string &document, Value &root);
403 bool parse (char const *beginDoc, char const *endDOc, Value &root);
404 bool parse (std::istream &, Value &root);
406 std::string error_msgs () const;
411 tokenEndOfStream = 0,
422 tokenMemberSeparator,
439 std::string message_;
443 typedef std::deque<ErrorInfo> Errors;
445 bool expectToken (TokenType type, Token &token, char const *message);
446 bool readToken (Token &token);
448 bool match (Location pattern,
455 bool decodeNumber (Token &token);
456 bool decodeString (Token &token);
457 bool decodeString (Token &token, std::string &decoded);
458 bool decodeDouble (Token &token);
459 bool decodeUnicodeEscapeSequence (Token &token,
463 bool addError (const std::string &message,
466 bool recoverFromError (TokenType skipUntilToken);
467 bool addErrorAndRecover (const std::string &message,
469 TokenType skipUntilToken);
470 void skipUntilSpace ();
471 Value ¤tValue ();
473 void getLocationLineAndColumn (Location location,
476 std::string getLocationLineAndColumn (Location location) const;
478 typedef std::stack<Value *> Nodes;
481 std::string document_;
485 Location lastValueEnd_;
489 std::istream& operator >> (std::istream&, Value&);
504 std::string write (const Value &root);
507 void writeValue (const Value &value);
509 std::string document_;
512 std::string valueToString (int value);
513 std::string valueToString (unsigned value);
514 std::string valueToString (double value);
515 std::string valueToString (bool value);
516 std::string valueToQuotedString (char const *value);
520 * JSON-RPC implementation in C++
527 typedef void (Module::*method) (HTTPRequest *http, Value &request, Value &response);
535 typedef std::map<std::string, mfp> method_map;
536 extern method_map methods;
538 void add_method (char *name, Module const *mod, method mth);
539 void service (HTTPRequest *http, Value &request, Value &response);
540 void process (HTTPRequest *http, std::string &response, char const *request);