diff options
author | Martijn Otto <martijn.otto@copernica.com> | 2014-02-14 16:30:23 +0100 |
---|---|---|
committer | Martijn Otto <martijn.otto@copernica.com> | 2014-02-14 16:30:23 +0100 |
commit | 06aa5fd5afaba69544b93654fb0a4f9c2651306e (patch) | |
tree | 99cd2ee120786a84531b450f9ef64e2319ef5192 | |
parent | 5c23fee5ce58ae66a70f3bd19a1dc2dff7220f13 (diff) |
Merged pull request #14
-rw-r--r-- | Examples/ConstStaticProp/cpp/Makefile | 32 | ||||
-rw-r--r-- | Examples/ConstStaticProp/cpp/mytestext.cpp | 56 | ||||
-rw-r--r-- | Examples/ConstStaticProp/cpp/mytestext.h | 25 | ||||
-rw-r--r-- | Examples/ConstStaticProp/cpp/mytestext.ini | 4 | ||||
-rw-r--r-- | Examples/ConstStaticProp/readme | 0 | ||||
-rw-r--r-- | Examples/ConstStaticProp/test.php | 15 | ||||
-rw-r--r-- | include/class.h | 44 | ||||
-rw-r--r-- | include/classinfo.h | 22 | ||||
-rw-r--r-- | include/extension.h | 2 | ||||
-rw-r--r-- | include/flag.h | 135 | ||||
-rw-r--r-- | include/function.h | 2 | ||||
-rw-r--r-- | include/member.h | 44 | ||||
-rw-r--r-- | include/membervisibility.h | 73 | ||||
-rw-r--r-- | include/protected.h | 55 | ||||
-rw-r--r-- | include/public.h | 56 | ||||
-rw-r--r-- | include/zend.h | 85 | ||||
-rw-r--r-- | phpcpp.h | 5 | ||||
-rw-r--r-- | src/boolmember.h | 16 | ||||
-rw-r--r-- | src/classinfo.cpp | 7 | ||||
-rw-r--r-- | src/doublemember.h | 19 | ||||
-rw-r--r-- | src/flag.cpp | 114 | ||||
-rw-r--r-- | src/function.cpp | 5 | ||||
-rw-r--r-- | src/includes.h | 15 | ||||
-rw-r--r-- | src/longmember.h | 18 | ||||
-rw-r--r-- | src/member.cpp | 57 | ||||
-rw-r--r-- | src/memberinfo.h | 13 | ||||
-rw-r--r-- | src/methodmember.h | 4 | ||||
-rw-r--r-- | src/nullmember.h | 16 | ||||
-rw-r--r-- | src/stringmember.h | 16 |
29 files changed, 773 insertions, 182 deletions
diff --git a/Examples/ConstStaticProp/cpp/Makefile b/Examples/ConstStaticProp/cpp/Makefile new file mode 100644 index 0000000..8272434 --- /dev/null +++ b/Examples/ConstStaticProp/cpp/Makefile @@ -0,0 +1,32 @@ +CPP = g++ +RM = rm -f +CPP_FLAGS = -Wall -c -I. -O2 -std=c++11 + +PREFIX = /usr +#Edit these lines to correspond with your own directories +LIBRARY_DIR = ${PREFIX}/lib/php5/20121212 +PHP_CONFIG_DIR = /etc/php5/cli/conf.d + +LD = g++ +LD_FLAGS = -Wall -shared -O2 +RESULT = mytestext.so + +PHPINIFILE = mytestext.ini + +SOURCES = $(wildcard *.cpp) +OBJECTS = $(SOURCES:%.cpp=%.o) + +all: ${OBJECTS} ${RESULT} + +${RESULT}: ${OBJECTS} + ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp + +clean: + ${RM} *.obj *~* ${OBJECTS} ${RESULT} + +${OBJECTS}: + ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp} + +install: + cp -f ${RESULT} ${LIBRARY_DIR} + cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR} diff --git a/Examples/ConstStaticProp/cpp/mytestext.cpp b/Examples/ConstStaticProp/cpp/mytestext.cpp new file mode 100644 index 0000000..7d8dc95 --- /dev/null +++ b/Examples/ConstStaticProp/cpp/mytestext.cpp @@ -0,0 +1,56 @@ +/** + * cppclassinphp.cpp + * @author Jasper van Eck<jasper.vaneck@copernica.com> + * + * An example file to show the working of using a C++ class in PHP. + */ + +#include "mytestext.h" +/** + * Namespace to use + */ +using namespace std; + +class MyTestExt : public Php::Base +{ + +public: + MyTestExt() {} + + virtual ~MyTestExt() {} + + virtual void __construct() {} + +}; + + + +// Symbols are exported according to the "C" language +extern "C" +{ + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension + static Php::Extension extension("my_test_ext","0.1a"); + + // add the custom class ot the extension + extension.add( + "MyTestClass", + Php::Class<MyTestExt>({ + + // Private PHP constructor! You can't instance object of MyTestClass + Php::Private("__construct", Php::Method<MyTestExt>(&MyTestExt::__construct)), + + Php::Const("version", "v0.01-alpha"), + Php::Const("PI", 3.14159265), + Php::Const("IMISNULL"), + + Php::Static("exp", 2.71828182846), + }) + ); + + // return the extension module + return extension.module(); + } +} diff --git a/Examples/ConstStaticProp/cpp/mytestext.h b/Examples/ConstStaticProp/cpp/mytestext.h new file mode 100644 index 0000000..c268a61 --- /dev/null +++ b/Examples/ConstStaticProp/cpp/mytestext.h @@ -0,0 +1,25 @@ +/** + * The includes.h + */ + +/** + * Default Cpp libraries + */ + +#include <string> +#include <iostream> + +/** + * Our own library. + */ +#include <phpcpp.h> + +/** + * Namespace to use + */ +using namespace std; + +/** + * The test class. + */ +//#include "mycustomclass.h" diff --git a/Examples/ConstStaticProp/cpp/mytestext.ini b/Examples/ConstStaticProp/cpp/mytestext.ini new file mode 100644 index 0000000..d503704 --- /dev/null +++ b/Examples/ConstStaticProp/cpp/mytestext.ini @@ -0,0 +1,4 @@ +; configuration for phpcpp module +; priority=30 +extension=mytestext.so + diff --git a/Examples/ConstStaticProp/readme b/Examples/ConstStaticProp/readme new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Examples/ConstStaticProp/readme diff --git a/Examples/ConstStaticProp/test.php b/Examples/ConstStaticProp/test.php new file mode 100644 index 0000000..5489461 --- /dev/null +++ b/Examples/ConstStaticProp/test.php @@ -0,0 +1,15 @@ +<?php + + var_dump(MyTestClass::version); // print: string(11) "v0.01-alpha" + + var_dump(MyTestClass::PI); // print: float(3.14159265) + + var_dump(MyTestClass::IMISNULL); // print: NULL + + var_dump(MyTestClass::$exp); // print: float(2.71828182846) + + /** + * Fatal error! + * Private PHP constructor. You can't instance object of MyTestClass. + */ + //$mytest = new MyTestClass();
\ No newline at end of file diff --git a/include/class.h b/include/class.h index 3746cf3..201f4de 100644 --- a/include/class.h +++ b/include/class.h @@ -11,6 +11,7 @@ * Note that YourClass must extend from Php::Object * * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * changed by Valeriy Dmitriev <ufabiz@gmail.com> * @copyright 2013 Copernica BV */ @@ -40,18 +41,18 @@ public: * Constructor with initializer list to define the properties * @param members */ - Class(const std::initializer_list<Member> &members) : _members(members) {} + Class(const std::initializer_list<Member> &members, FlagClass flags = FlagClass(Zend::AccClass::NOSET)) : _members(members), _flags(flags) {} /** * Move constructor * @param that */ - Class(Class &&that) : _members(std::move(that._members)) {} + Class(Class &&that) : _members(std::move(that._members)), _flags(std::move(that._flags)) {} /** * Copy constructor */ - Class(const Class &that) : _members(that._members) {} + Class(const Class &that) : _members(that._members), _flags(that._flags) {} /** * Destructor @@ -91,6 +92,14 @@ public: { return _members.methods(classname); } + + /** + * Retrieve the int access types flags for PHP class + * @return int flags of access types for classes + */ + int getFlags() { + return _flags; + } protected: /** @@ -99,8 +108,37 @@ protected: */ Members _members; +private: + /** + * The access types flags for class + */ + FlagClass _flags; + }; + +/** + * Class definition of the ClassFlagged + * template ClassFlagged designed for easy instance of Class<T> for concrete flags + */ +template <typename T, Zend::AccClass Flags> +class ClassFlagged : public Class<T> +{ +public: + ClassFlagged() : Class<T>() {} + ClassFlagged(const std::initializer_list<Member> &members) : Class<T>(members, FlagClass(Flags)) {} +}; + +template <typename T> +// C++11 analog of `typedef`. Equivalent to the following pseudocode: typedef ClassFlagged<T, Zend::AccClass::FINAL> FinalClass<T>; +using FinalClass = ClassFlagged<T, Zend::AccClass::FINAL>; +template <typename T> +using AbstractClass = ClassFlagged<T, Zend::AccClass::ABSTRACT>; +template <typename T> +using Interface = ClassFlagged<T, Zend::AccClass::INTERFACE>; +template <typename T> +using Trait = ClassFlagged<T, Zend::AccClass::TRAIT>; + /** * End of namespace */ diff --git a/include/classinfo.h b/include/classinfo.h index 276bc63..070dcd5 100644 --- a/include/classinfo.h +++ b/include/classinfo.h @@ -52,16 +52,16 @@ public: void initialize(); /** - * Construct the C++ object - * @return Base - */ - virtual Base *construct() = 0; - - /** * Initialize the class * @param entry */ virtual void initialize(struct _zend_class_entry *entry) = 0; + + /** + * Construct the C++ object + * @return Base + */ + virtual Base *construct() = 0; /** * Retrieve the methods @@ -69,6 +69,11 @@ public: */ virtual struct _zend_function_entry *methods() = 0; + /** + * set access types flags for class + */ + void seFlags(struct _zend_class_entry *entry, int flags); + protected: /** * The class entry @@ -130,8 +135,11 @@ public: * Initialize the class * @param entry */ - virtual void initialize(struct _zend_class_entry *entry) + virtual void initialize(struct _zend_class_entry *entry) override { + // set access types flags for class + seFlags(entry, _type.getFlags()); + // pass to the entry _type.initialize(entry); } diff --git a/include/extension.h b/include/extension.h index 862549a..1082bcb 100644 --- a/include/extension.h +++ b/include/extension.h @@ -178,7 +178,7 @@ public: void add(const char *name, const Class<T> &type) { // construct info - ClassInfo<T> *info = new ClassInfo<T>(name, type); + _ClassInfo *info = new ClassInfo<T>(name, type); // add class _classes.insert(std::unique_ptr<_ClassInfo>(info)); diff --git a/include/flag.h b/include/flag.h new file mode 100644 index 0000000..beb79ba --- /dev/null +++ b/include/flag.h @@ -0,0 +1,135 @@ +/** + * flag.h + * + * flag clases for the safe transfer of a Zend flag to a Zend functions + * + * @author Valeriy_Dmitriev <ufabiz@gmail.com> + */ + +#ifndef PHPCPP_FLAG_INCLUDE_C_H_ +#define PHPCPP_FLAG_INCLUDE_C_H_ + +/** + * Namespace Php + */ +namespace Php { + + + /** + * class FlagTemplate + * Designed for the safe transfer of a Zend flag to a Zend functions + */ + template <class AccT> + class FlagTemplate + { + public: + /** + * Constructor + */ + FlagTemplate(const AccT &zflag); + + /** + * Copy constructor + * @param FlagTemplate The FlagTemplate to copy + */ + FlagTemplate(const FlagTemplate &flags) : _val(flags._val) {} + + /** + * Move constructor + * @param FlagTemplate The FlagTemplate to move + */ + FlagTemplate(FlagTemplate &&flags) : _val(std::move(flags._val)){} + + /** + * Assignment operator + */ + FlagTemplate &operator=(const FlagTemplate &flags) { + if (this != &flags) { + _val = flags._val; + } + return *this; + } + + /** + * Move operator + */ + FlagTemplate &operator=(FlagTemplate &&flags) { + if (this != &flags) { + _val = std::move(flags._val); + } + return *this; + } + + /** + * Bitwise OR assignment operator + */ + FlagTemplate &operator|=(const FlagTemplate &flags) { + _val |= flags._val; + return *this; + } + + /** + * Bitwise OR operator + */ + FlagTemplate operator|(const FlagTemplate &flags) { + return FlagTemplate (_val | flags._val); + } + + /** + * Cast to a int + * @return int + */ + operator int () const { + return _val; + } + + /** + * Destructor + */ + ~FlagTemplate () {} + + private: + + /** + * Private constructor + * @param int val + */ + FlagTemplate(const int &val) :_val(val) {} + + /** + * Private constructor + * @param void + */ + FlagTemplate() {} + + /** + * value of flag + */ + int _val; + }; + + /** + * class FlagClass + * For the safe transfer of a Zend Class flags to a Zend functions + */ + typedef FlagTemplate<Zend::AccClass> FlagClass; + /** + * class FlagClass + * For the safe transfer of a Zend access types for methods and propertyes + */ + typedef FlagTemplate<Zend::AccMemb> FlagMemb; + + + /** + * factory function + */ + FlagClass Flag(const Zend::AccClass &zflag); + FlagMemb Flag(const Zend::AccMemb &zflag); + + +/** + * End of namespace Php + */ +} + +#endif /* PHPCPP_FLAG_INCLUDE_C_H_ */
\ No newline at end of file diff --git a/include/function.h b/include/function.h index d2461ef..ea7584f 100644 --- a/include/function.h +++ b/include/function.h @@ -141,7 +141,7 @@ protected: * @param classname Optional class name * @param pub Is this a public property? */ - void fill(struct _zend_function_entry *entry, const char *classname=NULL, bool pub=true) const; + void fill(struct _zend_function_entry *entry, const char *classname=NULL, int flags=Flag(Zend::AccMemb::PUBLIC)) const; /** * Fill function info diff --git a/include/member.h b/include/member.h index bc96e52..347555a 100644 --- a/include/member.h +++ b/include/member.h @@ -31,82 +31,82 @@ public: /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) */ - Member(const char *name, bool pub); + Member(const char *name, const FlagMemb &&flags); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, std::nullptr_t value); + Member(const char *name, const FlagMemb &&flags, std::nullptr_t value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, int value); + Member(const char *name, const FlagMemb &&flags, int value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, long value); + Member(const char *name, const FlagMemb &&flags, long value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, bool value); + Member(const char *name, const FlagMemb &&flags, bool value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, char value); + Member(const char *name, const FlagMemb &&flags, char value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, const std::string &value); + Member(const char *name, const FlagMemb &&flags, const std::string &value); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add * @param size String length */ - Member(const char *name, bool pub, const char *value, int size = -1); + Member(const char *name, const FlagMemb &&flags, const char *value, int size = -1); /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ - Member(const char *name, bool pub, double value); + Member(const char *name, const FlagMemb &&flags, double value); /** * Constructor * @param name Name of the method - * @param pub Is this a public method (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param method The method to add */ - Member(const char *name, bool pub, const _Method &method, const std::initializer_list<Argument> &arguments = {}); + Member(const char *name, const FlagMemb &&flags, const _Method &method, const std::initializer_list<Argument> &arguments = {}); /** * Copy constructor @@ -161,10 +161,10 @@ private: std::string _name; /** - * Is this a public property + * Flag access to a class member (bublic, protected etc) * @var bool */ - bool _public; + FlagMemb _accflag; /** * The implementation for the member diff --git a/include/membervisibility.h b/include/membervisibility.h new file mode 100644 index 0000000..a0a11ea --- /dev/null +++ b/include/membervisibility.h @@ -0,0 +1,73 @@ +/** + * membervisibility.h + * + * MemberVisibility - Template for a visibility of a property or method + * Instead of defining three classes Public, Protected and Private defined template class. And these classes are obtained by applying to the class template typedef MemberVisibility. + * In the future, so it is possible to create such a class scope `Static` + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * changed by Valeriy Dmitriev <ufabiz@gmail.com> + * @copyright 2013 Copernica BV + */ + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +template <Zend::AccMemb AccFlag> +class MemberVisibility : public Member +{ +public: + /** + * Constructor + * @param name Name of the property + * @param value Default value of the property + */ + MemberVisibility(const char *name) : Member(name, FlagMemb(AccFlag)) {} + MemberVisibility(const char *name, std::nullptr_t value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, int value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, long value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, bool value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, char value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, const std::string &value) : Member(name, FlagMemb(AccFlag), value) {} + MemberVisibility(const char *name, const char *value, int size=-1) : Member(name, FlagMemb(AccFlag), value, size) {} + MemberVisibility(const char *name, double value) : Member(name, FlagMemb(AccFlag), value) {} + + /** + * Constructor + * @param name Name of the property + * @param method Method to add + * @param arguments Optional argument information + */ + MemberVisibility(const char *name, const _Method &method, const std::initializer_list<Argument> &arguments = {}) : Member(name, FlagMemb(AccFlag), method, arguments) {} + + /** + * Destructor + */ + virtual ~MemberVisibility() {} + +}; + +typedef MemberVisibility<Zend::AccMemb::PUBLIC> Public; +typedef MemberVisibility<Zend::AccMemb::PROTECTED> Protected; +typedef MemberVisibility<Zend::AccMemb::PRIVATE> Private; +typedef MemberVisibility<Zend::AccMemb::CONSTANT> Const; + +/** + * In the current architecture, implementation of static methods is not possible. + * + * Static properties are supported. + * @todo: Requires some refactoring that it became possible. + */ +//typedef MemberVisibility<Zend::AccMemb::STATIC> Static; + + +/** + * End of namespace + */ +} + diff --git a/include/protected.h b/include/protected.h deleted file mode 100644 index f3e668a..0000000 --- a/include/protected.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Protected.h - * - * Class for adding public properties to a class - * - * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @copyright 2013 Copernica BV - */ - -/** - * Namespace - */ -namespace Php { - -/** - * Class definition - */ -class Protected : public Member -{ -public: - /** - * Constructor - * @param name Name of the property - * @param value Default value of the property - */ - Protected(const char *name) : Member(name, false) {} - Protected(const char *name, std::nullptr_t value) : Member(name, false, value) {} - Protected(const char *name, int value) : Member(name, false, value) {} - Protected(const char *name, long value) : Member(name, false, value) {} - Protected(const char *name, bool value) : Member(name, false, value) {} - Protected(const char *name, char value) : Member(name, false, value) {} - 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 - * @param arguments Optional argument information - */ - Protected(const char *name, const _Method &method, const std::initializer_list<Argument> &arguments = {}) : Member(name, false, method, arguments) {} - - /** - * Destructor - */ - virtual ~Protected() {} - -}; - -/** - * End of namespace - */ -} - diff --git a/include/public.h b/include/public.h deleted file mode 100644 index 12648a1..0000000 --- a/include/public.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Public.h - * - * Class for adding public properties to a class - * - * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @copyright 2013 Copernica BV - */ - -/** - * Namespace - */ -namespace Php { - -/** - * Class definition - */ -class Public : public Member -{ -public: - /** - * Constructor - * @param name Name of the property - * @param value Default value of the property - */ - Public(const char *name) : Member(name, true) {} - 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, 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 - * @param arguments Optional argument information - */ - Public(const char *name, const _Method &method, const std::initializer_list<Argument> &arguments = {}) : Member(name, true, method, arguments) {} - - /** - * Destructor - */ - virtual ~Public() {} - - -}; - -/** - * End of namespace - */ -} - diff --git a/include/zend.h b/include/zend.h new file mode 100644 index 0000000..66d82a2 --- /dev/null +++ b/include/zend.h @@ -0,0 +1,85 @@ +/** + * zend.h + * + * zend namespace + * + * @author Valeriy_Dmitriev <ufabiz@gmail.com> + */ + +#ifndef PHPCPP_ZEND_INCLUDE_C_H_ +#define PHPCPP_ZEND_INCLUDE_C_H_ + +/** + * Namespace Php + */ +namespace Php { + + /** + * collection of Zend constants + */ + namespace Zend { + + /** + * access types (flags) + * see Zend/zend_compile.h + */ + + /** + * access types for methods and properties + * (method flags) + */ + enum class AccMemb { + // method flags (types): + STATIC ,//= ZEND_ACC_STATIC, //0x01 + ABSTRACT ,//= ZEND_ACC_ABSTRACT, //0x02 + FINAL ,//= ZEND_ACC_FINAL, //0x04 + //IMPLEMENTED_ABSTRACT ,//= ZEND_ACC_IMPLEMENTED_ABSTRACT, //0x08 + //method flag (bc only), any method that has this flag can be used statically and non statically. + //ALLOW_STATIC ,//= ZEND_ACC_ALLOW_STATIC, //0x10000 + + // method flags (visibility) + // The order of those must be kept - public < protected < private + PUBLIC ,//= ZEND_ACC_PUBLIC, //0x100 + PROTECTED ,//= ZEND_ACC_PROTECTED, //0x200 + PRIVATE ,//= ZEND_ACC_PRIVATE, //0x400 + PPP_MASK ,//= ZEND_ACC_PPP_MASK, //(ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE) + + // method flags (special method detection) + //CTOR ,//= ZEND_ACC_CTOR, //0x2000 + //DTOR ,//= ZEND_ACC_DTOR, //0x4000 + //CLONE ,//= ZEND_ACC_CLONE, //0x8000 + + //shadow of parent's private method/property + //SHADOW ,//= ZEND_ACC_SHADOW //0x20000 + + // additional field for designation flag CONSTANT. Not in the Zend engine + CONSTANT ,//= 0 //0 + }; + + /** + * access types for classes + * (class flags) + */ + enum class AccClass { + NOSET ,//= .. , //0 + // ZEND_ACC_IMPLICIT_ABSTRACT_CLASS is used for abstract classes (since it is set by any abstract method even interfaces MAY have it set, too). + //IMPLICIT_ABSTRACT ,//= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, //0x10 + //ZEND_ACC_EXPLICIT_ABSTRACT_CLASS denotes that a class was explicitly defined as abstract by using the keyword. + ABSTRACT ,//= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS, //0x20 + FINAL ,//= ZEND_ACC_FINAL_CLASS, //0x40 + INTERFACE ,//= ZEND_ACC_INTERFACE, //0x80 + TRAIT ,//= ZEND_ACC_TRAIT, //0x120 + + //class implement interface(s) flag, + //IMPLEMENT_INTERFACES ,//= ZEND_ACC_IMPLEMENT_INTERFACES, //0x80000 + //IMPLEMENT_TRAITS ,//= ZEND_ACC_IMPLEMENT_TRAITS, //0x400000 + }; + + + } +/** + * End of namespace Php + */ +} + +#endif /* PHPCPP_ZEND_INCLUDE_C_H_ */
\ No newline at end of file @@ -25,6 +25,8 @@ /** * Include all headers files that are related to this library */ +#include <phpcpp/zend.h> +#include <phpcpp/flag.h> #include <phpcpp/type.h> #include <phpcpp/value.h> #include <phpcpp/array.h> @@ -41,8 +43,7 @@ #include <phpcpp/base.h> #include <phpcpp/method.h> #include <phpcpp/member.h> -#include <phpcpp/public.h> -#include <phpcpp/protected.h> +#include <phpcpp/membervisibility.h> #include <phpcpp/members.h> #include <phpcpp/class.h> #include <phpcpp/classinfo.h> diff --git a/src/boolmember.h b/src/boolmember.h index 84d7d44..84aa4e9 100644 --- a/src/boolmember.h +++ b/src/boolmember.h @@ -57,6 +57,22 @@ public: zend_declare_property_bool(entry, (char *) name, size, _value, flags); #endif } + + /** + * Virtual method to declare the class constant + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) + { +#if PHP_VERSION_ID >= 50400 + zend_declare_class_constant_bool(entry, name, size, _value); +#else + zend_declare_class_constant_bool(entry, (char *) name, size, _value); +#endif + } }; /** diff --git a/src/classinfo.cpp b/src/classinfo.cpp index 8778981..f34758e 100644 --- a/src/classinfo.cpp +++ b/src/classinfo.cpp @@ -164,6 +164,13 @@ void _ClassInfo::initialize(TSRMLS_DC) } /** + * set access types flags for class + */ +void _ClassInfo::seFlags(struct _zend_class_entry *entry, int flags) { + entry->ce_flags |= flags; +} + +/** * End of namespace */ } diff --git a/src/doublemember.h b/src/doublemember.h index e3b75eb..7c5e431 100644 --- a/src/doublemember.h +++ b/src/doublemember.h @@ -29,7 +29,7 @@ public: * Constructor * @param value */ - DoubleMember(bool value) : MemberInfo(), _value(value) {} + DoubleMember(double value) : MemberInfo(), _value(value) {} /** * Destructor @@ -57,6 +57,23 @@ public: zend_declare_property_double(entry, (char *)name, size, _value, flags); #endif } + + /** + * Virtual method to declare the class constant + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) + { +#if PHP_VERSION_ID >= 50400 + zend_declare_class_constant_double(entry, name, size, _value); +#else + zend_declare_class_constant_double(entry, (char *)name, size, _value); +#endif + } + }; /** diff --git a/src/flag.cpp b/src/flag.cpp new file mode 100644 index 0000000..4f48ee9 --- /dev/null +++ b/src/flag.cpp @@ -0,0 +1,114 @@ +/** + * flag.cpp + * + * flag clases for the safe transfer of a Zend flag to a Zend functions + * flags defined at Zend/zend_compile.h + * + * @author Valeriy_Dmitriev <ufabiz@gmail.com> + */ + +#include "includes.h" + +/** + * Namespace Php + */ +namespace Php { + + + /** + * Constructor + * @param flags instance of Zend::AccClass + */ + template <> + FlagClass::FlagTemplate(const Zend::AccClass &zflag) { + /** + * access types for classes + * (method flags) + */ + switch(zflag){ + // if a class no have specified flags + case Zend::AccClass::NOSET: + _val = 0; + break; + //ZEND_ACC_EXPLICIT_ABSTRACT_CLASS denotes that a class was explicitly defined as abstract by using the keyword. + case Zend::AccClass::ABSTRACT: + _val = ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; //0x20; + break; + case Zend::AccClass::FINAL: + _val = ZEND_ACC_FINAL_CLASS; //0x40; + break; + case Zend::AccClass::INTERFACE: + _val = ZEND_ACC_INTERFACE; //0x80; + break; + case Zend::AccClass::TRAIT: + _val = ZEND_ACC_TRAIT; //0x120; + break; + default: + _val = 0; + } + } + + /** + * Constructor + * @param flags instance of Zend::AccMemb + */ + template <> + FlagMemb::FlagTemplate(const Zend::AccMemb &zflag) { + /** + * access types for methods and propertyes (members) + * (class flags) + */ + switch(zflag){ + // method flags (visibility) + // The order of those must be kept - public < protected < private + case Zend::AccMemb::PUBLIC: + _val = ZEND_ACC_PUBLIC; //0x100 + break; + case Zend::AccMemb::PROTECTED: + _val = ZEND_ACC_PROTECTED; //0x200 + break; + case Zend::AccMemb::PRIVATE: + _val = ZEND_ACC_PRIVATE; //0x400 + break; + case Zend::AccMemb::PPP_MASK: + _val = ZEND_ACC_PPP_MASK; //(ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE) + break; + + case Zend::AccMemb::STATIC: + //_val = ZEND_ACC_STATIC; //0x01 + //_val = ZEND_ACC_ALLOW_STATIC | ZEND_ACC_PUBLIC; + _val = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC; + break; + + // Artificially entered field + case Zend::AccMemb::CONSTANT: + _val = 0; //0 + break; + + case Zend::AccMemb::ABSTRACT: + _val = ZEND_ACC_ABSTRACT; //0x02 + break; + case Zend::AccMemb::FINAL: + _val = ZEND_ACC_FINAL; //0x04 + break; + + default: + _val = ZEND_ACC_PUBLIC; + } + } + + + /** + * factory function + */ + FlagClass Flag(const Zend::AccClass &zflag) { + return FlagClass(zflag); + } + FlagMemb Flag(const Zend::AccMemb &zflag) { + return FlagMemb(zflag); + } + +/** + * End of namespace Php + */ +} diff --git a/src/function.cpp b/src/function.cpp index 376b453..3fa5fe6 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -101,7 +101,7 @@ Function::~Function() * @param classname Optional class name * @param pub Is this a public property? */ -void Function::fill(zend_function_entry *entry, const char *classname, bool pub) const +void Function::fill(zend_function_entry *entry, const char *classname, int flags) const { // fill the members of the entity, and hide a pointer to the current object in the name entry->fname = _ptr; @@ -110,7 +110,8 @@ void Function::fill(zend_function_entry *entry, const char *classname, bool pub) entry->num_args = _argc; // there are no flags like deprecated, private or protected - entry->flags = classname ? (pub ? ZEND_ACC_PUBLIC : ZEND_ACC_PROTECTED) : 0; + entry->flags = classname ? flags : 0; + // we should fill the first argument as well #if PHP_VERSION_ID >= 50400 diff --git a/src/includes.h b/src/includes.h index a0f883a..4492252 100644 --- a/src/includes.h +++ b/src/includes.h @@ -19,11 +19,21 @@ #include <set> #include <exception> + // for debug +#include <iostream> + +/** + * @todo: if ZTS defined very many errors. need debug. + */ +//#define ZTS 1 + /** * PHP includes */ #include <php.h> #include "zend_exceptions.h" +#include "zend_interfaces.h" + /** * Macro to convert results to success status */ @@ -32,6 +42,8 @@ /** * Include other files from this library */ +#include "../include/zend.h" +#include "../include/flag.h" #include "../include/type.h" #include "../include/value.h" #include "../include/array.h" @@ -48,8 +60,7 @@ #include "../include/base.h" #include "../include/method.h" #include "../include/member.h" -#include "../include/public.h" -#include "../include/protected.h" +#include "../include/membervisibility.h" #include "../include/members.h" #include "../include/class.h" #include "../include/classinfo.h" diff --git a/src/longmember.h b/src/longmember.h index b35c316..4adf59b 100644 --- a/src/longmember.h +++ b/src/longmember.h @@ -51,12 +51,28 @@ public: */ virtual void declare(struct _zend_class_entry *entry, const char *name, int size, int flags) { -#if PHP_VERSION_ID >= 50400 +#if PHP_VERSION_ID >= 50400 zend_declare_property_long(entry, name, size, _value, flags); #else zend_declare_property_long(entry, (char *) name, size, _value, flags); #endif } + + /** + * Virtual method to declare the class constant + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) + { +#if PHP_VERSION_ID >= 50400 + zend_declare_class_constant_long(entry, name, size, _value); +#else + zend_declare_class_constant_long(entry, (char *) name, size, _value); +#endif + } }; /** diff --git a/src/member.cpp b/src/member.cpp index d4f5d2d..70dec9f 100644 --- a/src/member.cpp +++ b/src/member.cpp @@ -16,9 +16,9 @@ namespace Php { /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) */ -Member::Member(const char *name, bool pub) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags) : _name(name), _accflag(flags) { // create a null member _info = new NullMember(); @@ -27,10 +27,10 @@ Member::Member(const char *name, bool pub) : _name(name), _public(pub) /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, std::nullptr_t value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, std::nullptr_t value) : _name(name), _accflag(flags) { // create a null member _info = new NullMember(); @@ -39,10 +39,10 @@ Member::Member(const char *name, bool pub, std::nullptr_t value) : _name(name), /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, int value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, int value) : _name(name), _accflag(flags) { // create a long member _info = new LongMember(value); @@ -51,10 +51,10 @@ Member::Member(const char *name, bool pub, int value) : _name(name), _public(pub /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, long value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, long value) : _name(name), _accflag(flags) { // create a long member _info = new LongMember(value); @@ -63,10 +63,10 @@ Member::Member(const char *name, bool pub, long value) : _name(name), _public(pu /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, bool value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, bool value) : _name(name), _accflag(flags) { // create a bool member _info = new BoolMember(value); @@ -75,10 +75,10 @@ Member::Member(const char *name, bool pub, bool value) : _name(name), _public(pu /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, char value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, char value) : _name(name), _accflag(flags) { // create a new string member _info = new StringMember(&value, 1); @@ -87,10 +87,10 @@ Member::Member(const char *name, bool pub, char value) : _name(name), _public(pu /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, const std::string &value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, const std::string &value) : _name(name), _accflag(flags) { // create a new string member _info = new StringMember(value); @@ -99,11 +99,11 @@ Member::Member(const char *name, bool pub, const std::string &value) : _name(nam /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add * @param size String length */ -Member::Member(const char *name, bool pub, const char *value, int size) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, const char *value, int size) : _name(name), _accflag(flags) { // create a new string member if (size < 0) size = strlen(value); @@ -113,10 +113,10 @@ Member::Member(const char *name, bool pub, const char *value, int size) : _name( /** * Constructor * @param name Name of the member - * @param pub Is this a public property (otherwise it is protected) + * @param flags Flag access to a class member (bublic, protected etc) * @param value The value to add */ -Member::Member(const char *name, bool pub, double value) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, double value) : _name(name), _accflag(flags) { // create a new double member _info = new DoubleMember(value); @@ -128,8 +128,12 @@ Member::Member(const char *name, bool pub, double value) : _name(name), _public( * @param pub Is this a public method (otherwise it is protected) * @param method The method to add */ -Member::Member(const char *name, bool pub, const _Method &method, const std::initializer_list<Argument> &arguments) : _name(name), _public(pub) +Member::Member(const char *name, const FlagMemb &&flags, const _Method &method, const std::initializer_list<Argument> &arguments) : _name(name), _accflag(flags) { + // If the flags specifies as Zend::AccMemb::CONSTANT. + // That is: if( flags == Flag(Zend::AccMemb::CONSTANT) ) ... + //XXX Flag(Zend::AccMemb::PUBLIC) -> Flag(Zend::AccMemb::STATIC) + if(!flags) _accflag = Flag(Zend::AccMemb::PUBLIC); // create method member _info = new MethodMember(name, method, arguments); } @@ -138,12 +142,12 @@ Member::Member(const char *name, bool pub, const _Method &method, const std::ini * Copy constructor * @param member The member to copy */ -Member::Member(const Member &member) +Member::Member(const Member &member) : _accflag(member._accflag) { // copy info object, and name and public members _info = member._info; _name = member._name; - _public = member._public; + //_accflag = member._accflag; // update refcount in info object _info->refcount(+1); @@ -153,12 +157,12 @@ Member::Member(const Member &member) * Move constructor * @param member The member to move */ -Member::Member(Member &&member) +Member::Member(Member &&member) : _accflag (std::move(member._accflag)) { // move info object, and name and public properties _info = member._info; _name = std::move(member._name); - _public = member._public; + //_accflag = std::move(member._accflag); // reset info in other object member._info = NULL; @@ -201,7 +205,10 @@ bool Member::isMethod() 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); + if(!_accflag) // That is: if( flags == Flag(Zend::AccMemb::CONSTANT) ) + _info->declareConst(entry, _name.c_str(), _name.size() TSRMLS_CC); + else + _info->declare(entry, _name.c_str(), _name.size(), _accflag TSRMLS_CC); } /** @@ -213,7 +220,7 @@ void Member::declare(struct _zend_class_entry *entry) void Member::fill(struct _zend_function_entry *entry, const char *classname) { // let the info object do this - _info->fill(entry, classname, _public); + _info->fill(entry, classname, _accflag); } /** diff --git a/src/memberinfo.h b/src/memberinfo.h index 040f0de..1c4b59d 100644 --- a/src/memberinfo.h +++ b/src/memberinfo.h @@ -68,14 +68,23 @@ public: * @param flags Additional flags */ virtual void declare(struct _zend_class_entry *entry, const char *name, int size, int flags) {}; + + /** + * Virtual method to declare the constant property + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) {}; /** * Fill a function entry object * @param entry Function entry * @param classname Name of the class - * @param pub Is this a public method? + * @param flags Is this a public method? */ - virtual void fill(struct _zend_function_entry *entry, const char *classname, bool pub) {}; + virtual void fill(struct _zend_function_entry *entry, const char *classname, int flags) {}; }; diff --git a/src/methodmember.h b/src/methodmember.h index 9d4bcd1..67b4dad 100644 --- a/src/methodmember.h +++ b/src/methodmember.h @@ -43,10 +43,10 @@ public: * @param classname Name of the class * @param pub Is this a public entry */ - virtual void fill(struct _zend_function_entry *entry, const char *classname, bool pub) + virtual void fill(struct _zend_function_entry *entry, const char *classname, int flags) { // call function object - Function::fill(entry, classname, pub); + Function::fill(entry, classname, flags); } /** diff --git a/src/nullmember.h b/src/nullmember.h index 3fdb6b3..e5fdd62 100644 --- a/src/nullmember.h +++ b/src/nullmember.h @@ -49,6 +49,22 @@ public: zend_declare_property_null(entry, (char *) name, size, flags); #endif } + + /** + * Virtual method to declare the class constant + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) + { +#if PHP_VERSION_ID >= 50400 + zend_declare_class_constant_null(entry, name, size); +#else + zend_declare_class_constant_null(entry, (char *) name, size); +#endif + }; }; /** diff --git a/src/stringmember.h b/src/stringmember.h index d5af4a5..d6ac6ea 100644 --- a/src/stringmember.h +++ b/src/stringmember.h @@ -64,6 +64,22 @@ public: zend_declare_property_stringl(entry, (char*) name, size, (char *) _value.c_str(), _value.size(), flags); #endif } + + /** + * Virtual method to declare the class constant + * @param entry Class entry + * @param name Name of the member + * @param size Size of the name + * @param flags Additional flags + */ + virtual void declareConst(struct _zend_class_entry *entry, const char *name, int size) + { +#if PHP_VERSION_ID >= 50400 + zend_declare_class_constant_stringl(entry, name, size, _value.c_str(), _value.size()); +#else + zend_declare_class_constant_stringl(entry, (char*) name, size, (char *) _value.c_str(), _value.size()); +#endif + } }; /** |