/* * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in doc/pod/license.pod. * * JSON-RPC for Ermyth * * $Id: value.h,v 1.4 2007/09/05 11:23:15 pippijn Exp $ */ //>>>>>>>>>>> value.h <<<<<<<<<< #include #include #include "svsconfig.h" namespace json { class ValueIterator; class ValueConstIterator; enum ValueType { nullValue, intValue, uintValue, realValue, stringValue, booleanValue, arrayValue, objectValue }; enum CommentPlacement { commentBefore, commentAfterOnSameLine, commentAfter, numberOfCommentPlacement }; /** \brief Lightweight wrapper to tag static string. * * Value constructor and objectValue member assignement takes advantage of the * StaticString and avoid the cost of string duplication when storing the * string or the member name. * * Example of usage: * * \code * json::Value aValue (StaticString ("some text")); * json::Value object; * static const StaticString code ("code"); * object[code] = 1234; * \endcode */ class StaticString { public: explicit StaticString (char const *czstring) : str_ (czstring) { } operator char const *() const { return str_; } char const *c_str() const { return str_; } private: char const *str_; }; class Value { friend class ValueIteratorBase; public: typedef std::vector Members; typedef ValueIterator iterator; typedef ValueConstIterator const_iterator; typedef unsigned ArrayIndex; static const Value null; static const int minInt; static const int maxInt; static const unsigned maxUInt; private: class CZString { public: enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy }; CZString (int index); CZString (char const *cstr, DuplicationPolicy allocate); CZString (const CZString &other); ~CZString (); CZString &operator = (const CZString &other); bool operator < (const CZString &other) const; bool operator == (const CZString &other) const; int index () const; char const *c_str () const; bool isStaticString () const; private: void swap (CZString &other); char const *cstr_; int index_; }; public: typedef std::map ObjectValues; public: Value (ValueType type = nullValue); Value (int value); Value (unsigned value); Value (double value); Value (char const *value); Value (const StaticString &value); Value (const std::string &value); Value (bool value); Value (const Value &other); ~Value (); Value &operator = (const Value &other); void swap (Value &other); ValueType type () const; bool operator < (const Value &other) const; bool operator <= (const Value &other) const; bool operator >= (const Value &other) const; bool operator > (const Value &other) const; bool operator == (const Value &other) const; bool operator != (const Value &other) const; operator char const * () const; operator std::string () const; operator int () const; operator unsigned () const; operator double () const; operator bool () const; bool isNull () const; bool isBool () const; bool isInt () const; bool isUInt () const; bool isIntegral () const; bool isDouble () const; bool isNumeric () const; bool isString () const; bool isArray () const; bool isObject () const; bool isConvertibleTo (ValueType other) const; unsigned size () const; bool empty () const; bool operator ! () const; void clear (); void resize (unsigned size); Value &operator [] (int index); Value &operator [] (unsigned index); const Value &operator [] (int index) const; const Value &operator [] (unsigned index) const; Value get (int index, const Value &defaultValue) const; Value get (unsigned index, const Value &defaultValue) const; bool isValidIndex (int index) const; bool isValidIndex (unsigned index) const; Value &append (const Value &value); Value &operator [] (char const *key); const Value &operator [] (char const *key) const; Value &operator [] (const std::string &key); const Value &operator [] (const std::string &key) const; Value &operator [] (const StaticString &key); Value get (char const *key, const Value &defaultValue) const; Value get (const std::string &key, const Value &defaultValue) const; Value removeMember (char const* key); Value removeMember (const std::string &key); bool isMember (char const *key) const; bool isMember (const std::string &key) const; Members getMemberNames () const; void setComment (char const *comment, CommentPlacement placement); void setComment (const std::string &comment, CommentPlacement placement); bool hasComment (CommentPlacement placement) const; std::string getComment (CommentPlacement placement) const; std::string toStyledString () const; const_iterator begin () const; const_iterator end () const; iterator begin (); iterator end (); private: Value &resolveReference (char const *key, bool isStatic); struct CommentInfo { CommentInfo (); ~CommentInfo (); void setComment (char const *text); char *comment_; }; struct MemberNamesTransform { typedef char const *result_type; char const *operator () (const CZString &name) const { return name.c_str (); } }; union ValueHolder { int int_; unsigned uint_; double real_; bool bool_; char *string_; ObjectValues *map_; } value_; ValueType type_ : 8; int allocated_ : 1; CommentInfo *comments_; }; class PathArgument { public: friend class Path; PathArgument (); PathArgument (unsigned index); PathArgument (char const *key); PathArgument (const std::string &key); private: enum Kind { kindNone = 0, kindIndex, kindKey }; std::string key_; unsigned index_; Kind kind_; }; class Path { public: Path (char const *path, const PathArgument &a1 = PathArgument (), const PathArgument &a2 = PathArgument (), const PathArgument &a3 = PathArgument (), const PathArgument &a4 = PathArgument (), const PathArgument &a5 = PathArgument ()); const Value &resolve (const Value &root) const; Value resolve (const Value &root, const Value &defaultValue) const; Value &make (Value &root) const; private: typedef std::vector InArgs; typedef std::vector Args; void makePath (char const *path, const InArgs &in); void addPathInArg (char const *path, const InArgs &in, InArgs::const_iterator &itInArg, PathArgument::Kind kind); void invalidPath (char const *path, int location); Args args_; }; class ValueAllocator { public: enum { unknown = ~0u // UINT_MAX }; virtual ~ValueAllocator (); virtual char *makeMemberName (char const *memberName) = 0; virtual void releaseMemberName (char *memberName) = 0; virtual char *duplicateStringValue (char const *value, unsigned length = unknown) = 0; virtual void releaseStringValue (char *value) = 0; }; class ValueIteratorBase { public: typedef unsigned size_t; typedef int difference_type; typedef ValueIteratorBase SelfType; ValueIteratorBase (); explicit ValueIteratorBase (const Value::ObjectValues::iterator ¤t); bool operator == (const SelfType &other) const { return isEqual (other); } bool operator != (const SelfType &other) const { return !isEqual (other); } difference_type operator - (const SelfType &other) const { return computeDistance (other); } Value key () const; unsigned index () const; char const *memberName () const; protected: Value &deref () const; void increment (); void decrement (); difference_type computeDistance (const SelfType &other) const; bool isEqual (const SelfType &other) const; void copy (const SelfType &other); private: Value::ObjectValues::iterator current_; }; class ValueConstIterator : public ValueIteratorBase { friend class Value; public: typedef unsigned size_t; typedef int difference_type; typedef const Value &reference; typedef const Value *pointer; typedef ValueConstIterator SelfType; ValueConstIterator (); private: explicit ValueConstIterator (const Value::ObjectValues::iterator ¤t); public: SelfType &operator = (const ValueIteratorBase &other); SelfType operator ++ (int) { SelfType temp (*this); ++*this; return temp; } SelfType operator -- (int) { SelfType temp (*this); --*this; return temp; } SelfType &operator -- () { decrement (); return *this; } SelfType &operator ++ () { increment (); return *this; } reference operator * () const { return deref (); } }; class ValueIterator : public ValueIteratorBase { friend class Value; public: typedef unsigned size_t; typedef int difference_type; typedef Value &reference; typedef Value *pointer; typedef ValueIterator SelfType; ValueIterator (); ValueIterator (const ValueConstIterator &other); ValueIterator (const ValueIterator &other); private: explicit ValueIterator (const Value::ObjectValues::iterator ¤t); public: SelfType &operator = (const SelfType &other); SelfType operator ++ (int) { SelfType temp (*this); ++*this; return temp; } SelfType operator -- (int) { SelfType temp (*this); --*this; return temp; } SelfType &operator -- () { decrement (); return *this; } SelfType &operator ++ () { increment (); return *this; } reference operator * () const { return deref (); } }; } // namespace json #endif // JSON_H