diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-04-01 16:39:52 +0200 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-04-01 16:39:52 +0200 |
commit | 61ded13eaa25b332208cd659c5c844890db0cb57 (patch) | |
tree | c50f5319fc975faf80e3a7a41a9b8cdb36656bec /include/hashmember.h | |
parent | 777949f8751ded6e56140bb513c6b93e4d5c3f08 (diff) |
much simpler implementation of hash member, i do not understand why i first used this complicated zval wrapping implementation, fixes problems reported in issue #56
Diffstat (limited to 'include/hashmember.h')
-rw-r--r-- | include/hashmember.h | 181 |
1 files changed, 115 insertions, 66 deletions
diff --git a/include/hashmember.h b/include/hashmember.h index 0bb5a4c..6603b57 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -24,26 +24,20 @@ namespace Php { class Value; /** - * Base class for hash members - */ -class HashMemberBase -{ -public: - /** - * Assignment operator - * @param param value - */ - virtual void assign(const Value &value) = 0; -}; - -/** * Member class */ template <typename Type> -class HashMember : public HashMemberBase +class HashMember : private HashParent { public: /** + * Constructor + * @param parent + * @param index + */ + HashMember(HashParent *parent, Type index) : _parent(parent), _index(index) {} + + /** * Destructor */ virtual ~HashMember() {} @@ -55,9 +49,9 @@ public: */ HashMember &operator=(const Value &value) { - // assign the property - assign(value); - + // set new value in the parent + _parent->set(_index, value); + // done return *this; } @@ -69,7 +63,8 @@ public: */ bool exists() const { - return _base.contains(_index); + // ask the parent + return _parent->contains(_index); } /** @@ -78,7 +73,7 @@ public: */ Value value() const { - return _base.get(_index); + return _parent->get(_index); } /** @@ -87,7 +82,7 @@ public: */ operator Value () const { - return _base.get(_index); + return _parent->get(_index); } /** @@ -96,7 +91,7 @@ public: */ operator int16_t () const { - return _base.get(_index).numericValue(); + return value().numericValue(); } /** @@ -105,7 +100,7 @@ public: */ operator int32_t () const { - return _base.get(_index).numericValue(); + return value().numericValue(); } /** @@ -114,7 +109,7 @@ public: */ operator int64_t () const { - return _base.get(_index).numericValue(); + return value().numericValue(); } /** @@ -123,7 +118,7 @@ public: */ operator bool () const { - return _base.get(_index).boolValue(); + return value().boolValue(); } /** @@ -132,7 +127,7 @@ public: */ operator std::string () const { - return _base.get(_index).stringValue(); + return value().stringValue(); } /** @@ -141,7 +136,7 @@ public: */ operator const char * () const { - return _base.get(_index).rawValue(); + return value().rawValue(); } /** @@ -150,7 +145,7 @@ public: */ operator double () const { - return _base.get(_index).decimalValue(); + return value().floatValue(); } /** @@ -161,7 +156,7 @@ public: */ HashMember<int> operator[](int index) { - return _base.get(_index)[index].add(this); + return HashMember<int>(this, index); } /** @@ -172,7 +167,7 @@ public: */ HashMember<std::string> operator[](const std::string &key) { - return _base.get(_index)[key].add(this); + return HashMember<std::string>(this, key); } /** @@ -183,11 +178,11 @@ public: */ HashMember<std::string> operator[](const char *key) { - return _base.get(_index)[key].add(this); + return HashMember<std::string>(this, key); } /** - * Add a value to the object + * Add a value to the object (or other arithmetric operators) * @param value * @return HashMember */ @@ -373,70 +368,124 @@ public: Value operator()(Value param0, Value param1, Value param2, Value param3, Value param4, Value param5, Value param6, Value param7, Value param8) { return value()(param0, param1, param2, param3, param4, param5, param6, param7, param8); } Value operator()(Value param0, Value param1, Value param2, Value param3, Value param4, Value param5, Value param6, Value param7, Value param8, Value param9) { return value()(param0, param1, param2, param3, param4, param5, param6, param7, param8, param9); } -private: /** - * Constructor - * @param base Base value - * @param index Index in the array + * Check if a certain key exists in the array/object + * @param key + * @return bool */ - HashMember(struct _zval_struct *base, Type index) : _base(base, true), _index(index) {} + virtual bool contains(const std::string &key) const override + { + // object must exist, and the value must contain the key + return exists() && value().contains(key); + } - // @todo add move constructor + /** + * Check if a certain index exists in the array/object + * @param key + * @return bool + */ + virtual bool contains(int index) const override + { + // object must exist, and the value must contain the key + return exists() && value().contains(index); + } /** - * Protected copy constructor - * @param value Other element + * Retrieve the value at a string index + * @param key + * @return Value */ - HashMember(const HashMember<Type> &member) : _base(member._base._val, true), _index(member._index), _parent(member._parent) {} + virtual Value get(const std::string &key) const override + { + // return null if it does not exist + if (!exists()) return nullptr; + + // ask the value + return value().get(key); + } /** - * Add parent - * @param parent + * Retrieve the value at a numeric index + * @param index + * @return Value */ - HashMember &add(HashMemberBase *parent) + virtual Value get(int index) const override { - _parent = parent; - return *this; + // return null if it does not exist + if (!exists()) return nullptr; + + // ask the value + return value().get(index); } - + /** - * Implementation of the assign method + * Overwrite the value at a certain string index + * @param key * @param value */ - virtual void assign(const Value &value) override + virtual void set(const std::string &key, const Value &value) override { - // set property in parent array - _base.set(_index, value); - - // if there is a parent, it should sets its value too - if (_parent) _parent->assign(_base); + // get the current value + Value current(this->value()); + + // add the value + current[key] = value; + + // pass this to the base + _parent->set(_index, current); } /** - * The original index - * @var Type + * Overwrite the value at a certain numeric index + * @param index + * @param value */ - Type _index; - + virtual void set(int index, const Value &value) override + { + // get the current value + Value current(value()); + + // add the value + current[index] = value; + + // pass this to the base + _parent->set(_index, current); + } + +protected: + /** + * Protected copy constructor + * @param value Other element + */ + HashMember(const HashMember<Type> &member) : _parent(member._parent), _index(member._index) {} + + /** + * Move constructor + * @param value Other element + */ +// HashMember(HashMember<Type> &&member) : +// _parent(std::move(member._parent)), _index(std::move(member._index)) {} + +private: /** * Base value - * @var Value + * @var */ - Value _base; + HashParent *_parent; /** - * Parent member (in case of nested members) - * @var HashMemberBase + * The original index + * @var Type */ - HashMemberBase *_parent = nullptr; + Type _index; /** - * Only value objects may construct members + * Friend classes */ - friend class Value; - friend class Base; - friend class HashMember<int>; friend class HashMember<std::string>; + friend class HashMember<int>; + friend class Base; + friend class Value; }; /** |