From 59cfe935248918c1151b300eb19496b76ed579a9 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Tue, 4 Mar 2014 09:27:01 +0100 Subject: removed forcedvalue and implemented array and object directly because now it is easier to finetune these classes, removed the Value::validate() method because it does not seem to be necessary, Object constructor now also accepts Php::Value objects that hold a string, to instantiate the described class --- include/object.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) (limited to 'include/object.h') diff --git a/include/object.h b/include/object.h index e2c424f..b3ea5bc 100644 --- a/include/object.h +++ b/include/object.h @@ -16,20 +16,37 @@ namespace Php { /** * Class definition */ -class Object : public ForcedValue +class Object : public Value { public: /** * Constructor for an empty stdClass object */ - Object() : ForcedValue() {} + Object() : Value() { setType(Type::Object); } /** - * Copy and move constructors are passed on to the base class + * Move constructor is passed to the parent + * @param value + */ + Object(Value &&value) : Value(std::move(value)) + { + // throw exception in case of problems + if (value.type() != Type::Object) throw Php::Exception("Constructing an object variable by moving a non object"); + } + + /** + * Copy constructor is valid if the passed in object is also an object, + * or when it is a string holding a classname * @param that An other object */ - Object(const Value &value) : ForcedValue(value) {} - Object(Value &&value) : ForcedValue(std::move(value)) {} + Object(const Value &value) : Value() + { + // string types are instantiated + if (value.isString()) instantiate(value); + + // otherwise copy the other object + else operator=(value); + } /** * Constructor to create a new instance @@ -66,6 +83,59 @@ public: */ virtual ~Object() {} + /** + * Change the internal type of the variable + * @param Type + */ + virtual Value &setType(Type type) override + { + // throw exception if things are going wrong + if (type != Type::Object) throw Php::Exception("Changing type of a fixed object variable"); + + // call base + return Value::setType(type); + } + + /** + * Assignment operator + * @param value + * @return ForcedValue + */ + Object &operator=(const Value &value) + { + // skip self assignment + if (this == &value) return *this; + + // type must be valid + if (value.type() != Type::Object) throw Php::Exception("Assiging a non-object to an object variable"); + + // call base + Value::operator=(value); + + // done + return *this; + } + + /** + * Move assignment operator + * @param value + * @return ForcedValue + */ + Object &operator=(Value &&value) + { + // skip self assignment + if (this == &value) return *this; + + // type must be valid + if (value.type() != Type::Object) throw Php::Exception("Moving a non-object to an object variable"); + + // call base + Value::operator=(std::move(value)); + + // done + return *this; + } + private: /** * Helper method to instantiate an object -- cgit v1.2.3