From 777eb276f635c949ccdcf9613ad55d42190cb387 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 30 Sep 2013 13:49:23 -0700 Subject: Work in progress on adding public and protected properties to classes --- include/class.h | 36 ++++++++++++++++++++++--- include/classinfo.h | 16 +++++++++++ include/member.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ include/protected.h | 39 +++++++++++++++++++++++++++ include/public.h | 40 +++++++++++++++++++++++++++ include/value.h | 1 + phpcpp.h | 3 +++ src/classinfo.cpp | 3 +++ src/includes.h | 3 +++ src/member.cpp | 32 ++++++++++++++++++++++ tests/simple/simple.cpp | 10 ++++--- tests/simple/simple.php | 4 +++ 12 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 include/member.h create mode 100644 include/protected.h create mode 100644 include/public.h create mode 100644 src/member.cpp diff --git a/include/class.h b/include/class.h index 38398b9..d5d48b4 100644 --- a/include/class.h +++ b/include/class.h @@ -13,7 +13,12 @@ * @author Emiel Bruijntjes * @copyright 2013 Copernica BV */ - + +/** + * Forward declarations + */ +struct _zend_class_entry; + /** * Set up namespace */ @@ -31,16 +36,22 @@ public: */ Class() {} + /** + * Constructor with initializer list to define the properties + * @param members + */ + Class(const std::initializer_list &members) : _members(members) {} + /** * Move constructor * @param that */ - Class(Class &&that) {} + Class(Class &&that) : _members(std::move(that._members)) {} /** * Copy constructor */ - Class(const Class &that) {} + Class(const Class &that) : _members(that._members) {} /** * Destructor @@ -57,7 +68,26 @@ public: return new T(); } + /** + * Initialize the class + * This will declare all members + * @param entry + */ + void initialize(struct _zend_class_entry *entry) + { + // loop through the members + for (auto iter = _members.begin(); iter != _members.end(); iter++) + { + iter->declare(entry); + } + } + protected: + /** + * The initial arguments + * @var vector + */ + std::vector _members; }; diff --git a/include/classinfo.h b/include/classinfo.h index fb92ce4..cbf2c66 100644 --- a/include/classinfo.h +++ b/include/classinfo.h @@ -56,6 +56,12 @@ public: * @return Base */ virtual Base *construct() = 0; + + /** + * Initialize the class + * @param entry + */ + virtual void initialize(struct _zend_class_entry *entry) = 0; private: /** @@ -113,6 +119,16 @@ public: { return _type.construct(); } + + /** + * Initialize the class + * @param entry + */ + virtual void initialize(struct _zend_class_entry *entry) + { + // pass to the entry + _type.initialize(entry); + } private: /** diff --git a/include/member.h b/include/member.h new file mode 100644 index 0000000..4a9a754 --- /dev/null +++ b/include/member.h @@ -0,0 +1,72 @@ +/** + * Member.h + * + * Base class for elements of a class + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ + +/** + * Forward declarations + */ +struct _zend_class_entry; + +/** + * Namespace + */ +namespace Php { + +/** + * Class definition + */ +class Member +{ +public: + /** + * Constructor + * @param name Name of the member + * @param pub Is this a public property (otherwise it is protected) + * @param value The value to add + */ + Member(const char *name, bool pub, const Value &value) : + _name(name), _public(pub), _value(value) {} + + /** + * Destructor + */ + virtual ~Member() {} + + /** + * Internal method to declare the property + * @var zend_class_entry + */ + void declare(struct _zend_class_entry *entry); + + +private: + /** + * Name of the member + * @var string + */ + std::string _name; + + /** + * Is this a public property + * @var bool + */ + bool _public; + + /** + * The default value + * @var Value + */ + Value _value; + +}; + +/** + * End of namespace + */ +} + diff --git a/include/protected.h b/include/protected.h new file mode 100644 index 0000000..e660f76 --- /dev/null +++ b/include/protected.h @@ -0,0 +1,39 @@ +/** + * Protected.h + * + * Class for adding public properties to a class + * + * @author Emiel Bruijntjes + * @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, const Value &value) : Member(name, false, value) {} + + /** + * Destructor + */ + virtual ~Protected() {} + +}; + +/** + * End of namespace + */ +} + diff --git a/include/public.h b/include/public.h new file mode 100644 index 0000000..17a5d0c --- /dev/null +++ b/include/public.h @@ -0,0 +1,40 @@ +/** + * Public.h + * + * Class for adding public properties to a class + * + * @author Emiel Bruijntjes + * @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, const Value &value) : Member(name, true, value) {} + + /** + * Destructor + */ + virtual ~Public() {} + + +}; + +/** + * End of namespace + */ +} + diff --git a/include/value.h b/include/value.h index 652b7ef..6c879e6 100644 --- a/include/value.h +++ b/include/value.h @@ -497,6 +497,7 @@ protected: * The environment can access the zval directly */ friend class Environment; + friend class Member; }; /** diff --git a/phpcpp.h b/phpcpp.h index 1179467..2f059a7 100644 --- a/phpcpp.h +++ b/phpcpp.h @@ -33,6 +33,9 @@ #include #include #include +#include +#include +#include #include #include #include diff --git a/src/classinfo.cpp b/src/classinfo.cpp index 7de7522..aa4d67f 100644 --- a/src/classinfo.cpp +++ b/src/classinfo.cpp @@ -177,6 +177,9 @@ void _ClassInfo::initialize(TSRMLS_D) // store pointer to the class in the unused doc_comment member _entry->info.user.doc_comment = (const char *)this; + + // initialize the entry + initialize(_entry); } /** diff --git a/src/includes.h b/src/includes.h index 5602e06..92de4ab 100644 --- a/src/includes.h +++ b/src/includes.h @@ -46,6 +46,9 @@ #include "../include/parameters.h" #include "../include/function.h" #include "../include/base.h" +#include "../include/member.h" +#include "../include/public.h" +#include "../include/protected.h" #include "../include/class.h" #include "../include/classinfo.h" #include "../include/extension.h" diff --git a/src/member.cpp b/src/member.cpp new file mode 100644 index 0000000..82faf95 --- /dev/null +++ b/src/member.cpp @@ -0,0 +1,32 @@ +/** + * Member.cpp + * + * Implementation for class members + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Internal method to declare the property + * @var zend_class_entry + */ +void Member::declare(struct _zend_class_entry *entry) +{ + std::cout << "declare property " << _name << std::endl; + + // declare the property + zend_declare_property(entry, _name.c_str(), _name.size(), _value._val, _public ? ZEND_ACC_PUBLIC : ZEND_ACC_PROTECTED TSRMLS_CC); +} + +/** + * End of namespace + */ +} + diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index e8558a0..8b50496 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -59,8 +59,7 @@ public: { cout << "MyCustomClass::~MyCustomClass" << endl; } - - + virtual void __construct() { cout << "MyCustomClass::__construct" << endl; @@ -86,7 +85,7 @@ extern "C" // create extension static Php::Extension extension("simple","1.0"); - // define the functionsnm + // define the functions extension.add("my_plus", my_plus, { Php::ByVal("a", Php::stringType), Php::ByVal("b", Php::arrayType), @@ -95,7 +94,10 @@ extern "C" }); // define classes - extension.add("my_class", Php::Class()); + extension.add("my_class", Php::Class({ + Php::Public("a", 123), + Php::Protected("b", "abc") + })); // return the module entry return extension.module(); diff --git a/tests/simple/simple.php b/tests/simple/simple.php index 07e3f78..f9919be 100644 --- a/tests/simple/simple.php +++ b/tests/simple/simple.php @@ -35,6 +35,10 @@ 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"); -- cgit v1.2.3