summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Examples/CppClassesInPhp/cppclassinphp.cpp15
-rw-r--r--include/array.h115
-rw-r--r--include/forcedvalue.h150
-rw-r--r--include/object.h80
-rw-r--r--phpcpp.h2
-rw-r--r--src/includes.h2
-rw-r--r--src/value.cpp29
7 files changed, 216 insertions, 177 deletions
diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp
index 2d17e19..95f909b 100644
--- a/Examples/CppClassesInPhp/cppclassinphp.cpp
+++ b/Examples/CppClassesInPhp/cppclassinphp.cpp
@@ -50,9 +50,22 @@ public:
if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied");
std::cout << "myMethod is called." << std::endl;
+
+ // create a new PHP DateTime object representing the current time
+ Php::Object now("DateTime", "now");
+
+ // show it
+ std::cout << "current time: " << now.call("format", "Y-m-d H:i:s") << std::endl;
+ std::cout << "construct " << params[0] << std::endl;
+
// construct a new class
- return Php::Object(params[0]);
+ Php::Object obj(params[0]);
+
+ std::cout << "return " << params[0] << std::endl;
+
+ // return it
+ return obj;
// std::cout << "get property1 " << value()["property1"] << std::endl;
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
diff --git a/phpcpp.h b/phpcpp.h
index e7f4c7a..91abe0c 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -27,7 +27,7 @@
#include <phpcpp/exception.h>
#include <phpcpp/type.h>
#include <phpcpp/value.h>
-#include <phpcpp/forcedvalue.h>
+#include <phpcpp/array.h>
#include <phpcpp/object.h>
#include <phpcpp/hiddenpointer.h>
#include <phpcpp/globals.h>
diff --git a/src/includes.h b/src/includes.h
index 072fab2..60783eb 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -46,7 +46,7 @@
#include "../include/exception.h"
#include "../include/type.h"
#include "../include/value.h"
-#include "../include/forcedvalue.h"
+#include "../include/array.h"
#include "../include/object.h"
#include "../include/hiddenpointer.h"
#include "../include/globals.h"
diff --git a/src/value.cpp b/src/value.cpp
index 8047dee..13c4d88 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -309,9 +309,6 @@ Value &Value::operator=(Value &&value)
// the other object is no longer valid
value._val = nullptr;
}
-
- // update the object
- return validate();
}
/**
@@ -357,7 +354,7 @@ Value &Value::operator=(const Value &value)
}
// update the object
- return validate();
+ return *this;
}
@@ -378,7 +375,7 @@ Value &Value::operator=(std::nullptr_t value)
ZVAL_NULL(_val);
// update the object
- return validate();
+ return *this;
}
/**
@@ -398,7 +395,7 @@ Value &Value::operator=(int16_t value)
ZVAL_LONG(_val, value);
// update the object
- return validate();
+ return *this;
}
/**
@@ -418,7 +415,7 @@ Value &Value::operator=(int32_t value)
ZVAL_LONG(_val, value);
// update the object
- return validate();
+ return *this;
}
/**
@@ -438,7 +435,7 @@ Value &Value::operator=(int64_t value)
ZVAL_LONG(_val, value);
// update the object
- return validate();
+ return *this;
}
/**
@@ -458,7 +455,7 @@ Value &Value::operator=(bool value)
ZVAL_BOOL(_val, value);
// update the object
- return validate();
+ return *this;
}
/**
@@ -478,7 +475,7 @@ Value &Value::operator=(char value)
ZVAL_STRINGL(_val, &value, 1, 1);
// update the object
- return validate();
+ return *this;
}
/**
@@ -498,7 +495,7 @@ Value &Value::operator=(const std::string &value)
ZVAL_STRINGL(_val, value.c_str(), value.size(), 1);
// update the object
- return validate();
+ return *this;
}
/**
@@ -518,7 +515,7 @@ Value &Value::operator=(const char *value)
ZVAL_STRING(_val, value, 1);
// update the object
- return validate();
+ return *this;
}
/**
@@ -538,7 +535,7 @@ Value &Value::operator=(double value)
ZVAL_DOUBLE(_val, value);
// update the object
- return validate();
+ return *this;
}
/**
@@ -1499,9 +1496,6 @@ const Value &Value::set(int index, const Value &value)
// the variable has one more reference (the array entry)
Z_ADDREF_P(value._val);
- // object should stay valid
- validate();
-
// done
return value;
}
@@ -1552,9 +1546,6 @@ const Value &Value::set(const char *key, int size, const Value &value)
Z_ADDREF_P(value._val);
}
- // object should stay valid
- validate();
-
// done
return value;
}