From 21edee4d584dbf757b006349e903ff7a3b398a1d Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Wed, 12 Mar 2014 15:45:19 +0100 Subject: support for static methods --- include/class.h | 28 ++++++++++++++++++++++++++-- include/classbase.h | 43 +++++++++++++++++++++++++++++++++++-------- include/namespace.h | 18 ++++-------------- 3 files changed, 65 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/class.h b/include/class.h index 427aa64..d1afab2 100644 --- a/include/class.h +++ b/include/class.h @@ -73,7 +73,7 @@ public: virtual ~Class() {} /** - * Add a method to the class + * Add a regular method to the class * * The method will be accessible as one of the class methods in your PHP * code. When the method is called, it will automatically be forwarded @@ -104,6 +104,30 @@ public: void method(const char *name, Value (T::*method)() const, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } void method(const char *name, Value (T::*method)(Parameters ¶ms) const, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } + /** + * Add a static method to a class + * + * In C++ a static method is just a plain function, that only at compile + * time has access to the private variables. You can therefore also supply + * global functions as static method, and real static methods (that do not + * even have to come from the same class. + * + * In PHP scripts, the function will only be callable as real static method + * + * @param name Name of the method + * @param method The actual method + * @param flags Optional flags + * @param args Argument descriptions + */ + void method(const char *name, const native_callback_0 &function, int flags, const Arguments &args = {}) { ClassBase::method(name, function, flags, args); } + void method(const char *name, const native_callback_1 &function, int flags, const Arguments &args = {}) { ClassBase::method(name, function, flags, args); } + void method(const char *name, const native_callback_2 &function, int flags, const Arguments &args = {}) { ClassBase::method(name, function, flags, args); } + void method(const char *name, const native_callback_3 &function, int flags, const Arguments &args = {}) { ClassBase::method(name, function, flags, args); } + void method(const char *name, const native_callback_0 &function, const Arguments &args = {}) { ClassBase::method(name, function, Public, args); } + void method(const char *name, const native_callback_1 &function, const Arguments &args = {}) { ClassBase::method(name, function, Public, args); } + void method(const char *name, const native_callback_2 &function, const Arguments &args = {}) { ClassBase::method(name, function, Public, args); } + void method(const char *name, const native_callback_3 &function, const Arguments &args = {}) { ClassBase::method(name, function, Public, args); } + /** * Add an abstract method to the class * @@ -198,7 +222,7 @@ private: // compare the two objects if (*t1 < *t2) return -1; - if (*t1 > *t2) return 1; + if (*t2 < *t1) return 1; // they must be identical return 0; diff --git a/include/classbase.h b/include/classbase.h index 01f1d55..814feaf 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -27,6 +27,16 @@ union _zend_function; */ namespace Php { +/** + * A couple of predefined native callback functions that can be registered. + * These are functions that optional accept a Request and/or Parameters object, + * and that either return void or a Value object. + */ +typedef void (*native_callback_0)(); +typedef void (*native_callback_1)(Parameters &); +typedef Value (*native_callback_2)(); +typedef Value (*native_callback_3)(Parameters &); + /** * Method signatures */ @@ -151,14 +161,31 @@ protected: * @param flags Optional flags * @param args Description of the supported arguments */ - void method(const char *name, method_callback_0, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_1, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_2, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_3, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_4, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_5, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_6, int flags=0, const Arguments &args = {}); - void method(const char *name, method_callback_7, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_0 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_1 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_2 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_3 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_4 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_5 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_6 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const method_callback_7 &method, int flags=0, const Arguments &args = {}); + + /** + * Add a static method to the class + * + * Because a C++ static method is just a regular function, that happens to + * have access to the private variables of the class at compile time, you + * can register any function that matches one of the function signatures + * + * @param name Name of the method + * @param method The actual method + * @param flags Optional flags + * @param args Description of the supported arguments + */ + void method(const char *name, const native_callback_0 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const native_callback_1 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const native_callback_2 &method, int flags=0, const Arguments &args = {}); + void method(const char *name, const native_callback_3 &method, int flags=0, const Arguments &args = {}); /** * Add an abstract method to the class diff --git a/include/namespace.h b/include/namespace.h index 4cbf43e..9d6e6f2 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -13,16 +13,6 @@ */ namespace Php { -/** - * A couple of predefined native callback functions that can be registered. - * These are functions that optional accept a Request and/or Parameters object, - * and that either return void or a Value object. - */ -typedef void (*native_callback_0)(); -typedef void (*native_callback_1)(Parameters &); -typedef Value (*native_callback_2)(); -typedef Value (*native_callback_3)(Parameters &); - /** * Forward declaration */ @@ -71,10 +61,10 @@ public: * @param function The function to add * @param arguments Optional argument specification */ - void add(const char *name, native_callback_0 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_1 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_2 function, const Arguments &arguments = {}); - void add(const char *name, native_callback_3 function, const Arguments &arguments = {}); + void add(const char *name, const native_callback_0 &function, const Arguments &arguments = {}); + void add(const char *name, const native_callback_1 &function, const Arguments &arguments = {}); + void add(const char *name, const native_callback_2 &function, const Arguments &arguments = {}); + void add(const char *name, const native_callback_3 &function, const Arguments &arguments = {}); /** * Add a native class to the extension by moving it -- cgit v1.2.3