From 51f4788b2b51a21894ae49821abc67c2fab4a68a Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sun, 2 Mar 2014 15:49:55 +0100 Subject: fixed settings Base::_self variable to a valid, editable value object --- include/base.h | 95 +++++++++++++++++++++++++++++++++++++++++++++------ include/classbase.h | 5 +-- include/exception.h | 2 +- include/forcedvalue.h | 49 +++++++++++++++++++++++++- include/value.h | 6 ++++ 5 files changed, 142 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/base.h b/include/base.h index 04f0c0e..7d7be98 100644 --- a/include/base.h +++ b/include/base.h @@ -29,45 +29,118 @@ public: /** * Convert the object to a Php::Value object (how it is used externally) + * @return Object + */ + Object &value() + { + return _self; + } + + /** + * Convert the object to a Php::Value object (how it is used externally) + * @return Object + */ + const Object &value() const + { + return _self; + } + + /** + * Get access to a property by name using the [] operator + * @param string * @return Value */ - Value value() const; + Value operator[](const char *name) + { + return _self[name]; + } + + /** + * Alternative way to access a property using the [] operator + * @param string + * @return Value + */ + Value operator[](const std::string &name) + { + return _self[name]; + } /** - * Get access to a property by name + * Retrieve a property by name + * @param string + * @return Value + */ + Value property(const char *name) + { + return _self[name]; + } + + /** + * Retrieve a property by name + * @param string + * @return Value + */ + Value property(const std::string &name) + { + return _self[name]; + } + + /** + * Get access to a property by name using the [] operator * @param string * @return Value */ Value operator[](const char *name) const { - return value()[name]; + return _self[name]; } /** - * Alternative way to access a property + * Alternative way to access a property using the [] operator * @param string * @return Value */ Value operator[](const std::string &name) const { - return value()[name]; + return _self[name]; } -private: /** - * The zend_object - * @var zend_object + * Retrieve a property by name + * @param string + * @return Value + */ + Value property(const char *name) const + { + return _self[name]; + } + + /** + * Retrieve a property by name + * @param string + * @return Value */ - struct _zend_object *_object = nullptr; + Value property(const std::string &name) const + { + return _self[name]; + } +protected: + /** + * The zend_object + * @var Value + */ + Object _self; + +private: /** * Private method to assign the zend object * @param zend_object */ - void assign(struct _zend_object *object) + void assign(Value &&object) { // copy pointer - _object = object; + _self = std::move(object); } /** diff --git a/include/classbase.h b/include/classbase.h index a9d9ef5..e664105 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -79,16 +79,17 @@ public: /** * Construct a new instance of the object + * @param value * @return Base */ - Base* construct(struct _zend_object *object) + Base* construct(Value &&value) { // construct the base auto *result = construct(); if (!result) return nullptr; // assign the zend object to it - result->assign(object); + result->assign(std::move(value)); // done return result; diff --git a/include/exception.h b/include/exception.h index 429b2cf..5bcc26a 100644 --- a/include/exception.h +++ b/include/exception.h @@ -3,7 +3,7 @@ * Implementation of Php Exceptions. * * @author Jasper van Eck - * @copyright 2013 Copernica BV + * @copyright 2013, 2014 Copernica BV */ #include diff --git a/include/forcedvalue.h b/include/forcedvalue.h index 29e630a..c6663e0 100644 --- a/include/forcedvalue.h +++ b/include/forcedvalue.h @@ -41,7 +41,11 @@ public: * Copy constructor from a value object * @param value */ - ForcedValue(const Value &value) : Value(value) { setType(TYPE); } + ForcedValue(const Value &value) : Value(value) + { + // type must be valid + if (value.type() != TYPE) throw Php::Exception("Assiging a wrong value type to a forced typed variable"); + } /** * Wrap object around zval @@ -61,10 +65,53 @@ public: */ virtual Value &setType(Type type) override { + // throw exception if things are going wrong + if (type != TYPE) throw Php::Exception("Variable has a forced type"); + // call base return Value::setType(TYPE); } + /** + * Assignment operator + * @param value + * @return ForcedValue + */ + ForcedValue &operator=(const Value &value) + { + // skip self assignment + if (this == &value) return *this; + + // type must be valid + if (value.type() != TYPE) throw Php::Exception("Assiging a wrong value type to a forced typed variable"); + + // call base + Value::operator=(value); + + // done + return *this; + } + + /** + * Move assignment operator + * @param value + * @return ForcedValue + */ + ForcedValue &operator=(Value &&value) + { + // skip self assignment + if (this == &value) return *this; + + // type must be valid + if (value.type() != TYPE) throw Php::Exception("Assiging a wrong value type to a forced typed variable"); + + // call base + Value::operator=(std::move(value)); + + // done + return *this; + } + protected: /** * Validate the object diff --git a/include/value.h b/include/value.h index fc6e4fe..0ebb2d5 100644 --- a/include/value.h +++ b/include/value.h @@ -66,6 +66,12 @@ public: */ Value(struct _zval_struct *zval, bool ref = false); + /** + * Wrap around an object + * @param value The object value + */ + Value(const struct _zend_object_value &value); + /** * Copy constructor * @param value -- cgit v1.2.3