From 9634a336f080bc15c1e67495eb9216d1863808f8 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Thu, 12 Sep 2013 05:46:02 -0700 Subject: It now is possible to access global variables, using environment[varname], and to set global variable using environment[varname] = "value" --- include/global.h | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 include/global.h (limited to 'include/global.h') 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 + * @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 + 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 + */ +} + + -- cgit v1.2.3