summaryrefslogtreecommitdiff
path: root/include/function.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-25 17:40:03 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-25 17:40:03 +0200
commite14055694478d70e58b5fc653b08a9a514bbc455 (patch)
treeab47d723699a7fe86f47f68c182ab695bf9b3bab /include/function.h
parente838e180f5bbcf19e7235f30311645e942ff92d2 (diff)
{more work in progress: the function that was defined with c++ now gets calls, but it does not yet call the actual implementation
Diffstat (limited to 'include/function.h')
-rw-r--r--include/function.h132
1 files changed, 132 insertions, 0 deletions
diff --git a/include/function.h b/include/function.h
new file mode 100644
index 0000000..1a35671
--- /dev/null
+++ b/include/function.h
@@ -0,0 +1,132 @@
+/**
+ * Function.h
+ *
+ * Object represents a callable function that is defined with the CPP API.
+ * After you've instantiated the extension, you can add function objects to
+ * it.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace PhpCpp {
+
+/**
+ * Forward definitions
+ */
+class Callable;
+
+/**
+ * Class definition
+ */
+class Function
+{
+public:
+ /**
+ * Constructor
+ * @param name Name of the function
+ * @param arguments The arguments that can be passed to the function
+ */
+ Function(const std::string &name, const std::initializer_list<Argument> &arguments);
+
+ /**
+ * Constructor
+ * @param name Name of the function
+ */
+ Function(const char *name) : Function(name, {}) {}
+
+ /**
+ * Copy constructor
+ * @param function The other function
+ */
+ Function(const Function &function)
+ {
+ // copy other object
+ _refcount = function._refcount;
+ _callable = function._callable;
+
+ // increate number of references
+ (*_refcount)++;
+ }
+
+ /**
+ * Destructor
+ */
+ virtual ~Function()
+ {
+ // cleanup the object
+ cleanup();
+ }
+
+ /**
+ * Assignment operator
+ * @param function The other function
+ * @return Function
+ */
+ Function &operator=(const Function &function)
+ {
+ // skip self assignment
+ if (&function == this) return *this;
+
+ // cleanup the object
+ cleanup();
+
+ // copy other object
+ _refcount = function._refcount;
+ _callable = function._callable;
+
+ // increate number of references
+ (*_refcount)++;
+
+ // done
+ return *this;
+ }
+
+ /**
+ * Method that gets called every time the function is executed
+ * @param request The request during which the call was made
+ * @param arguments The actual arguments that were passed
+ * @return Variable Return value
+ */
+ virtual Variable invoke(const Request *request, const std::initializer_list<Variable> &arguments)
+ {
+ }
+
+ /**
+ * Get access to the internal object
+ * @return Callable
+ * @internal
+ */
+ Callable *internal() const
+ {
+ return _callable;
+ }
+
+protected:
+ /**
+ * Pointer to the callable object
+ * @var smart_ptr
+ */
+ Callable *_callable;
+
+ /**
+ * Counter with the number of references
+ * @var integer
+ */
+ int *_refcount;
+
+
+ /**
+ * Remove one reference
+ */
+ void cleanup();
+};
+
+/**
+ * End of namespace
+ */
+}
+