diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/argument.h | 125 | ||||
-rw-r--r-- | include/extension.h | 22 | ||||
-rw-r--r-- | include/function.h | 132 | ||||
-rw-r--r-- | include/type.h | 38 | ||||
-rw-r--r-- | include/variable.h | 134 |
5 files changed, 446 insertions, 5 deletions
diff --git a/include/argument.h b/include/argument.h new file mode 100644 index 0000000..862b677 --- /dev/null +++ b/include/argument.h @@ -0,0 +1,125 @@ +/** + * Argument.h + * + * Class holds information about an argument that is passed to a function. + * You'll need this class when you're defining your own functions. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Forward definitions + */ +class ArgInfo; + +/** + * Class definition + */ +class Argument +{ +public: + /** + * Constructor if this argument should be an instance of a certain class + * @param name Name of the argument + * @param classname If a specific class is required, the class type + * @param null Are NULL values allowed in stead of an instance? + * @param ref Is this a pass-by-reference argument? + */ + Argument(const std::string &name, const std::string &classname, bool null = true, bool ref = false); + + /** + * Constructor if the argument can be anything + * @param name Name of the argument + * @param type Type hint + * @param ref Is this a pass-by-reference argument? + */ + Argument(const std::string &name, Type type = nullType, bool ref = false); + + /** + * Copy constructor + * @param argument The argument to copy + */ + Argument(const Argument &argument) + { + // copy members + _refcount = argument._refcount; + _info = argument._info; + + // increase references + (*_refcount)++; + } + + /** + * Destructor + */ + virtual ~Argument() + { + // cleanup current object + cleanup(); + } + + /** + * Copy operator + * @param argument The argument to copy + * @return Argument + */ + Argument &operator=(const Argument &argument) + { + // skip self assignment + if (this == &argument) return *this; + + // clean up current object + cleanup(); + + // copy members + _refcount = argument._refcount; + _info = argument._info; + + // increase references + (*_refcount)++; + + // done + return *this; + } + + /** + * Retrieve argument info + * @return ArgInfo + * @internal + */ + ArgInfo *internal() const + { + return _info; + } + +private: + /** + * Number of references + * @var int + */ + int *_refcount; + + /** + * Pointer to the implementation + * @var ArgInfo + */ + ArgInfo *_info; + + /** + * Remove one reference from the object + */ + void cleanup(); + +}; + +/** + * End of namespace + */ +} + diff --git a/include/extension.h b/include/extension.h index eb7eb4c..701da86 100644 --- a/include/extension.h +++ b/include/extension.h @@ -26,17 +26,23 @@ struct _zend_module_entry; namespace PhpCpp { /** + * Forward definitions + */ +class Functions; + +/** * Class definition */ class Extension { public: /** - * Constructor + * Extension that defines a number of functions right away * @param name Extension name - * @param version EXtension version + * @param version Extension version string + * @param functions The functions that are defined */ - Extension(const char *name, const char *version); + Extension(const char *name, const char *version, const std::initializer_list<Function> &functions = {}); /** * Destructor @@ -151,18 +157,24 @@ private: * @var char* */ const char *_version; + + /** + * The functions that are defined + * @var vector + */ + Functions *_functions; /** * The information that is passed to the Zend engine * @var zend_module_entry */ - _zend_module_entry *_entry; + _zend_module_entry *_entry = NULL; /** * The current request being processed * @var Request */ - Request *_request; + Request *_request = NULL; }; diff --git a/include/function.h b/include/function.h new file mode 100644 index 0000000..1a35671 --- /dev/null +++ b/include/function.h @@ -0,0 +1,132 @@ +/** + * Function.h + * + * Object represents a callable function that is defined with the CPP API. + * After you've instantiated the extension, you can add function objects to + * it. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Forward definitions + */ +class Callable; + +/** + * Class definition + */ +class Function +{ +public: + /** + * Constructor + * @param name Name of the function + * @param arguments The arguments that can be passed to the function + */ + Function(const std::string &name, const std::initializer_list<Argument> &arguments); + + /** + * Constructor + * @param name Name of the function + */ + Function(const char *name) : Function(name, {}) {} + + /** + * Copy constructor + * @param function The other function + */ + Function(const Function &function) + { + // copy other object + _refcount = function._refcount; + _callable = function._callable; + + // increate number of references + (*_refcount)++; + } + + /** + * Destructor + */ + virtual ~Function() + { + // cleanup the object + cleanup(); + } + + /** + * Assignment operator + * @param function The other function + * @return Function + */ + Function &operator=(const Function &function) + { + // skip self assignment + if (&function == this) return *this; + + // cleanup the object + cleanup(); + + // copy other object + _refcount = function._refcount; + _callable = function._callable; + + // increate number of references + (*_refcount)++; + + // done + return *this; + } + + /** + * Method that gets called every time the function is executed + * @param request The request during which the call was made + * @param arguments The actual arguments that were passed + * @return Variable Return value + */ + virtual Variable invoke(const Request *request, const std::initializer_list<Variable> &arguments) + { + } + + /** + * Get access to the internal object + * @return Callable + * @internal + */ + Callable *internal() const + { + return _callable; + } + +protected: + /** + * Pointer to the callable object + * @var smart_ptr + */ + Callable *_callable; + + /** + * Counter with the number of references + * @var integer + */ + int *_refcount; + + + /** + * Remove one reference + */ + void cleanup(); +}; + +/** + * End of namespace + */ +} + diff --git a/include/type.h b/include/type.h new file mode 100644 index 0000000..f3e357f --- /dev/null +++ b/include/type.h @@ -0,0 +1,38 @@ +/** + * Type.h + * + * In this file an enumeration type is defined with all supporteded variable + * types. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Supported types for variables + * The values are the same as the ones used internally in Zend + */ +typedef enum _Type { + nullType = 0, + intType = 1, + decimalType = 2, + boolType = 3, + arrayType = 4, + objectType = 5, + stringType = 6, + resourceType = 7, + constantType = 8, + constantArrayType = 9, + callableType = 10 +} Type; + +/** + * End of namespace + */ +} + diff --git a/include/variable.h b/include/variable.h new file mode 100644 index 0000000..393239f --- /dev/null +++ b/include/variable.h @@ -0,0 +1,134 @@ +/** + * Variable.h + * + * Base class for variables that are stored in the Zend engine. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Class definition + */ +class Variable +{ +public: + /** + * Empty constructor (value = NULL) + */ + Variable(); + + /** + * Constructor based on integer value + * @param value + */ + Variable(int value); + + /** + * Constructor based on boolean value + * @param value + */ + Variable(bool value); + + /** + * Constructor based on string value + * @param value + */ + Variable(const std::string &value); + + /** + * Constructor based on decimal value + * @param value + */ + Variable(double value); + + /** + * Copy constructor + * @param value + */ + Variable(const Variable &that); + + /** + * Destructor + */ + virtual ~Variable(); + + /** + * Assignment operator + * @param value + * @return Variable + */ + virtual Variable &operator=(const Variable &value); + + /** + * Is this an integer value? + * @return bool + */ + virtual bool isInt(); + + /** + * Is this a boolean value? + * @return bool + */ + virtual bool isBool(); + + /** + * Is this a string value? + * @return bool + */ + virtual bool isString(); + + /** + * Is this a decimal value? + * @return bool + */ + virtual bool isDecimal(); + + /** + * Is this an object value? + * @return bool + */ + virtual bool isObject(); + + /** + * Is this an array value? + * @return bool + */ + virtual bool isArray(); + + /** + * Retrieve the value as integer + * @return int + */ + virtual int intValue(); + + /** + * Retrieve the value as boolean + * @return bool + */ + virtual bool boolValue(); + + /** + * Retrieve the value as string + * @return string + */ + std::string stringValue(); + + /** + * Retrieve the value as decimal + * @return double + */ + virtual double decimalValue(); +}; + +/** + * End of namespace + */ +} + + |