From 6a760adf754ed4b736b2a2e4c08936f005a71bcb Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 29 Mar 2014 14:57:47 +0100 Subject: all methods to register functions, methods, properties and classes not return *this, to allow chaining these methods. This was suggested by valmat in issue #55 --- include/class.h | 88 +++++++++++++++++++++++++++-------------------------- include/classbase.h | 63 ++++++++++++++++++++------------------ include/namespace.h | 41 +++++++++++++++++++------ include/value.h | 4 +-- 4 files changed, 112 insertions(+), 84 deletions(-) (limited to 'include') diff --git a/include/class.h b/include/class.h index 192e653..a63d35d 100644 --- a/include/class.h +++ b/include/class.h @@ -78,23 +78,24 @@ public: * @param method The actual method * @param flags Optional flags * @param args Argument descriptions - */ - void method(const char *name, void (T::*method)(), int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, void (T::*method)(Parameters ¶ms), int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, Value (T::*method)(), int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, Value (T::*method)(Parameters ¶ms), int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, void (T::*method)(), const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - void method(const char *name, void (T::*method)(Parameters ¶ms), const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - void method(const char *name, Value (T::*method)(), const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - void method(const char *name, Value (T::*method)(Parameters ¶ms), const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - void method(const char *name, void (T::*method)() const, int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, void (T::*method)(Parameters ¶ms) const, int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, Value (T::*method)() const, int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, Value (T::*method)(Parameters ¶ms) const, int flags, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), flags, args); } - void method(const char *name, void (T::*method)() const, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - void method(const char *name, void (T::*method)(Parameters ¶ms) const, const Arguments &args = {}) { ClassBase::method(name, static_cast(method), Public, args); } - 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); } + * @return Class Same object to allow chaining + */ + Class &method(const char *name, void (T::*method)(), int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, void (T::*method)(Parameters ¶ms), int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, Value (T::*method)(), int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, Value (T::*method)(Parameters ¶ms), int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, void (T::*method)(), const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, void (T::*method)(Parameters ¶ms), const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, Value (T::*method)(), const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, Value (T::*method)(Parameters ¶ms), const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, void (T::*method)() const, int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, void (T::*method)(Parameters ¶ms) const, int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, Value (T::*method)() const, int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, Value (T::*method)(Parameters ¶ms) const, int flags, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), flags, args); } + Class &method(const char *name, void (T::*method)() const, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, void (T::*method)(Parameters ¶ms) const, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, Value (T::*method)() const, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } + Class &method(const char *name, Value (T::*method)(Parameters ¶ms) const, const Arguments &args = {}) { return ClassBase::method(name, static_cast(method), Public, args); } /** * Add a static method to a class @@ -110,15 +111,16 @@ public: * @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); } + * @return Class Same object to allow chaining + */ + Class &method(const char *name, const native_callback_0 &function, int flags, const Arguments &args = {}) { return ClassBase::method(name, function, flags, args); } + Class &method(const char *name, const native_callback_1 &function, int flags, const Arguments &args = {}) { return ClassBase::method(name, function, flags, args); } + Class &method(const char *name, const native_callback_2 &function, int flags, const Arguments &args = {}) { return ClassBase::method(name, function, flags, args); } + Class &method(const char *name, const native_callback_3 &function, int flags, const Arguments &args = {}) { return ClassBase::method(name, function, flags, args); } + Class &method(const char *name, const native_callback_0 &function, const Arguments &args = {}) { return ClassBase::method(name, function, Public, args); } + Class &method(const char *name, const native_callback_1 &function, const Arguments &args = {}) { return ClassBase::method(name, function, Public, args); } + Class &method(const char *name, const native_callback_2 &function, const Arguments &args = {}) { return ClassBase::method(name, function, Public, args); } + Class &method(const char *name, const native_callback_3 &function, const Arguments &args = {}) { return ClassBase::method(name, function, Public, args); } /** * Add an abstract method to the class @@ -131,8 +133,8 @@ public: * @param flags Optional flags * @param args Argument descriptions */ - void method(const char *name, int flags, const Arguments &args = {}) { ClassBase::method(name, flags | Abstract, args); } - void method(const char *name, const Arguments &args = {}) { ClassBase::method(name, Public | Abstract, args); } + Class &method(const char *name, int flags, const Arguments &args = {}) { return ClassBase::method(name, flags | Abstract, args); } + Class &method(const char *name, const Arguments &args = {}) { return ClassBase::method(name, Public | Abstract, args); } /** * Add a property to the class @@ -147,15 +149,15 @@ public: * @param value Actual property value * @param flags Optional flags */ - void property(const char *name, std::nullptr_t value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, int64_t value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, int32_t value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, int16_t value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, char value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, const char *value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, const std::string &value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, bool value, int flags = Public) { ClassBase::property(name, value, flags); } - void property(const char *name, double value, int flags = Public) { ClassBase::property(name, value, flags); } + Class &property(const char *name, std::nullptr_t value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, int64_t value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, int32_t value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, int16_t value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, char value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, const char *value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, const std::string &value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, bool value, int flags = Public) { return ClassBase::property(name, value, flags); } + Class &property(const char *name, double value, int flags = Public) { return ClassBase::property(name, value, flags); } /** * Properties as methods @@ -170,12 +172,12 @@ public: * @param getter The getter method * @param setter The setter method */ - void property(const char *name, Value (T::*getter)() ) { ClassBase::property(name, static_cast(getter)); } - void property(const char *name, Value (T::*getter)() const ) { ClassBase::property(name, static_cast(getter)); } - void property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) ) { ClassBase::property(name, static_cast(getter), static_cast(setter)); } - void property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) ) { ClassBase::property(name, static_cast(getter), static_cast(setter)); } - void property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) const) { ClassBase::property(name, static_cast(getter), static_cast(setter)); } - void property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) const) { ClassBase::property(name, static_cast(getter), static_cast(setter)); } + Class &property(const char *name, Value (T::*getter)() ) { return ClassBase::property(name, static_cast(getter)); } + Class &property(const char *name, Value (T::*getter)() const ) { return ClassBase::property(name, static_cast(getter)); } + Class &property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) ) { return ClassBase::property(name, static_cast(getter), static_cast(setter)); } + Class &property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) ) { return ClassBase::property(name, static_cast(getter), static_cast(setter)); } + Class &property(const char *name, Value (T::*getter)() , void (T::*setter)(const Value &value) const) { return ClassBase::property(name, static_cast(getter), static_cast(setter)); } + Class &property(const char *name, Value (T::*getter)() const, void (T::*setter)(const Value &value) const) { return ClassBase::property(name, static_cast(getter), static_cast(setter)); } private: /** diff --git a/include/classbase.h b/include/classbase.h index d0c67fa..4e03f1c 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -224,15 +224,16 @@ protected: * @param method The actual method * @param flags Optional flags * @param args Description of the supported arguments + * @return ClassBase Same object to allow chaining */ - 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 = {}); + ClassBase &method(const char *name, const method_callback_0 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_1 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_2 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_3 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_4 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_5 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_6 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const method_callback_7 &method, int flags=0, const Arguments &args = {}); /** * Add a static method to the class @@ -245,11 +246,12 @@ protected: * @param method The actual method * @param flags Optional flags * @param args Description of the supported arguments + * @return ClassBase Same object to allow chaining */ - 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 = {}); + ClassBase &method(const char *name, const native_callback_0 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const native_callback_1 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const native_callback_2 &method, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, const native_callback_3 &method, int flags=0, const Arguments &args = {}); /** * Add an abstract method to the class @@ -257,8 +259,9 @@ protected: * @param name Name of the method * @param flags Optional flags (like public or protected) * @param args Description of the supported arguments + * @return ClassBase Same object to allow chaining */ - void method(const char *name, int flags=0, const Arguments &args = {}); + ClassBase &method(const char *name, int flags=0, const Arguments &args = {}); /** * Add a property to the class @@ -272,29 +275,31 @@ protected: * @param name Name of the property * @param value Actual property value * @param flags Optional flags + * @return ClassBase Same object to allow chaining */ - void property(const char *name, std::nullptr_t value, int flags = Php::Public); - void property(const char *name, int16_t value, int flags = Php::Public); - void property(const char *name, int32_t value, int flags = Php::Public); - void property(const char *name, int64_t value, int flags = Php::Public); - void property(const char *name, bool value, int flags = Php::Public); - void property(const char *name, char value, int flags = Php::Public); - void property(const char *name, const std::string &value, int flags = Php::Public); - void property(const char *name, const char *value, int flags = Php::Public); - void property(const char *name, double value, int flags = Php::Public); + ClassBase &property(const char *name, std::nullptr_t value, int flags = Php::Public); + ClassBase &property(const char *name, int16_t value, int flags = Php::Public); + ClassBase &property(const char *name, int32_t value, int flags = Php::Public); + ClassBase &property(const char *name, int64_t value, int flags = Php::Public); + ClassBase &property(const char *name, bool value, int flags = Php::Public); + ClassBase &property(const char *name, char value, int flags = Php::Public); + ClassBase &property(const char *name, const std::string &value, int flags = Php::Public); + ClassBase &property(const char *name, const char *value, int flags = Php::Public); + ClassBase &property(const char *name, double value, int flags = Php::Public); /** * Set property with callbacks * @param name Name of the property * @param getter Getter method * @param setter Setter method - */ - void property(const char *name, const getter_callback_0 &getter); - void property(const char *name, const getter_callback_1 &getter); - void property(const char *name, const getter_callback_0 &getter, const setter_callback_0 &setter); - void property(const char *name, const getter_callback_1 &getter, const setter_callback_0 &setter); - void property(const char *name, const getter_callback_0 &getter, const setter_callback_1 &setter); - void property(const char *name, const getter_callback_1 &getter, const setter_callback_1 &setter); + * @return ClassBase Same object to allow chaining + */ + ClassBase &property(const char *name, const getter_callback_0 &getter); + ClassBase &property(const char *name, const getter_callback_1 &getter); + ClassBase &property(const char *name, const getter_callback_0 &getter, const setter_callback_0 &setter); + ClassBase &property(const char *name, const getter_callback_1 &getter, const setter_callback_0 &setter); + ClassBase &property(const char *name, const getter_callback_0 &getter, const setter_callback_1 &setter); + ClassBase &property(const char *name, const getter_callback_1 &getter, const setter_callback_1 &setter); private: /** diff --git a/include/namespace.h b/include/namespace.h index 1319976..de73924 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -60,90 +60,111 @@ public: * @param name Name of the function * @param function The function to add * @param arguments Optional argument specification + * @return Namespace Same object to allow chaining */ - 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 = {}); + Namespace &add(const char *name, const native_callback_0 &function, const Arguments &arguments = {}); + Namespace &add(const char *name, const native_callback_1 &function, const Arguments &arguments = {}); + Namespace &add(const char *name, const native_callback_2 &function, const Arguments &arguments = {}); + Namespace &add(const char *name, const native_callback_3 &function, const Arguments &arguments = {}); /** * Add a native class to the extension by moving it * @param type The class implementation + * @return Namespace Same object to allow chaining */ template - void add(Class &&type) + Namespace &add(Class &&type) { // make a copy of the object auto *copy = new Class(std::move(type)); // and add it to the list of classes _classes.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } /** * Add a native class to the extension by copying it * @param type The class implementation + * @param Namespace Same object to allow chaining */ template - void add(const Class &type) + Namespace &add(const Class &type) { // make a copy of the object auto *copy = new Class(std::move(type)); // and add it to the list of classes _classes.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } /** * Add an interface to the extension by moving it * @param interface The interface properties */ - void add(Interface &&interface) + Namespace &add(Interface &&interface) { // make a copy of the object auto *copy = new Interface(std::move(interface)); // and add it to the list of classes _classes.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } /** * Add an interface to the extension by copying it * @param interface The interface properties */ - void add(const Interface &interface) + Namespace &add(const Interface &interface) { // make a copy of the object auto *copy = new Interface(interface); // and add it to the list of classes _classes.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } /** * Add a namespace to the extension by moving it * @param ns The namespace */ - void add(Namespace &&ns) + Namespace &add(Namespace &&ns) { // make a copy of the object auto *copy = new Namespace(std::move(ns)); // add it to the list of namespaces _namespaces.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } /** * Add a namespace to the extension by copying it * @param ns The namespace */ - void add(const Namespace &ns) + Namespace &add(const Namespace &ns) { // make a copy of the object auto *copy = new Namespace(std::move(ns)); // add it to the list of namespaces _namespaces.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; } diff --git a/include/value.h b/include/value.h index 47cebcf..36f82af 100644 --- a/include/value.h +++ b/include/value.h @@ -580,7 +580,7 @@ public: */ operator int16_t () const { - return numericValue(); + return (int16_t)numericValue(); } /** @@ -589,7 +589,7 @@ public: */ operator int32_t () const { - return numericValue(); + return (int32_t)numericValue(); } /** -- cgit v1.2.3