summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-29 16:01:04 -0700
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-29 16:01:04 -0700
commita2b81c7e8642da249acbfa1ca4fe0362a900f0e2 (patch)
tree875785f2e9a1533ae22552f5856e854f3d7d0cd8 /include
parentb8d747580db76bc3e51bae6c3cdae49b5b44e20b (diff)
More progress on implementing nested arrays
Diffstat (limited to 'include')
-rw-r--r--include/member.h251
-rw-r--r--include/type.h2
-rw-r--r--include/value.h154
3 files changed, 232 insertions, 175 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
*/
}
diff --git a/include/type.h b/include/type.h
index f3e357f..200d658 100644
--- a/include/type.h
+++ b/include/type.h
@@ -19,7 +19,7 @@ namespace PhpCpp {
*/
typedef enum _Type {
nullType = 0,
- intType = 1,
+ longType = 1,
decimalType = 2,
boolType = 3,
arrayType = 4,
diff --git a/include/value.h b/include/value.h
index 6619dcb..2cc9029 100644
--- a/include/value.h
+++ b/include/value.h
@@ -63,6 +63,12 @@ public:
Value(bool value);
/**
+ * Constructor based on single character
+ * @param value
+ */
+ Value(char value);
+
+ /**
* Constructor based on string value
* @param value
*/
@@ -95,6 +101,12 @@ public:
Value(const Value &that);
/**
+ * Move constructor
+ * @param value
+ */
+ Value(Value &&that);
+
+ /**
* Destructor
*/
virtual ~Value();
@@ -132,6 +144,13 @@ public:
* @param value
* @return Value
*/
+ Value &operator=(char value);
+
+ /**
+ * Assignment operator
+ * @param value
+ * @return Value
+ */
Value &operator=(const std::string &value);
/**
@@ -158,49 +177,83 @@ public:
* Change the internal type of the variable
* @param Type
*/
- void setType(Type type);
+ Value &setType(Type type);
+
+ /**
+ * Make a clone of the value with the same type
+ * @return Value
+ */
+ Value clone() const;
+
+ /**
+ * Make a clone of the value with a different type
+ * @param type
+ * @return Value
+ */
+ Value clone(Type type) const;
/**
* Is this a NULL value?
* @return bool
*/
- bool isNull() const;
+ bool isNull() const
+ {
+ return type() == nullType;
+ }
/**
* Is this an integer value?
* @return bool
*/
- bool isLong() const;
+ bool isLong() const
+ {
+ return type() == longType;
+ }
/**
* Is this a boolean value?
* @return bool
*/
- bool isBool() const;
+ bool isBool() const
+ {
+ return type() == boolType;
+ }
/**
* Is this a string value?
* @return bool
*/
- bool isString() const;
+ bool isString() const
+ {
+ return type() == stringType;
+ }
/**
* Is this a decimal value?
* @return bool
*/
- bool isDecimal() const;
+ bool isDecimal() const
+ {
+ return type() == decimalType;
+ }
/**
* Is this an object value?
* @return bool
*/
- bool isObject() const;
+ bool isObject() const
+ {
+ return type() == objectType;
+ }
/**
* Is this an array value?
* @return bool
*/
- bool isArray() const;
+ bool isArray() const
+ {
+ return type() == arrayType;
+ }
/**
* Retrieve the value as integer
@@ -217,7 +270,7 @@ public:
/**
* Retrieve the raw string value
* Warning: Only use this for NULL terminated strings, or use it in combination
- * with the string size to prevent that you access data outside the buffer
+ * with the string size to prevent that you access data outside the buffer
* @return const char *
*/
const char *rawValue() const;
@@ -270,12 +323,15 @@ public:
* @param key
* @return bool
*/
- bool contains(const std::string &key) const;
+ bool contains(const std::string &key) const
+ {
+ return contains(key.c_str(), key.size());
+ }
/**
* Is a certain key set in the array
* @param key
- * @param size
+ * @param size
* @return bool
*/
bool contains(const char *key, int size) const;
@@ -313,8 +369,8 @@ public:
*/
operator const char * () const
{
- return rawValue();
- }
+ return rawValue();
+ }
/**
* Cast to a floating point
@@ -327,63 +383,71 @@ public:
/**
* Get access to a certain array member
- * @param index
+ * @param index
* @return Value
*/
Value get(int index) const;
/**
* Get access to a certain assoc member
- * @param key
- * @param size
- * @return Value
+ * @param key
+ * @param size
+ * @return Value
*/
Value get(const char *key, int size=-1) const;
/**
* Get access to a certain assoc member
- * @param key
- * @return Value
+ * @param key
+ * @return Value
*/
Value get(const std::string &key) const
{
- return get(key.c_str(), key.size());
- }
+ return get(key.c_str(), key.size());
+ }
/**
* Set a certain property
- * @param index
- * @param value
+ * Calling this method will turn the value into an array
+ * @param index Index of the property to set
+ * @param value Value to set
+ * @return Value The value that was set
*/
- void set(int index, const Value &value);
+ const Value &set(int index, const Value &value);
/**
* Set a certain property
- * @param key
- * @param value
+ * Calling this method will turn the value into an array
+ * @param key Key of the property to set
+ * @param size Size of the key
+ * @param value Value to set
+ * @return Value The value that was set
*/
- void set(const char *key, int size, const Value &value);
+ const Value &set(const char *key, int size, const Value &value);
/**
* Set a certain property
- * @param key
- * @param size
- * @param value
+ * Calling this method will turn the object into an array
+ * @param key Key to set
+ * @param value Value to set
+ * @return Value The value that was set
*/
- void set(const char *key, const Value &value)
+ const Value &set(const char *key, const Value &value)
{
- set(key, strlen(key), value);
- }
+ return set(key, strlen(key), value);
+ }
/**
* Set a certain property
- * @param key
- * @param value
+ * Calling this method will turn the object into an array
+ * @param key Key to set
+ * @param value Value to set
+ * @return Value The value that was set
*/
- void set(const std::string &key, const Value &value)
+ const Value &set(const std::string &key, const Value &value)
{
- set(key.c_str(), key.size(), value);
- }
+ return set(key.c_str(), key.size(), value);
+ }
/**
* Array access operator
@@ -401,13 +465,13 @@ public:
*/
Member<std::string> operator[](const std::string &key);
- /**
- * Array access operator
- * This can be used for accessing associative arrays
- * @param key
- * @return Member
- */
- Member<std::string> operator[](const char *key);
+ /**
+ * Array access operator
+ * This can be used for accessing associative arrays
+ * @param key
+ * @return Member
+ */
+ Member<std::string> operator[](const char *key);
protected: