diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 14:59:58 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 14:59:58 -0700 |
commit | ffdaa0590d33ea89d116f6c56df2474cc0675ec9 (patch) | |
tree | 6b0d71337879892b43a4af90e6757d53ecc77853 /include | |
parent | 753402a84b40ff4dc9697dea1d2d4aa037ea7624 (diff) |
{auto} PHP objects can now be implemented in C++. Constructors and destructors get called at the appropriate time, but not yet any of the other methods
Diffstat (limited to 'include')
-rw-r--r-- | include/base.h | 19 | ||||
-rw-r--r-- | include/classinfo.h | 45 | ||||
-rw-r--r-- | include/extension.h | 4 | ||||
-rw-r--r-- | include/function.h | 68 | ||||
-rw-r--r-- | include/hiddenpointer.h | 30 |
5 files changed, 108 insertions, 58 deletions
diff --git a/include/base.h b/include/base.h index eceb114..797aff6 100644 --- a/include/base.h +++ b/include/base.h @@ -17,6 +17,11 @@ class Base { public: /** + * Virtual destructor + */ + virtual ~Base() {} + + /** * The pseudo constructor that is called from PHP after the object is constructed * @param environment * @param parameters @@ -46,6 +51,20 @@ public: */ virtual void __construct() {} + /** + * The pseudo destructor that is called from PHP right before the object is destructed + * @param environment + */ + virtual void __destruct(Environment &environment) + { + // call the other implementation + __destruct(); + } + + /** + * The pseudo destructor that is called from PHP right before the object is destructed + */ + virtual void __destruct() {} }; diff --git a/include/classinfo.h b/include/classinfo.h index 3941259..fb92ce4 100644 --- a/include/classinfo.h +++ b/include/classinfo.h @@ -22,6 +22,11 @@ struct _zend_class_entry; namespace Php { /** + * Forward declarations + */ +class InternalFunction; + +/** * Virtual base class of the classInfo * * We need this virtual base class to store pointers to class objects, @@ -34,31 +39,49 @@ public: * Constructor * @param name */ - _ClassInfo(const char *name) : _name(name), _entry(NULL) {} + _ClassInfo(const char *name); /** * Destructor */ - virtual ~_ClassInfo() {} + virtual ~_ClassInfo(); /** * Initialize the class */ void initialize(); - -private: + /** - * Class name - * @var string + * Construct the C++ object + * @return Base */ - std::string _name; + virtual Base *construct() = 0; +private: /** * The class entry * @var zend_class_entry */ struct _zend_class_entry *_entry; + /** + * The name + * @var string + */ + std::string _name; + + /** + * Constructor function + * @var InternalFunction + */ + InternalFunction *_constructor; + + /** + * Destructor function + * @var InternalFunction + */ + InternalFunction *_destructor; + }; /** @@ -82,6 +105,14 @@ public: */ virtual ~ClassInfo() {} + /** + * Construct the object + * @return Base + */ + virtual Base *construct() + { + return _type.construct(); + } private: /** diff --git a/include/extension.h b/include/extension.h index 1984aa9..bf945f7 100644 --- a/include/extension.h +++ b/include/extension.h @@ -173,7 +173,7 @@ public: * It is only possible to create functions during the initialization of * the library, before the Extension::module() method is called. * - * Note that the function must have been allocated on the HEAP (using + * Note that the function must have been allocated on the HEAP (using * "new") and that the object will be destructed (using "delete") * by the extension object (you thus do not have to destruct it * yourself!) @@ -187,7 +187,7 @@ public: * Add a native function directly to the extension * @param name Name of the function * @param function The function to add - * @param arguments Optional argument specification + * @param arguments Optional argument specification * @return Function The added function */ Function *add(const char *name, native_callback_0 function, const std::initializer_list<Argument> &arguments = {}); diff --git a/include/function.h b/include/function.h index c78fdd6..89ec3fd 100644 --- a/include/function.h +++ b/include/function.h @@ -28,7 +28,7 @@ class Function public: /** * Constructor - * @param name Name of the function + * @param name Name of the function * @param min Min number of arguments * @param max Max number of arguments */ @@ -55,42 +55,42 @@ public: /** * Comparison - * @param function The other function - * @return bool + * @param function The other function + * @return bool */ bool operator<(const Function &function) const { - return strcmp(name(), function.name()) < 0; - } + return strcmp(name(), function.name()) < 0; + } /** * Comparison - * @param function The other function - * @return bool + * @param function The other function + * @return bool */ bool operator>(const Function &function) const { - return strcmp(name(), function.name()) > 0; - } + return strcmp(name(), function.name()) > 0; + } /** * Comparison - * @param function The other function - * @return bool + * @param function The other function + * @return bool */ bool operator==(const Function &function) const { - return strcmp(name(), function.name()) == 0; - } + return strcmp(name(), function.name()) == 0; + } - /** - * Function name - * @return const char * - */ - const char *name() const - { - return _ptr.text(); - } + /** + * Function name + * @return const char * + */ + const char *name() const + { + return _ptr.text(); + } /** * Method that gets called every time the function is executed @@ -111,17 +111,17 @@ protected: */ Type _type = nullType; - /** - * Required number of arguments - * @var integer - */ - int _required; - - /** - * Total number of arguments - * @var integer - */ - int _argc; + /** + * Required number of arguments + * @var integer + */ + int _required; + + /** + * Total number of arguments + * @var integer + */ + int _argc; /** * The arguments @@ -130,8 +130,8 @@ protected: struct _zend_arg_info *_argv; /** - * The name is stored in a hidden pointer, so that we have access to the function - * @var HiddenPointer + * The object address is stored in a hidden pointer, so that we have access to the function object + * @var HiddenPointer */ HiddenPointer<Function> _ptr; diff --git a/include/hiddenpointer.h b/include/hiddenpointer.h index 3502bc9..f432f65 100644 --- a/include/hiddenpointer.h +++ b/include/hiddenpointer.h @@ -113,19 +113,19 @@ public: /** * Change the pointer - * @param Type* + * @param Type* */ void setPointer(Type *pointer) { - // store pointer - _pointer = pointer; + // store pointer + _pointer = pointer; - // overwrite in data - _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *)); - + // overwrite in data + _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *)); + // for safety reasons, we recalculate text pointer _text = _data.c_str() + sizeof(Type *); - } + } /** * Retrieve the text @@ -138,14 +138,14 @@ public: /** * Change the text - * @param text - * @param size + * @param text + * @param size */ void setText(const char *text, int size=-1) { - // check if size was set - if (size < 0) size = strlen(text); - + // check if size was set + if (size < 0) size = strlen(text); + // reserve enough room for the text and the pointer _data.reserve(size + sizeof(Type *)); @@ -157,7 +157,7 @@ public: // store new text _text = _data.c_str() + sizeof(Type *); - } + } /** * Cast to the pointer @@ -183,8 +183,8 @@ public: */ int length() const { - return _data.size() - sizeof(Type *); - } + return _data.size() - sizeof(Type *); + } private: /** |