diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-08-31 08:09:51 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-08-31 08:09:51 -0700 |
commit | 708e9cf9da9571a38ac8d2529d016cd78ce8ec54 (patch) | |
tree | daa1bd156b2eaa7259f5b29753a94e879b54710d /src | |
parent | c2343400688366e567f67e89a50d573786f98bec (diff) |
{auto} work in progress
Diffstat (limited to 'src')
-rw-r--r-- | src/callable.cpp | 17 | ||||
-rw-r--r-- | src/callable.h | 30 | ||||
-rw-r--r-- | src/hiddenpointer.h | 11 |
3 files changed, 23 insertions, 35 deletions
diff --git a/src/callable.cpp b/src/callable.cpp index b9f67d0..69ef148 100644 --- a/src/callable.cpp +++ b/src/callable.cpp @@ -28,9 +28,8 @@ void invoke_callable(INTERNAL_FUNCTION_PARAMETERS) // find the function name const char *function = get_active_function_name(TSRMLS_C); - // when we registered the function name, we have hidden the pointer to the - // callable right in front of the function name - we retrieve it back - Callable *callable = *((Callable **)(function - sizeof(Callable *))); + // uncover the hidden pointer inside the function name + Callable *callable = HiddenPointer<Callable>(function); // call the appropriate object callable->invoke(INTERNAL_FUNCTION_PARAM_PASSTHRU); @@ -46,8 +45,8 @@ void invoke_callable(INTERNAL_FUNCTION_PARAMETERS) */ void Callable::fill(zend_function_entry *entry) { - // fill the members of the entity - entry->fname = _name; + // fill the members of the entity, and hide a pointer to the current object in the name + entry->fname = HiddenPointer<Callable>(this, _name); entry->handler = invoke_callable; entry->arg_info = _argv; entry->num_args = _argc; @@ -64,9 +63,11 @@ void Callable::fill(zend_function_entry *entry) */ void Callable::fill(zend_internal_function_info *info) { - // fill in all the members, not that the returning by reference is not used - info->_name = _name; - info->_name_len = _data.size() - sizeof(this); + // fill in all the members, note that return reference is false by default, + // because we do want to return references, inside the name we hide a pointer + // to the current object + info->_name = HiddenPointer<Callable>(this, _name); + info->_name_len = _name.size(); info->_class_name = _classname.size() ? _classname.c_str() : NULL; info->required_num_args = _required; info->_type_hint = _type; diff --git a/src/callable.h b/src/callable.h index 893dcdb..b6f1e98 100644 --- a/src/callable.h +++ b/src/callable.h @@ -31,22 +31,8 @@ public: * @param flags Optional flags to be passed to the function */ Callable(const std::string &classname, const std::string &function, Type type = nullType, const std::initializer_list<Argument> &arguments = {}, int flags = 0) : - _classname(classname), _type(type), _flags(flags) + _classname(classname), _name(function), _type(type), _flags(flags) { - // somehow "&this" is not accepted by the compiler, so we make a copy - Callable *callable = this; - - // append function name to the data (the data contains a pointer - // to this object, appended with the function name. this is a trick - // so that we have the pointer to this function available in the - // function name by going back a number of bytes) - _data.reserve(function.size() + sizeof(this)); - _data.assign(std::string((const char *)&callable, sizeof(callable))); - _data.append(function); - - // find the name - _name = _data.c_str() + sizeof(this); - // process the arguments process(arguments); } @@ -110,23 +96,17 @@ public: private: /** - * Classname + * Classname (in case of a member function) * @var string */ std::string _classname; /** - * Pointer to current object, appended with function name + * The function name * @var string */ - std::string _data; - - /** - * Pointer to the function name - * @var char* - */ - const char *_name; - + std::string _name; + /** * The return type * @var Type diff --git a/src/hiddenpointer.h b/src/hiddenpointer.h index 616dfee..8e03eb2 100644 --- a/src/hiddenpointer.h +++ b/src/hiddenpointer.h @@ -23,7 +23,7 @@ class HiddenPointer public: /** * Constructor to hide the pointer in a buffer - * @param pointer The hidden pointer + * @param pointer The pointer to hide * @param text The visible text * @param size Optional text size */ @@ -45,7 +45,14 @@ public: _pointer = pointer; _text = _data.c_str() + sizeof(Type *); } - + + /** + * Hide pointer in buffer + * @param pointer + * @param text + */ + HiddenPointer(Type *pointer, const std::string &text) : HiddenPointer(pointer, text.c_str(), text.size()) {} + /** * Constructor to retrieve the object given a buffer * @param text The visible text |