ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/include/json/value.h
Revision: 1.3
Committed: Thu Aug 30 19:56:24 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.2: +37 -38 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

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