summaryrefslogtreecommitdiff
path: root/include/object.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-04 09:27:01 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-04 09:27:01 +0100
commit59cfe935248918c1151b300eb19496b76ed579a9 (patch)
treee4f00380d599fe3f7cc9399edead14ca1b74a6f9 /include/object.h
parentecc297108d2851af885c1fb28434769f9478649d (diff)
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
Diffstat (limited to 'include/object.h')
-rw-r--r--include/object.h80
1 files changed, 75 insertions, 5 deletions
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<Type::Object>
+class Object : public Value
{
public:
/**
* Constructor for an empty stdClass object
*/
- Object() : ForcedValue<Type::Object>() {}
+ 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<Type::Object>(value) {}
- Object(Value &&value) : ForcedValue<Type::Object>(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