summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-09-30 13:49:23 -0700
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-09-30 13:49:23 -0700
commit777eb276f635c949ccdcf9613ad55d42190cb387 (patch)
treef5451ae464a37414412d122ff6a283f713638dfa
parentecfca6b1197658afb85fffd6f6ac9e92311a4b07 (diff)
Work in progress on adding public and protected properties to classes
-rw-r--r--include/class.h36
-rw-r--r--include/classinfo.h16
-rw-r--r--include/member.h72
-rw-r--r--include/protected.h39
-rw-r--r--include/public.h40
-rw-r--r--include/value.h1
-rw-r--r--phpcpp.h3
-rw-r--r--src/classinfo.cpp3
-rw-r--r--src/includes.h3
-rw-r--r--src/member.cpp32
-rw-r--r--tests/simple/simple.cpp10
-rw-r--r--tests/simple/simple.php4
12 files changed, 252 insertions, 7 deletions
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 <emiel.bruijntjes@copernica.com>
* @copyright 2013 Copernica BV
*/
-
+
+/**
+ * Forward declarations
+ */
+struct _zend_class_entry;
+
/**
* Set up namespace
*/
@@ -32,15 +37,21 @@ public:
Class() {}
/**
+ * Constructor with initializer list to define the properties
+ * @param members
+ */
+ Class(const std::initializer_list<Member> &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<Member> _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 <emiel.bruijntjes@copernica.com>
+ * @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 <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, 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 <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, 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 <phpcpp/parameters.h>
#include <phpcpp/function.h>
#include <phpcpp/base.h>
+#include <phpcpp/member.h>
+#include <phpcpp/public.h>
+#include <phpcpp/protected.h>
#include <phpcpp/class.h>
#include <phpcpp/classinfo.h>
#include <phpcpp/extension.h>
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 <emiel.bruijntjes@copernica.com>
+ * @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<MyCustomClass>());
+ extension.add("my_class", Php::Class<MyCustomClass>({
+ 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");