summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Examples/CppClassesInPhp/cppclassinphp.cpp5
-rw-r--r--Examples/CppClassesInPhp/cppclassinphp.php1
-rw-r--r--include/base.h95
-rw-r--r--include/classbase.h5
-rw-r--r--include/exception.h2
-rw-r--r--include/forcedvalue.h49
-rw-r--r--include/value.h6
-rw-r--r--phpcpp.h2
-rw-r--r--src/base.cpp29
-rw-r--r--src/classbase.cpp5
-rw-r--r--src/includes.h2
-rw-r--r--src/value.cpp12
12 files changed, 165 insertions, 48 deletions
diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp
index 741a44d..70233c9 100644
--- a/Examples/CppClassesInPhp/cppclassinphp.cpp
+++ b/Examples/CppClassesInPhp/cppclassinphp.cpp
@@ -48,6 +48,11 @@ public:
_x = params[0];
std::cout << "get property1 " << value()["property1"] << std::endl;
+
+ // set it to something else
+ value().set("property1", "new value");
+
+ std::cout << "get property1 " << value()["property1"] << std::endl;
}
};
diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php
index 7c06675..b535f0e 100644
--- a/Examples/CppClassesInPhp/cppclassinphp.php
+++ b/Examples/CppClassesInPhp/cppclassinphp.php
@@ -11,6 +11,7 @@ $object = new MyClass();
// run a function of the class
$object->myMethod(1);
+$object->myMethod(2);
echo($object->property1."\n");
echo($object->property2."\n");
diff --git a/include/base.h b/include/base.h
index 04f0c0e..7d7be98 100644
--- a/include/base.h
+++ b/include/base.h
@@ -29,45 +29,118 @@ public:
/**
* Convert the object to a Php::Value object (how it is used externally)
+ * @return Object
+ */
+ Object &value()
+ {
+ return _self;
+ }
+
+ /**
+ * Convert the object to a Php::Value object (how it is used externally)
+ * @return Object
+ */
+ const Object &value() const
+ {
+ return _self;
+ }
+
+ /**
+ * Get access to a property by name using the [] operator
+ * @param string
* @return Value
*/
- Value value() const;
+ Value operator[](const char *name)
+ {
+ return _self[name];
+ }
+
+ /**
+ * Alternative way to access a property using the [] operator
+ * @param string
+ * @return Value
+ */
+ Value operator[](const std::string &name)
+ {
+ return _self[name];
+ }
/**
- * Get access to a property by name
+ * Retrieve a property by name
+ * @param string
+ * @return Value
+ */
+ Value property(const char *name)
+ {
+ return _self[name];
+ }
+
+ /**
+ * Retrieve a property by name
+ * @param string
+ * @return Value
+ */
+ Value property(const std::string &name)
+ {
+ return _self[name];
+ }
+
+ /**
+ * Get access to a property by name using the [] operator
* @param string
* @return Value
*/
Value operator[](const char *name) const
{
- return value()[name];
+ return _self[name];
}
/**
- * Alternative way to access a property
+ * Alternative way to access a property using the [] operator
* @param string
* @return Value
*/
Value operator[](const std::string &name) const
{
- return value()[name];
+ return _self[name];
}
-private:
/**
- * The zend_object
- * @var zend_object
+ * Retrieve a property by name
+ * @param string
+ * @return Value
+ */
+ Value property(const char *name) const
+ {
+ return _self[name];
+ }
+
+ /**
+ * Retrieve a property by name
+ * @param string
+ * @return Value
*/
- struct _zend_object *_object = nullptr;
+ Value property(const std::string &name) const
+ {
+ return _self[name];
+ }
+protected:
+ /**
+ * The zend_object
+ * @var Value
+ */
+ Object _self;
+
+private:
/**
* Private method to assign the zend object
* @param zend_object
*/
- void assign(struct _zend_object *object)
+ void assign(Value &&object)
{
// copy pointer
- _object = object;
+ _self = std::move(object);
}
/**
diff --git a/include/classbase.h b/include/classbase.h
index a9d9ef5..e664105 100644
--- a/include/classbase.h
+++ b/include/classbase.h
@@ -79,16 +79,17 @@ public:
/**
* Construct a new instance of the object
+ * @param value
* @return Base
*/
- Base* construct(struct _zend_object *object)
+ Base* construct(Value &&value)
{
// construct the base
auto *result = construct();
if (!result) return nullptr;
// assign the zend object to it
- result->assign(object);
+ result->assign(std::move(value));
// done
return result;
diff --git a/include/exception.h b/include/exception.h
index 429b2cf..5bcc26a 100644
--- a/include/exception.h
+++ b/include/exception.h
@@ -3,7 +3,7 @@
* Implementation of Php Exceptions.
*
* @author Jasper van Eck <jasper.vaneck@copernica.com>
- * @copyright 2013 Copernica BV
+ * @copyright 2013, 2014 Copernica BV
*/
#include <exception>
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
diff --git a/include/value.h b/include/value.h
index fc6e4fe..0ebb2d5 100644
--- a/include/value.h
+++ b/include/value.h
@@ -67,6 +67,12 @@ public:
Value(struct _zval_struct *zval, bool ref = false);
/**
+ * Wrap around an object
+ * @param value The object value
+ */
+ Value(const struct _zend_object_value &value);
+
+ /**
* Copy constructor
* @param value
*/
diff --git a/phpcpp.h b/phpcpp.h
index daef707..b7680ca 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -24,6 +24,7 @@
/**
* Include all headers files that are related to this library
*/
+#include <phpcpp/exception.h>
#include <phpcpp/type.h>
#include <phpcpp/value.h>
#include <phpcpp/forcedvalue.h>
@@ -45,7 +46,6 @@
#include <phpcpp/interface.h>
#include <phpcpp/namespace.h>
#include <phpcpp/extension.h>
-#include <phpcpp/exception.h>
/**
* Macro to export a function
diff --git a/src/base.cpp b/src/base.cpp
deleted file mode 100644
index 867fd9c..0000000
--- a/src/base.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * Base.cpp
- *
- * Implementation of the base class
- *
- * @documentation private
- */
-#include "includes.h"
-
-/**
- * Set up namespace
- */
-namespace Php {
-
-/**
- * Convert the object to a Php::Value object (how it is used externally)
- * @return Value
- */
-Value Base::value() const
-{
- // wrap the properties table, as a reference
- return Object(*_object->properties_table, true);
-}
-
-/**
- * End of namespace
- */
-}
-
diff --git a/src/classbase.cpp b/src/classbase.cpp
index 6a679b3..87a1cd2 100644
--- a/src/classbase.cpp
+++ b/src/classbase.cpp
@@ -49,7 +49,8 @@ static void clone_object(void *object, void **clone TSRMLS_DC)
/**
* Function that is called when an instance of the class needs to be created.
* This function will create the C++ class, and the PHP object
- * @param type Pointer to the class
+ * @param type Pointer to the class information
+ * @return zend_object_value The newly created object
*/
static zend_object_value create_object(zend_class_entry *type TSRMLS_DC)
{
@@ -100,7 +101,7 @@ static zend_object_value create_object(zend_class_entry *type TSRMLS_DC)
result.handle = zend_objects_store_put(object, NULL, deallocate_object, clone_object TSRMLS_CC);
// finally, construct the cpp object
- object->cpp = info->construct(&object->php);
+ object->cpp = info->construct(Value(result));
// done
return result;
diff --git a/src/includes.h b/src/includes.h
index c15ae93..8babe21 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -45,6 +45,7 @@
/**
* Include other files from this library
*/
+#include "../include/exception.h"
#include "../include/type.h"
#include "../include/value.h"
#include "../include/forcedvalue.h"
@@ -66,7 +67,6 @@
#include "../include/interface.h"
#include "../include/namespace.h"
#include "../include/extension.h"
-#include "../include/exception.h"
#include "../include/init.h"
/**
diff --git a/src/value.cpp b/src/value.cpp
index 644e60c..f7eff56 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -170,6 +170,18 @@ Value::Value(struct _zval_struct *val, bool ref)
}
/**
+ * Wrap around an object
+ * @param value The object value
+ */
+Value::Value(const struct _zend_object_value &value)
+{
+ // make a normal zval
+ MAKE_STD_ZVAL(_val);
+ Z_TYPE_P(_val) = IS_OBJECT;
+ Z_OBJVAL_P(_val) = value;
+}
+
+/**
* Copy constructor
* @param value
*/