ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/rpc/include/json/value.h
Revision: 1.1
Committed: Thu Jul 19 08:24:56 2007 UTC (19 years, 3 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Log Message:
initial import. the most important changes since Atheme are:
- fixed many memory leaks
- fixed many bugs
- converted to C++ and use more STL containers
- added a (not very enhanced yet) perl module
- greatly improved XML-RPC speed
- added a JSON-RPC module with code from json-cpp
- added a valgrind memcheck module to operserv
- added a more object oriented base64 implementation
- added a specialised unit test framework
- improved stability
- use gettimeofday() if available
- reworked adding/removing commands
- MemoServ IGNORE DEL can now remove indices

File Contents

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