summaryrefslogtreecommitdiff
path: root/src
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 /src
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 'src')
-rw-r--r--src/config.m44
-rw-r--r--src/environment.cpp70
-rw-r--r--src/global.cpp42
-rw-r--r--src/includes.h1
-rw-r--r--src/value.cpp63
5 files changed, 149 insertions, 31 deletions
diff --git a/src/config.m4 b/src/config.m4
deleted file mode 100644
index a81d11d..0000000
--- a/src/config.m4
+++ /dev/null
@@ -1,4 +0,0 @@
-dnl PHP_REQUIRE_CXX()
-dnl PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)
-PHP_NEW_EXTENSION(phpcpp, extension.cpp, $ext_shared,,"","yes")
-
diff --git a/src/environment.cpp b/src/environment.cpp
new file mode 100644
index 0000000..b2fdad6
--- /dev/null
+++ b/src/environment.cpp
@@ -0,0 +1,70 @@
+/**
+ * Environment.cpp
+ *
+ * Implementation of the environment class
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+#include "includes.h"
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Get access to a global variable
+ * @param name
+ * @return Global
+ */
+Global Environment::operator[](const char *name)
+{
+ // pointer to a zval
+ zval **varvalue;
+
+ // check if the variable already exists
+ if (zend_hash_find(&EG(symbol_table), name, strlen(name)+1, (void**)&varvalue) == FAILURE)
+ {
+ // the variable does not already exist, return a global object
+ // that will automatically set the value when it is updated
+ return Global(name);
+ }
+ else
+ {
+ // we are in the happy situation that the variable exists, we turn
+ // this value into a reference value, and return that
+ return Global(name, *varvalue);
+ }
+}
+
+/**
+ * Get access to a global variable
+ * @param name
+ * @return Global
+ */
+Global Environment::operator[](const std::string &name)
+{
+ // pointer to a zval
+ zval **varvalue;
+
+ // check if the variable already exists
+ if (zend_hash_find(&EG(symbol_table), name.c_str(), name.size()+1, (void**)&varvalue) == FAILURE)
+ {
+ // the variable does not already exist, return a global object
+ // that will automatically set the value when it is updated
+ return Global(name);
+ }
+ else
+ {
+ // we are in the happy situation that the variable exists, we turn
+ // this value into a reference value, and return that
+ return Global(name, *varvalue);
+ }
+}
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/src/global.cpp b/src/global.cpp
new file mode 100644
index 0000000..af72bc0
--- /dev/null
+++ b/src/global.cpp
@@ -0,0 +1,42 @@
+/**
+ * Global.cpp
+ *
+ * Implementation for the global variable
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+#include "includes.h"
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Function that is called when the value is updated
+ * @return Value
+ */
+Global &Global::update()
+{
+ // skip if the variable already exists
+ if (_exists) return *this;
+
+ // add the variable to the globals
+ zend_hash_add(EG(active_symbol_table), _name.c_str(), _name.size()+1, &_val, sizeof(zval*), NULL);
+
+ // add one extra reference because the variable now is a global var too
+ Z_ADDREF_P(_val);
+
+ // remember that the variable now exists
+ _exists = true;
+
+ // done
+ return *this;
+}
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/src/includes.h b/src/includes.h
index 1a60b67..7fb8c5f 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -41,6 +41,7 @@
#include "../include/byval.h"
#include "../include/byref.h"
#include "../include/value.h"
+#include "../include/global.h"
#include "../include/member.h"
#include "../include/parameters.h"
#include "../include/function.h"
diff --git a/src/value.cpp b/src/value.cpp
index 4cdac58..5239b33 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -134,19 +134,28 @@ Value::Value(double value)
* @param zval Value to wrap
* @param ref Force this to be a reference
*/
-Value::Value(struct _zval_struct *zval, bool ref)
+Value::Value(struct _zval_struct *val, bool ref)
{
// just copy the zval into this object
- _val = zval;
+ _val = val;
+
+ // if the variable is not already a reference, and it has more than one
+ // variable pointing to it, we should seperate it so that any changes
+ // we're going to make will not change the other variable
+ if (ref && Z_REFCOUNT_P(_val) > 1)
+ {
+ // separate the zval
+ SEPARATE_ZVAL_IF_NOT_REF(&_val);
+ }
// we see ourselves as reference too
Z_ADDREF_P(_val);
// we're ready if we do not have to force it as a reference
- if (!ref || Z_ISREF_P(zval)) return;
+ if (!ref || Z_ISREF_P(_val)) return;
// make this a reference
- Z_SET_ISREF_P(zval);
+ Z_SET_ISREF_P(_val);
}
/**
@@ -238,9 +247,9 @@ Value &Value::operator=(const Value &value)
// and we have one more reference
Z_ADDREF_P(_val);
}
-
- // done
- return *this;
+
+ // update the object
+ return *this;
}
/**
@@ -309,9 +318,9 @@ Value &Value::operator=(Value &&value)
// the other object is no longer valid
value._val = nullptr;
}
-
- // done
- return *this;
+
+ // update the object
+ return *this;
}
/**
@@ -329,9 +338,9 @@ Value &Value::operator=(int value)
// set new value
ZVAL_LONG(_val, value);
-
- // done
- return *this;
+
+ // update the object
+ return *this;
}
/**
@@ -349,9 +358,9 @@ Value &Value::operator=(long value)
// set new value
ZVAL_LONG(_val, value);
-
- // done
- return *this;
+
+ // update the object
+ return *this;
}
/**
@@ -369,9 +378,9 @@ Value &Value::operator=(bool value)
// set new value
ZVAL_BOOL(_val, value);
-
- // done
- return *this;
+
+ // update the object
+ return *this;
}
/**
@@ -390,8 +399,8 @@ Value &Value::operator=(char value)
// set new value
ZVAL_STRINGL(_val, &value, 1, 1);
- // done
- return *this;
+ // update the object
+ return *this;
}
/**
@@ -410,8 +419,8 @@ Value &Value::operator=(const std::string &value)
// set new value
ZVAL_STRINGL(_val, value.c_str(), value.size(), 1);
- // done
- return *this;
+ // update the object
+ return *this;
}
/**
@@ -430,8 +439,8 @@ Value &Value::operator=(const char *value)
// set new value
ZVAL_STRING(_val, value, 1);
- // done
- return *this;
+ // update the object
+ return *this;
}
/**
@@ -450,8 +459,8 @@ Value &Value::operator=(double value)
// set new value
ZVAL_DOUBLE(_val, value);
- // done
- return *this;
+ // update the object
+ return *this;
}
/**