diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/class.h | 2 | ||||
-rw-r--r-- | include/classbase.h | 12 | ||||
-rw-r--r-- | include/extension.h | 95 | ||||
-rw-r--r-- | include/hiddenpointer.h | 27 | ||||
-rw-r--r-- | include/namespace.h | 203 |
5 files changed, 269 insertions, 70 deletions
diff --git a/include/class.h b/include/class.h index 51cfb46..1ac4f00 100644 --- a/include/class.h +++ b/include/class.h @@ -41,6 +41,8 @@ public: * @param name Name of the class */ Class(const char *name) : ClassBase(name) {} + + // @todo add copy and move constructor /** * Destructor diff --git a/include/classbase.h b/include/classbase.h index 9726e5a..98a430e 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -88,8 +88,10 @@ public: * this means that this method is called after the module is already available. * This function will inform the Zend engine about the existence of the * class. + * + * @param ns Namespace name */ - void initialize(); + void initialize(const std::string &ns); protected: /** @@ -173,15 +175,15 @@ private: /** * All class methods - * @var set + * @var std::list */ - std::set<std::shared_ptr<Method>> _methods; + std::list<std::shared_ptr<Method>> _methods; /** * All class members (class properties) - * @var set + * @var std::list */ - std::set<std::shared_ptr<Member>> _members; + std::list<std::shared_ptr<Member>> _members; }; /** diff --git a/include/extension.h b/include/extension.h index 4a89cf9..72c7d56 100644 --- a/include/extension.h +++ b/include/extension.h @@ -29,20 +29,9 @@ struct _zend_module_entry; namespace Php { /** - * A couple of predefined native callback functions that can be registered. - * These are functions that optional accept a Request and/or Parameters object, - * and that either return void or a Value object. - */ -typedef void (*native_callback_0)(); -typedef void (*native_callback_1)(Parameters &); -typedef Value (*native_callback_2)(); -typedef Value (*native_callback_3)(Parameters &); - -/** * Forward declaration */ class Extension; -class Function; /** * Optional callback types for starting and stopping the request @@ -53,7 +42,7 @@ typedef bool (*request_callback)(Extension *extension); /** * Class definition */ -class Extension +class Extension : public Namespace { public: /** @@ -77,36 +66,36 @@ public: virtual ~Extension(); /** - * Initialize the extension. - * - * This method is called after the extension has been loaded, constructed - * and after the compatibility has been checked, but before the requests - * are handled. You can override this method to add your own initialization. - * - * The default behavior of this function is to enable all classes that are - * defined in this extension, so that they are also available in PHP. + * Initialize the extension after it was registered * - * The method should return true on success, and false on failure (in which - * case the extension will not be used) + * You can override this method to add your own initialization code. The + * default implementation registers all classes and namespaces * * @return bool */ - virtual bool initialize(); + virtual bool initialize() + { + // initialize the namespace + Namespace::initialize(""); + + // ok + return true; + } /** - * Finalize the extension - * - * This method gets called after all requests were handled, and right before - * the Apache module or CLI script will exit. You can override it to add - * your own cleanup code. + * Finalize the extension after it was registered + * + * You can override this method to do your own cleanup right before the + * extension object is going to be destructed * * @return bool */ virtual bool finalize() { + // ok return true; } - + /** * Start a request * @@ -143,32 +132,6 @@ 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 - */ - void add(const char *name, native_callback_0 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_1 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_2 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_3 function, const Arguments &arguments = {}); - - /** - * Add a native class to the extension - * @param name Name of the class - * @param type The class implementation - */ - template<typename T> - void add(const Class<T> &type) - { - // make a copy of the object - auto *info = new Class<T>(type); - - // and copy it to the list of classes - _classes.insert(std::unique_ptr<ClassBase>(info)); - } - - /** * Retrieve the module entry * * This is the memory address that should be exported by the get_module() @@ -178,19 +141,23 @@ public: */ _zend_module_entry *module(); - -private: /** - * Functions defined in the library - * @var set + * Cast to a module entry + * @return _zend_module_entry* */ - std::set<std::unique_ptr<Function>> _functions; - + operator _zend_module_entry * () + { + return module(); + } + +private: /** - * Classes defined in the library, indexed by their name - * @var set + * Initialize all functions in this namespace + * @param ns Namespace prefix + * @param entries The array to be filled + * @return int Number of functions that were initialized */ - std::set<std::unique_ptr<ClassBase>> _classes; + size_t initialize(const std::string &ns, zend_function_entry entries[]); /** * The information that is passed to the Zend engine diff --git a/include/hiddenpointer.h b/include/hiddenpointer.h index 96bb26a..93fdc7c 100644 --- a/include/hiddenpointer.h +++ b/include/hiddenpointer.h @@ -94,7 +94,32 @@ public: * @param that * @return HiddenPointer */ - HiddenPointer<Type> operator=(const HiddenPointer &that) = delete; + HiddenPointer<Type> operator=(const HiddenPointer &that) + { + // skip self assignmend + if (this == &that) return *this; + + // deallocate current object + if (_allocated) delete[] _buffer; + + // copy allocated setting + _allocated = that._allocated; + + // is the other object allocated? + if (_allocated) + { + // allocate this object too, call constructor + HiddenPointer(that, that); + } + else + { + // just copy the data + _buffer = that._buffer; + } + + // done + return *this; + } /** * Retrieve the pointer diff --git a/include/namespace.h b/include/namespace.h new file mode 100644 index 0000000..727f1dd --- /dev/null +++ b/include/namespace.h @@ -0,0 +1,203 @@ +/** + * Namespace.h + * + * Class that can be used to group various functions and classes into one + * namespace. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * A couple of predefined native callback functions that can be registered. + * These are functions that optional accept a Request and/or Parameters object, + * and that either return void or a Value object. + */ +typedef void (*native_callback_0)(); +typedef void (*native_callback_1)(Parameters &); +typedef Value (*native_callback_2)(); +typedef Value (*native_callback_3)(Parameters &); + +/** + * Forward declaration + */ +class Function; + +/** + * Class definition + */ +class Namespace +{ +public: + /** + * Constructor + * @param name Name of the namespace + */ + Namespace(const char *name) : _name(name) {} + + /** + * Copy constructor + * @param ns Namespace to copy + */ + Namespace(const Namespace &ns) : + _name(ns._name), + _functions(ns._functions), + _classes(ns._classes), + _namespaces(ns._namespaces) {} + + /** + * Move constructor + * @param ns + */ + Namespace(Namespace &&ns) : + _name(std::move(ns._name)), + _functions(std::move(ns._functions)), + _classes(std::move(ns._classes)), + _namespaces(std::move(ns._namespaces)) {} + + /** + * Destructor + */ + virtual ~Namespace() {} + + /** + * 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 + */ + void add(const char *name, native_callback_0 function, const Arguments &arguments = {}); + void add(const char *name, native_callback_1 function, const Arguments &arguments = {}); + void add(const char *name, native_callback_2 function, const Arguments &arguments = {}); + void add(const char *name, native_callback_3 function, const Arguments &arguments = {}); + + /** + * Add a native class to the extension by moving it + * @param name Name of the class + * @param type The class implementation + */ + template<typename T> + void add(Class<T> &&type) + { + // make a copy of the object + auto *copy = new Class<T>(std::move(type)); + + // and add it to the list of classes + _classes.push_back(std::unique_ptr<ClassBase>(copy)); + } + + /** + * Add a native class to the extension by copying it + * @param name Name of the class + * @param type The class implementation + */ + template<typename T> + void add(const Class<T> &type) + { + // make a copy of the object + auto *copy = new Class<T>(std::move(type)); + + // and add it to the list of classes + _classes.push_back(std::unique_ptr<ClassBase>(copy)); + } + + /** + * Add a namespace to the extension by moving it + * @param ns The namespace + */ + void add(Namespace &&ns) + { + // make a copy of the object + auto *copy = new Namespace(std::move(ns)); + + // add it to the list of namespaces + _namespaces.push_back(std::unique_ptr<Namespace>(copy)); + } + + /** + * Add a namespace to the extension by copying it + * @param ns The namespace + */ + void add(const Namespace &ns) + { + // make a copy of the object + auto *copy = new Namespace(std::move(ns)); + + // add it to the list of namespaces + _namespaces.push_back(std::unique_ptr<Namespace>(copy)); + } + + +protected: + /** + * Name of the namespace + * @var string + */ + std::string _name; + + /** + * Functions defined by the extension + * @var list + */ + std::list<std::shared_ptr<Function>> _functions; + + /** + * Classes defined by the extension + * @var list + */ + std::list<std::shared_ptr<ClassBase>> _classes; + + /** + * Namespaces defined by the extension + * @var list + */ + std::list<std::shared_ptr<Namespace>> _namespaces; + + /** + * The total number of functions + * @return size_t + */ + size_t functions() + { + // number of functions in this namespace + int result = _functions.size(); + + // number of functions in sub-namespace + for (auto &ns : _namespaces) result += ns->functions(); + + // done + return result; + } + + /** + * Initialize all functions in this namespace + * @param ns Namespace prefix + * @param entries The array to be filled + * @return int Number of functions that were initialized + */ + size_t initialize(const std::string &ns, zend_function_entry entries[]); + + /** + * Initialize the namespace after it was registered + * @param parent Parent namespace + */ + void initialize(const std::string &parent) + { + // loop through the classes in this namespace + for (auto &c : _classes) c->initialize(parent+"\\"+_name); + + // and loop through the other namespaces + for (auto &n : _namespaces) n->initialize(parent+"\\"+_name); + } +}; + +/** + * End namespace + */ +} + |