summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/callable.cpp2
-rw-r--r--src/callable.h2
-rw-r--r--src/classbase.cpp33
-rw-r--r--src/extension.cpp83
-rw-r--r--src/function.h14
-rw-r--r--src/includes.h5
-rw-r--r--src/namespace.cpp103
7 files changed, 149 insertions, 93 deletions
diff --git a/src/callable.cpp b/src/callable.cpp
index d410c0b..5745d79 100644
--- a/src/callable.cpp
+++ b/src/callable.cpp
@@ -94,7 +94,7 @@ void Callable::initialize(zend_internal_function_info *info, const char *classna
// because we do not support returning references in PHP-CPP, although Zend
// engine allows it. Inside the name we hide a pointer to the current object
info->_name = _ptr;
- info->_name_len = _ptr.length();
+ info->_name_len = strlen(_ptr);
info->_class_name = classname;
// number of required arguments, and the expected return type
diff --git a/src/callable.h b/src/callable.h
index 1b761ae..d08dc42 100644
--- a/src/callable.h
+++ b/src/callable.h
@@ -68,6 +68,7 @@ public:
/**
* Fill a function entry
* @param entry Entry to be filled
+ * @param ns Active namespace
* @param classname Optional class name
* @param flags Access flags
*/
@@ -76,6 +77,7 @@ public:
/**
* Fill function info
* @param info Info object to be filled
+ * @param ns Active namespace
* @param classname Optional class name
*/
void initialize(struct _zend_internal_function_info *info, const char *classname = nullptr) const;
diff --git a/src/classbase.cpp b/src/classbase.cpp
index 38972f5..b38f6d3 100644
--- a/src/classbase.cpp
+++ b/src/classbase.cpp
@@ -157,12 +157,17 @@ const struct _zend_function_entry *ClassBase::entries()
* 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 prefix namespace prefix
*/
-void ClassBase::initialize()
+void ClassBase::initialize(const std::string &prefix)
{
// the class entry
zend_class_entry entry;
+ // update the name
+ if (prefix.size() > 0) _name = prefix + "\\" + _name;
+
// initialize the class entry
INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), entries());
@@ -214,7 +219,7 @@ void ClassBase::initialize()
void ClassBase::add(const char *name, method_callback_0 callback, int flags, const Arguments &args)
{
// add the method
- _methods.insert(std::make_shared<Method>(name, callback, flags, args));
+ _methods.push_back(std::make_shared<Method>(name, callback, flags, args));
}
/**
@@ -227,7 +232,7 @@ void ClassBase::add(const char *name, method_callback_0 callback, int flags, con
void ClassBase::add(const char *name, method_callback_1 callback, int flags, const Arguments &args)
{
// add the method
- _methods.insert(std::make_shared<Method>(name, callback, flags, args));
+ _methods.push_back(std::make_shared<Method>(name, callback, flags, args));
}
/**
@@ -240,7 +245,7 @@ void ClassBase::add(const char *name, method_callback_1 callback, int flags, con
void ClassBase::add(const char *name, method_callback_2 callback, int flags, const Arguments &args)
{
// add the method
- _methods.insert(std::make_shared<Method>(name, callback, flags, args));
+ _methods.push_back(std::make_shared<Method>(name, callback, flags, args));
}
/**
@@ -253,7 +258,7 @@ void ClassBase::add(const char *name, method_callback_2 callback, int flags, con
void ClassBase::add(const char *name, method_callback_3 callback, int flags, const Arguments &args)
{
// add the method
- _methods.insert(std::make_shared<Method>(name, callback, flags, args));
+ _methods.push_back(std::make_shared<Method>(name, callback, flags, args));
}
/**
@@ -265,7 +270,7 @@ void ClassBase::add(const char *name, method_callback_3 callback, int flags, con
void ClassBase::add(const char *name, std::nullptr_t value, int flags)
{
// add property
- _members.insert(std::make_shared<NullMember>(name, flags));
+ _members.push_back(std::make_shared<NullMember>(name, flags));
}
/**
@@ -277,7 +282,7 @@ void ClassBase::add(const char *name, std::nullptr_t value, int flags)
void ClassBase::add(const char *name, int16_t value, int flags)
{
// add property
- _members.insert(std::make_shared<LongMember>(name, value, flags));
+ _members.push_back(std::make_shared<LongMember>(name, value, flags));
}
/**
@@ -289,7 +294,7 @@ void ClassBase::add(const char *name, int16_t value, int flags)
void ClassBase::add(const char *name, int32_t value, int flags)
{
// add property
- _members.insert(std::make_shared<LongMember>(name, value, flags));
+ _members.push_back(std::make_shared<LongMember>(name, value, flags));
}
/**
@@ -301,7 +306,7 @@ void ClassBase::add(const char *name, int32_t value, int flags)
void ClassBase::add(const char *name, int64_t value, int flags)
{
// add property
- _members.insert(std::make_shared<LongMember>(name, value, flags));
+ _members.push_back(std::make_shared<LongMember>(name, value, flags));
}
/**
@@ -313,7 +318,7 @@ void ClassBase::add(const char *name, int64_t value, int flags)
void ClassBase::add(const char *name, bool value, int flags)
{
// add property
- _members.insert(std::make_shared<BoolMember>(name, value, flags));
+ _members.push_back(std::make_shared<BoolMember>(name, value, flags));
}
/**
@@ -325,7 +330,7 @@ void ClassBase::add(const char *name, bool value, int flags)
void ClassBase::add(const char *name, char value, int flags)
{
// add property
- _members.insert(std::make_shared<StringMember>(name, &value, 1, flags));
+ _members.push_back(std::make_shared<StringMember>(name, &value, 1, flags));
}
/**
@@ -337,7 +342,7 @@ void ClassBase::add(const char *name, char value, int flags)
void ClassBase::add(const char *name, const std::string &value, int flags)
{
// add property
- _members.insert(std::make_shared<StringMember>(name, value, flags));
+ _members.push_back(std::make_shared<StringMember>(name, value, flags));
}
/**
@@ -349,7 +354,7 @@ void ClassBase::add(const char *name, const std::string &value, int flags)
void ClassBase::add(const char *name, const char *value, int flags)
{
// add property
- _members.insert(std::make_shared<StringMember>(name, value, strlen(value), flags));
+ _members.push_back(std::make_shared<StringMember>(name, value, strlen(value), flags));
}
/**
@@ -361,7 +366,7 @@ void ClassBase::add(const char *name, const char *value, int flags)
void ClassBase::add(const char *name, double value, int flags)
{
// add property
- _members.insert(std::make_shared<FloatMember>(name, value, flags));
+ _members.push_back(std::make_shared<FloatMember>(name, value, flags));
}
/**
diff --git a/src/extension.cpp b/src/extension.cpp
index dae5051..20d5627 100644
--- a/src/extension.cpp
+++ b/src/extension.cpp
@@ -156,7 +156,8 @@ static int request_shutdown(INIT_FUNC_ARGS)
* @param start Request start callback
* @param stop Request stop callback
*/
-Extension::Extension(const char *name, const char *version, request_callback start, request_callback stop) : _start(start), _stop(stop)
+Extension::Extension(const char *name, const char *version, request_callback start, request_callback stop) :
+ Namespace(""), _start(start), _stop(stop)
{
// keep extension pointer based on the name
name2extension[name] = this;
@@ -215,54 +216,6 @@ Extension::~Extension()
}
/**
- * 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 Extension::add(const char *name, native_callback_0 function, const Arguments &arguments)
-{
- // add a function
- _functions.insert(std::unique_ptr<Function>(new Function(name, function, arguments)));
-}
-
-/**
- * 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 Extension::add(const char *name, native_callback_1 function, const Arguments &arguments)
-{
- // add a function
- _functions.insert(std::unique_ptr<Function>(new Function(name, function, arguments)));
-}
-
-/**
- * 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 Extension::add(const char *name, native_callback_2 function, const Arguments &arguments)
-{
- // add a function
- _functions.insert(std::unique_ptr<Function>(new Function(name, function, arguments)));
-}
-
-/**
- * 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 Extension::add(const char *name, native_callback_3 function, const Arguments &arguments)
-{
- // add a function
- _functions.insert(std::unique_ptr<Function>(new Function(name, function, arguments)));
-}
-
-/**
* Retrieve the module entry
* @return zend_module_entry
*/
@@ -272,23 +225,13 @@ zend_module_entry *Extension::module()
if (_entry->functions || _functions.size() == 0) return _entry;
// allocate memory for the functions
- zend_function_entry *entries = new zend_function_entry[_functions.size() + 1];
-
- // keep iterator counter
- int i = 0;
+ zend_function_entry *entries = new zend_function_entry[functions() + 1];
- // loop through the functions
- for (auto &function : _functions)
- {
- // retrieve entry
- zend_function_entry *entry = &entries[i++];
-
- // let the function fill the entry
- function->initialize(entry);
- }
+ // initialize the entries
+ int count = initialize(_name, entries);
// last entry should be set to all zeros
- zend_function_entry *last = &entries[i];
+ zend_function_entry *last = &entries[count];
// all should be set to zero
memset(last, 0, sizeof(zend_function_entry));
@@ -301,20 +244,6 @@ zend_module_entry *Extension::module()
}
/**
- * Initialize the extension
- * @return bool
- */
-bool Extension::initialize()
-{
- // loop through the classes
- for (auto &iter : _classes) iter->initialize();
-
- // done
- return true;
-}
-
-
-/**
* End of namespace
*/
}
diff --git a/src/function.h b/src/function.h
index 84d7dcc..40c392f 100644
--- a/src/function.h
+++ b/src/function.h
@@ -50,6 +50,20 @@ public:
}
}
+ /**
+ * Fill a function entry
+ * @param prefix Active namespace prefix
+ * @param entry Entry to be filled
+ */
+ void initialize(const std::string &prefix, struct _zend_function_entry *entry)
+ {
+ // if there is a namespace prefix, we should adjust the name
+ if (prefix.size()) _ptr = HiddenPointer<Callable>(this, prefix+"\\"+(const char *)_ptr);
+
+ // call base initialize
+ Callable::initialize(entry);
+ }
+
private:
/**
* Union of supported callbacks
diff --git a/src/includes.h b/src/includes.h
index d2355c1..4117884 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -16,9 +16,11 @@
#include <vector>
#include <map>
#include <memory>
-#include <set>
+#include <list>
#include <exception>
+// @todo do we really nead shared_ptr?
+
// for debug
#include <iostream>
@@ -62,6 +64,7 @@
#include "../include/abstractclass.h"
#include "../include/finalclass.h"
#include "../include/interface.h"
+#include "../include/namespace.h"
#include "../include/extension.h"
#include "../include/exception.h"
#include "../include/init.h"
diff --git a/src/namespace.cpp b/src/namespace.cpp
new file mode 100644
index 0000000..0938a90
--- /dev/null
+++ b/src/namespace.cpp
@@ -0,0 +1,103 @@
+/**
+ * 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
+ */
+void Namespace::add(const char *name, native_callback_0 function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+}
+
+/**
+ * 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 Namespace::add(const char *name, native_callback_1 function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+}
+
+/**
+ * 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 Namespace::add(const char *name, native_callback_2 function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+}
+
+/**
+ * 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 Namespace::add(const char *name, native_callback_3 function, const Arguments &arguments)
+{
+ // add a function
+ _functions.push_back(std::make_shared<Function>(name, function, arguments));
+}
+
+/**
+ * Initialize all functions in this namespace
+ * @param parent Namespace prefix of the parent
+ * @param entries The array to be filled
+ * @return int Number of functions that were initialized
+ */
+size_t Namespace::initialize(const std::string &parent, zend_function_entry entries[])
+{
+ // keep iterator counter
+ int count = 0;
+
+ // the namespace to use
+ std::string prefix = parent.size() ? parent + "\\" + _name : _name;
+
+ // loop through the functions
+ for (auto &function : _functions)
+ {
+ // retrieve entry
+ zend_function_entry *entry = &entries[count++];
+
+ // let the function fill the entry
+ function->initialize(prefix, entry);
+ }
+
+ // loop through the namespace
+ for (auto &ns : _namespaces)
+ {
+ // let the namespace initialize
+ count += ns->initialize(prefix, &entries[count]);
+ }
+
+ // done
+ return count;
+}
+
+/**
+ * End namespace
+ */
+}
+