/** * Namespace.h * * Class that can be used to group various functions and classes into one * namespace. * * @author Emiel Bruijntjes * @copyright 2014 Copernica BV */ /** * Set up namespace */ namespace Php { /** * Forward declaration */ class Function; /** * Class definition */ class Namespace { protected: /** * Name of the namespace * @var string */ std::string _name; /** * Functions defined in the namespace * @var list */ std::list> _functions; /** * Classes defined in the namespace * @var list */ std::list> _classes; /** * Namespaces defined inside the namespace * @var list */ std::list> _namespaces; /** * Ini entry defined by the extension * @var list */ std::set, Ini::Compare> _ini_entries; public: /** * Constructor * @param name Name of the namespace */ Namespace(const char *name) : _name(name) {} /** * Destructor */ virtual ~Namespace() {} /** * Add a native function directly to the namespace * @param name Name of the function * @param function The function to add * @param arguments Optional argument specification * @return Namespace Same object to allow chaining */ Namespace &add(const char *name, const native_callback_0 &function, const Arguments &arguments = {}); Namespace &add(const char *name, const native_callback_1 &function, const Arguments &arguments = {}); Namespace &add(const char *name, const native_callback_2 &function, const Arguments &arguments = {}); Namespace &add(const char *name, const native_callback_3 &function, const Arguments &arguments = {}); /** * Add a native class to the namespace by moving it * @param type The class implementation * @return Namespace Same object to allow chaining */ template Namespace &add(Class &&type) { // make a copy of the object, and add it to the list of classes _classes.push_back(std::unique_ptr(new Class(std::move(type)))); // allow chaining return *this; } /** * Add a native class to the namespace by copying it * @param type The class implementation * @return Namespace Same object to allow chaining */ template Namespace &add(const Class &type) { // and add it to the list of classes _classes.push_back(std::unique_ptr(new Class(type))); // allow chaining return *this; } /** * Add an interface to the namespace by moving it * @param interface The interface properties * @return Namespace Same object to allow chaining */ Namespace &add(Interface &&interface) { // make a copy and add it to the list of classes _classes.push_back(std::unique_ptr(new Interface(std::move(interface)))); // allow chaining return *this; } /** * Add an interface to the namespace by copying it * @param interface The interface properties * @return Namespace Same object to allow chaining */ Namespace &add(const Interface &interface) { // make a copy and add it to the list of classes _classes.push_back(std::unique_ptr(new Interface(interface))); // allow chaining return *this; } /** * Add a namespace to the namespace by moving it * @param ns The namespace * @return Namespace Same object to allow chaining */ Namespace &add(Namespace &&ns) { // add it to the list of namespaces _namespaces.push_back(std::unique_ptr(new Namespace(std::move(ns)))); // allow chaining return *this; } /** * Add a namespace to the namespace by copying it * @param ns The namespace * @return Namespace Same object to allow chaining */ Namespace &add(const Namespace &ns) { // make a copy and add it to the list of namespaces _namespaces.push_back(std::unique_ptr(new Namespace(ns))); // allow chaining return *this; } /** * Add a ini entry to the extension by moving it * @param ini The class implementation * @return Namespace Same object to allow chaining */ Namespace &add(Ini &&ini) { // and add it to the list of classes _ini_entries.emplace(new Ini(std::move(ini))); // allow chaining return *this; } /** * Add a ini entry to the extension by copying it * @param ini The class implementation * @param Namespace Same object to allow chaining */ Namespace &add(const Ini &ini) { // and add it to the list of classes _ini_entries.emplace(new Ini(ini)); // allow chaining return *this; } /** * 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; } /** * The total number of ini entries * @return size_t */ size_t ini_size() { return _ini_entries.size(); } /** * Apply a callback to each registered function * * The callback will be called with the name of the namespace, and * a reference to the registered function. * * @param callback */ void functions(const std::function &callback); /** * Apply a callback to each registered class * * The callback will be called with the name of the namespace, and * a reference to the registered class. * * @param callback */ void classes(const std::function &callback); /** * Filling ini entries into external zend_ini_entry array * @param zend_ini_entry* */ void fill_ini(_zend_ini_entry *ini_entries, int module_number); }; /** * End namespace */ }