summaryrefslogtreecommitdiff
path: root/include
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
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')
-rw-r--r--include/array.h115
-rw-r--r--include/forcedvalue.h150
-rw-r--r--include/object.h80
3 files changed, 190 insertions, 155 deletions
diff --git a/include/array.h b/include/array.h
new file mode 100644
index 0000000..94d2824
--- /dev/null
+++ b/include/array.h
@@ -0,0 +1,115 @@
+/**
+ * Array.h
+ *
+ * The Array is a wrapper around the value class that ensures that a
+ * certain property always is an array
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013, 2014 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Array : public Value
+{
+public:
+ /**
+ * Constructor
+ */
+ Array() : Value() { setType(Type::Array); }
+
+ /**
+ * Copy constructor from a value object
+ * @param value
+ */
+ Array(const Value &value) : Value(value)
+ {
+ // type must be valid
+ if (value.type() != Type::Array) throw Php::Exception("Assiging a non-array to an array variable");
+ }
+
+ /**
+ * Move constructor from a value object
+ * @param value
+ */
+ Array(Value &&value) : Value(std::move(value))
+ {
+ // type must be valid
+ if (value.type() != Type::Array) throw Php::Exception("Moving a non-array to an array variable");
+ }
+
+ /**
+ * Destructor
+ */
+ virtual ~Array() {}
+
+ /**
+ * 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::Array) throw Php::Exception("Changing type of a fixed array variable");
+
+ // call base
+ return Value::setType(Type::Array);
+ }
+
+ /**
+ * Assignment operator
+ * @param value
+ * @return Array
+ */
+ Array &operator=(const Value &value)
+ {
+ // skip self assignment
+ if (this == &value) return *this;
+
+ // type must be valid
+ if (value.type() != Type::Array) throw Php::Exception("Assiging a non-array to a fixed array variable");
+
+ // call base
+ Value::operator=(value);
+
+ // done
+ return *this;
+ }
+
+ /**
+ * Move assignment operator
+ * @param value
+ * @return Array
+ */
+ Array &operator=(Value &&value)
+ {
+ // skip self assignment
+ if (this == &value) return *this;
+
+ // type must be valid
+ if (value.type() != Type::Array) throw Php::Exception("Moving a non-array to a fixed array variable");
+
+ // call base
+ Value::operator=(std::move(value));
+
+ // done
+ return *this;
+ }
+};
+
+/**
+ * Define for arrays and objects
+ */
+using Array = Array;
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/forcedvalue.h b/include/forcedvalue.h
deleted file mode 100644
index 4fc2fcc..0000000
--- a/include/forcedvalue.h
+++ /dev/null
@@ -1,150 +0,0 @@
-/**
- * ForcedValue.h
- *
- * The ForcedValue is a wrapper around the value class that ensures that a
- * certain property always has a certain type.
- *
- * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
- * @copyright 2013 Copernica BV
- */
-
-/**
- * Set up namespace
- */
-namespace Php {
-
-/**
- * Class definition
- */
-template <Type TYPE>
-class ForcedValue : public Value
-{
-public:
- /**
- * Constructor
- */
- ForcedValue() : Value() { setType(TYPE); }
-
- /**
- * Copy constructor
- * @param that
- */
- ForcedValue(const ForcedValue<TYPE> &that) : Value(that) {}
-
- /**
- * Move constructor
- * @param that
- */
- ForcedValue(ForcedValue<TYPE> &&that) : Value(std::move(that)) {}
-
- /**
- * Copy constructor from a value object
- * @param value
- */
- 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");
- }
-
- /**
- * Move constructor from a value object
- * @param value
- */
- ForcedValue(Value &&value) : Value(std::move(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
- * @param zval Zval to wrap
- * @param ref Force this to be a reference
- */
- ForcedValue(struct _zval_struct *zval, bool ref = false) : Value(zval, ref) { setType(TYPE); }
-
- /**
- * Destructor
- */
- virtual ~ForcedValue() {}
-
- /**
- * 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) 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
- * @return Value
- */
- virtual Value &validate() override
- {
- // make sure the object has a valid type
- setType(TYPE);
-
- // call base
- return Value::validate();
- }
-
-};
-
-/**
- * Define for arrays and objects
- */
-using Array = ForcedValue<Type::Array>;
-
-/**
- * End of namespace
- */
-}
-
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