diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 05:23:34 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 05:23:34 -0700 |
commit | 49ac629257835426c311fb7b92b23a9138a9d77b (patch) | |
tree | d7f8d6d843206ecdf29b3877a5e7faf7815f3f24 /include | |
parent | 9e1ecc534bace7d00a265a49018c0148a56361ae (diff) |
Work in progress on implementing classes
Diffstat (limited to 'include')
-rw-r--r-- | include/base.h | 56 | ||||
-rw-r--r-- | include/class.h | 68 | ||||
-rw-r--r-- | include/classinfo.h | 79 | ||||
-rw-r--r-- | include/extension.h | 31 |
4 files changed, 229 insertions, 5 deletions
diff --git a/include/base.h b/include/base.h new file mode 100644 index 0000000..eceb114 --- /dev/null +++ b/include/base.h @@ -0,0 +1,56 @@ +/** + * Base class for defining your own objects + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +class Base +{ +public: + /** + * The pseudo constructor that is called from PHP after the object is constructed + * @param environment + * @param parameters + */ + virtual void __construct(Environment &environment, const Parameters ¶meters) + { + // call all other possible implementations + __construct(environment); + __construct(parameters); + __construct(); + } + + /** + * The pseudo constructor that is called from PHP after the object is constructed + * @param environment + */ + virtual void __construct(Environment &environment) {} + + /** + * The pseudo constructor that is called from PHP after the object is constructed + * @param parameters + */ + virtual void __construct(const Parameters ¶meters) {} + + /** + * The pseudo constructor that is called from PHP after the object is constructed + */ + virtual void __construct() {} + + +}; + +/** + * End of namespace + */ +} + diff --git a/include/class.h b/include/class.h new file mode 100644 index 0000000..38398b9 --- /dev/null +++ b/include/class.h @@ -0,0 +1,68 @@ +/** + * Class.h + * + * When a class is registered in the extension, you need this helper class + * for that. + * + * The use of it is simple: + * + * Extension::add(Class<YourClass>); + * + * Note that YourClass must extend from Php::Object + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition of the class + */ +template <typename T> +class Class +{ +public: + /** + * Constructor + */ + Class() {} + + /** + * Move constructor + * @param that + */ + Class(Class &&that) {} + + /** + * Copy constructor + */ + Class(const Class &that) {} + + /** + * Destructor + */ + virtual ~Class() {} + + /** + * Construct an instance + * @return Base + */ + Base* construct() + { + // allocate the object + return new T(); + } + +protected: + +}; + +/** + * End of namespace + */ +} + diff --git a/include/classinfo.h b/include/classinfo.h new file mode 100644 index 0000000..dfa480c --- /dev/null +++ b/include/classinfo.h @@ -0,0 +1,79 @@ +/** + * ClassInfo.h + * + * Internal class that is constructed by the library and that contains + * the information about a class, including its name. + * + * Users of the PHP-CPP libraries are not supposed to interact with + * this class, or instantiate objects of this class. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Virtual base class of the classInfo + * + * We need this virtual base class to store pointers to class objects, + * without knowing in advance what sort of object they will hold + */ +class _ClassInfo +{ +public: + /** + * Initialize the class + */ + virtual void initialize() = 0; +}; + +/** + * Class definition + */ +template <typename T> +class ClassInfo : public _ClassInfo +{ +public: + /** + * Constructor + * @param name Name of the class + * @param type The class type + */ + ClassInfo(const char *name, const Class<T> &type) : _name(name), _type(type) + { + } + + /** + * Destructor + */ + virtual ~ClassInfo() {} + + /** + * Initialize the class + */ + virtual void initialize(); + +private: + /** + * Class name + * @var string + */ + std::string _name; + + /** + * The class object + * @var Class + */ + Class<T> _type; + +}; + +/** + * End of namespace + */ +} + diff --git a/include/extension.h b/include/extension.h index ceeed78..1984aa9 100644 --- a/include/extension.h +++ b/include/extension.h @@ -80,15 +80,15 @@ public: * and after the compatibility has been checked, but before the requests * are handled. You can override this method to add your own initialization. * + * The default behavior of this function is to enable all classes that are + * defined in this extension, so that they are also available in PHP. + * * The method should return true on success, and false on failure (in which * case the extension will not be used) * * @return bool */ - virtual bool initialize() - { - return true; - } + virtual bool initialize(); /** * Finalize the extension @@ -200,6 +200,21 @@ public: Function *add(const char *name, native_callback_7 function, const std::initializer_list<Argument> &arguments = {}); /** + * Add a native class to the extension + * @param name Name of the class + * @param type The class implementation + */ + template<typename T> + void add(const char *name, const Class<T> &type) + { + // construct info + ClassInfo<T> *info = new ClassInfo<T>(name, type); + + // add class + _classes.insert(std::unique_ptr<_ClassInfo>(info)); + } + + /** * Retrieve the module entry * * This is the memory address that should be exported by the get_module() @@ -213,11 +228,17 @@ public: private: /** * Set of function objects defined in the library - * @var map + * @var set */ std::set<std::unique_ptr<Function>> _functions; /** + * Set of classes defined in the library + * @var set + */ + std::set<std::unique_ptr<_ClassInfo>> _classes; + + /** * The information that is passed to the Zend engine * * Although it would be slightly faster to not make this a pointer, this |