diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-04 18:21:58 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-04 18:21:58 +0100 |
commit | c028e8f932cc4206ab8446409693fba8dfe18ffb (patch) | |
tree | 4ffe8a5d77888cff291eb7dd412bbd03e51c6f29 /include | |
parent | 200952ad4004f6ee5527598622505adbe84df8af (diff) |
Php::Value and Php::Object classes can now be used to wrap around Php::Base objects
Diffstat (limited to 'include')
-rw-r--r-- | include/base.h | 115 | ||||
-rw-r--r-- | include/classbase.h | 60 | ||||
-rw-r--r-- | include/object.h | 27 | ||||
-rw-r--r-- | include/value.h | 6 |
4 files changed, 117 insertions, 91 deletions
diff --git a/include/base.h b/include/base.h index 46f5678..b027d5a 100644 --- a/include/base.h +++ b/include/base.h @@ -11,6 +11,11 @@ namespace Php { /** + * Forward declarations + */ +class MixedObject; + +/** * Class definition */ class Base @@ -22,109 +27,121 @@ protected: Base() {} public: - - // @todo should we delete the copy and move operators because we do not - // allow the CPP code to make copies of itself? - - /** * Virtual destructor */ virtual ~Base() {} /** - * Convert the object to a Php::Value object (how it is used externally) - * @return Object - */ -// Object value(); - - /** - * Convert the object to a Php::Value object (how it is used externally) - * @return Object - */ -// Object value() const; - - /** * Get access to a property by name using the [] operator * @param string * @return HashMember */ -// HashMember<std::string> operator[](const char *name) -// { -// return value()[name]; -// } + HashMember<std::string> operator[](const char *name) + { + return Value(this)[name]; + } /** * Alternative way to access a property using the [] operator * @param string * @return HashMember */ -// HashMember<std::string> operator[](const std::string &name) -// { -// return value()[name]; -// } + HashMember<std::string> operator[](const std::string &name) + { + return Value(this)[name]; + } /** * Retrieve a property by name * @param string * @return HashMember */ -// HashMember<std::string> property(const char *name) -// { -// return value()[name]; -// } + HashMember<std::string> property(const char *name) + { + return Value(this)[name]; + } /** * Retrieve a property by name * @param string * @return HashMember */ -// HashMember<std::string> property(const std::string &name) -// { -// return value()[name]; -// } + HashMember<std::string> property(const std::string &name) + { + return Value(this)[name]; + } /** * Get access to a property by name using the [] operator * @param string * @return Value */ -// Value operator[](const char *name) const -// { -// return value()[name]; -// } + Value operator[](const char *name) const + { + return Value(this)[name]; + } /** * Alternative way to access a property using the [] operator * @param string * @return Value */ -// Value operator[](const std::string &name) const -// { -// return value()[name]; -// } + Value operator[](const std::string &name) const + { + return Value(this)[name]; + } /** * Retrieve a property by name * @param string * @return Value */ -// Value property(const char *name) const -// { -// return value()[name]; -// } + Value property(const char *name) const + { + return Value(this)[name]; + } /** * Retrieve a property by name * @param string * @return Value */ -// Value property(const std::string &name) const -// { -// return value()[name]; -// } + Value property(const std::string &name) const + { + return Value(this)[name]; + } private: + /** + * Store the object in the zend object cache + * @param entry + * @return MixedObject + */ + MixedObject *store(struct _zend_class_entry *entry); + + /** + * Retrieve the handle + * @return int + */ + int handle() const + { + return _handle; + } + + /** + * The handle in the zend object cache + * @var int + */ + int _handle = 0; + + /** + * Friends that have access to the private members + */ + friend class Value; + friend class Object; + friend class ClassBase; + }; diff --git a/include/classbase.h b/include/classbase.h index a380840..8a691fa 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -15,6 +15,11 @@ */ /** + * Forward declarations + */ +struct _zend_object_value; + +/** * Set up namespace */ namespace Php { @@ -82,45 +87,6 @@ public: /** * Construct a new instance of the object - * @param value - * @return Base - */ - Base* construct(const struct _zend_object_value &value) - { - // construct the base - auto *result = construct(); - if (!result) return nullptr; - - // assign the zend object to it - // @todo fix this -// result->assign(value); - - // done - return result; - } - - /** - * Construct a clone of the object - * @param orig - * @param value - * @return Base - */ - Base* clone(Base *orig, const struct _zend_object_value &value) - { - // construct the base - auto *result = clone(orig); - if (!result) return nullptr; - - // assign the zend object to it - // @todo fix this -// result->assign(value); - - // done - return result; - } - - /** - * Construct a new instance of the object * @return Base */ virtual Base* construct() = 0; @@ -144,6 +110,7 @@ public: * @param ns Namespace name */ void initialize(const std::string &ns); + protected: /** @@ -232,6 +199,21 @@ private: const struct _zend_function_entry *entries(); /** + * Static member functions to clone objects based on this class + * @param val The object to be cloned + * @return zend_object_value Object info + */ + static struct _zend_object_value cloneObject(struct _zval_struct *val); + + /** + * 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 entry Pointer to the class information + * @return zend_object_value The newly created object + */ + static struct _zend_object_value createObject(struct _zend_class_entry *entry); + + /** * Name of the class * @var string */ diff --git a/include/object.h b/include/object.h index b3ea5bc..ff2f7c1 100644 --- a/include/object.h +++ b/include/object.h @@ -49,6 +49,33 @@ public: } /** + * Constructor to create a new instance of a builtin class + * + * You can use this constructor if you have created an instance of your + * own class, but has not assigned it to a variable yet. This happens + * for example for classes that are not constructed from PHP userspace, + * but from your own functions: + * + * Php::Value yourFunction() + * { + * return Php::Object("MyClass", new MyClass()); + * } + * + * When you construct objects like this, the __construct function is not + * going to be called. If you want to construct the object just as if it + * was constructed from PHP user space, do this: + * + * Php::Value yourFunction() + * { + * return Php::Object("MyClass"); + * } + * + * @param name Name of the class to instantiate + * @param base Implementation of the class + */ + Object(const char *name, Base *base); + + /** * Constructor to create a new instance * * This constructor comes in many different forms, to support all possible diff --git a/include/value.h b/include/value.h index 8af95a5..d0079dc 100644 --- a/include/value.h +++ b/include/value.h @@ -67,10 +67,10 @@ public: Value(struct _zval_struct *zval, bool ref = false); /** - * Wrap around an object - * @param value The object value + * Wrap around an object implemented by us + * @param object Object to be wrapped */ - Value(const struct _zend_object_value &value); + Value(Base *base); /** * Copy constructor |