summaryrefslogtreecommitdiff
path: root/zend/namespace.cpp
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-06 21:53:24 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-04-06 21:53:24 +0200
commit35fd3ccbeb4def71b4d8a59dfbb5c31201b099b9 (patch)
tree915223360aed4743aa6127fde4836aa413a260e5 /zend/namespace.cpp
parentda4710512865e6816585ac4ab8edab2fa125e2d8 (diff)
renamed src directory to zend directory, disabled TSRM debug code
Diffstat (limited to 'zend/namespace.cpp')
-rw-r--r--zend/namespace.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/zend/namespace.cpp b/zend/namespace.cpp
new file mode 100644
index 0000000..bea31a1
--- /dev/null
+++ b/zend/namespace.cpp
@@ -0,0 +1,134 @@
+/**
+ * Namespace.cpp
+ *
+ * Implementation of the namespace class
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2014 Copernica BV
+ */
+#include "includes.h"
+
+/**
+ * Open namespace
+ */
+namespace Php {
+
+/**
+ * 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
+ * @return Namespace Same object to allow chaining
+ */
+Namespace &Namespace::add(const char *name, const native_callback_0 &function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+
+ // allow chaining
+ return *this;
+}
+
+/**
+ * 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
+ * @return Namespace Same object to allow chaining
+ */
+Namespace &Namespace::add(const char *name, const native_callback_1 &function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+
+ // allow chaining
+ return *this;
+}
+
+/**
+ * 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
+ * @return Namespace Same object to allow chaining
+ */
+Namespace &Namespace::add(const char *name, const native_callback_2 &function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+
+ // allow chaining
+ return *this;
+}
+
+/**
+ * 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
+ * @return Namespace Same object to allow chaining
+ */
+Namespace &Namespace::add(const char *name, const native_callback_3 &function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+
+ // allow chaining
+ return *this;
+}
+
+/**
+ * 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 Namespace::apply(const std::function<void(const std::string &ns, Function &func)> &callback)
+{
+ // loop through the functions, and apply the callback
+ for (auto &function : _functions) callback(_name, *function);
+
+ // loop through the other namespaces
+ for (auto &ns : _namespaces) ns->apply([this, callback](const std::string &ns, Function &func) {
+
+ // if this is the root namespace, we don't have to change the prefix
+ if (_name.size() == 0) return callback(ns, func);
+
+ // construct a new prefix
+ // @todo this could be slightly inefficient
+ return callback(_name + "\\" + ns, func);
+ });
+}
+
+/**
+ * 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 Namespace::apply(const std::function<void(const std::string &ns, ClassBase &clss)> &callback)
+{
+ // loop through the classes, and apply the callback
+ for (auto &c : _classes) callback(_name, *c);
+
+ // loop through the other namespaces
+ for (auto &ns : _namespaces) ns->apply([this, callback](const std::string &ns, ClassBase &clss) {
+
+ // if this is the root namespace, we don't have to change the prefix
+ if (_name.size() == 0) return callback(ns, clss);
+
+ // construct a new prefix
+ // @todo this could be slightly inefficient
+ return callback(_name + "\\" + ns, clss);
+ });
+}
+
+/**
+ * End namespace
+ */
+}
+