diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-10-22 13:39:21 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-10-22 13:39:21 -0700 |
commit | f96fc6c53bc8bd8888aeb291441f61a65b439413 (patch) | |
tree | 030815351f20cfa6dfb36c816c5c0d737516e784 | |
parent | f16847ab29d474e2b20d7f8c9f3a0f229b54c850 (diff) |
Initial setup for dealing with object properties
-rw-r--r-- | include/base.h | 27 | ||||
-rw-r--r-- | include/hashmember.h | 11 | ||||
-rw-r--r-- | include/properties.h | 72 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/base.cpp | 19 | ||||
-rw-r--r-- | src/classinfo.cpp | 9 | ||||
-rw-r--r-- | src/hashmember.cpp | 40 | ||||
-rw-r--r-- | src/includes.h | 1 | ||||
-rw-r--r-- | src/value.cpp | 11 | ||||
-rw-r--r-- | tests/simple/simple.cpp | 5 | ||||
-rw-r--r-- | tests/simple/simple.php | 8 |
11 files changed, 196 insertions, 8 deletions
diff --git a/include/base.h b/include/base.h index 6acd805..1dc131e 100644 --- a/include/base.h +++ b/include/base.h @@ -17,6 +17,11 @@ class Base { public: /** + * Constructor + */ + Base() {} + + /** * Virtual destructor */ virtual ~Base() {} @@ -66,6 +71,28 @@ public: */ virtual void __destruct() {} + /** + * Get access to a property by name + * @param string + * @return Property + */ +// Property operator[](const char *name); + + /** + * Alternative way to access a property + * @param string + * @return Property + */ +// Property operator[](const std::string &name); + +protected: + /** + * All properties of the object + * @var Properties + */ +// Properties _properties; + +private: }; /** diff --git a/include/hashmember.h b/include/hashmember.h index d476b55..e0964a5 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -213,8 +213,19 @@ private: * Only value objects may construct members */ friend class Value; + friend class Properties; }; + +/** + * Custom output stream operator + * @param stream + * @param value + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const HashMember<int> &value); +std::ostream &operator<<(std::ostream &stream, const HashMember<std::string> &value); + /** * End of namespace diff --git a/include/properties.h b/include/properties.h new file mode 100644 index 0000000..cb8b38e --- /dev/null +++ b/include/properties.h @@ -0,0 +1,72 @@ +/** + * Properties.h + * + * The properties of a class are accessible using the protected _properties + * member. This is a class that implements the [] operator, so that all + * properties can be accessed using ["name"]. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class properties + */ +class Properties +{ +public: + /** + * Destructor + */ + virtual ~Properties() {} + + /** + * Get access to a property by name + * @param name + * @return HashMember + */ + HashMember<std::string> operator[](const char *name) + { + // map to value + return _value[name]; + } + + /** + * Another way to get access to a property + * @param name + * @return HashMember + */ + HashMember<std::string> operator[](const std::string &name) + { + // map to value + return _value[name]; + } + +private: + /** + * Private constructor - outside users are not supposed to instantiate this object + * @param zval + */ + Properties(struct _zval_struct *zval) : _value(zval) {} + + /** + * The value object + * @var Value + */ + Value _value; + + /** + * Only the base class can create properties + */ + friend class Base; +}; + +/** + * End of namespace + */ +} @@ -32,6 +32,7 @@ #include <phpcpp/hashmember.h> #include <phpcpp/parameters.h> #include <phpcpp/function.h> +#include <phpcpp/properties.h> #include <phpcpp/base.h> #include <phpcpp/method.h> #include <phpcpp/member.h> diff --git a/src/base.cpp b/src/base.cpp new file mode 100644 index 0000000..60e8c2d --- /dev/null +++ b/src/base.cpp @@ -0,0 +1,19 @@ +/** + * Base.cpp + * + * Implementation of the base class + * + * @documentation private + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * End of namespace + */ +} + diff --git a/src/classinfo.cpp b/src/classinfo.cpp index f1ffb8e..8e78df7 100644 --- a/src/classinfo.cpp +++ b/src/classinfo.cpp @@ -65,9 +65,6 @@ static zend_object_value create_object(zend_class_entry *type TSRMLS_DC) // retrieve the classinfo object _ClassInfo *info = (_ClassInfo *)base->info.user.doc_comment; - // construct the cpp object - object->cpp = info->construct(); - // store the class object->php.ce = type; @@ -90,6 +87,12 @@ static zend_object_value create_object(zend_class_entry *type TSRMLS_DC) // put the object in the storage, and assign a method for deallocating and cloning result.handle = zend_objects_store_put(object, NULL, deallocate_object, clone_object TSRMLS_CC); + // finally, construct the cpp object + object->cpp = info->construct(); + + std::cout << "Allocate object" << std::endl; + std::cout << object->cpp << " " << object << std::endl; + // done return result; } diff --git a/src/hashmember.cpp b/src/hashmember.cpp new file mode 100644 index 0000000..f6f8483 --- /dev/null +++ b/src/hashmember.cpp @@ -0,0 +1,40 @@ +/** + * HashMember.cpp + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Custom output stream operator + * @param stream + * @param value + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const HashMember<int> &value) +{ + return stream << value.value(); +} + +/** + * Custom output stream operator + * @param stream + * @param value + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const HashMember<std::string> &value) +{ + return stream << value.value(); +} + +/** + * End of namespace + */ +} + diff --git a/src/includes.h b/src/includes.h index 4effa90..87b5d64 100644 --- a/src/includes.h +++ b/src/includes.h @@ -45,6 +45,7 @@ #include "../include/hashmember.h" #include "../include/parameters.h" #include "../include/function.h" +#include "../include/properties.h" #include "../include/base.h" #include "../include/method.h" #include "../include/member.h" diff --git a/src/value.cpp b/src/value.cpp index 5e6d9b0..ded6c4a 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1006,6 +1006,17 @@ HashMember<std::string> Value::operator[](const char *key) } /** + * Custom output stream operator + * @param stream + * @param value + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const Value &value) +{ + return stream << value.stringValue(); +} + +/** * End of namespace */ } diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index 69cf9ce..7942a3c 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -143,7 +143,8 @@ public: cout << "myMethod GETS CALLED!!!!" << endl; cout << this << endl; cout << _x << endl; - +// cout << "A: " << _properties["a"] << endl; +// cout << "Z: " << _properties["z"] << endl; } }; @@ -153,8 +154,6 @@ extern "C" // export the "get_module" function that will be called by the Zend engine PHPCPP_EXPORT void *get_module() { - cout << "call get_module()" << endl; - // create extension static Php::Extension extension("simple","1.0"); diff --git a/tests/simple/simple.php b/tests/simple/simple.php index 5f3bed7..3abaa77 100644 --- a/tests/simple/simple.php +++ b/tests/simple/simple.php @@ -70,6 +70,10 @@ function slowsort($input) //$x = new my_extended_class(); //$x->myMethod(123); +$x = new my_class(); +$x->myMethod(); + + //echo(my_plus(1,2,3,4)."\n"); $array = array(); @@ -78,9 +82,9 @@ for ($i=0; $i<10000; $i++) $array[] = rand(); //$array = array(1,2,3); //print_r($array); -bubblesort($array); +//bubblesort($array); -print_r($array); +//print_r($array); //echo("my_class::a = ".$x->a."\n"); |