diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/extension.h | 12 | ||||
-rw-r--r-- | include/function.h | 73 | ||||
-rw-r--r-- | include/hiddenpointer.h | 56 | ||||
-rw-r--r-- | include/value.h | 5 |
4 files changed, 118 insertions, 28 deletions
diff --git a/include/extension.h b/include/extension.h index ad00593..fb33c99 100644 --- a/include/extension.h +++ b/include/extension.h @@ -166,11 +166,15 @@ public: * It is only possible to create functions during the initialization of * the library, before the Extension::module() method is called. * - * @param name Name of the function + * Note that the function must have been allocated on the HEAP (using + * "new") and that the object will be destructed (using "delete") + * by the extension object (you thus do not have to destruct it + * yourself!) + * * @param function The function to add * @return Function The added function */ - Function &add(const char *name, const Function &function); + Function &add(Function *function); /** * Add a native function directly to the extension @@ -200,10 +204,10 @@ public: private: /** - * Map of function objects defined in the library + * Set of function objects defined in the library * @var map */ - std::map<const char *,Function> _functions; + std::set<std::unique_ptr<Function>> _functions; /** * Hidden pointer to self diff --git a/include/function.h b/include/function.h index daf8572..c2dd9b8 100644 --- a/include/function.h +++ b/include/function.h @@ -26,24 +26,13 @@ namespace Php { class Function { public: - -// Function(std::function<Value()> &function); -// Function(std::function<Value(Value&)> &function); -// Function(std::function<Value(Value&,Value&)> &function); -// Function(std::function<Value(Value&,Value&,Value&)> &function); -// Function(std::function<Value(Value&,Value&,Value&,Value&)> &function); -// Function(std::function<void()> &function); -// Function(std::function<void(Value&)> &function); -// Function(std::function<void(Value&,Value&)> &function); -// Function(std::function<void(Value&,Value&,Value&)> &function); -// Function(std::function<void(Value&,Value&,Value&,Value&)> &function); - /** * Constructor + * @param name Name of the function * @param min Min number of arguments * @param max Max number of arguments */ - Function(int min = 0, int max = 0) + Function(const char *name, int min = 0, int max = 0) : _ptr(this, name) { // construct the arguments _arguments = std::shared_ptr<Arguments>(new Arguments(min, max)); @@ -53,7 +42,7 @@ public: * No copy constructor * @param function The other function */ - Function(const Function &function) + Function(const Function &function) : _ptr(this, function.name()) { // copy members _arguments = function._arguments; @@ -64,7 +53,7 @@ public: * Move constructor * @param function The other function */ - Function(Function &&function) + Function(Function &&function) : _ptr(this, function.name()) { // copy arguments _arguments = function._arguments; @@ -90,12 +79,52 @@ public: if (this == &function) return *this; // copy members + _ptr.setText(function.name()); _arguments = function._arguments; _type = function._type; // done return *this; } + + /** + * Comparison + * @param function The other function + * @return bool + */ + bool operator<(const Function &function) const + { + return strcmp(name(), function.name()) < 0; + } + + /** + * Comparison + * @param function The other function + * @return bool + */ + bool operator>(const Function &function) const + { + return strcmp(name(), function.name()) > 0; + } + + /** + * Comparison + * @param function The other function + * @return bool + */ + bool operator==(const Function &function) const + { + return strcmp(name(), function.name()) == 0; + } + + /** + * Function name + * @return const char * + */ + const char *name() const + { + return _ptr.text(); + } /** * Method that gets called every time the function is executed @@ -105,7 +134,7 @@ public: */ virtual Value invoke(Request &request, Parameters ¶ms) { - return 0; + return nullptr; } protected: @@ -121,20 +150,24 @@ protected: */ std::shared_ptr<Arguments> _arguments; + /** + * The name is stored in a hidden pointer, so that we have access to the function + * @var HiddenPointer + */ + HiddenPointer<Function> _ptr; + private: /** * Fill a function entry - * @param name Name of the function * @param entry Entry to be filled */ - void fill(const char *name, struct _zend_function_entry *entry); + void fill(struct _zend_function_entry *entry) const; /** * Fill function info - * @param name Name of the function * @param info Info object to be filled */ - void fill(const char *name, struct _zend_internal_function_info *info); + void fill(struct _zend_internal_function_info *info) const; /** * Extension has access to the private members diff --git a/include/hiddenpointer.h b/include/hiddenpointer.h index f29082f..3502bc9 100644 --- a/include/hiddenpointer.h +++ b/include/hiddenpointer.h @@ -106,25 +106,64 @@ public: * Retrieve the pointer * @return Type* */ - Type *pointer() + Type *pointer() const { return _pointer; } /** + * Change the pointer + * @param Type* + */ + void setPointer(Type *pointer) + { + // store pointer + _pointer = pointer; + + // overwrite in data + _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *)); + + // for safety reasons, we recalculate text pointer + _text = _data.c_str() + sizeof(Type *); + } + + /** * Retrieve the text * @return const char * */ - const char *text() + const char *text() const { return _text; } /** + * Change the text + * @param text + * @param size + */ + void setText(const char *text, int size=-1) + { + // check if size was set + if (size < 0) size = strlen(text); + + // reserve enough room for the text and the pointer + _data.reserve(size + sizeof(Type *)); + + // store the pointer + _data.assign(std::string((const char *)&_pointer, sizeof(Type *))); + + // append the text + _data.append(text, size); + + // store new text + _text = _data.c_str() + sizeof(Type *); + } + + /** * Cast to the pointer * @return Type* */ - operator Type* () + operator Type* () const { return _pointer; } @@ -133,10 +172,19 @@ public: * Cast to text * @return const char * */ - operator const char * () + operator const char * () const { return _text; } + + /** + * Length of the text + * @return int + */ + int length() const + { + return _data.size() - sizeof(Type *); + } private: /** diff --git a/include/value.h b/include/value.h index 64f92df..e4af1b7 100644 --- a/include/value.h +++ b/include/value.h @@ -45,6 +45,11 @@ public: Value(); /** + * Constructor for null ptr + */ + Value(std::nullptr_t value); + + /** * Constructor based on integer value * @param value */ |