summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-09-12 05:46:02 -0700
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-09-12 05:46:02 -0700
commit9634a336f080bc15c1e67495eb9216d1863808f8 (patch)
tree139d3abc65f156d5e72e02364481fea370c807dc /include
parent68fd128d82819db1022137a45ca3224cee8ef029 (diff)
It now is possible to access global variables, using environment[varname], and to set global variable using environment[varname] = "value"
Diffstat (limited to 'include')
-rw-r--r--include/environment.h17
-rw-r--r--include/global.h193
-rw-r--r--include/value.h8
3 files changed, 214 insertions, 4 deletions
diff --git a/include/environment.h b/include/environment.h
index b8b4f74..6484d83 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -23,6 +23,7 @@ namespace Php {
* Forward definitions
*/
class Extension;
+class Global;
/**
* Class definition
@@ -91,6 +92,22 @@ public:
{
_data = data;
}
+
+ /**
+ * Get access to a global variable
+ * @param name
+ * @return Global
+ */
+ Global operator[](const char *name);
+
+ /**
+ * Get access to a global variable
+ * @param name
+ * @return Global
+ */
+ Global operator[](const std::string &name);
+
+
protected:
/**
diff --git a/include/global.h b/include/global.h
new file mode 100644
index 0000000..ead2e08
--- /dev/null
+++ b/include/global.h
@@ -0,0 +1,193 @@
+/**
+ * Global variable
+ *
+ * A global variable is a value that - once updated - also updates
+ * the global scope
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Forward definitions
+ */
+struct _zval_struct;
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Global : public Value
+{
+public:
+ /**
+ * No copy constructor
+ * @param global
+ */
+ Global(const Global &global) = delete;
+
+ /**
+ * Move constructor
+ * @param global
+ */
+ Global(Global &&global) : Value(std::move(global)), _name(std::move(global._name)), _exists(global._exists) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~Global() {}
+
+ /**
+ * Assignment operator
+ * @param global
+ * @return Global
+ */
+ /*
+ Global &operator=(const Global &global)
+ {
+ // skip self assignment
+ if (&global == this) return *this;
+
+ // call base
+ Value::operator=(global);
+
+ // copy name and exists setting
+ _name = global._name;
+ _exists = global._exists;
+
+ // done
+ return *this;
+ }
+ */
+
+ /**
+ * Move operator
+ * @param global
+ * @return Global
+ */
+ /*
+ Global &operator=(Global &&global)
+ {
+ // skip self assignment
+ if (&global == this) return *this;
+
+ // call base
+ Value::operator=(std::move(global));
+
+ // copy name and exists setting
+ _name = std::move(global._name);
+ _exists = global._exists;
+
+ // done
+ return *this;
+ }
+ */
+
+ /**
+ * Assignment operator
+ * @param value
+ * @return Global
+ */
+ template <typename T>
+ Global &operator=(const T &value)
+ {
+ Value::operator=(value);
+ return update();
+ }
+
+ /**
+ * Set a certain property
+ * Calling this method will turn the value into an array
+ * @param index Index of the property to set
+ * @param value Value to set
+ * @return Value The value that was set
+ */
+ virtual const Value &set(int index, const Value &value) override
+ {
+ // update current object
+ update();
+
+ // call base
+ return Value::set(index, value);
+ }
+
+ /**
+ * Set a certain property
+ * Calling this method will turn the value into an array
+ * @param key Key of the property to set
+ * @param size Size of the key
+ * @param value Value to set
+ * @return Value The value that was set
+ */
+ virtual const Value &set(const char *key, int size, const Value &value) override
+ {
+ // update current object
+ update();
+
+ // call base
+ return Value::set(key, size, value);
+ }
+
+
+protected:
+ /**
+ * Function that is called when the value is updated
+ * @return Value
+ */
+ Global &update();
+
+private:
+ /**
+ * Constructor for non-existing var
+ * @param name
+ */
+ Global(const char *name) : Value(), _name(name), _exists(false) {}
+
+ /**
+ * Alternative constructor for non-existing var
+ * @param name
+ */
+ Global(const std::string &name) : Value(), _name(name), _exists(false) {}
+
+ /**
+ * Constructor to wrap zval for existing global bar
+ * @param name
+ * @param val
+ */
+ Global(const char *name, struct _zval_struct *val) : Value(val, true), _name(name), _exists(true) {}
+
+ /**
+ * Alternative constructor to wrap zval
+ * @param name
+ * @param val
+ */
+ Global(const std::string &name, struct _zval_struct *val) : Value(val, true), _name(name), _exists(true) {}
+
+ /**
+ * Name of the variable
+ * @var string
+ */
+ std::string _name;
+
+ /**
+ * Does it already exist?
+ * @var bool
+ */
+ bool _exists;
+
+ /**
+ * The environment can access the private method from this class
+ */
+ friend class Environment;
+};
+
+/**
+ * End of namespace
+ */
+}
+
+
diff --git a/include/value.h b/include/value.h
index 4854437..fa2b608 100644
--- a/include/value.h
+++ b/include/value.h
@@ -121,14 +121,14 @@ public:
* @param value
* @return Value
*/
- virtual Value &operator=(const Value &value);
+ Value &operator=(const Value &value);
/**
* Move assignment
* @param value
* @return Value
*/
- virtual Value &operator=(Value &&value);
+ Value &operator=(Value &&value);
/**
* Assignment operator
@@ -425,7 +425,7 @@ public:
* @param value Value to set
* @return Value The value that was set
*/
- const Value &set(int index, const Value &value);
+ virtual const Value &set(int index, const Value &value);
/**
* Set a certain property
@@ -435,7 +435,7 @@ public:
* @param value Value to set
* @return Value The value that was set
*/
- const Value &set(const char *key, int size, const Value &value);
+ virtual const Value &set(const char *key, int size, const Value &value);
/**
* Set a certain property