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)
21 static void unreachable_internal (char const *file, int line, char const *function);
22 static void throw_unless_internal (char const *file, int line, char const *function, char const *condition);
23 static void throw_msg_unless_internal (char const *file, int line, char const *function, char const *message);
26 # define CURFUNC __PRETTY_FUNCTION__
27 #elif defined(__BORLANDC__)
28 # define CURFUNC __FUNC__
30 # define CURFUNC __FUNCTION__
33 #define throw_unreachable unreachable_internal (__FILE__, __LINE__, CURFUNC)
34 #define throw_unless(condition) if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, CURFUNC, #condition)
35 #define throw_msg_unless(condition, message) if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, CURFUNC, message)
40 class ValueConstIterator;
57 explicit StaticString (char const *czstring)
62 operator char const *() const
67 char const *c_str() const
78 friend class ValueIteratorBase;
80 typedef std::vector<std::string> Members;
81 typedef ValueIterator iterator;
82 typedef ValueConstIterator const_iterator;
83 typedef unsigned ArrayIndex;
85 static const Value null;
86 static const int minInt;
87 static const int maxInt;
88 static const unsigned maxUInt;
94 enum DuplicationPolicy
100 CZString (int index);
101 CZString (char const *cstr, DuplicationPolicy allocate);
102 CZString (const CZString &other);
104 CZString &operator = (const CZString &other);
105 bool operator < (const CZString &other) const;
106 bool operator == (const CZString &other) const;
108 char const *c_str () const;
109 bool isStaticString () const;
111 void swap (CZString &other);
117 typedef std::map<CZString, Value> ObjectValues;
120 Value (ValueType type = nullValue);
122 Value (unsigned value);
123 Value (double value);
124 Value (char const *value);
125 Value (const StaticString &value);
126 Value (const std::string &value);
128 Value (const Value &other);
131 Value &operator = (const Value &other);
133 void swap (Value &other);
135 ValueType type () const;
137 bool operator < (const Value &other) const;
138 bool operator <= (const Value &other) const;
139 bool operator >= (const Value &other) const;
140 bool operator > (const Value &other) const;
142 bool operator == (const Value &other) const;
143 bool operator != (const Value &other) const;
145 operator char const * () const;
146 operator std::string () const;
147 operator int () const;
148 operator unsigned () const;
149 operator double () const;
150 operator bool () const;
152 bool isNull () const;
153 bool isBool () const;
155 bool isUInt () const;
156 bool isIntegral () const;
157 bool isDouble () const;
158 bool isNumeric () const;
159 bool isString () const;
160 bool isArray () const;
161 bool isObject () const;
163 bool isConvertibleTo (ValueType other) const;
165 unsigned size () const;
169 bool operator ! () const;
173 void resize (unsigned size);
175 Value &operator [] (int index);
176 Value &operator [] (unsigned index);
177 const Value &operator [] (int index) const;
178 const Value &operator [] (unsigned index) const;
179 Value get (int index, const Value &defaultValue) const;
180 Value get (unsigned index, const Value &defaultValue) const;
181 bool isValidIndex (int index) const;
182 bool isValidIndex (unsigned index) const;
184 Value &append (const Value &value);
186 Value &operator [] (char const *key);
187 const Value &operator [] (char const *key) const;
188 Value &operator [] (const std::string &key);
189 const Value &operator [] (const std::string &key) const;
190 Value &operator [] (const StaticString &key);
191 Value get (char const *key, const Value &defaultValue) const;
192 Value get (const std::string &key, const Value &defaultValue) const;
193 Value removeMember (char const* key);
194 Value removeMember (const std::string &key);
196 bool isMember (char const *key) const;
197 bool isMember (const std::string &key) const;
199 Members getMemberNames () const;
201 const_iterator begin () const;
202 const_iterator end () const;
208 Value &resolveReference (char const *key, bool isStatic);
210 struct MemberNamesTransform
212 typedef char const *result_type;
213 char const *operator () (const CZString &name) const
215 return name.c_str ();
237 enum { unknown = (unsigned)-1 };
239 virtual ~ValueAllocator ();
241 virtual char *makeMemberName (char const *memberName) = 0;
242 virtual void releaseMemberName (char *memberName) = 0;
243 virtual char *duplicateStringValue (char const *value,
244 unsigned length = unknown) = 0;
245 virtual void releaseStringValue (char *value) = 0;
248 class ValueIteratorBase
251 typedef unsigned size_t;
252 typedef int difference_type;
253 typedef ValueIteratorBase SelfType;
255 ValueIteratorBase ();
256 explicit ValueIteratorBase (const Value::ObjectValues::iterator ¤t);
258 bool operator == (const SelfType &other) const
260 return isEqual (other);
263 bool operator != (const SelfType &other) const
265 return !isEqual (other);
268 difference_type operator - (const SelfType &other) const
270 return computeDistance (other);
275 unsigned index () const;
277 char const *memberName () const;
280 Value &deref () const;
286 difference_type computeDistance (const SelfType &other) const;
288 bool isEqual (const SelfType &other) const;
290 void copy (const SelfType &other);
293 Value::ObjectValues::iterator current_;
296 class ValueConstIterator : public ValueIteratorBase
300 typedef unsigned size_t;
301 typedef int difference_type;
302 typedef const Value &reference;
303 typedef const Value *pointer;
304 typedef ValueConstIterator SelfType;
306 ValueConstIterator ();
308 explicit ValueConstIterator (const Value::ObjectValues::iterator ¤t);
310 SelfType &operator = (const ValueIteratorBase &other);
312 SelfType operator ++ (int)
314 SelfType temp (*this);
319 SelfType operator -- (int)
321 SelfType temp (*this);
326 SelfType &operator -- ()
332 SelfType &operator ++ ()
338 reference operator * () const
345 class ValueIterator : public ValueIteratorBase
349 typedef unsigned size_t;
350 typedef int difference_type;
351 typedef Value &reference;
352 typedef Value *pointer;
353 typedef ValueIterator SelfType;
356 ValueIterator (const ValueConstIterator &other);
357 ValueIterator (const ValueIterator &other);
360 explicit ValueIterator (const Value::ObjectValues::iterator ¤t);
364 SelfType &operator = (const SelfType &other);
366 SelfType operator ++ (int)
368 SelfType temp (*this);
373 SelfType operator -- (int)
375 SelfType temp (*this);
380 SelfType &operator -- ()
386 SelfType &operator ++ ()
392 reference operator * () const
406 typedef char const *Location;
410 bool parse (const std::string &document, Value &root);
411 bool parse (char const *beginDoc, char const *endDOc, Value &root);
412 bool parse (std::istream &, Value &root);
414 std::string error_msgs () const;
419 tokenEndOfStream = 0,
430 tokenMemberSeparator,
447 std::string message_;
451 typedef std::deque<ErrorInfo> Errors;
453 bool expectToken (TokenType type, Token &token, char const *message);
454 bool readToken (Token &token);
456 bool match (Location pattern,
463 bool decodeNumber (Token &token);
464 bool decodeString (Token &token);
465 bool decodeString (Token &token, std::string &decoded);
466 bool decodeDouble (Token &token);
467 bool decodeUnicodeEscapeSequence (Token &token,
471 bool addError (const std::string &message,
474 bool recoverFromError (TokenType skipUntilToken);
475 bool addErrorAndRecover (const std::string &message,
477 TokenType skipUntilToken);
478 void skipUntilSpace ();
479 Value ¤tValue ();
481 void getLocationLineAndColumn (Location location,
484 std::string getLocationLineAndColumn (Location location) const;
486 typedef std::stack<Value *> Nodes;
489 std::string document_;
493 Location lastValueEnd_;
497 std::istream& operator >> (std::istream&, Value&);
512 std::string write (const Value &root);
515 void writeValue (const Value &value);
517 std::string document_;
520 std::string valueToString (int value);
521 std::string valueToString (unsigned value);
522 std::string valueToString (double value);
523 std::string valueToString (bool value);
524 std::string valueToQuotedString (char const *value);
528 * JSON-RPC implementation in C++
535 typedef void (Module::*method) (HTTPRequest *http, Value &request, Value &response);
543 typedef std::map<std::string, mfp> method_map;
544 extern method_map methods;
546 void add_method (char *name, Module const *mod, method mth);
547 void service (HTTPRequest *http, Value &request, Value &response);
548 void process (HTTPRequest *http, std::string &response, char const *request);