rename ArrayProxy to ElementProxy and ObjectProxy to PropertyProxy
[jansson.git] / janssonxx.h
index 6039646..bac4317 100644 (file)
 #include <cstdlib>
 
 namespace jansson {
+       // include in the jansson namespace
+#      include <jansson.h>
 
-// include in the jansson namespace
-#include <jansson.h>
+       class Iterator;
+       class Value;
+
+       namespace _private {
+               class ElementProxy;
+               class PropertyProxy;
+
+               // base class for JSON value interface
+               template <typename _Base>
+               class ValueBase : public _Base {
+               public:
+                       // empty constructor
+                       ValueBase() : _Base() {}
+
+                       // copy constructor
+                       ValueBase(const _Base& base) : _Base(base) {}
+
+                       // create reference to value
+                       ValueBase(json_t* json) : _Base(json) {}
 
-class Iterator;
-
-// represents any JSON value
-class Value {
-public:
-       // construct new Value with an undefined value
-       Value() : _value(0) {}
-
-       // free Value resources
-       ~Value() { json_decref(_value); }
-
-       // copy an existing Value
-       Value(const Value& e) : _value(json_incref(e._value)) {}
-
-       // make a reference to an existing json_t value
-       explicit Value(json_t* value) : _value(json_incref(value)) {}
-
-       // copy an existing Value
-       Value& operator=(const Value& e) {
-               if (&e != this) {
-                       json_decref(_value);
-                       _value = json_incref(e._value);
+                       // assignment operator
+                       ValueBase& operator=(const Value& value) { _Base::operator=(value); return *this; }
+
+                       // check value type
+                       bool is_undefined() const { return _Base::as_json() == 0; }
+                       bool is_object() const { return json_is_object(_Base::as_json()); }
+                       bool is_array() const { return json_is_array(_Base::as_json()); }
+                       bool is_string() const { return json_is_string(_Base::as_json()); }
+                       bool is_integer() const { return json_is_integer(_Base::as_json()); }
+                       bool is_real() const { return json_is_real(_Base::as_json()); }
+                       bool is_number() const { return json_is_number(_Base::as_json()); }
+                       bool is_true() const { return json_is_true(_Base::as_json()); }
+                       bool is_false() const { return json_is_false(_Base::as_json()); }
+                       bool is_boolean() const { return json_is_boolean(_Base::as_json()); }
+                       bool is_null() const { return json_is_null(_Base::as_json()); }
+
+                       // get size of array or object
+                       inline unsigned int size() const;
+
+                       // get value at array index (const version)
+                       inline const Value at(unsigned int index) const;
+
+                       inline const Value operator[](signed int index) const;
+                       inline const Value operator[](unsigned int index) const;
+                       inline const Value operator[](signed short index) const;
+                       inline const Value operator[](unsigned short index) const;
+                       inline const Value operator[](signed long index) const;
+                       inline const Value operator[](unsigned long index) const;
+
+                       // get value at array index (non-const version)
+                       inline ValueBase<ElementProxy> at(unsigned int index);
+
+                       inline ValueBase<ElementProxy> operator[](signed int index);
+                       inline ValueBase<ElementProxy> operator[](unsigned int index);
+                       inline ValueBase<ElementProxy> operator[](signed short index);
+                       inline ValueBase<ElementProxy> operator[](unsigned short index);
+                       inline ValueBase<ElementProxy> operator[](signed long index);
+                       inline ValueBase<ElementProxy> operator[](unsigned long index);
+
+                       // get object property (const version)
+                       inline const Value get(const char* key) const;
+
+                       inline const Value get(const std::string& key) const;
+                       inline const Value operator[](const char* key) const;
+                       inline const Value operator[](const std::string& key) const;
+
+                       // get object property (non-const version)
+                       inline ValueBase<PropertyProxy> get(const char* key);
+
+                       inline ValueBase<PropertyProxy> get(const std::string& key);
+                       inline ValueBase<PropertyProxy> operator[](const char* key);
+                       inline ValueBase<PropertyProxy> operator[](const std::string& key);
+
+                       // clear all array/object values
+                       inline void clear();
+
+                       // get value cast to specified type
+                       inline const char* as_cstring() const;
+                       inline std::string as_string() const;
+                       inline int as_integer() const;
+                       inline double as_real() const;
+                       inline double as_number() const;
+                       inline bool as_boolean() const;
+
+                       // set an object property (converts value to object is not one already)
+                       inline _Base& set_key(const char* key, const Value& value);
+
+                       inline _Base& set_key(const std::string& key, const Value& value);
+
+                       // set an array index (converts value to object is not one already)
+                       inline _Base& set_at(unsigned int index, const Value& value);
+
+                       // delete an object key
+                       inline _Base& del_key(const char* key);
+
+                       inline _Base& del_key(const std::string& key);
+
+                       // delete an item from an array by index
+                       inline _Base& del_at(unsigned int index);
+
+                       // insert an item into an array at a given index
+                       inline _Base& insert_at(unsigned int index, const Value& value);
+               };
+
+               // represents any JSON value, private base
+               class Basic {
+               public:
+                       // construct new Value with an undefined value
+                       Basic() : _value(0) {}
+
+                       // copy constructor
+                       Basic(const Basic& value) : _value(json_incref(value._value)) {}
+
+                       // make a reference to an existing json_t value
+                       explicit Basic(json_t* value) : _value(json_incref(value)) {}
+
+                       // free Value resources
+                       ~Basic() { json_decref(_value); }
+
+                       // copy an existing Value
+                       Basic& operator=(const Basic& e) {
+                               if (&e != this) {
+                                       json_decref(_value);
+                                       _value = json_incref(e._value);
+                               }
+                               return *this;
+                       }
+
+                       // get the underlying json_t
+                       json_t* as_json() const { return _value; }
+
+               protected:
+                       // take ownership of a json_t (does not increase reference count)
+                       static Basic _take(json_t* json) {
+                               Basic v;
+                               v._value = json;
+                               return v;
+                       }
+
+               private:
+                       // internal value pointer
+                       json_t* _value;
+               };
+
+               // proxies an array element
+               class ElementProxy {
+               public:
+                       // constructor
+                       ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
+
+                       // assign to the proxied element
+                       inline ElementProxy& operator=(const Value& value);
+
+                       // get the proxied element
+                       json_t* as_json() const { return json_array_get(_array, _index); }
+
+               private:
+                       // array object we wrap
+                       json_t* _array;
+
+                       // index of property
+                       unsigned int _index;
+               };
+
+               // proxies an object property
+               class PropertyProxy {
+               public:
+                       // constructor
+                       PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
+
+                       // assign to the proxied element
+                       inline PropertyProxy& operator=(const Value& value);
+
+                       // get the proxied element
+                       json_t* as_json() const { return json_object_get(_object, _key); }
+
+               private:
+                       // array object we wrap
+                       json_t* _object;
+
+                       // key of property
+                       const char* _key;
+               };
+
+       } // namespace jansson::_private
+
+       // represents any JSON value
+       class Value : public _private::ValueBase<_private::Basic> {
+       public:
+               // empty constructor
+               Value() : _private::ValueBase<_private::Basic>() {}
+
+               // copy constructor for base
+               Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
+       
+               // copy constructor for base
+               Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
+
+               // copy constructor
+               Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
+
+               // create reference to value
+               explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
+
+               // construct Value from input
+               static inline Value from(const char* value) { return Value::_take(json_string(value)); }
+               static inline Value from(const std::string& value) { return from(value.c_str()); }
+               static inline Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
+               static inline Value from(signed int value) { return Value::_take(json_integer(value)); }
+               static inline Value from(unsigned int value) { return Value::_take(json_integer(value)); }
+               static inline Value from(signed short value) { return Value::_take(json_integer(value)); }
+               static inline Value from(unsigned short value) { return Value::_take(json_integer(value)); }
+               static inline Value from(signed long value) { return Value::_take(json_integer(value)); }
+               static inline Value from(unsigned long value) { return Value::_take(json_integer(value)); }
+               static inline Value from(float value) { return Value::_take(json_real(value)); }
+               static inline Value from(double value) { return Value::_take(json_real(value)); }
+
+               // create a new empty object
+               static inline Value object() { return Value::_take(json_object()); }
+
+               // create a new empty array
+               static inline Value array() { return Value::_take(json_array()); }
+
+               // create a new null value
+               static inline Value null() { return Value::_take(json_null()); }
+
+               // load a file as a JSON value
+               static Value load_file(const char* path, json_error_t* error = 0) {
+                       return Value::_take(json_load_file(path, error));
                }
-               return *this;
-       }
-
-       // load a file as a JSON value
-       static Value load_file(const char* path, json_error_t* error = 0) {
-               return Value::_take(json_load_file(path, error));
-       }
-
-       // load a string as a JSON value
-       static Value load_string(const char* string, json_error_t* error = 0) {
-               return Value::_take(json_loads(string, error));
-       }
-
-       // write the value to a file
-       int save_file(const char* path, int flags = JSON_INDENT(2)) const {
-               return json_dump_file(_value, path, flags);
-       }
-
-       // write the value to a string (caller must deallocate with free()!)
-       char* save_string(int flags = JSON_INDENT(2)) const {
-               return json_dumps(_value, flags);
-       }
-
-       // construct Value from input
-       static Value from(const char* value) { return Value::_take(json_string(value)); }
-       static Value from(const std::string& value) { return from(value.c_str()); }
-       static Value from(bool value) { return Value::_take(value ? json_true() : json_false()); }
-       static Value from(int value) { return Value::_take(json_integer(value)); }
-       static Value from(double value) { return Value::_take(json_real(value)); }
-
-       // create a new empty object
-       static Value object() { return Value::_take(json_object()); }
-
-       // create a new empty array
-       static Value array() { return Value::_take(json_array()); }
-
-       // create a new null value
-       static Value null() { return Value::_take(json_null()); }
-
-       // get the underlying json_t
-       json_t* as_json() const { return _value; }
-
-       // check value type
-       bool is_undefined() const { return _value == 0; }
-       bool is_object() const { return json_is_object(_value); }
-       bool is_array() const { return json_is_array(_value); }
-       bool is_string() const { return json_is_string(_value); }
-       bool is_integer() const { return json_is_integer(_value); }
-       bool is_real() const { return json_is_real(_value); }
-       bool is_number() const { return json_is_number(_value); }
-       bool is_true() const { return json_is_true(_value); }
-       bool is_false() const { return json_is_false(_value); }
-       bool is_boolean() const { return json_is_boolean(_value); }
-       bool is_null() const { return json_is_null(_value); }
-
-       // get size of array or object
-       unsigned int size() const {
-               if (is_object())
-                       return json_object_size(_value);
-               else
-                       return json_array_size(_value);
-       }
-
-       // get value at array index (const version)
-       const Value at(unsigned int index) const {
-               return Value(json_array_get(_value, index));
-       }
-
-       const Value operator[](signed int index) const { return at(index); }
-       const Value operator[](unsigned int index) const { return at(index); }
-       const Value operator[](signed short index) const { return at(index); }
-       const Value operator[](unsigned short index) const { return at(index); }
-       const Value operator[](signed long index) const { return at(index); }
-       const Value operator[](unsigned long index) const { return at(index); }
-
-       // get value at array index (non-const version)
-       Value at(unsigned int index) {
-               return Value(json_array_get(_value, index));
-       }
-
-       Value operator[](signed int index) { return at(index); }
-       Value operator[](unsigned int index) { return at(index); }
-       Value operator[](signed short index) { return at(index); }
-       Value operator[](unsigned short index) { return at(index); }
-       Value operator[](signed long index) { return at(index); }
-       Value operator[](unsigned long index) { return at(index); }
-
-       // get object property
-       const Value get(const char* key) const {
-               return Value(json_object_get(_value, key));
-       }
-
-       const Value get(const std::string& key) const { return get(key.c_str()); }
-       const Value operator[](const char* key) const { return get(key); }
-       const Value operator[](const std::string& key) const { return get(key.c_str()); }
 
-       // clear all array/object values
-       void clear() {
-               if (is_object())
-                       json_object_clear(_value);
-               else
-                       json_array_clear(_value);
-       }
-
-       // get value cast to specified type
-       const char* as_cstring() const { return json_string_value(_value); }
-       std::string as_string() const { return as_cstring(); }
-       int as_integer() const { return json_integer_value(_value); }
-       double as_real() const { return json_real_value(_value); }
-       double as_number() const { return json_number_value(_value); }
-       bool as_boolean() const { return is_true(); }
-
-       // set an object property (converts value to object is not one already)
-       Value& set_key(const char* key, const Value& value) {
-               json_object_set(_value, key, value.as_json());
-               return *this;
-       }
-
-       Value& set_key(const std::string& key, const Value& value) {
-               return set_key(key.c_str(), value);
-       }
-
-       // set an array index (converts value to object is not one already)
-       Value& set_at(unsigned int index, const Value& value) {
-               if (index == size())
-                       json_array_append(_value, value.as_json());
-               else
-                       json_array_set(_value, index, value.as_json());
-               return *this;
-       }
-
-       Value& set_at(int index, const Value& value) {
-               return set_at(static_cast<unsigned int>(index), value);
-       }
+               // load a string as a JSON value
+               static Value load_string(const char* string, json_error_t* error = 0) {
+                       return Value::_take(json_loads(string, error));
+               }
 
-private:
-       // take ownership of a json_t (does not increase reference count)
-       static Value _take(json_t* json) {
-               Value v;
-               v._value = json;
-               return v;
-       }
+               // write the value to a file
+               int save_file(const char* path, int flags = 0) const {
+                       return json_dump_file(as_json(), path, flags);
+               }
 
-       // internal value pointer
-       json_t* _value;
-};
+               // write the value to a string (caller must deallocate with free()!)
+               char* save_string(int flags = 0) const {
+                       return json_dumps(as_json(), flags);
+               }
+       };
+
+       // iterators over a JSON object
+       class Iterator {
+       public:
+               // construct a new iterator for a given object
+               Iterator(const Value& value) : _object(value), _iter(0) {
+                       _iter = json_object_iter(_object.as_json());
+               }
 
-// iterators over a JSON object
-class Iterator {
-public:
-       // construct a new iterator for a given object
-       Iterator(const Value& value) : _object(value), _iter(0) {
-               _iter = json_object_iter(_object.as_json());
-       }
+               // construct a new iterator for a given object
+               Iterator(const _private::ValueBase<_private::PropertyProxy>& value) : _object(value.as_json()), _iter(0) {
+                       _iter = json_object_iter(_object.as_json());
+               }
 
-       // increment iterator
-       void next() {
-               _iter = json_object_iter_next(_object.as_json(), _iter);
-       }
+               // increment iterator
+               void next() {
+                       _iter = json_object_iter_next(_object.as_json(), _iter);
+               }
 
-       Iterator& operator++() { next(); return *this; }
+               Iterator& operator++() { next(); return *this; }
 
-       // test if iterator is still valid
-       bool valid() const { return _iter != 0; }
-       operator bool() const { return valid(); }
+               // test if iterator is still valid
+               bool valid() const { return _iter != 0; }
+               operator bool() const { return valid(); }
 
-       // get key
-       const char* ckey() const {
-               return json_object_iter_key(_iter);
-       }
+               // get key
+               const char* ckey() const {
+                       return json_object_iter_key(_iter);
+               }
 
-       std::string key() const { return ckey(); }
+               std::string key() const { return ckey(); }
 
-       // get value
-       const Value value() const {
-               return Value(json_object_iter_value(_iter));
-       }
+               // get value
+               const Value value() const {
+                       return Value(json_object_iter_value(_iter));
+               }
 
-       // dereference value
-       const Value operator*() const { return value(); }
+               // dereference value
+               const Value operator*() const { return value(); }
 
-private:
-       // disallow copying
-       Iterator(const Iterator&);
-       Iterator& operator=(const Iterator&);
+       private:
+               // disallow copying
+               Iterator(const Iterator&);
+               Iterator& operator=(const Iterator&);
 
-       // object being iterated over
-       Value _object;
+               // object being iterated over
+               Value _object;
 
-       // iterator value
-       void* _iter;
-};
+               // iterator value
+               void* _iter;
+       };
 
 } // namespace jansson
 
 // stream JSON value out
 std::ostream& operator<<(std::ostream& os, const jansson::Value& value) {
        char* tmp = value.save_string();
-       os << tmp;
-       free(tmp);
+       if (tmp != 0) {
+               os << tmp;
+               free(tmp);
+       }
        return os;
 }
 
@@ -255,4 +338,6 @@ std::istream& operator>>(std::istream& is, jansson::Value& value) {
        return is;
 }
 
+#include "janssonxx.tcc"
+
 #endif