diff options
Diffstat (limited to 'include/forcedvalue.h')
-rw-r--r-- | include/forcedvalue.h | 49 |
1 files changed, 48 insertions, 1 deletions
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<TYPE> &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<TYPE> &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 |