diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-10 09:54:14 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-10 09:54:14 -0700 |
commit | 37c38c70de43d9d9ee3c9f0e2f175e19bcc0b485 (patch) | |
tree | 29cf02cfabad5eb22306ac05eaee96efbdb79cd9 /include | |
parent | 85507088051bdfabf8bc71346290949b78c3c914 (diff) |
When registering functions, it now is also possible to specify the signature of the parameters
Diffstat (limited to 'include')
-rw-r--r-- | include/argument.h | 71 | ||||
-rw-r--r-- | include/arguments.h | 98 | ||||
-rw-r--r-- | include/byref.h | 60 | ||||
-rw-r--r-- | include/byval.h | 60 | ||||
-rw-r--r-- | include/extension.h | 25 | ||||
-rw-r--r-- | include/function.h | 67 |
6 files changed, 185 insertions, 196 deletions
diff --git a/include/argument.h b/include/argument.h index 7a95f7d..c29073d 100644 --- a/include/argument.h +++ b/include/argument.h @@ -4,6 +4,9 @@ * Class holds information about an argument that is passed to a function. * You'll need this class when you're defining your own functions. * + * The constructor of the argument is protected. Use the ByVal or ByRef + * classes instead. + * * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> * @copyright 2013 Copernica BV */ @@ -25,65 +28,49 @@ class Argument { public: /** - * Prevent copying + * Copy constructor * @param argument */ - Argument(const Argument &argument) = delete; - + Argument(const Argument &argument); + /** * Move constructor * @param argument */ Argument(Argument &&argument); - + /** * Destructor */ - virtual ~Argument() {}; - - /** - * Change the name - * @param name - * @return Argument - */ - Argument &name(const char *name); - - /** - * Change the type - * @param type - * @return Argument - */ - Argument &type(Type type = nullType); + virtual ~Argument(); +protected: /** - * Require the parameter to be a certain class - * @param name Name of the class - * @param null Are null values allowed? - * @return Argument + * Constructor + * @param name Name of the argument + * @param type Argument type + * @param required Is this argument required? + * @param byref Is this a reference argument */ - Argument &object(const char *classname, bool null = true); + Argument(const char *name, Type type, bool required = true, bool byref = false); /** - * Is this a by-ref argument? - * @param bool Mark as by-ref variable - * @return Argument + * Constructor + * @param name Name of the argument + * @param classname Name of the class + * @param nullable Can it be null? + * @param required Is this argument required? + * @param byref Is this a reference argument? */ - Argument &byref(bool value = true); + Argument(const char *name, const char *classname, bool nullable = true, bool required = true, bool byref = false); +public: /** - * Prevent copy - * @param argument The argument to copy - * @return Argument - */ - Argument &operator=(const Argument &argument) = delete; - -protected: - /** - * Protected constructor, to prevent that users can instantiate the - * argument object themselves + * Fill an arg_info structure with data * @param info + * @internal */ - Argument(struct _zend_arg_info *info) : _info(info) {} + void fill(struct _zend_arg_info *info) const; private: /** @@ -91,6 +78,12 @@ private: * @var zend_arg_info */ struct _zend_arg_info *_info; + + /** + * Is this a required argument + * @var bool + */ + bool _required; }; /** diff --git a/include/arguments.h b/include/arguments.h deleted file mode 100644 index e57903f..0000000 --- a/include/arguments.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Arguments.h - * - * When a function is invoked, it is passed a vector of arguments. This - * arguments class, that overrides from vector, takes care of that. - * - * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @copyright 2013 Copernica BV - */ - -/** - * Forward declaration - */ -struct _zend_arg_info; - -/** - * Set up namespace - */ -namespace Php { - -/** - * Class definition - */ -class Arguments -{ -public: - /** - * Constructor - * @param min The min number of arguments - * @param max The max number of arguments - */ - Arguments(int min, int max); - - /** - * No copy or move operations - * @param arguments - */ - Arguments(const Arguments &arguments) = delete; - Arguments(Arguments &&arguments) = delete; - - /** - * Destructor - */ - virtual ~Arguments(); - - /** - * Number of arguments - * @return int - */ - int argc() - { - return _max; - } - - /** - * Number of required arguments - * @return int - */ - int required() - { - return _min; - } - - /** - * Get access to internal data - * @return zend_arg_info* - */ - struct _zend_arg_info *internal() - { - return _argv; - } - -private: - /** - * Min number of arguments - * @var integer - */ - int _min; - - /** - * Max number of arguments - * @var integer - */ - int _max; - - /** - * The arguments - * @var zend_arg_info[] - */ - struct _zend_arg_info *_argv; - -}; - -/** - * End of namespace - */ -} - diff --git a/include/byref.h b/include/byref.h new file mode 100644 index 0000000..a10fff1 --- /dev/null +++ b/include/byref.h @@ -0,0 +1,60 @@ +/** + * ByRef.h + * + * Overridden Argument class to specify by-reference function arguments + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +class ByRef : public Argument +{ +public: + /** + * Constructor + * @param name Name of the argument + * @param type Argument type + * @param required Is this argument required? + */ + ByRef(const char *name, Type type, bool required = true) : Argument(name, type, required, true) {} + + /** + * Constructor + * @param name Name of the argument + * @param classname Name of the class + * @param nullable Can it be null? + * @param required Is this argument required? + */ + ByRef(const char *name, const char *classname, bool nullable = true, bool required = true) : Argument(name, classname, nullable, required, true) {} + + /** + * Copy constructor + * @param argument + */ + ByRef(const ByRef &argument) : Argument(argument) {} + + /** + * Move constructor + * @param argument + */ + ByRef(ByRef &&argument) : Argument(argument) {} + + /** + * Destructor + */ + virtual ~ByRef() {} +}; + +/** + * End of namespace + */ +} + diff --git a/include/byval.h b/include/byval.h new file mode 100644 index 0000000..a31c1f7 --- /dev/null +++ b/include/byval.h @@ -0,0 +1,60 @@ +/** + * ByVal.h + * + * Overridden Argument class to specify by-value function arguments + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +class ByVal : public Argument +{ +public: + /** + * Constructor + * @param name Name of the argument + * @param type Argument type + * @param required Is this argument required? + */ + ByVal(const char *name, Type type, bool required = true) : Argument(name, type, required, false) {} + + /** + * Constructor + * @param name Name of the argument + * @param classname Name of the class + * @param nullable Can it be null? + * @param required Is this argument required? + */ + ByVal(const char *name, const char *classname, bool nullable = true, bool required = true) : Argument(name, classname, nullable, required, false) {} + + /** + * Copy constructor + * @param argument + */ + ByVal(const ByVal &argument) : Argument(argument) {} + + /** + * Move constructor + * @param argument + */ + ByVal(ByVal &&argument) : Argument(argument) {} + + /** + * Destructor + */ + virtual ~ByVal() {} +}; + +/** + * End of namespace + */ +} + diff --git a/include/extension.h b/include/extension.h index fb33c99..f5987e2 100644 --- a/include/extension.h +++ b/include/extension.h @@ -174,22 +174,23 @@ public: * @param function The function to add * @return Function The added function */ - Function &add(Function *function); + Function *add(Function *function); /** * Add a native function directly to the extension * @param name Name of the function * @param function The function to add + * @param arguments Optional argument specification * @return Function The added function */ - Function &add(const char *name, native_callback_0 function); - Function &add(const char *name, native_callback_1 function); - Function &add(const char *name, native_callback_2 function); - Function &add(const char *name, native_callback_3 function); - Function &add(const char *name, native_callback_4 function); - Function &add(const char *name, native_callback_5 function); - Function &add(const char *name, native_callback_6 function); - Function &add(const char *name, native_callback_7 function); + Function *add(const char *name, native_callback_0 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_1 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_2 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_3 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_4 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_5 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_6 function, const std::initializer_list<Argument> &arguments = {}); + Function *add(const char *name, native_callback_7 function, const std::initializer_list<Argument> &arguments = {}); /** * Retrieve the module entry @@ -210,12 +211,6 @@ private: std::set<std::unique_ptr<Function>> _functions; /** - * Hidden pointer to self - * @var HiddenPointer - */ - HiddenPointer<Extension> _ptr; - - /** * The information that is passed to the Zend engine * * Although it would be slightly faster to not make this a pointer, this diff --git a/include/function.h b/include/function.h index c2dd9b8..ff4396c 100644 --- a/include/function.h +++ b/include/function.h @@ -32,60 +32,26 @@ public: * @param min Min number of arguments * @param max Max number of arguments */ - Function(const char *name, int min = 0, int max = 0) : _ptr(this, name) - { - // construct the arguments - _arguments = std::shared_ptr<Arguments>(new Arguments(min, max)); - } + Function(const char *name, const std::initializer_list<Argument> &arguments = {}); /** - * No copy constructor - * @param function The other function - */ - Function(const Function &function) : _ptr(this, function.name()) - { - // copy members - _arguments = function._arguments; - _type = function._type; - } - - /** - * Move constructor + * No copy and move constructors * @param function The other function */ - Function(Function &&function) : _ptr(this, function.name()) - { - // copy arguments - _arguments = function._arguments; - _type = function._type; - - // no longer need the other arguments - function._arguments.reset(); - } + Function(const Function &function) = delete; + Function(Function &&function) = delete; /** * Destructor */ - virtual ~Function() {} + virtual ~Function(); /** - * Assignment operator + * No assignment operator * @param function The other function * @return Function */ - Function &operator=(const Function &function) - { - // skip self reference - if (this == &function) return *this; - - // copy members - _ptr.setText(function.name()); - _arguments = function._arguments; - _type = function._type; - - // done - return *this; - } + Function &operator=(const Function &function) = delete; /** * Comparison @@ -136,6 +102,7 @@ public: { return nullptr; } + protected: /** @@ -144,11 +111,23 @@ protected: */ Type _type = nullType; + /** + * Required number of arguments + * @var integer + */ + int _required; + + /** + * Total number of arguments + * @var integer + */ + int _argc; + /** - * Pointer to the arguments - * @var shared_ptr + * The arguments + * @var zend_arg_info[] */ - std::shared_ptr<Arguments> _arguments; + struct _zend_arg_info *_argv; /** * The name is stored in a hidden pointer, so that we have access to the function |