ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/include/json/value.h
Revision: 1.2
Committed: Tue Aug 28 17:08:12 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +9 -0 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# User Rev Content
1 pippijn 1.2 /*
2     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
3     * Rights to this code are as documented in doc/pod/gplicense.pod.
4     *
5     * JSON-RPC for Ermyth
6     *
7     * $Id: jsonrpc.C,v 1.4 2007-07-25 00:03:21 pippijn Exp $
8     */
9    
10 pippijn 1.1 //>>>>>>>>>>> value.h <<<<<<<<<<<//
11     #ifndef VALUE_H
12     #define VALUE_H
13    
14     #include <string>
15     #include <vector>
16     #include <map>
17    
18     #include "svsconfig.h"
19    
20     #define throw_unreachable unreachable_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__)
21     #define throw_unless(condition) if (!expect_false (condition)) throw_unless_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
22     #define throw_msg_unless(condition, message) if (!expect_false (condition)) throw_msg_unless_internal (__FILE__, __LINE__, __PRETTY_FUNCTION__, message)
23    
24     namespace json
25     {
26     class ValueIterator;
27     class ValueConstIterator;
28    
29     enum ValueType
30     {
31     nullValue = 0,
32     intValue,
33     uintValue,
34     realValue,
35     stringValue,
36     booleanValue,
37     arrayValue,
38     objectValue
39     };
40    
41     enum CommentPlacement
42     {
43     commentBefore = 0,
44     commentAfterOnSameLine,
45     commentAfter,
46     numberOfCommentPlacement
47     };
48    
49     /** \brief Lightweight wrapper to tag static string.
50     *
51     * Value constructor and objectValue member assignement takes advantage of the
52     * StaticString and avoid the cost of string duplication when storing the
53     * string or the member name.
54     *
55     * Example of usage:
56     * \code
57     * json::Value aValue (StaticString ("some text"));
58     * json::Value object;
59     * static const StaticString code ("code");
60     * object[code] = 1234;
61     * \endcode
62     */
63     class StaticString
64     {
65     public:
66     explicit StaticString (char const *czstring)
67     : str_ (czstring)
68     {
69     }
70    
71     operator char const *() const
72     {
73     return str_;
74     }
75    
76     char const *c_str() const
77     {
78     return str_;
79     }
80    
81     private:
82     char const *str_;
83     };
84    
85     class Value
86     {
87     friend class ValueIteratorBase;
88     public:
89     typedef std::vector<std::string> Members;
90     typedef ValueIterator iterator;
91     typedef ValueConstIterator const_iterator;
92     typedef unsigned ArrayIndex;
93    
94     static const Value null;
95     static const int minInt;
96     static const int maxInt;
97     static const unsigned maxUInt;
98    
99     private:
100     class CZString
101     {
102     public:
103     enum DuplicationPolicy
104     {
105     noDuplication = 0,
106     duplicate,
107     duplicateOnCopy
108     };
109     CZString (int index);
110     CZString (char const *cstr, DuplicationPolicy allocate);
111     CZString (const CZString &other);
112     ~CZString ();
113     CZString &operator = (const CZString &other);
114     bool operator < (const CZString &other) const;
115     bool operator == (const CZString &other) const;
116     int index () const;
117     char const *c_str () const;
118     bool isStaticString () const;
119     private:
120     void swap (CZString &other);
121     char const *cstr_;
122     int index_;
123     };
124    
125     public:
126     typedef std::map<CZString, Value> ObjectValues;
127    
128     public:
129     Value (ValueType type = nullValue);
130     Value (int value);
131     Value (unsigned value);
132     Value (double value);
133     Value (char const *value);
134     Value (const StaticString &value);
135     Value (const std::string &value);
136     Value (bool value);
137     Value (const Value &other);
138     ~Value ();
139    
140     Value &operator = (const Value &other);
141    
142     void swap (Value &other);
143    
144     ValueType type () const;
145    
146     bool operator < (const Value &other) const;
147     bool operator <= (const Value &other) const;
148     bool operator >= (const Value &other) const;
149     bool operator > (const Value &other) const;
150    
151     bool operator == (const Value &other) const;
152     bool operator != (const Value &other) const;
153    
154     operator char const * () const;
155     operator std::string () const;
156     operator int () const;
157     operator unsigned () const;
158     operator double () const;
159     operator bool () const;
160    
161     bool isNull () const;
162     bool isBool () const;
163     bool isInt () const;
164     bool isUInt () const;
165     bool isIntegral () const;
166     bool isDouble () const;
167     bool isNumeric () const;
168     bool isString () const;
169     bool isArray () const;
170     bool isObject () const;
171    
172     bool isConvertibleTo (ValueType other) const;
173    
174     unsigned size () const;
175    
176     bool empty () const;
177    
178     bool operator ! () const;
179    
180     void clear ();
181    
182     void resize (unsigned size);
183    
184     Value &operator [] (int index);
185     Value &operator [] (unsigned index);
186     const Value &operator [] (int index) const;
187     const Value &operator [] (unsigned index) const;
188     Value get (int index, const Value &defaultValue) const;
189     Value get (unsigned index, const Value &defaultValue) const;
190     bool isValidIndex (int index) const;
191     bool isValidIndex (unsigned index) const;
192    
193     Value &append (const Value &value);
194    
195     Value &operator [] (char const *key);
196     const Value &operator [] (char const *key) const;
197     Value &operator [] (const std::string &key);
198     const Value &operator [] (const std::string &key) const;
199     Value &operator [] (const StaticString &key);
200     Value get (char const *key, const Value &defaultValue) const;
201     Value get (const std::string &key, const Value &defaultValue) const;
202     Value removeMember (char const* key);
203     Value removeMember (const std::string &key);
204    
205     bool isMember (char const *key) const;
206     bool isMember (const std::string &key) const;
207    
208     Members getMemberNames () const;
209    
210     void setComment (char const *comment, CommentPlacement placement);
211     void setComment (const std::string &comment, CommentPlacement placement);
212     bool hasComment (CommentPlacement placement) const;
213     std::string getComment (CommentPlacement placement) const;
214    
215     std::string toStyledString () const;
216    
217     const_iterator begin () const;
218     const_iterator end () const;
219    
220     iterator begin ();
221     iterator end ();
222    
223     private:
224     Value &resolveReference (char const *key, bool isStatic);
225    
226     struct CommentInfo
227     {
228     CommentInfo ();
229     ~CommentInfo ();
230    
231     void setComment (char const *text);
232    
233     char *comment_;
234     };
235    
236     struct MemberNamesTransform
237     {
238     typedef char const *result_type;
239     char const *operator () (const CZString &name) const
240     {
241     return name.c_str ();
242     }
243     };
244    
245     union ValueHolder
246     {
247     int int_;
248     unsigned uint_;
249     double real_;
250     bool bool_;
251     char *string_;
252     ObjectValues *map_;
253     } value_;
254    
255     ValueType type_ : 8;
256     int allocated_ : 1;
257     CommentInfo *comments_;
258     };
259    
260    
261     class PathArgument
262     {
263     public:
264     friend class Path;
265    
266     PathArgument ();
267     PathArgument (unsigned index);
268     PathArgument (char const *key);
269     PathArgument (const std::string &key);
270    
271     private:
272     enum Kind
273     {
274     kindNone = 0,
275     kindIndex,
276     kindKey
277     };
278    
279     std::string key_;
280     unsigned index_;
281     Kind kind_;
282     };
283    
284     class Path
285     {
286     public:
287     Path (char const *path,
288     const PathArgument &a1 = PathArgument (),
289     const PathArgument &a2 = PathArgument (),
290     const PathArgument &a3 = PathArgument (),
291     const PathArgument &a4 = PathArgument (),
292     const PathArgument &a5 = PathArgument ());
293    
294     const Value &resolve (const Value &root) const;
295     Value resolve (const Value &root,
296     const Value &defaultValue) const;
297     Value &make (Value &root) const;
298    
299     private:
300     typedef std::vector<const PathArgument *> InArgs;
301     typedef std::vector<PathArgument> Args;
302    
303     void makePath (char const *path,
304     const InArgs &in);
305     void addPathInArg (char const *path,
306     const InArgs &in,
307     InArgs::const_iterator &itInArg,
308     PathArgument::Kind kind);
309     void invalidPath (char const *path,
310     int location);
311    
312     Args args_;
313     };
314    
315     class ValueAllocator
316     {
317     public:
318     enum { unknown = (unsigned)-1 };
319    
320     virtual ~ValueAllocator ();
321    
322     virtual char *makeMemberName (char const *memberName) = 0;
323     virtual void releaseMemberName (char *memberName) = 0;
324     virtual char *duplicateStringValue (char const *value,
325     unsigned length = unknown) = 0;
326     virtual void releaseStringValue (char *value) = 0;
327     };
328    
329     class ValueIteratorBase
330     {
331     public:
332     typedef unsigned size_t;
333     typedef int difference_type;
334     typedef ValueIteratorBase SelfType;
335    
336     ValueIteratorBase ();
337     explicit ValueIteratorBase (const Value::ObjectValues::iterator &current);
338    
339     bool operator == (const SelfType &other) const
340     {
341     return isEqual (other);
342     }
343    
344     bool operator != (const SelfType &other) const
345     {
346     return !isEqual (other);
347     }
348    
349     difference_type operator - (const SelfType &other) const
350     {
351     return computeDistance (other);
352     }
353    
354     Value key () const;
355    
356     unsigned index () const;
357    
358     char const *memberName () const;
359    
360     protected:
361     Value &deref () const;
362    
363     void increment ();
364    
365     void decrement ();
366    
367     difference_type computeDistance (const SelfType &other) const;
368    
369     bool isEqual (const SelfType &other) const;
370    
371     void copy (const SelfType &other);
372    
373     private:
374     Value::ObjectValues::iterator current_;
375     };
376    
377     class ValueConstIterator : public ValueIteratorBase
378     {
379     friend class Value;
380     public:
381     typedef unsigned size_t;
382     typedef int difference_type;
383     typedef const Value &reference;
384     typedef const Value *pointer;
385     typedef ValueConstIterator SelfType;
386    
387     ValueConstIterator ();
388     private:
389     explicit ValueConstIterator (const Value::ObjectValues::iterator &current);
390     public:
391     SelfType &operator = (const ValueIteratorBase &other);
392    
393     SelfType operator ++ (int)
394     {
395     SelfType temp (*this);
396     ++*this;
397     return temp;
398     }
399    
400     SelfType operator -- (int)
401     {
402     SelfType temp (*this);
403     --*this;
404     return temp;
405     }
406    
407     SelfType &operator -- ()
408     {
409     decrement ();
410     return *this;
411     }
412    
413     SelfType &operator ++ ()
414     {
415     increment ();
416     return *this;
417     }
418    
419     reference operator * () const
420     {
421     return deref ();
422     }
423     };
424    
425    
426     class ValueIterator : public ValueIteratorBase
427     {
428     friend class Value;
429     public:
430     typedef unsigned size_t;
431     typedef int difference_type;
432     typedef Value &reference;
433     typedef Value *pointer;
434     typedef ValueIterator SelfType;
435    
436     ValueIterator ();
437     ValueIterator (const ValueConstIterator &other);
438     ValueIterator (const ValueIterator &other);
439    
440     private:
441     explicit ValueIterator (const Value::ObjectValues::iterator &current);
442    
443     public:
444    
445     SelfType &operator = (const SelfType &other);
446    
447     SelfType operator ++ (int)
448     {
449     SelfType temp (*this);
450     ++*this;
451     return temp;
452     }
453    
454     SelfType operator -- (int)
455     {
456     SelfType temp (*this);
457     --*this;
458     return temp;
459     }
460    
461     SelfType &operator -- ()
462     {
463     decrement ();
464     return *this;
465     }
466    
467     SelfType &operator ++ ()
468     {
469     increment ();
470     return *this;
471     }
472    
473     reference operator * () const
474     {
475     return deref ();
476     }
477     };
478     } // namespace json
479    
480    
481     #endif // JSON_H