summaryrefslogtreecommitdiff
path: root/src/method.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/method.h')
-rw-r--r--src/method.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/method.h b/src/method.h
new file mode 100644
index 0000000..cc9f973
--- /dev/null
+++ b/src/method.h
@@ -0,0 +1,104 @@
+/**
+ * Method.h
+ *
+ * Internal class that represents a native class method, that can be called
+ * from PHP scripts.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2014 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Method : public Callable
+{
+public:
+ /**
+ * Constructor
+ *
+ * @param name Method name
+ * @param callback Native callback
+ * @param flags Access flags
+ * @param args Argument description
+ */
+ Method(const char *name, method_callback_0 callback, int flags, const Arguments &args) : Callable(name, args), _type(0), _flags(flags) { _callback.m0 = callback; }
+ Method(const char *name, method_callback_1 callback, int flags, const Arguments &args) : Callable(name, args), _type(1), _flags(flags) { _callback.m1 = callback; }
+ Method(const char *name, method_callback_2 callback, int flags, const Arguments &args) : Callable(name, args), _type(2), _flags(flags) { _callback.m2 = callback; }
+ Method(const char *name, method_callback_3 callback, int flags, const Arguments &args) : Callable(name, args), _type(3), _flags(flags) { _callback.m3 = callback; }
+
+ /**
+ * Destructor
+ * @param type
+ * @param callback
+ */
+ virtual ~Method() {}
+
+ /**
+ * Internal method to fill a function entry
+ * @param zend_function_entry
+ * @param classname
+ */
+ void initialize(struct _zend_function_entry *entry, const std::string &classname)
+ {
+ // call base
+ Callable::initialize(entry, classname.c_str(), _flags);
+ }
+
+ /**
+ * Invoke the method
+ * @param parameters
+ * @return Value
+ */
+ virtual Value invoke(Parameters &parameters) override
+ {
+ // the object to call a method on
+ Base *base = parameters.object();
+
+ // find out which method to call, and call it
+ switch (_type) {
+ case 0: (base->*_callback.m0)(); return Value();
+ case 1: (base->*_callback.m1)(parameters); return Value();
+ case 2: return (base->*_callback.m2)();
+ case 3: return (base->*_callback.m3)(parameters);
+ default: return Value();
+ }
+ }
+
+
+private:
+ /**
+ * Callback type
+ * @var int
+ */
+ int _type;
+
+ /**
+ * Access flags (protected, public, abstract, final, private, etc)
+ * @var int
+ * @todo use this
+ */
+ int _flags;
+
+ /**
+ * The actual callback
+ * @var void*
+ */
+ union {
+ method_callback_0 m0;
+ method_callback_1 m1;
+ method_callback_2 m2;
+ method_callback_3 m3;
+ } _callback;
+};
+
+/**
+ * End of namespace
+ */
+}
+