diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-08-31 15:41:04 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-08-31 15:41:04 -0700 |
commit | d762ee103bee45bcd18df457c2c7a9f36991c75f (patch) | |
tree | bd8937a36a205a8755ac485c4ede65c10078b375 | |
parent | 708e9cf9da9571a38ac8d2529d016cd78ce8ec54 (diff) |
Work in progress on a simpler api
-rw-r--r-- | include/extension.h | 15 | ||||
-rw-r--r-- | include/function.h | 55 | ||||
-rw-r--r-- | include/functions.h | 9 | ||||
-rw-r--r-- | phpcpp.h | 6 | ||||
-rw-r--r-- | src/extension.cpp | 60 | ||||
-rw-r--r-- | src/function.cpp | 17 | ||||
-rw-r--r-- | tests/simple/simple.cpp | 31 |
7 files changed, 94 insertions, 99 deletions
diff --git a/include/extension.h b/include/extension.h index daff45e..1962fd7 100644 --- a/include/extension.h +++ b/include/extension.h @@ -17,7 +17,11 @@ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> * @copyright 2013 Copernica BV */ -#include <php5/Zend/zend_modules.h> + +/** + * Structures referenced in this class + */ +struct _zend_module_entry; /** * Set up namespace @@ -47,7 +51,7 @@ public: /** * Destructor */ - virtual ~Extension() {} + virtual ~Extension() { delete _entry; } /** * Initialize the extension. @@ -141,9 +145,14 @@ public: private: /** * The information that is passed to the Zend engine + * + * Although it would be slightly faster to not make this a pointer, this + * would require that client code also includes the PHP header files, which + * we try to prevent with the PHP-CPP library, so we allocate it dynamically. + * * @var zend_module_entry */ - zend_module_entry _entry; + _zend_module_entry *_entry; }; diff --git a/include/function.h b/include/function.h index fec83e9..ef185ce 100644 --- a/include/function.h +++ b/include/function.h @@ -39,51 +39,32 @@ public: Function(const char *name) : Function(name, {}) {} /** - * Copy constructor + * No copy constructor * @param function The other function */ - Function(const Function &function) + Function(const Function &function) = delete; + + /** + * Move constructor + * @param function The other function + */ + Function(Function &&function) { - // copy other object - _refcount = function._refcount; _callable = function._callable; - - // increate number of references - (*_refcount)++; + function._callable = nullptr; } /** * Destructor */ - virtual ~Function() - { - // cleanup the object - cleanup(); - } + virtual ~Function(); /** - * Assignment operator + * No assignment operator * @param function The other function * @return Function */ - Function &operator=(const Function &function) - { - // skip self assignment - if (&function == this) return *this; - - // cleanup the object - cleanup(); - - // copy other object - _refcount = function._refcount; - _callable = function._callable; - - // increate number of references - (*_refcount)++; - - // done - return *this; - } + Function &operator=(const Function &function) {} /** * Method that gets called every time the function is executed @@ -109,18 +90,6 @@ protected: * @var smart_ptr */ Callable *_callable; - - /** - * Counter with the number of references - * @var integer - */ - int *_refcount; - - - /** - * Remove one reference - */ - void cleanup(); }; /** diff --git a/include/functions.h b/include/functions.h index 49f38b7..fc44f1c 100644 --- a/include/functions.h +++ b/include/functions.h @@ -9,6 +9,11 @@ */ /** + * Define structures + */ +struct _zend_function_entry; + +/** * Set up namespace */ namespace PhpCpp { @@ -35,7 +40,7 @@ private: * Retrieve the internal data * @return zend_function_entry* */ - zend_function_entry *internal() const + _zend_function_entry *internal() const { return _entries; } @@ -44,7 +49,7 @@ private: * The internal entries * @var zend_function_entry* */ - zend_function_entry *_entries; + _zend_function_entry *_entries; /** * Vector of functions (we need this because the function objects must @@ -25,6 +25,7 @@ #include <phpcpp/member.h> #include <phpcpp/arguments.h> #include <phpcpp/function.h> +#include <phpcpp/functions.h> #include <phpcpp/extension.h> /** @@ -36,8 +37,3 @@ # define PHPCPP_EXPORT #endif -/** - * Macro to activate the extension - */ -#define PHP_CPP_EXTENSION(classname) extern "C" { PHPCPP_EXPORT void *get_module() { static classname extension; return extension.entry(); } } - diff --git a/src/extension.cpp b/src/extension.cpp index ee22794..2ed4fa7 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -65,11 +65,6 @@ static ZEND_DECLARE_MODULE_GLOBALS(phpcpp) */ static void php_phpcpp_init_globals(zend_phpcpp_globals *globals) {} -/** - * The extension is a sort of singleton, so we keep one pointer to it here - * @var Extension - */ -static Extension *extension = nullptr; /** @@ -149,37 +144,40 @@ static int request_shutdown(INIT_FUNC_ARGS) */ Extension::Extension(const char *name, const char *version, const Functions &functions) { + // allocate memory + _entry = new zend_module_entry; + // assign all members (apart from the globals) - _entry.size = sizeof(zend_module_entry); // size of the data - _entry.zend_api = ZEND_MODULE_API_NO; // api number - _entry.zend_debug = ZEND_DEBUG; // debug mode enabled? - _entry.zts = USING_ZTS; // is thread safety enabled? - _entry.ini_entry = NULL; // the php.ini record - _entry.deps = NULL; // dependencies on other modules - _entry.name = HiddenPointer<Extension>(this, name); // extension name, with a hidden pointer to the extension object - _entry.functions = functions.internal(); // functions supported by this module - _entry.module_startup_func = extension_startup; // startup function for the whole extension - _entry.module_shutdown_func = extension_shutdown; // shutdown function for the whole extension - _entry.request_startup_func = request_startup; // startup function per request - _entry.request_shutdown_func = request_shutdown; // shutdown function per request - _entry.info_func = NULL; // information for retrieving info - _entry.version = version; // version string - _entry.globals_size = 0; // size of the global variables - _entry.globals_ptr = NULL; // pointer to the globals - _entry.globals_ctor = NULL; // constructor for global variables - _entry.globals_dtor = NULL; // destructor for global variables - _entry.post_deactivate_func = NULL; // unknown function - _entry.module_started = 0; // module is not yet started - _entry.type = 0; // temporary or persistent module, will be filled by Zend engine - _entry.handle = NULL; // dlopen() handle, will be filled by Zend engine - _entry.module_number = 0; // module number will be filled in by Zend engine - _entry.build_id = ZEND_MODULE_BUILD_ID; // check if extension and zend engine are compatible + _entry->size = sizeof(zend_module_entry); // size of the data + _entry->zend_api = ZEND_MODULE_API_NO; // api number + _entry->zend_debug = ZEND_DEBUG; // debug mode enabled? + _entry->zts = USING_ZTS; // is thread safety enabled? + _entry->ini_entry = NULL; // the php.ini record + _entry->deps = NULL; // dependencies on other modules + _entry->name = HiddenPointer<Extension>(this, name); // extension name, with a hidden pointer to the extension object + _entry->functions = functions.internal(); // functions supported by this module + _entry->module_startup_func = extension_startup; // startup function for the whole extension + _entry->module_shutdown_func = extension_shutdown; // shutdown function for the whole extension + _entry->request_startup_func = request_startup; // startup function per request + _entry->request_shutdown_func = request_shutdown; // shutdown function per request + _entry->info_func = NULL; // information for retrieving info + _entry->version = version; // version string + _entry->globals_size = 0; // size of the global variables + _entry->globals_ptr = NULL; // pointer to the globals + _entry->globals_ctor = NULL; // constructor for global variables + _entry->globals_dtor = NULL; // destructor for global variables + _entry->post_deactivate_func = NULL; // unknown function + _entry->module_started = 0; // module is not yet started + _entry->type = 0; // temporary or persistent module, will be filled by Zend engine + _entry->handle = NULL; // dlopen() handle, will be filled by Zend engine + _entry->module_number = 0; // module number will be filled in by Zend engine + _entry->build_id = ZEND_MODULE_BUILD_ID; // check if extension and zend engine are compatible // things that only need to be initialized #ifdef ZTS - _entry.globals_id_ptr = NULL; + _entry->globals_id_ptr = NULL; #else - _entry.globals_ptr = NULL; + _entry->globals_ptr = NULL; #endif } diff --git a/src/function.cpp b/src/function.cpp index 54f0717..cb56d78 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -20,25 +20,16 @@ namespace PhpCpp { */ Function::Function(const std::string &name, const std::initializer_list<Argument> &arguments) { - // one reference to the callable - _refcount = new int(1); + // create callable object _callable = new Callable(name, arguments); } /** - * Remove one reference + * Destructor */ -void Function::cleanup() +Function::~Function() { - // decrease number of references - (*_refcount)--; - - // leap out if there are still other references - if (*_refcount > 0) return; - - // release memory - delete _refcount; - delete _callable; + if (_callable) delete _callable; } /** diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index 761c7a0..b0259ca 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -15,6 +15,33 @@ */ using namespace std; +static int my_plus(int a, int b) +{ + return a + b; +} + +static std::string my_concat(std::string &a, std::string &b) +{ + return a + b; +} + +Function f({"my_plus", my_plus}); + + +// symbols are exported according to the "C" language +extern "C" +{ + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension + static PhpCpp::Extension extension("simple","1.0", { + {"my_plus", my_plus}, + {"my_concat", my_concat} + }); + + // return the module entry + return extension.getEntry(); + } +} -// create the object for the PHP extension -PHP_CPP_EXTENSION(Extension("simple","1.0")); |