summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/base.h56
-rw-r--r--include/class.h68
-rw-r--r--include/classinfo.h79
-rw-r--r--include/extension.h31
-rw-r--r--phpcpp.h3
-rw-r--r--src/class.cpp11
-rw-r--r--src/extension.cpp18
-rw-r--r--src/includes.h3
-rw-r--r--tests/simple/simple.cpp25
9 files changed, 275 insertions, 19 deletions
diff --git a/include/base.h b/include/base.h
new file mode 100644
index 0000000..eceb114
--- /dev/null
+++ b/include/base.h
@@ -0,0 +1,56 @@
+/**
+ * Base class for defining your own objects
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Base
+{
+public:
+ /**
+ * The pseudo constructor that is called from PHP after the object is constructed
+ * @param environment
+ * @param parameters
+ */
+ virtual void __construct(Environment &environment, const Parameters &parameters)
+ {
+ // call all other possible implementations
+ __construct(environment);
+ __construct(parameters);
+ __construct();
+ }
+
+ /**
+ * The pseudo constructor that is called from PHP after the object is constructed
+ * @param environment
+ */
+ virtual void __construct(Environment &environment) {}
+
+ /**
+ * The pseudo constructor that is called from PHP after the object is constructed
+ * @param parameters
+ */
+ virtual void __construct(const Parameters &parameters) {}
+
+ /**
+ * The pseudo constructor that is called from PHP after the object is constructed
+ */
+ virtual void __construct() {}
+
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/class.h b/include/class.h
new file mode 100644
index 0000000..38398b9
--- /dev/null
+++ b/include/class.h
@@ -0,0 +1,68 @@
+/**
+ * Class.h
+ *
+ * When a class is registered in the extension, you need this helper class
+ * for that.
+ *
+ * The use of it is simple:
+ *
+ * Extension::add(Class<YourClass>);
+ *
+ * Note that YourClass must extend from Php::Object
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition of the class
+ */
+template <typename T>
+class Class
+{
+public:
+ /**
+ * Constructor
+ */
+ Class() {}
+
+ /**
+ * Move constructor
+ * @param that
+ */
+ Class(Class &&that) {}
+
+ /**
+ * Copy constructor
+ */
+ Class(const Class &that) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~Class() {}
+
+ /**
+ * Construct an instance
+ * @return Base
+ */
+ Base* construct()
+ {
+ // allocate the object
+ return new T();
+ }
+
+protected:
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/classinfo.h b/include/classinfo.h
new file mode 100644
index 0000000..dfa480c
--- /dev/null
+++ b/include/classinfo.h
@@ -0,0 +1,79 @@
+/**
+ * ClassInfo.h
+ *
+ * Internal class that is constructed by the library and that contains
+ * the information about a class, including its name.
+ *
+ * Users of the PHP-CPP libraries are not supposed to interact with
+ * this class, or instantiate objects of this class.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Virtual base class of the classInfo
+ *
+ * We need this virtual base class to store pointers to class objects,
+ * without knowing in advance what sort of object they will hold
+ */
+class _ClassInfo
+{
+public:
+ /**
+ * Initialize the class
+ */
+ virtual void initialize() = 0;
+};
+
+/**
+ * Class definition
+ */
+template <typename T>
+class ClassInfo : public _ClassInfo
+{
+public:
+ /**
+ * Constructor
+ * @param name Name of the class
+ * @param type The class type
+ */
+ ClassInfo(const char *name, const Class<T> &type) : _name(name), _type(type)
+ {
+ }
+
+ /**
+ * Destructor
+ */
+ virtual ~ClassInfo() {}
+
+ /**
+ * Initialize the class
+ */
+ virtual void initialize();
+
+private:
+ /**
+ * Class name
+ * @var string
+ */
+ std::string _name;
+
+ /**
+ * The class object
+ * @var Class
+ */
+ Class<T> _type;
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/extension.h b/include/extension.h
index ceeed78..1984aa9 100644
--- a/include/extension.h
+++ b/include/extension.h
@@ -80,15 +80,15 @@ public:
* and after the compatibility has been checked, but before the requests
* are handled. You can override this method to add your own initialization.
*
+ * The default behavior of this function is to enable all classes that are
+ * defined in this extension, so that they are also available in PHP.
+ *
* The method should return true on success, and false on failure (in which
* case the extension will not be used)
*
* @return bool
*/
- virtual bool initialize()
- {
- return true;
- }
+ virtual bool initialize();
/**
* Finalize the extension
@@ -200,6 +200,21 @@ public:
Function *add(const char *name, native_callback_7 function, const std::initializer_list<Argument> &arguments = {});
/**
+ * Add a native class to the extension
+ * @param name Name of the class
+ * @param type The class implementation
+ */
+ template<typename T>
+ void add(const char *name, const Class<T> &type)
+ {
+ // construct info
+ ClassInfo<T> *info = new ClassInfo<T>(name, type);
+
+ // add class
+ _classes.insert(std::unique_ptr<_ClassInfo>(info));
+ }
+
+ /**
* Retrieve the module entry
*
* This is the memory address that should be exported by the get_module()
@@ -213,11 +228,17 @@ public:
private:
/**
* Set of function objects defined in the library
- * @var map
+ * @var set
*/
std::set<std::unique_ptr<Function>> _functions;
/**
+ * Set of classes defined in the library
+ * @var set
+ */
+ std::set<std::unique_ptr<_ClassInfo>> _classes;
+
+ /**
* The information that is passed to the Zend engine
*
* Although it would be slightly faster to not make this a pointer, this
diff --git a/phpcpp.h b/phpcpp.h
index b0d4f48..724e88d 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -32,6 +32,9 @@
#include <phpcpp/member.h>
#include <phpcpp/parameters.h>
#include <phpcpp/function.h>
+#include <phpcpp/base.h>
+#include <phpcpp/class.h>
+#include <phpcpp/classinfo.h>
#include <phpcpp/extension.h>
/**
diff --git a/src/class.cpp b/src/class.cpp
new file mode 100644
index 0000000..ebbc9ae
--- /dev/null
+++ b/src/class.cpp
@@ -0,0 +1,11 @@
+/**
+ * Class.cpp
+ *
+ * Implementation for the class entry
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+#include "includes.h"
+
+
diff --git a/src/extension.cpp b/src/extension.cpp
index b0d78f2..7f6c9dd 100644
--- a/src/extension.cpp
+++ b/src/extension.cpp
@@ -247,6 +247,24 @@ zend_module_entry *Extension::module()
}
/**
+ * Initialize the extension
+ * @return bool
+ */
+bool Extension::initialize()
+{
+ // loop through the classes
+ for (auto iter = _classes.begin(); iter != _classes.end(); iter++)
+ {
+ // initialize the class
+ (*iter)->initialize();
+ }
+
+ // done
+ return true;
+}
+
+
+/**
* End of namespace
*/
}
diff --git a/src/includes.h b/src/includes.h
index 41989c8..8eae5f7 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -45,6 +45,9 @@
#include "../include/member.h"
#include "../include/parameters.h"
#include "../include/function.h"
+#include "../include/base.h"
+#include "../include/class.h"
+#include "../include/classinfo.h"
#include "../include/extension.h"
#include "../include/globals.h"
diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp
index fbc29d4..f2a5207 100644
--- a/tests/simple/simple.cpp
+++ b/tests/simple/simple.cpp
@@ -15,6 +15,12 @@
*/
using namespace std;
+/**
+ * Our own "my_plus" function that will be available in PHP
+ * @param environment
+ * @param params
+ * @return Value
+ */
static Php::Value my_plus(Php::Environment &env, Php::Parameters &params)
{
string p1 = params[0];
@@ -34,20 +40,14 @@ static Php::Value my_plus(Php::Environment &env, Php::Parameters &params)
return p1 + p2;
}
-class MyCustomFunction : public Php::Function
+/**
+ * Custom class that will be available in PHP
+ */
+class MyCustomClass : public Php::Base
{
public:
- MyCustomFunction(const char *name) : Function(name) {}
-
-
-
};
-
-
-
-
-
// symbols are exported according to the "C" language
extern "C"
{
@@ -65,11 +65,8 @@ extern "C"
Php::ByRef("d", Php::stringType)
});
-// extension.add("my_concat", my_concat);
- extension.add(new MyCustomFunction("my_custom"));
-
// define classes
-// extension.add("my_class", MyCustomClass());
+ extension.add("my_class", Php::Class<MyCustomClass>());
// return the module entry
return extension.module();