summaryrefslogtreecommitdiff
path: root/include/namespace.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-01 10:32:26 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-01 10:32:26 +0100
commitc8d1519f31baed0fb399dac9333e48e2f9e910ad (patch)
tree1e2192f0b91d55512d26f4a14c8eae6b5a1f6ece /include/namespace.h
parent73945a9cb2b096a5379d17c028bda102b87aedce (diff)
namespace implementation, compile issue for php 5.4 and higher
Diffstat (limited to 'include/namespace.h')
-rw-r--r--include/namespace.h203
1 files changed, 203 insertions, 0 deletions
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
+ */
+}
+