diff options
-rw-r--r-- | include/base.h | 12 | ||||
-rw-r--r-- | include/class.h | 14 | ||||
-rw-r--r-- | include/classinfo.h | 18 | ||||
-rw-r--r-- | include/function.h | 8 | ||||
-rw-r--r-- | include/member.h | 38 | ||||
-rw-r--r-- | include/members.h | 57 | ||||
-rw-r--r-- | include/protected.h | 14 | ||||
-rw-r--r-- | include/public.h | 18 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/boolmember.h | 6 | ||||
-rw-r--r-- | src/class.cpp | 1 | ||||
-rw-r--r-- | src/classinfo.cpp | 4 | ||||
-rw-r--r-- | src/doublemember.h | 6 | ||||
-rw-r--r-- | src/function.cpp | 14 | ||||
-rw-r--r-- | src/includes.h | 2 | ||||
-rw-r--r-- | src/longmember.h | 6 | ||||
-rw-r--r-- | src/member.cpp | 174 | ||||
-rw-r--r-- | src/memberinfo.h | 22 | ||||
-rw-r--r-- | src/members.cpp | 94 | ||||
-rw-r--r-- | src/methodmember.h | 85 | ||||
-rw-r--r-- | src/nullmember.h | 6 | ||||
-rw-r--r-- | src/stringmember.h | 6 | ||||
-rw-r--r-- | tests/simple/simple.cpp | 6 | ||||
-rw-r--r-- | tests/simple/simple.php | 75 |
24 files changed, 628 insertions, 59 deletions
diff --git a/include/base.h b/include/base.h index 797aff6..6acd805 100644 --- a/include/base.h +++ b/include/base.h @@ -69,6 +69,18 @@ public: }; /** + * Definition of a method + */ +typedef void (Base::*method_callback_0)(); +typedef void (Base::*method_callback_1)(Parameters &); +typedef void (Base::*method_callback_2)(Environment &); +typedef void (Base::*method_callback_3)(Environment &, Parameters &); +typedef Value (Base::*method_callback_4)(); +typedef Value (Base::*method_callback_5)(Parameters &); +typedef Value (Base::*method_callback_6)(Environment &); +typedef Value (Base::*method_callback_7)(Environment &, Parameters &); + +/** * End of namespace */ } diff --git a/include/class.h b/include/class.h index d5d48b4..3746cf3 100644 --- a/include/class.h +++ b/include/class.h @@ -81,13 +81,23 @@ public: iter->declare(entry); } } + + /** + * Retrieve the functions + * @param classname + * @return zend_function_entry* + */ + struct _zend_function_entry *methods(const char *classname) + { + return _members.methods(classname); + } protected: /** * The initial arguments - * @var vector + * @var Members */ - std::vector<Member> _members; + Members _members; }; diff --git a/include/classinfo.h b/include/classinfo.h index cbf2c66..276bc63 100644 --- a/include/classinfo.h +++ b/include/classinfo.h @@ -63,7 +63,13 @@ public: */ virtual void initialize(struct _zend_class_entry *entry) = 0; -private: + /** + * Retrieve the methods + * @return zend_function_entry[] + */ + virtual struct _zend_function_entry *methods() = 0; + +protected: /** * The class entry * @var zend_class_entry @@ -130,6 +136,16 @@ public: _type.initialize(entry); } + /** + * Retrieve the methods + * @return zend_function_entry[] + */ + virtual struct _zend_function_entry *methods() + { + // ask class object + return _type.methods(_name.c_str()); + } + private: /** * The class object diff --git a/include/function.h b/include/function.h index 89ec3fd..3cb0e2d 100644 --- a/include/function.h +++ b/include/function.h @@ -135,18 +135,20 @@ protected: */ HiddenPointer<Function> _ptr; -private: +protected: /** * Fill a function entry * @param entry Entry to be filled + * @param classname Optional class name */ - void fill(struct _zend_function_entry *entry) const; + void fill(struct _zend_function_entry *entry, const char *classname=NULL) const; /** * Fill function info * @param info Info object to be filled + * @param classname Optional class name */ - void fill(struct _zend_internal_function_info *info) const; + void fill(struct _zend_internal_function_info *info, const char *classname=NULL) const; /** * Extension has access to the private members diff --git a/include/member.h b/include/member.h index 5043ead..0a7eb29 100644 --- a/include/member.h +++ b/include/member.h @@ -99,6 +99,21 @@ public: * @param value The value to add */ Member(const char *name, bool pub, double value); + + /** + * Constructor + * @param name Name of the method + * @param pub Is this a public method (otherwise it is protected) + * @param method The method to add + */ + Member(const char *name, bool pub, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, bool pub, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}); /** * Copy constructor @@ -119,10 +134,31 @@ public: /** * Internal method to declare the property - * @var zend_class_entry + * @param zend_class_entry + * @internal */ void declare(struct _zend_class_entry *entry); + /** + * Internal method to fill a function entry + * @param zend_function_entry + * @param classname + * @internal + */ + void fill(struct _zend_function_entry *entry, const char *classname); + + /** + * Is this a property member + * @return bool + */ + bool isProperty(); + + /** + * Is this a method member + * @return bool + */ + bool isMethod(); + private: /** diff --git a/include/members.h b/include/members.h new file mode 100644 index 0000000..47cbebd --- /dev/null +++ b/include/members.h @@ -0,0 +1,57 @@ +/** + * Members.h + * + * Internal helper class that holds all members of a class + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +class Members : public std::vector<Member> +{ +public: + /** + * Constructor + * @param arguments + */ + Members(const std::initializer_list<Member> &members) : std::vector<Member>(members), _methods(NULL) {} + + /** + * Destructor + */ + virtual ~Members(); + + /** + * Get access to the methods + * @param classname + * @return Methods + */ + struct _zend_function_entry *methods(const char *classname); + +private: + /** + * Number of methods + * @return integer + */ + int methods(); + + /** + * Array of method structures used internally in the Zend engine + * @var zend_function_entry + */ + struct _zend_function_entry *_methods; +}; + +/** + * End of namespace + */ +} + diff --git a/include/protected.h b/include/protected.h index a1fdde5..af2c252 100644 --- a/include/protected.h +++ b/include/protected.h @@ -32,6 +32,20 @@ public: Protected(const char *name, const std::string &value) : Member(name, false, value) {} Protected(const char *name, const char *value, int size=-1) : Member(name, false, value, size) {} Protected(const char *name, double value) : Member(name, false, value) {} + + /** + * Constructor + * @param name Name of the property + * @param method Method to add + */ + Protected(const char *name, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} + Protected(const char *name, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} /** * Destructor diff --git a/include/public.h b/include/public.h index b37230b..09625fc 100644 --- a/include/public.h +++ b/include/public.h @@ -27,12 +27,26 @@ public: Public(const char *name, std::nullptr_t value) : Member(name, true, value) {} Public(const char *name, int value) : Member(name, true, value) {} Public(const char *name, long value) : Member(name, true, value) {} - Public(const char *name, bool value) : Member(name, true, value) {} +// Public(const char *name, bool value) : Member(name, true, value) {} Public(const char *name, char value) : Member(name, true, value) {} Public(const char *name, const std::string &value) : Member(name, true, value) {} Public(const char *name, const char *value, int size=-1) : Member(name, true, value, size) {} Public(const char *name, double value) : Member(name, true, value) {} - + + /** + * Constructor + * @param name Name of the property + * @param method Method to add + */ + Public(const char *name, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + Public(const char *name, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} + /** * Destructor */ @@ -36,6 +36,7 @@ #include <phpcpp/member.h> #include <phpcpp/public.h> #include <phpcpp/protected.h> +#include <phpcpp/members.h> #include <phpcpp/class.h> #include <phpcpp/classinfo.h> #include <phpcpp/extension.h> diff --git a/src/boolmember.h b/src/boolmember.h index c7db592..fa482f3 100644 --- a/src/boolmember.h +++ b/src/boolmember.h @@ -37,6 +37,12 @@ public: virtual ~BoolMember() {} /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return true; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member diff --git a/src/class.cpp b/src/class.cpp index ebbc9ae..6263147 100644 --- a/src/class.cpp +++ b/src/class.cpp @@ -8,4 +8,3 @@ */ #include "includes.h" - diff --git a/src/classinfo.cpp b/src/classinfo.cpp index 6d96494..a8f7a8d 100644 --- a/src/classinfo.cpp +++ b/src/classinfo.cpp @@ -162,13 +162,13 @@ _ClassInfo::~_ClassInfo() * Initialize the class * @param mixed Optional threading ID */ -void _ClassInfo::initialize(TSRMLS_D) +void _ClassInfo::initialize(TSRMLS_DC) { // the class entry zend_class_entry entry; // initialize the class entry - INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), NULL); + INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), methods()); // we need a special constructor entry.create_object = create_object; diff --git a/src/doublemember.h b/src/doublemember.h index 9293932..f03b248 100644 --- a/src/doublemember.h +++ b/src/doublemember.h @@ -37,6 +37,12 @@ public: virtual ~DoubleMember() {} /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return true; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member diff --git a/src/function.cpp b/src/function.cpp index 79117e5..d255518 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -82,8 +82,9 @@ Function::~Function() * function or method introces himself * * @param entry Entry to be filled + * @param classname Optional class name */ -void Function::fill(zend_function_entry *entry) const +void Function::fill(zend_function_entry *entry, const char *classname) const { // fill the members of the entity, and hide a pointer to the current object in the name entry->fname = _ptr; @@ -95,21 +96,22 @@ void Function::fill(zend_function_entry *entry) const entry->flags = 0; // we should fill the first argument as well - fill((zend_internal_function_info *)entry->arg_info); + fill((zend_internal_function_info *)entry->arg_info, classname); } /** * Fill a function entry * @param info Info to be filled + * @param classname Optional classname */ -void Function::fill(zend_internal_function_info *info) const +void Function::fill(zend_internal_function_info *info, const char *classname) const { // fill in all the members, note that return reference is false by default, - // because we do want to return references, inside the name we hide a pointer - // to the current object + // because we do not support returning references in PHP-CPP, although Zend + // engine allows it. Inside the name we hide a pointer to the current object info->_name = _ptr; info->_name_len = _ptr.length(); - info->_class_name = NULL; + info->_class_name = classname; // number of required arguments, and the expected return type info->required_num_args = _required; diff --git a/src/includes.h b/src/includes.h index a131f7f..2e8271b 100644 --- a/src/includes.h +++ b/src/includes.h @@ -49,6 +49,7 @@ #include "../include/member.h" #include "../include/public.h" #include "../include/protected.h" +#include "../include/members.h" #include "../include/class.h" #include "../include/classinfo.h" #include "../include/extension.h" @@ -65,3 +66,4 @@ #include "boolmember.h" #include "stringmember.h" #include "doublemember.h" +#include "methodmember.h" diff --git a/src/longmember.h b/src/longmember.h index c4107bf..2d0be50 100644 --- a/src/longmember.h +++ b/src/longmember.h @@ -37,6 +37,12 @@ public: virtual ~LongMember() {} /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return true; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member diff --git a/src/member.cpp b/src/member.cpp index 1c32777..5a44a74 100644 --- a/src/member.cpp +++ b/src/member.cpp @@ -20,6 +20,8 @@ namespace Php { */ Member::Member(const char *name, bool pub) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a null member _info = new NullMember(); } @@ -32,6 +34,8 @@ Member::Member(const char *name, bool pub) : _name(name), _public(pub) */ Member::Member(const char *name, bool pub, std::nullptr_t value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a null member _info = new NullMember(); } @@ -44,6 +48,8 @@ Member::Member(const char *name, bool pub, std::nullptr_t value) : _name(name), */ Member::Member(const char *name, bool pub, int value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a long member _info = new LongMember(value); } @@ -56,6 +62,8 @@ Member::Member(const char *name, bool pub, int value) : _name(name), _public(pub */ Member::Member(const char *name, bool pub, long value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a long member _info = new LongMember(value); } @@ -68,6 +76,8 @@ Member::Member(const char *name, bool pub, long value) : _name(name), _public(pu */ Member::Member(const char *name, bool pub, bool value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a bool member _info = new BoolMember(value); } @@ -80,6 +90,8 @@ Member::Member(const char *name, bool pub, bool value) : _name(name), _public(pu */ Member::Member(const char *name, bool pub, char value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a new string member _info = new StringMember(&value, 1); } @@ -92,6 +104,8 @@ Member::Member(const char *name, bool pub, char value) : _name(name), _public(pu */ Member::Member(const char *name, bool pub, const std::string &value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a new string member _info = new StringMember(value); } @@ -105,6 +119,8 @@ Member::Member(const char *name, bool pub, const std::string &value) : _name(nam */ Member::Member(const char *name, bool pub, const char *value, int size) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a new string member if (size < 0) size = strlen(value); _info = new StringMember(value, size); @@ -118,16 +134,140 @@ Member::Member(const char *name, bool pub, const char *value, int size) : _name( */ Member::Member(const char *name, bool pub, double value) : _name(name), _public(pub) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // create a new double member _info = new DoubleMember(value); } /** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_0 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_1 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_2 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_3 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_4 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_5 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_6 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param method The method to call + * @param arguments Argument meta data + */ +Member::Member(const char *name, bool pub, method_callback_7 method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +{ + std::cout << "Allocate member " << __LINE__ << std::endl; + + // create method member + _info = new MethodMember(name, method, arguments); +} + +/** * Copy constructor * @param member The member to copy */ Member::Member(const Member &member) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // copy info object, and name and public members _info = member._info; _name = member._name; @@ -143,6 +283,8 @@ Member::Member(const Member &member) */ Member::Member(Member &&member) { + std::cout << "Allocate member " << __LINE__ << std::endl; + // move info object, and name and public properties _info = member._info; _name = std::move(member._name); @@ -165,6 +307,26 @@ Member::~Member() } /** + * Is this a property member + * @return bool + */ +bool Member::isProperty() +{ + return _info && _info->isProperty(); +} + +/** + * Is this a method member + * @return bool + */ +bool Member::isMethod() +{ + std::cout << "call isMethod" << std::endl; + + return _info && _info->isMethod(); +} + +/** * Internal method to declare the property * @var zend_class_entry */ @@ -173,6 +335,18 @@ void Member::declare(struct _zend_class_entry *entry) // let the info object handle stuff _info->declare(entry, _name.c_str(), _name.size(), _public ? ZEND_ACC_PUBLIC : ZEND_ACC_PROTECTED TSRMLS_CC); } + +/** + * Internal method to fill a function entry + * @param zend_function_entry + * @param classname + * @internal + */ +void Member::fill(struct _zend_function_entry *entry, const char *classname) +{ + // let the info object do this + _info->fill(entry, classname); +} /** * End of namespace diff --git a/src/memberinfo.h b/src/memberinfo.h index e0cfbd8..fd9ce3d 100644 --- a/src/memberinfo.h +++ b/src/memberinfo.h @@ -49,13 +49,33 @@ public: int refcount(int change) { return _refcount += change; } /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return false; } + + /** + * Is this a method member + * @return bool + */ + virtual bool isMethod() { return false; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member * @param size Size of the name * @param flags Additional flags */ - virtual void declare(struct _zend_class_entry *entry, const char *name, int size, int flags)=0; + virtual void declare(struct _zend_class_entry *entry, const char *name, int size, int flags) {}; + + /** + * Fill a function entry object + * @param entry Function entry + * @param classname Name of the class + */ + virtual void fill(struct _zend_function_entry *entry, const char *classname) {}; + }; /** diff --git a/src/members.cpp b/src/members.cpp new file mode 100644 index 0000000..5711a4b --- /dev/null +++ b/src/members.cpp @@ -0,0 +1,94 @@ +/** + * Members.cpp + * + * Implementation of the members class + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Destructor + */ +Members::~Members() +{ + // check if there are methods + if (_methods) delete[] _methods; +} + +/** + * Number of methods + * @return integer + */ +int Members::methods() +{ + // result variable + int result = 0; + + // loop through the functions + for (auto it = begin(); it != end(); it++) + { + std::cout << "iter" << std::endl; + + // check if this is a method + if (it->isMethod()) result++; + } + + // done + return result; +} + +/** + * Get access to the methods + * @return Methods + */ +struct _zend_function_entry *Members::methods(const char *classname) +{ + // already set? + if (_methods) return _methods; + + // the number of methods + int count = methods(); + + std::cout << "allocate " << count << " methods" << std::endl; + + // allocate memory for the functions + _methods = new zend_function_entry[count + 1]; + + // keep iterator counter + int i = 0; + + // loop through the functions + for (auto it = begin(); it != end(); it++) + { + // skip if this is not a method + if (!it->isMethod()) continue; + + // retrieve entry + zend_function_entry *entry = &_methods[i++]; + + // let the function fill the entry + it->fill(entry, classname); + } + + // last entry should be set to all zeros + zend_function_entry *last = &_methods[i]; + + // all should be set to zero + memset(last, 0, sizeof(zend_function_entry)); + + // done + return _methods; +} + +/** + * End of namespace + */ +} + diff --git a/src/methodmember.h b/src/methodmember.h new file mode 100644 index 0000000..16f0dd0 --- /dev/null +++ b/src/methodmember.h @@ -0,0 +1,85 @@ +/** + * MethodMember.h + * + * Implementation for a method in a class + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class MethodMember : public MemberInfo, public Function +{ +public: + /** + * Constructor + * @param name + * @param method + * @param arguments + */ + MethodMember(const char *name, method_callback_0 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(0) { _method.m0 = method; } + MethodMember(const char *name, method_callback_1 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(1) { _method.m1 = method; } + MethodMember(const char *name, method_callback_2 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(2) { _method.m2 = method; } + MethodMember(const char *name, method_callback_3 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(3) { _method.m3 = method; } + MethodMember(const char *name, method_callback_4 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(4) { _method.m4 = method; } + MethodMember(const char *name, method_callback_5 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(5) { _method.m5 = method; } + MethodMember(const char *name, method_callback_6 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(6) { _method.m6 = method; } + MethodMember(const char *name, method_callback_7 method, const std::initializer_list<Argument> &arguments = {}) : Function(name, arguments), _type(7) { _method.m7 = method; } + + /** + * Destructor + */ + virtual ~MethodMember() {} + + /** + * Is this a method member + * @return bool + */ + virtual bool isMethod() { return true; } + + /** + * Fill a function entry object + * @param entry Function entry + * @param classname Name of the class + */ + virtual void fill(struct _zend_function_entry *entry, const char *classname) + { + // call function object + Function::fill(entry, classname); + } + +private: + /** + * Union of supported callbacks + * One of the callbacks will be set + */ + union { + method_callback_0 m0; + method_callback_1 m1; + method_callback_2 m2; + method_callback_3 m3; + method_callback_4 m4; + method_callback_5 m5; + method_callback_6 m6; + method_callback_7 m7; + } _method; + + /** + * The method type that is set + * @var integer + */ + int _type; +}; + +/** + * End of namespace + */ +} + diff --git a/src/nullmember.h b/src/nullmember.h index ea336ea..6312709 100644 --- a/src/nullmember.h +++ b/src/nullmember.h @@ -29,6 +29,12 @@ public: virtual ~NullMember() {} /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return true; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member diff --git a/src/stringmember.h b/src/stringmember.h index 8818da4..83432e3 100644 --- a/src/stringmember.h +++ b/src/stringmember.h @@ -44,6 +44,12 @@ public: virtual ~StringMember() {} /** + * Is this a property member + * @return bool + */ + virtual bool isProperty() { return true; } + + /** * Virtual method to declare the property * @param entry Class entry * @param name Name of the member diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index 8b50496..b3f30bc 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -72,7 +72,6 @@ public: void myMethod(Php::Parameters ¶ms) { - } }; @@ -95,8 +94,9 @@ extern "C" // define classes extension.add("my_class", Php::Class<MyCustomClass>({ - Php::Public("a", 123), - Php::Protected("b", "abc") +// Php::Public("a", 123), +// Php::Protected("b", "abc"), + Php::Public("myMethod", static_cast<Php::method_callback_1>(&MyCustomClass::myMethod)) })); // return the module entry diff --git a/tests/simple/simple.php b/tests/simple/simple.php index f9919be..379f285 100644 --- a/tests/simple/simple.php +++ b/tests/simple/simple.php @@ -1,47 +1,48 @@ <?php -class XXX -{ - public function __toString() - { - return "MyClass"; - } -} - -$myvar = "hoi"; - -class MyClass { - - public function __toString() - { - return "aksjdfhsdfkj"; - } -} - -$g1 = 123; -$g2 = "abc"; - -$result = my_plus(new MyClass(), array(), new MyClass(), $myvar, "blabla", new XXX()); - -echo("myvar = $myvar\n"); - -echo("resultaat: $result\n"); - -echo("g1: $g1\n"); -echo("g2: $g2\n"); -echo("g3: $g3\n"); +//class XXX +//{ +// public function __toString() +// { +// return "MyClass"; +// } +//} +// +//$myvar = "hoi"; +// +//class MyClass { +// +// public function __toString() +// { +// return "aksjdfhsdfkj"; +// } +//} +// +//$g1 = 123; +//$g2 = "abc"; +// +//$result = my_plus(new MyClass(), array(), new MyClass(), $myvar, "blabla", new XXX()); +// +//echo("myvar = $myvar\n"); +// +//echo("resultaat: $result\n"); +// +//echo("g1: $g1\n"); +//echo("g2: $g2\n"); +//echo("g3: $g3\n"); if (class_exists("my_class")) echo("Warempel, de class bestaat\n"); $x = new my_class(); - -echo("my_class::a = ".$x->a."\n"); -echo("my_class::b = ".$x->b."\n"); - -unset($x); - -echo("done\n"); +$x->myMethod(); + +//echo("my_class::a = ".$x->a."\n"); +//echo("my_class::b = ".$x->b."\n"); +// +//unset($x); +// +//echo("done\n"); //$x->my_method(); |