rename and move the _* private classes to _private namespace

This commit is contained in:
Sean Middleditch 2010-01-16 01:36:13 -08:00
parent f0be52f9f8
commit 49a64a6edf
2 changed files with 189 additions and 185 deletions

View File

@ -21,203 +21,207 @@ namespace jansson {
class Iterator; class Iterator;
class Value; class Value;
class _ArrayProxy;
class _ObjectProxy;
// base class for JSON value interface namespace _private {
template <typename _Base> class ArrayProxy;
class _ValueBase : public _Base { class ObjectProxy;
public:
// empty constructor
_ValueBase() : _Base() {}
// copy constructor // base class for JSON value interface
_ValueBase(const _Base& base) : _Base(base) {} template <typename _Base>
class ValueBase : public _Base {
public:
// empty constructor
ValueBase() : _Base() {}
// create reference to value // copy constructor
_ValueBase(json_t* json) : _Base(json) {} ValueBase(const _Base& base) : _Base(base) {}
// assignment operator // create reference to value
_ValueBase& operator=(const Value& value) { _Base::operator=(value); return *this; } ValueBase(json_t* json) : _Base(json) {}
// check value type // assignment operator
bool is_undefined() const { return _Base::as_json() == 0; } ValueBase& operator=(const Value& value) { _Base::operator=(value); return *this; }
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 // check value type
inline unsigned int size() const; 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 value at array index (const version) // get size of array or object
inline const Value at(unsigned int index) const; inline unsigned int size() const;
inline const Value operator[](signed int index) const; // get value at array index (const version)
inline const Value operator[](unsigned int index) const; inline const Value at(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 const Value operator[](signed int index) const;
inline _ValueBase<_ArrayProxy> at(unsigned int index); 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;
inline _ValueBase<_ArrayProxy> operator[](signed int index); // get value at array index (non-const version)
inline _ValueBase<_ArrayProxy> operator[](unsigned int index); inline ValueBase<ArrayProxy> at(unsigned int index);
inline _ValueBase<_ArrayProxy> operator[](signed short index);
inline _ValueBase<_ArrayProxy> operator[](unsigned short index);
inline _ValueBase<_ArrayProxy> operator[](signed long index);
inline _ValueBase<_ArrayProxy> operator[](unsigned long index);
// get object property (const version) inline ValueBase<ArrayProxy> operator[](signed int index);
inline const Value get(const char* key) const; inline ValueBase<ArrayProxy> operator[](unsigned int index);
inline ValueBase<ArrayProxy> operator[](signed short index);
inline ValueBase<ArrayProxy> operator[](unsigned short index);
inline ValueBase<ArrayProxy> operator[](signed long index);
inline ValueBase<ArrayProxy> operator[](unsigned long index);
inline const Value get(const std::string& key) const; // get object property (const version)
inline const Value operator[](const char* key) const; inline const Value get(const char* key) const;
inline const Value operator[](const std::string& key) const;
// get object property (non-const version) inline const Value get(const std::string& key) const;
inline _ValueBase<_ObjectProxy> get(const char* key); inline const Value operator[](const char* key) const;
inline const Value operator[](const std::string& key) const;
inline _ValueBase<_ObjectProxy> get(const std::string& key); // get object property (non-const version)
inline _ValueBase<_ObjectProxy> operator[](const char* key); inline ValueBase<ObjectProxy> get(const char* key);
inline _ValueBase<_ObjectProxy> operator[](const std::string& key);
// clear all array/object values inline ValueBase<ObjectProxy> get(const std::string& key);
inline void clear(); inline ValueBase<ObjectProxy> operator[](const char* key);
inline ValueBase<ObjectProxy> operator[](const std::string& key);
// get value cast to specified type // clear all array/object values
inline const char* as_cstring() const; inline void clear();
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) // get value cast to specified type
inline _Base& set_key(const char* key, const Value& value); 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;
inline _Base& set_key(const std::string& key, const Value& value); // set an object property (converts value to object is not one already)
inline _Base& set_key(const char* key, const Value& value);
// set an array index (converts value to object is not one already) inline _Base& set_key(const std::string& key, const Value& value);
inline _Base& set_at(unsigned int index, const Value& value);
// delete an object key // set an array index (converts value to object is not one already)
inline _Base& del_key(const char* key); inline _Base& set_at(unsigned int index, const Value& value);
inline _Base& del_key(const std::string& key); // delete an object key
inline _Base& del_key(const char* key);
// delete an item from an array by index inline _Base& del_key(const std::string& key);
inline _Base& del_at(unsigned int index);
// insert an item into an array at a given index // delete an item from an array by index
inline _Base& insert_at(unsigned int index, const Value& value); inline _Base& del_at(unsigned int index);
};
// represents any JSON value, private base // insert an item into an array at a given index
class _Value { inline _Base& insert_at(unsigned int index, const Value& value);
public: };
// construct new Value with an undefined value
_Value() : _value(0) {}
// copy constructor // represents any JSON value, private base
_Value(const _Value& value) : _value(json_incref(value._value)) {} class Basic {
public:
// construct new Value with an undefined value
Basic() : _value(0) {}
// make a reference to an existing json_t value // copy constructor
explicit _Value(json_t* value) : _value(json_incref(value)) {} Basic(const Basic& value) : _value(json_incref(value._value)) {}
// free Value resources // make a reference to an existing json_t value
~_Value() { json_decref(_value); } explicit Basic(json_t* value) : _value(json_incref(value)) {}
// copy an existing Value // free Value resources
_Value& operator=(const _Value& e) { ~Basic() { json_decref(_value); }
if (&e != this) {
json_decref(_value); // copy an existing Value
_value = json_incref(e._value); Basic& operator=(const Basic& e) {
if (&e != this) {
json_decref(_value);
_value = json_incref(e._value);
}
return *this;
} }
return *this;
}
// get the underlying json_t // get the underlying json_t
json_t* as_json() const { return _value; } json_t* as_json() const { return _value; }
protected: protected:
// take ownership of a json_t (does not increase reference count) // take ownership of a json_t (does not increase reference count)
static _Value _take(json_t* json) { static Basic _take(json_t* json) {
_Value v; Basic v;
v._value = json; v._value = json;
return v; return v;
} }
private: private:
// internal value pointer // internal value pointer
json_t* _value; json_t* _value;
}; };
// proxies an array element // proxies an array element
class _ArrayProxy { class ArrayProxy {
public: public:
// constructor // constructor
_ArrayProxy(json_t* array, unsigned int index) : _array(array), _index(index) {} ArrayProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
// assign to the proxied element // assign to the proxied element
inline _ArrayProxy& operator=(const Value& value); inline ArrayProxy& operator=(const Value& value);
// get the proxied element // get the proxied element
json_t* as_json() const { return json_array_get(_array, _index); } json_t* as_json() const { return json_array_get(_array, _index); }
private: private:
// array object we wrap // array object we wrap
json_t* _array; json_t* _array;
// index of property // index of property
unsigned int _index; unsigned int _index;
}; };
// proxies an object property // proxies an object property
class _ObjectProxy { class ObjectProxy {
public: public:
// constructor // constructor
_ObjectProxy(json_t* array, const char* key) : _object(array), _key(key) {} ObjectProxy(json_t* array, const char* key) : _object(array), _key(key) {}
// assign to the proxied element // assign to the proxied element
inline _ObjectProxy& operator=(const Value& value); inline ObjectProxy& operator=(const Value& value);
// get the proxied element // get the proxied element
json_t* as_json() const { return json_object_get(_object, _key); } json_t* as_json() const { return json_object_get(_object, _key); }
private: private:
// array object we wrap // array object we wrap
json_t* _object; json_t* _object;
// key of property // key of property
const char* _key; const char* _key;
}; };
} // namespace jansson::_private
// represents any JSON value // represents any JSON value
class Value : public _ValueBase<_Value> { class Value : public _private::ValueBase<_private::Basic> {
public: public:
// empty constructor // empty constructor
Value() : _ValueBase<_Value>() {} Value() : _private::ValueBase<_private::Basic>() {}
// copy constructor for base // copy constructor for base
Value(const _Value& value) : _ValueBase<_Value>(value) {} Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
// copy constructor for base // copy constructor for base
Value(const _ValueBase<_Value>& value) : _ValueBase<_Value>(value) {} Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
// copy constructor // copy constructor
Value(const Value& value) : _ValueBase<_Value>(value) {} Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
// create reference to value // create reference to value
explicit Value(json_t* json) : _ValueBase<_Value>(json) {} explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
// construct Value from input // construct Value from input
static inline Value from(const char* value) { return Value::_take(json_string(value)); } static inline Value from(const char* value) { return Value::_take(json_string(value)); }
@ -271,7 +275,7 @@ namespace jansson {
} }
// construct a new iterator for a given object // construct a new iterator for a given object
Iterator(const _ValueBase<_ObjectProxy>& value) : _object(value.as_json()), _iter(0) { Iterator(const _private::ValueBase<_private::ObjectProxy>& value) : _object(value.as_json()), _iter(0) {
_iter = json_object_iter(_object.as_json()); _iter = json_object_iter(_object.as_json());
} }

View File

@ -1,6 +1,6 @@
// get size of array or object // get size of array or object
template <typename _Base> template <typename _Base>
unsigned int jansson::_ValueBase<_Base>::size() const { unsigned int jansson::_private::ValueBase<_Base>::size() const {
if (is_object()) if (is_object())
return json_object_size(_Base::as_json()); return json_object_size(_Base::as_json());
else else
@ -9,71 +9,71 @@ unsigned int jansson::_ValueBase<_Base>::size() const {
// get value at array index (const version) // get value at array index (const version)
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::at(unsigned int index) const { const jansson::Value jansson::_private::ValueBase<_Base>::at(unsigned int index) const {
return jansson::Value(json_array_get(_Base::as_json(), index)); return jansson::Value(json_array_get(_Base::as_json(), index));
} }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed int index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed int index) const { return at(index); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned int index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned int index) const { return at(index); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed short index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed short index) const { return at(index); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned short index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned short index) const { return at(index); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](signed long index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](signed long index) const { return at(index); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](unsigned long index) const { return at(index); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](unsigned long index) const { return at(index); }
// get value at array index (non-const version) // get value at array index (non-const version)
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::at(unsigned int index) { jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::at(unsigned int index) {
return _ArrayProxy(_Base::as_json(), index); return ArrayProxy(_Base::as_json(), index);
} }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed int index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed int index) { return at(index); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned int index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned int index) { return at(index); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed short index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed short index) { return at(index); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned short index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned short index) { return at(index); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](signed long index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](signed long index) { return at(index); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ArrayProxy> jansson::_ValueBase<_Base>::operator[](unsigned long index) { return at(index); } jansson::_private::ValueBase<jansson::_private::ArrayProxy> jansson::_private::ValueBase<_Base>::operator[](unsigned long index) { return at(index); }
// get object property (const version) // get object property (const version)
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::get(const char* key) const { const jansson::Value jansson::_private::ValueBase<_Base>::get(const char* key) const {
return jansson::Value(json_object_get(_Base::as_json(), key)); return jansson::Value(json_object_get(_Base::as_json(), key));
} }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::get(const std::string& key) const { return get(key.c_str()); } const jansson::Value jansson::_private::ValueBase<_Base>::get(const std::string& key) const { return get(key.c_str()); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](const char* key) const { return get(key); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](const char* key) const { return get(key); }
template <typename _Base> template <typename _Base>
const jansson::Value jansson::_ValueBase<_Base>::operator[](const std::string& key) const { return get(key.c_str()); } const jansson::Value jansson::_private::ValueBase<_Base>::operator[](const std::string& key) const { return get(key.c_str()); }
// get object property (non-const version) // get object property (non-const version)
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::get(const char* key) { jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::get(const char* key) {
return _ObjectProxy(_Base::as_json(), key); return ObjectProxy(_Base::as_json(), key);
} }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::get(const std::string& key) { return get(key.c_str()); } jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::get(const std::string& key) { return get(key.c_str()); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::operator[](const char* key) { return get(key); } jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::operator[](const char* key) { return get(key); }
template <typename _Base> template <typename _Base>
jansson::_ValueBase<jansson::_ObjectProxy> jansson::_ValueBase<_Base>::operator[](const std::string& key) { return get(key.c_str()); } jansson::_private::ValueBase<jansson::_private::ObjectProxy> jansson::_private::ValueBase<_Base>::operator[](const std::string& key) { return get(key.c_str()); }
// clear all array/object values // clear all array/object values
template <typename _Base> template <typename _Base>
void jansson::_ValueBase<_Base>::clear() { void jansson::_private::ValueBase<_Base>::clear() {
if (is_object()) if (is_object())
json_object_clear(_Base::as_json()); json_object_clear(_Base::as_json());
else else
@ -82,36 +82,36 @@ void jansson::_ValueBase<_Base>::clear() {
// get value cast to specified type // get value cast to specified type
template <typename _Base> template <typename _Base>
const char* jansson::_ValueBase<_Base>::as_cstring() const { return json_string_value(_Base::as_json()); } const char* jansson::_private::ValueBase<_Base>::as_cstring() const { return json_string_value(_Base::as_json()); }
template <typename _Base> template <typename _Base>
std::string jansson::_ValueBase<_Base>::as_string() const { std::string jansson::_private::ValueBase<_Base>::as_string() const {
const char* tmp = as_cstring(); const char* tmp = as_cstring();
return tmp == 0 ? "" : tmp; return tmp == 0 ? "" : tmp;
} }
template <typename _Base> template <typename _Base>
int jansson::_ValueBase<_Base>::as_integer() const { return json_integer_value(_Base::as_json()); } int jansson::_private::ValueBase<_Base>::as_integer() const { return json_integer_value(_Base::as_json()); }
template <typename _Base> template <typename _Base>
double jansson::_ValueBase<_Base>::as_real() const { return json_real_value(_Base::as_json()); } double jansson::_private::ValueBase<_Base>::as_real() const { return json_real_value(_Base::as_json()); }
template <typename _Base> template <typename _Base>
double jansson::_ValueBase<_Base>::as_number() const { return json_number_value(_Base::as_json()); } double jansson::_private::ValueBase<_Base>::as_number() const { return json_number_value(_Base::as_json()); }
template <typename _Base> template <typename _Base>
bool jansson::_ValueBase<_Base>::as_boolean() const { return is_true(); } bool jansson::_private::ValueBase<_Base>::as_boolean() const { return is_true(); }
// set an object property (converts value to object is not one already) // set an object property (converts value to object is not one already)
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_key(const char* key, const jansson::Value& value) { _Base& jansson::_private::ValueBase<_Base>::set_key(const char* key, const jansson::Value& value) {
json_object_set(_Base::as_json(), key, value._Base::as_json()); json_object_set(_Base::as_json(), key, value._Base::as_json());
return *this; return *this;
} }
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_key(const std::string& key, const jansson::Value& value) { _Base& jansson::_private::ValueBase<_Base>::set_key(const std::string& key, const jansson::Value& value) {
return set_key(key.c_str(), value); return set_key(key.c_str(), value);
} }
// set an array index (converts value to object is not one already) // set an array index (converts value to object is not one already)
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::set_at(unsigned int index, const jansson::Value& value) { _Base& jansson::_private::ValueBase<_Base>::set_at(unsigned int index, const jansson::Value& value) {
if (index == size()) if (index == size())
json_array_append(_Base::as_json(), value._Base::as_json()); json_array_append(_Base::as_json(), value._Base::as_json());
else else
@ -121,38 +121,38 @@ template <typename _Base>
// delete an object key // delete an object key
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_key(const char* key) { _Base& jansson::_private::ValueBase<_Base>::del_key(const char* key) {
json_object_del(_Base::as_json(), key); json_object_del(_Base::as_json(), key);
return *this; return *this;
} }
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_key(const std::string& key) { _Base& jansson::_private::ValueBase<_Base>::del_key(const std::string& key) {
return del_key(key.c_str()); return del_key(key.c_str());
} }
// delete an item from an array by index // delete an item from an array by index
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::del_at(unsigned int index) { _Base& jansson::_private::ValueBase<_Base>::del_at(unsigned int index) {
json_array_remove(_Base::as_json(), index); json_array_remove(_Base::as_json(), index);
return *this; return *this;
} }
// insert an item into an array at a given index // insert an item into an array at a given index
template <typename _Base> template <typename _Base>
_Base& jansson::_ValueBase<_Base>::insert_at(unsigned int index, const jansson::Value& value) { _Base& jansson::_private::ValueBase<_Base>::insert_at(unsigned int index, const jansson::Value& value) {
json_array_insert(_Base::as_json(), index, value._Base::as_json()); json_array_insert(_Base::as_json(), index, value._Base::as_json());
return *this; return *this;
} }
// assign value to proxied array element // assign value to proxied array element
jansson::_ArrayProxy& jansson::_ArrayProxy::operator=(const Value& value) { jansson::_private::ArrayProxy& jansson::_private::ArrayProxy::operator=(const Value& value) {
json_array_set(_array, _index, value.as_json()); json_array_set(_array, _index, value.as_json());
return *this; return *this;
} }
// assign value to proxied object property // assign value to proxied object property
jansson::_ObjectProxy& jansson::_ObjectProxy::operator=(const Value& value) { jansson::_private::ObjectProxy& jansson::_private::ObjectProxy::operator=(const Value& value) {
json_object_set(_object, _key, value.as_json()); json_object_set(_object, _key, value.as_json());
return *this; return *this;
} }