summaryrefslogtreecommitdiff
path: root/include/member.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/member.h')
-rw-r--r--include/member.h251
1 files changed, 122 insertions, 129 deletions
diff --git a/include/member.h b/include/member.h
index 49ac246..d56b495 100644
--- a/include/member.h
+++ b/include/member.h
@@ -1,23 +1,23 @@
/**
- * Member.h
+ * Member.h
*
- * When you're accessing members in an array or an object, you're
- * doing so via an internal member object. This is an object that
- * keeps track of the array to which it belonged, and that updates
- * the class when the object is modified
+ * When you're accessing members in an array or an object, you're
+ * doing this via an internal member object. This is an object that
+ * keeps track of the array to which it belongs, and that will update
+ * the array when the member is modified
*
- * This is an abstract class. You are not supposed to instantiate it
- * yourself. An instance of it is created when you call Value::operator[]
+ * You are not supposed to instantiate this class. An instance of it is
+ * created when you call Value::operator[]
*
- * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
- * @copyright 2013 Copernica BV
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
*/
/**
- * Set up namespace
+ * Set up namespace
*/
namespace PhpCpp {
-
+
/**
* Forward definitions
*/
@@ -26,52 +26,49 @@ class Value;
/**
* Member class
*/
-template <class Type>
+template <typename Type>
class Member
{
public:
- /**
- * Destructor
- */
- virtual ~Member() {}
+ /**
+ * Destructor
+ */
+ virtual ~Member() {}
- /**
- * Assign a value object to the array
- * @param value
- * @return Member
- */
- Member &operator=(const Value &value)
- {
- // set property in parent array
- _value->set(_index, value);
-
- // leap out if this is not a nested array access
- //if (!_parent) return *this;
-
- // nested array access, we need to update the parent too
- //_parent->operator=(*_value);
-
- // done
- return *this;
- }
+ /**
+ * Assign a value object to the array
+ * @param value
+ * @return Member
+ */
+ Member &operator=(const Value &value)
+ {
+ // set property in parent array
+ _base.set(_index, value);
- /**
- * Retrieve the original value
- * @return Value
- */
- Value value() const
- {
- return _value->get(_index);
- }
+ // if there is a parent, it should sets its value too
+ if (_parent) _parent->operator=(_base);
+
+ // done
+ return *this;
+ }
- /**
- * Cast to a value object
- * @return Value
- */
- operator Value () const
- {
- return _value->get(_index);
- }
+ /**
+ * Retrieve the original value
+ * @return Value
+ */
+ Value value() const
+ {
+ return _base.get(_index);
+ }
+
+ /**
+ * Cast to a value object
+ * @return Value
+ */
+ operator Value () const
+ {
+ return _base.get(_index);
+ }
/**
* Cast to a long
@@ -79,8 +76,8 @@ public:
*/
operator long () const
{
- return _value->get(_index).longValue();
- }
+ return _base.get(_index).longValue();
+ }
/**
* Cast to a boolean
@@ -88,8 +85,8 @@ public:
*/
operator bool () const
{
- return _value->get(_index).boolValue();
- }
+ return _base.get(_index).boolValue();
+ }
/**
* Cast to a string
@@ -97,8 +94,8 @@ public:
*/
operator std::string () const
{
- return _value->get(_index).stringValue();
- }
+ return _base.get(_index).stringValue();
+ }
/**
* Cast to byte array
@@ -106,8 +103,8 @@ public:
*/
operator const char * () const
{
- return _value->get(_index).rawValue();
- }
+ return _base.get(_index).rawValue();
+ }
/**
* Cast to a floating point
@@ -115,9 +112,9 @@ public:
*/
operator double () const
{
- return _value->get(_index).decimalValue();
- }
-
+ return _base.get(_index).decimalValue();
+ }
+
/**
* Array access operator
* This can be used for accessing arrays
@@ -126,8 +123,8 @@ public:
*/
Member operator[](int index)
{
- return _value->get(_index)[index];
- }
+ return _base.get(_index)[index].add(this);
+ }
/**
* Array access operator
@@ -137,76 +134,72 @@ public:
*/
Member operator[](const std::string &key)
{
- return _value->get(_index)[key];
- }
+ return _base.get(_index)[key].add(this);
+ }
- /**
- * Array access operator
- * This can be used for accessing associative arrays
- * @param key
- * @return Member
- */
- Member operator[](const char *key)
- {
- return _value->get(_index)[key];
- }
+ /**
+ * Array access operator
+ * This can be used for accessing associative arrays
+ * @param key
+ * @return Member
+ */
+ Member operator[](const char *key)
+ {
+ return _base.get(_index)[key].add(this);
+ }
private:
- /**
- * Constructor
- * @param value Parent element
- * @param index Index in the array
- */
- Member(Value *value, Type index) : _value(value), _index(index) {}
-
- /**
- * Private copy constructor
- * @param value Other element
- */
- Member(const Member<Type> &member) : _value(member._value), _index(member._index) {}
-
- /**
- * Set the member
- * @param member
- */
- Member<Type> &add(Member *parent)
- {
- _parent = parent;
- return *this;
- }
-
- /**
- * The array of which this is a member
- * @var Value
- */
- Value *_value;
-
- /**
- * The original index
- * @var Type
- */
- Type _index;
-
- /**
- * Parent member
- *
- * When accessing nested arrays a["a"]["b"] = 'true', the member
- * object that represents the "b" entry holds a pointer to the member
- * object that represents "a", so that it can tell its parent to
- * store itself in the top array too
- *
- * @var Member
- */
- Member *_parent = nullptr;
-
- /**
- * Value objects may create members
- */
- friend class Value;
+ /**
+ * Constructor
+ * @param base Base value
+ * @param index Index in the array
+ */
+ Member(const Value *base, Type index) : _base(*base), _index(index) {}
+
+ /**
+ * Protected copy constructor
+ * @param value Other element
+ */
+ Member(const Member<Type> &member) : _base(member._base), _index(member._index), _parent(member._parent) {}
+
+ /**
+ * Add parent
+ * @param parent
+ * @return Member
+ */
+ Member &add(Member *parent)
+ {
+ _parent = parent;
+ return *this;
+ }
+
+ /**
+ * The original index
+ * @var Type
+ */
+ Type _index;
+
+ /**
+ * Base value
+ * @var Value
+ */
+ Value _base;
+
+ /**
+ * Parent member (in case of nested members)
+ * @var Member
+ */
+ Member *_parent = nullptr;
+
+ /**
+ * Only value objects may construct members
+ */
+ friend class Value;
+
};
-
+
/**
- * End of namespace
+ * End of namespace
*/
}