diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-24 09:59:06 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-24 09:59:06 +0100 |
commit | d41e5b430bd7024044319f5be5229c18cb61e160 (patch) | |
tree | b3def77f15bc11d9f618a014b759bb028435848d /include/hashmember.h | |
parent | 638993442a3ef0829c3a6fdd590adeb10cfb8c75 (diff) |
fixed nested arrays, issue #50
Diffstat (limited to 'include/hashmember.h')
-rw-r--r-- | include/hashmember.h | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/include/hashmember.h b/include/hashmember.h index b8f4dcd..0bb5a4c 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -24,10 +24,23 @@ 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 +class HashMember : public HashMemberBase { public: /** @@ -42,11 +55,8 @@ public: */ HashMember &operator=(const Value &value) { - // set property in parent array - _base->set(_index, value); - - // if there is a parent, it should sets its value too - if (_parent) _parent->operator=(_base); + // assign the property + assign(value); // done return *this; @@ -59,7 +69,7 @@ public: */ bool exists() const { - return _base->contains(_index); + return _base.contains(_index); } /** @@ -68,7 +78,7 @@ public: */ Value value() const { - return _base->get(_index); + return _base.get(_index); } /** @@ -77,7 +87,7 @@ public: */ operator Value () const { - return _base->get(_index); + return _base.get(_index); } /** @@ -86,7 +96,7 @@ public: */ operator int16_t () const { - return _base->get(_index).numericValue(); + return _base.get(_index).numericValue(); } /** @@ -95,7 +105,7 @@ public: */ operator int32_t () const { - return _base->get(_index).numericValue(); + return _base.get(_index).numericValue(); } /** @@ -104,7 +114,7 @@ public: */ operator int64_t () const { - return _base->get(_index).numericValue(); + return _base.get(_index).numericValue(); } /** @@ -113,7 +123,7 @@ public: */ operator bool () const { - return _base->get(_index).boolValue(); + return _base.get(_index).boolValue(); } /** @@ -122,7 +132,7 @@ public: */ operator std::string () const { - return _base->get(_index).stringValue(); + return _base.get(_index).stringValue(); } /** @@ -131,7 +141,7 @@ public: */ operator const char * () const { - return _base->get(_index).rawValue(); + return _base.get(_index).rawValue(); } /** @@ -140,7 +150,7 @@ public: */ operator double () const { - return _base->get(_index).decimalValue(); + return _base.get(_index).decimalValue(); } /** @@ -149,9 +159,9 @@ public: * @param index * @return HashMember */ - HashMember operator[](int index) + HashMember<int> operator[](int index) { - return _base->get(_index)[index].add(this); + return _base.get(_index)[index].add(this); } /** @@ -160,9 +170,9 @@ public: * @param key * @return HashMember */ - HashMember operator[](const std::string &key) + HashMember<std::string> operator[](const std::string &key) { - return _base->get(_index)[key].add(this); + return _base.get(_index)[key].add(this); } /** @@ -171,9 +181,9 @@ public: * @param key * @return HashMember */ - HashMember operator[](const char *key) + HashMember<std::string> operator[](const char *key) { - return _base->get(_index)[key].add(this); + return _base.get(_index)[key].add(this); } /** @@ -369,7 +379,7 @@ private: * @param base Base value * @param index Index in the array */ - HashMember(Value *base, Type index) : _base(base), _index(index) {} + HashMember(struct _zval_struct *base, Type index) : _base(base, true), _index(index) {} // @todo add move constructor @@ -377,18 +387,30 @@ private: * Protected copy constructor * @param value Other element */ - HashMember(const HashMember<Type> &member) : _base(member._base), _index(member._index), _parent(member._parent) {} + HashMember(const HashMember<Type> &member) : _base(member._base._val, true), _index(member._index), _parent(member._parent) {} /** * Add parent * @param parent - * @return HashMember */ - HashMember &add(HashMember *parent) + HashMember &add(HashMemberBase *parent) { _parent = parent; return *this; } + + /** + * Implementation of the assign method + * @param value + */ + virtual void assign(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); + } /** * The original index @@ -400,20 +422,21 @@ private: * Base value * @var Value */ - Value *_base; + Value _base; /** * Parent member (in case of nested members) - * @var HashMember + * @var HashMemberBase */ - HashMember *_parent = nullptr; + HashMemberBase *_parent = nullptr; /** * Only value objects may construct members */ friend class Value; friend class Base; - + friend class HashMember<int>; + friend class HashMember<std::string>; }; /** |