diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-10-15 05:54:52 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-10-15 05:54:52 -0700 |
commit | 61ba30d716dab670a5f2ed0ee2f6650375b2058d (patch) | |
tree | 711db9359015de260071088ef027b020cd95d4b3 /include | |
parent | b2042dbd58c043ab49e9b0dbb51bf8516fe8cea8 (diff) |
Calling custom member methods is now functional
Diffstat (limited to 'include')
-rw-r--r-- | include/function.h | 3 | ||||
-rw-r--r-- | include/member.h | 9 | ||||
-rw-r--r-- | include/method.h | 127 | ||||
-rw-r--r-- | include/parameters.h | 23 | ||||
-rw-r--r-- | include/protected.h | 10 | ||||
-rw-r--r-- | include/public.h | 12 |
6 files changed, 156 insertions, 28 deletions
diff --git a/include/function.h b/include/function.h index 3cb0e2d..3704ce8 100644 --- a/include/function.h +++ b/include/function.h @@ -140,8 +140,9 @@ protected: * Fill a function entry * @param entry Entry to be filled * @param classname Optional class name + * @param pub Is this a public property? */ - void fill(struct _zend_function_entry *entry, const char *classname=NULL) const; + void fill(struct _zend_function_entry *entry, const char *classname=NULL, bool pub=true) const; /** * Fill function info diff --git a/include/member.h b/include/member.h index 0a7eb29..bc96e52 100644 --- a/include/member.h +++ b/include/member.h @@ -106,14 +106,7 @@ public: * @param pub Is this a public method (otherwise it is protected) * @param method The method to add */ - Member(const char *name, bool pub, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}); - Member(const char *name, bool pub, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, const _Method &method, const std::initializer_list<Argument> &arguments = {}); /** * Copy constructor diff --git a/include/method.h b/include/method.h new file mode 100644 index 0000000..9398fd7 --- /dev/null +++ b/include/method.h @@ -0,0 +1,127 @@ +/** + * Method.h + */ + +/** + * Namespace + */ +namespace Php { + +/** + * A very generic function pointer + */ +typedef void (*function_ptr)(); + +/** + * Base class of the method + */ +class _Method +{ +public: + /** + * Copy constructor + * @param method + */ + _Method(const _Method &method) : _type(method._type), _callback(method._callback) {} + + /** + * Destructor + * @param type + * @param callback + */ + virtual ~_Method() {} + + /** + * Invoke the method + * @param environment + * @param parameters + * @return Value + */ + Value invoke(Environment &environment, Parameters ¶meters) + { + // the object to call a method on + Base *base = parameters.object(); + + // find out which method to call, and call it + switch (_type) { + case 0: (base->*_callback.m0)(); return Value(); + case 1: (base->*_callback.m1)(parameters); return Value(); + case 2: (base->*_callback.m2)(environment); return Value(); + case 3: (base->*_callback.m3)(environment, parameters); return Value(); + case 4: return (base->*_callback.m4)(); + case 5: return (base->*_callback.m5)(parameters); + case 6: return (base->*_callback.m6)(environment); + case 7: return (base->*_callback.m7)(environment, parameters); + default: return Value(); + } + } + +protected: + /** + * Protected constructor to prevent that anyone instantiates this object + */ + _Method(method_callback_0 callback) : _type(0) { _callback.m0 = callback; } + _Method(method_callback_1 callback) : _type(1) { _callback.m1 = callback; } + _Method(method_callback_2 callback) : _type(2) { _callback.m2 = callback; } + _Method(method_callback_3 callback) : _type(3) { _callback.m3 = callback; } + _Method(method_callback_4 callback) : _type(4) { _callback.m4 = callback; } + _Method(method_callback_5 callback) : _type(5) { _callback.m5 = callback; } + _Method(method_callback_6 callback) : _type(6) { _callback.m6 = callback; } + _Method(method_callback_7 callback) : _type(7) { _callback.m7 = callback; } + +private: + /** + * Callback type + * @var int + */ + int _type; + + /** + * The actual callback + * @var void* + */ + union { + method_callback_0 m0; + method_callback_1 m1; + method_callback_2 m2; + method_callback_3 m3; + method_callback_4 m4; + method_callback_5 m5; + method_callback_6 m6; + method_callback_7 m7; + } _callback; +}; + +/** + * Actual template class of the method + */ +template <typename T> +class Method : public _Method +{ +public: + /** + * Constructor + * @param callback + */ + Method(void(T::*callback)()) : _Method(static_cast<method_callback_0>(callback)) {} + Method(void(T::*callback)(Parameters&)) : _Method(static_cast<method_callback_1>(callback)) {} + Method(void(T::*callback)(Environment&)) : _Method(static_cast<method_callback_2>(callback)) {} + Method(void(T::*callback)(Environment&,Parameters&)) : _Method(static_cast<method_callback_3>(callback)) {} + Method(Value(T::*callback)()) : _Method(static_cast<method_callback_4>(callback)) {} + Method(Value(T::*callback)(Parameters&)) : _Method(static_cast<method_callback_5>(callback)) {} + Method(Value(T::*callback)(Environment&)) : _Method(static_cast<method_callback_6>(callback)) {} + Method(Value(T::*callback)(Environment&,Parameters&)) : _Method(static_cast<method_callback_7>(callback)) {} + + /** + * Destructor + */ + virtual ~Method() {} + +}; + +/** + * End of namespace + */ +} + + diff --git a/include/parameters.h b/include/parameters.h index 138d973..b4aca16 100644 --- a/include/parameters.h +++ b/include/parameters.h @@ -13,6 +13,11 @@ namespace Php { /** + * Forward declarations + */ +class Base; + +/** * Class definition */ class Parameters : public std::vector<Value> @@ -20,15 +25,29 @@ class Parameters : public std::vector<Value> public: /** * Constructor - * @param argc Number of arguments + * @param this_ptr Optional this_ptr + * @param argc Number of arguments * @param tsrm_ls */ - Parameters(int argc);// TSRMLS_DC); + Parameters(struct _zval_struct *this_ptr, int argc);// TSRMLS_DC); /** * Destructor */ virtual ~Parameters() {} + + /** + * The the object that is called + * @return Base + */ + Base *object(); + +private: + /** + * The this pointer + * @var zval + */ + struct _zval_struct *_this; }; /** diff --git a/include/protected.h b/include/protected.h index af2c252..f3e668a 100644 --- a/include/protected.h +++ b/include/protected.h @@ -37,15 +37,9 @@ public: * Constructor * @param name Name of the property * @param method Method to add + * @param arguments Optional argument information */ - Protected(const char *name, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - Protected(const char *name, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, const _Method &method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} /** * Destructor diff --git a/include/public.h b/include/public.h index 09625fc..12648a1 100644 --- a/include/public.h +++ b/include/public.h @@ -27,7 +27,7 @@ public: Public(const char *name, std::nullptr_t value) : Member(name, true, value) {} Public(const char *name, int value) : Member(name, true, value) {} Public(const char *name, long value) : Member(name, true, value) {} -// Public(const char *name, bool value) : Member(name, true, value) {} + Public(const char *name, bool value) : Member(name, true, value) {} Public(const char *name, char value) : Member(name, true, value) {} Public(const char *name, const std::string &value) : Member(name, true, value) {} Public(const char *name, const char *value, int size=-1) : Member(name, true, value, size) {} @@ -37,15 +37,9 @@ public: * Constructor * @param name Name of the property * @param method Method to add + * @param arguments Optional argument information */ - Public(const char *name, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - Public(const char *name, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, const _Method &method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} /** * Destructor |