aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2010-02-11 21:06:19 +0200
committerPetri Lehtinen <petri@digip.org>2010-02-11 21:06:19 +0200
commit8e61b7c0f0b796f238253b2a61f82870786d81c8 (patch)
treeae1d09d6adaed518950521aefef0bce9296e1e35 /src
parent35ddd2de2058efdb3443760c85a8aa3ce8078584 (diff)
parent7e8b128740336bad50e32bbe9dc86f47b406ce6a (diff)
downloadjansson-8e61b7c0f0b796f238253b2a61f82870786d81c8.zip
jansson-8e61b7c0f0b796f238253b2a61f82870786d81c8.tar.gz
jansson-8e61b7c0f0b796f238253b2a61f82870786d81c8.tar.bz2
Merge branch 'c++-enhance-proxies'
Diffstat (limited to 'src')
-rw-r--r--src/jansson.hpp15
-rw-r--r--src/jansson.ipp46
2 files changed, 54 insertions, 7 deletions
diff --git a/src/jansson.hpp b/src/jansson.hpp
index bf723bd..efb4b52 100644
--- a/src/jansson.hpp
+++ b/src/jansson.hpp
@@ -166,8 +166,9 @@ namespace json {
// proxies an array element
class ElementProxy {
public:
- // constructor
- ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
+ ElementProxy(json_t* array, unsigned int index);
+ ElementProxy(const ElementProxy& other);
+ ~ElementProxy();
// assign to the proxied element
inline ElementProxy& operator=(const Value& value);
@@ -186,8 +187,9 @@ namespace json {
// proxies an object property
class PropertyProxy {
public:
- // constructor
- PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
+ PropertyProxy(json_t* object, const char *key);
+ PropertyProxy(const PropertyProxy& other);
+ ~PropertyProxy();
// assign to the proxied element
inline PropertyProxy& operator=(const Value& value);
@@ -199,8 +201,11 @@ namespace json {
// array object we wrap
json_t* _object;
+ // iterator pointing to property
+ void* _iter;
+
// key of property
- const char* _key;
+ char* _key;
};
} // namespace json::detail
diff --git a/src/jansson.ipp b/src/jansson.ipp
index 5938f0f..e965ef7 100644
--- a/src/jansson.ipp
+++ b/src/jansson.ipp
@@ -8,6 +8,8 @@
#error "jansson.ipp may only be included from jansson.hpp"
#endif
+#include <string.h>
+
namespace json {
namespace detail {
// assignment operator
@@ -310,6 +312,20 @@ namespace json {
return v;
}
+ ElementProxy::ElementProxy(json_t* array, unsigned int index)
+ : _array(array), _index(index) {
+ json_incref(_array);
+ }
+
+ ElementProxy::ElementProxy(const ElementProxy& other)
+ : _array(other._array), _index(other._index) {
+ json_incref(_array);
+ }
+
+ ElementProxy::~ElementProxy() {
+ json_decref(_array);
+ }
+
// assign value to proxied array element
ElementProxy& ElementProxy::operator=(const Value& value) {
json_array_set(_array, _index, value.as_json());
@@ -321,14 +337,40 @@ namespace json {
return json_array_get(_array, _index);
}
+ PropertyProxy::PropertyProxy(json_t* object, const char* key)
+ : _object(object), _key(0) {
+ _iter = json_object_iter_at(object, key);
+ if(!_iter)
+ _key = strdup(key);
+ json_incref(_object);
+ }
+
+ PropertyProxy::PropertyProxy(const PropertyProxy& other)
+ : _object(other._object), _iter(other._iter), _key(0) {
+ if(other._key)
+ _key = strdup(other._key);
+ json_incref(_object);
+ }
+
+ PropertyProxy::~PropertyProxy() {
+ free(_key);
+ json_decref(_object);
+ }
+
// assign value to proxied object property
PropertyProxy& PropertyProxy::operator=(const Value& value) {
- json_object_set(_object, _key, value.as_json());
+ if(_iter)
+ json_object_iter_set(_object, _iter, value.as_json());
+ else
+ json_object_set(_object, _key, value.as_json());
return *this;
}
json_t* PropertyProxy::as_json() const {
- return json_object_get(_object, _key);
+ if(_iter)
+ return json_object_iter_value(_iter);
+ else
+ return json_object_get(_object, _key);
}
} // namespace json::detail