summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-31 08:09:51 -0700
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-31 08:09:51 -0700
commit708e9cf9da9571a38ac8d2529d016cd78ce8ec54 (patch)
treedaa1bd156b2eaa7259f5b29753a94e879b54710d
parentc2343400688366e567f67e89a50d573786f98bec (diff)
{auto} work in progress
-rw-r--r--src/callable.cpp17
-rw-r--r--src/callable.h30
-rw-r--r--src/hiddenpointer.h11
-rw-r--r--tests/simple/simple.cpp62
4 files changed, 24 insertions, 96 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
diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp
index db314ca..761c7a0 100644
--- a/tests/simple/simple.cpp
+++ b/tests/simple/simple.cpp
@@ -15,66 +15,6 @@
*/
using namespace std;
-/**
- * Override the request class
- */
-class SimpleRequest : public PhpCpp::Request
-{
-public:
- SimpleRequest(PhpCpp::Extension *extension) : PhpCpp::Request(extension)
- {
- }
-
- virtual bool initialize()
- {
- cout << "Request::initialize" << endl;
- return true;
- }
-
- virtual bool finalize()
- {
- cout << "Request::finalize" << endl;
- return true;
- }
-};
-
-/**
- * Override the extension class
- */
-class SimpleExtension : public PhpCpp::Extension
-{
-public:
- /**
- * Constructor
- */
- SimpleExtension() : Extension("simple", "1.0", {
- PhpCpp::Function("hallo", {
- PhpCpp::Argument("arg1", true)
- })
- })
- {
- }
-
- virtual bool initialize()
- {
- cout << "Extension::initialize" << endl;
- return true;
- }
-
- virtual bool finalize()
- {
- cout << "Extension::finalize" << endl;
- return true;
- }
-
- virtual PhpCpp::Request *request()
- {
- return new SimpleRequest(this);
- }
-};
-
-
-
// create the object for the PHP extension
-PHP_CPP_EXTENSION(SimpleExtension);
+PHP_CPP_EXTENSION(Extension("simple","1.0"));