summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-17 22:19:08 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-17 22:19:08 +0100
commit5fd8b29a1981d2d4f7c4e9925729fbe9f1c558bb (patch)
treee0738b3ebb653d1a7ae8a8c1f62f309f1895bfe5 /include
parent821e65d876cc0ce2b32471791b02d9f7cc784c99 (diff)
added initial implementation for registering constants
Diffstat (limited to 'include')
-rw-r--r--include/class.h14
-rw-r--r--include/constant.h67
-rw-r--r--include/namespace.h50
-rw-r--r--include/value.h1
4 files changed, 127 insertions, 5 deletions
diff --git a/include/class.h b/include/class.h
index e11074c..2c875fa 100644
--- a/include/class.h
+++ b/include/class.h
@@ -91,12 +91,16 @@ public:
/**
* Add a static method to a class
*
- * In C++ a static method is just a plain function, that only at compile
- * time has access to the private variables. You can therefore also supply
- * global functions as static method, and real static methods (that do not
- * even have to come from the same class.
+ * In C++ a static method is in reality just a plain function, that at
+ * compile time has access to private properties of the class that it is a
+ * static member of.
*
- * In PHP scripts, the function will only be callable as real static method
+ * Because a C++ static method is not a real method with a 'this' pointer,
+ * it has the same signature as a normal C++ (non-method) function. Therefore,
+ * you can register real static member functions (&MyClass::myMethod) as well
+ * as normal functions (myFunction) as class methods.
+ *
+ * In PHP scripts, such functions will be callable as static class methods
*
* @param name Name of the method
* @param method The actual method
diff --git a/include/constant.h b/include/constant.h
new file mode 100644
index 0000000..c6be099
--- /dev/null
+++ b/include/constant.h
@@ -0,0 +1,67 @@
+/**
+ * Constant.h
+ *
+ * If you want to define global PHP constants, or class constants you can
+ * use this constant class for it. Wrap the constant you'd like to create
+ * in a Php::Constant object and add it to the extension or the class:
+ *
+ * extension.add(Php::Constant("CONSTANT_NAME", "value"));
+ * myclass.add(Php::Constant("CLASS_CONSTANT", "value"));
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2015 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Forward declarations
+ */
+class ConstantImpl;
+
+/**
+ * Class definition
+ */
+class Constant
+{
+public:
+ /**
+ * Constructor
+ * @param name Constant name
+ * @param value Constant value
+ */
+ Constant(const char *name, const Value &value);
+
+ /**
+ * Destructor
+ */
+ virtual ~Constant() {}
+
+private:
+ /**
+ * Pointer to the actual implementation of the constant
+ * @var std::shared_ptr
+ */
+ std::shared_ptr<ConstantImpl> _impl;
+
+ /**
+ * Get access to the implementation object
+ * @return std::shared_ptr
+ */
+ const std::shared_ptr<ConstantImpl> &implementation() const { return _impl; }
+
+ /**
+ * The extension object has access to privates
+ */
+ friend class ExtensionImpl;
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/namespace.h b/include/namespace.h
index 84f0740..4bb23db 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -43,6 +43,12 @@ protected:
std::list<std::shared_ptr<ClassBase>> _classes;
/**
+ * Constants defined in the namespace
+ * @var list
+ */
+ std::list<std::shared_ptr<Constant>> _constants;
+
+ /**
* Namespaces defined inside the namespace
* @var list
*/
@@ -163,6 +169,40 @@ public:
}
/**
+ * Add a constant to the namespace
+ * @param constant The constant to add
+ * @return Namespace Same object to allow chaining
+ */
+ Namespace &add(Constant &&constant)
+ {
+ // skip when locked
+ if (locked()) return *this;
+
+ // and add it to the list of constants
+ _constants.push_back(std::unique_ptr<Constant>(new Constant(std::move(constant))));
+
+ // allow chaining
+ return *this;
+ }
+
+ /**
+ * Add a constant to the namespace
+ * @param constant The constant to add
+ * @return Namespace Same object to allow chaining
+ */
+ Namespace &add(const Constant &constant)
+ {
+ // skip when locked
+ if (locked()) return *this;
+
+ // and add it to the list of constants
+ _constants.push_back(std::unique_ptr<Constant>(new Constant(constant)));
+
+ // allow chaining
+ return *this;
+ }
+
+ /**
* Add a namespace to the namespace by moving it
* @param ns The namespace
* @return Namespace Same object to allow chaining
@@ -231,6 +271,16 @@ public:
* @param callback
*/
void classes(const std::function<void(const std::string &ns, ClassBase &clss)> &callback);
+
+ /**
+ * Apply a callback to each registered constant
+ *
+ * The callback will be called with the name of the namespace, and
+ * a reference to the registered constant
+ *
+ * @param callback
+ */
+ void constants(const std::function<void(const std::string &ns, Constant &constant)> &callback);
};
diff --git a/include/value.h b/include/value.h
index c3de4d5..a450755 100644
--- a/include/value.h
+++ b/include/value.h
@@ -1171,6 +1171,7 @@ protected:
friend class HashMember<std::string>;
friend class Callable;
friend class Script;
+ friend class ConstantImpl;
};
/**