diff options
Diffstat (limited to 'src')
-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 |
13 files changed, 416 insertions, 10 deletions
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 |