summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-15 18:31:42 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-15 18:31:42 +0100
commit3bcd6e21d1142b5ec35f99c4bdcd925bf7ae5083 (patch)
tree08903d9b7e7b2bd57fa1a01fc873d618a17b0a8f /src
parent38b866988761f4da01eab769dc660b06b07e97be (diff)
added empty() function, and added HardCoded class
Diffstat (limited to 'src')
-rw-r--r--src/arithmetic.h13
-rw-r--r--src/classbase.cpp6
-rw-r--r--src/includes.h1
-rw-r--r--src/value.cpp81
4 files changed, 73 insertions, 28 deletions
diff --git a/src/arithmetic.h b/src/arithmetic.h
index 21343d1..00d5641 100644
--- a/src/arithmetic.h
+++ b/src/arithmetic.h
@@ -127,7 +127,7 @@ public:
// convert string to integer
return apply(atoi(value.c_str()));
}
-
+
/**
* Apply a string (representing a number), and return a new value object after running the arithmetic function
* @param value
@@ -138,6 +138,17 @@ public:
// convert string to integer
return apply(atoi(value));
}
+
+ /**
+ * Apply a string (representing a number), and return a new value object after running the arithmetic function
+ * @param value
+ * @return Value
+ */
+ Value apply(const HardCoded &value)
+ {
+ // convert string to integer
+ return apply(atoi(value.buffer()));
+ }
/**
* Apply a string (representing a number), and return a new value object after running the arithmetic function
diff --git a/src/classbase.cpp b/src/classbase.cpp
index 0de62dd..fbc6ed2 100644
--- a/src/classbase.cpp
+++ b/src/classbase.cpp
@@ -739,9 +739,9 @@ int ClassBase::hasDimension(zval *object, zval *member, int check_empty)
// we know for certain that the offset exists, but should we check
// more, like whether the value is empty or not?
if (!check_empty) return true;
-
- // it should not be empty
- return !arrayaccess->offsetGet(member).isEmpty();
+
+ // the user wants to know if the property is empty
+ return empty(arrayaccess->offsetGet(member));
}
catch (Exception &exception)
{
diff --git a/src/includes.h b/src/includes.h
index 0c00df3..55aa103 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -46,6 +46,7 @@
*/
#include "../include/exception.h"
#include "../include/type.h"
+#include "../include/hardcoded.h"
#include "../include/value.h"
#include "../include/array.h"
#include "../include/object.h"
diff --git a/src/value.cpp b/src/value.cpp
index 9af2d14..7ce49fe 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -130,6 +130,17 @@ Value::Value(const char *value, int size)
}
/**
+ * Contructor based on hardcoded string that does not have to be copied
+ * @param value
+ */
+Value::Value(const HardCoded &value)
+{
+ // create a string zval
+ MAKE_STD_ZVAL(_val);
+ ZVAL_STRINGL(_val, value.buffer(), value.size(), 0);
+}
+
+/**
* Constructor based on decimal value
* @param value
*/
@@ -663,6 +674,26 @@ Value &Value::operator=(const char *value)
return *this;
}
+/**
+ * Assignment operator
+ * @param value
+ * @return Value
+ */
+Value &Value::operator=(const HardCoded &value)
+{
+ // if this is not a reference variable, we should detach it to implement copy on write
+ SEPARATE_ZVAL_IF_NOT_REF(&_val);
+
+ // deallocate current zval (without cleaning the zval structure)
+ zval_dtor(_val);
+
+ // set new non-duplicated value
+ ZVAL_STRINGL(_val, value.buffer(), value.size(), 0);
+
+ // update the object
+ return *this;
+}
+
/**
* Assignment operator
* @param value
@@ -696,6 +727,7 @@ Value &Value::operator+=(bool value) { return Arithmetic<std::plu
Value &Value::operator+=(char value) { return Arithmetic<std::plus>(this).assign(value); }
Value &Value::operator+=(const std::string &value) { return Arithmetic<std::plus>(this).assign(value); }
Value &Value::operator+=(const char *value) { return Arithmetic<std::plus>(this).assign(value); }
+Value &Value::operator+=(const HardCoded &value) { return Arithmetic<std::plus>(this).assign(value); }
Value &Value::operator+=(double value) { return Arithmetic<std::plus>(this).assign(value); }
/**
@@ -711,6 +743,7 @@ Value &Value::operator-=(bool value) { return Arithmetic<std::min
Value &Value::operator-=(char value) { return Arithmetic<std::minus>(this).assign(value); }
Value &Value::operator-=(const std::string &value) { return Arithmetic<std::minus>(this).assign(value); }
Value &Value::operator-=(const char *value) { return Arithmetic<std::minus>(this).assign(value); }
+Value &Value::operator-=(const HardCoded &value) { return Arithmetic<std::minus>(this).assign(value); }
Value &Value::operator-=(double value) { return Arithmetic<std::minus>(this).assign(value); }
/**
@@ -726,6 +759,7 @@ Value &Value::operator*=(bool value) { return Arithmetic<std::mul
Value &Value::operator*=(char value) { return Arithmetic<std::multiplies>(this).assign(value); }
Value &Value::operator*=(const std::string &value) { return Arithmetic<std::multiplies>(this).assign(value); }
Value &Value::operator*=(const char *value) { return Arithmetic<std::multiplies>(this).assign(value); }
+Value &Value::operator*=(const HardCoded &value) { return Arithmetic<std::multiplies>(this).assign(value); }
Value &Value::operator*=(double value) { return Arithmetic<std::multiplies>(this).assign(value); }
/**
@@ -741,6 +775,7 @@ Value &Value::operator/=(bool value) { return Arithmetic<std::div
Value &Value::operator/=(char value) { return Arithmetic<std::divides>(this).assign(value); }
Value &Value::operator/=(const std::string &value) { return Arithmetic<std::divides>(this).assign(value); }
Value &Value::operator/=(const char *value) { return Arithmetic<std::divides>(this).assign(value); }
+Value &Value::operator/=(const HardCoded &value) { return Arithmetic<std::divides>(this).assign(value); }
Value &Value::operator/=(double value) { return Arithmetic<std::divides>(this).assign(value); }
/**
@@ -757,6 +792,7 @@ Value &Value::operator%=(bool value) { return operator=(numericVa
Value &Value::operator%=(char value) { return operator=(numericValue() % value); }
Value &Value::operator%=(const std::string &value) { return operator=(numericValue() % atoi(value.c_str())); }
Value &Value::operator%=(const char *value) { return operator=(numericValue() % atoi(value)); }
+Value &Value::operator%=(const HardCoded &value) { return operator=(numericValue() % atoi(value.buffer())); }
Value &Value::operator%=(double value) { return operator=(numericValue() % (int)value); }
/**
@@ -772,6 +808,7 @@ Value Value::operator+(bool value) { return Arithmetic<std::plu
Value Value::operator+(char value) { return Arithmetic<std::plus>(this).apply(value); }
Value Value::operator+(const std::string &value) { return Arithmetic<std::plus>(this).apply(value); }
Value Value::operator+(const char *value) { return Arithmetic<std::plus>(this).apply(value); }
+Value Value::operator+(const HardCoded &value) { return Arithmetic<std::plus>(this).apply(value); }
Value Value::operator+(double value) { return Arithmetic<std::plus>(this).apply(value); }
/**
@@ -787,6 +824,7 @@ Value Value::operator-(bool value) { return Arithmetic<std::min
Value Value::operator-(char value) { return Arithmetic<std::minus>(this).apply(value); }
Value Value::operator-(const std::string &value) { return Arithmetic<std::minus>(this).apply(value); }
Value Value::operator-(const char *value) { return Arithmetic<std::minus>(this).apply(value); }
+Value Value::operator-(const HardCoded &value) { return Arithmetic<std::minus>(this).apply(value); }
Value Value::operator-(double value) { return Arithmetic<std::minus>(this).apply(value); }
/**
@@ -802,6 +840,7 @@ Value Value::operator*(bool value) { return Arithmetic<std::mul
Value Value::operator*(char value) { return Arithmetic<std::multiplies>(this).apply(value); }
Value Value::operator*(const std::string &value) { return Arithmetic<std::multiplies>(this).apply(value); }
Value Value::operator*(const char *value) { return Arithmetic<std::multiplies>(this).apply(value); }
+Value Value::operator*(const HardCoded &value) { return Arithmetic<std::multiplies>(this).apply(value); }
Value Value::operator*(double value) { return Arithmetic<std::multiplies>(this).apply(value); }
/**
@@ -817,6 +856,7 @@ Value Value::operator/(bool value) { return Arithmetic<std::div
Value Value::operator/(char value) { return Arithmetic<std::divides>(this).apply(value); }
Value Value::operator/(const std::string &value) { return Arithmetic<std::divides>(this).apply(value); }
Value Value::operator/(const char *value) { return Arithmetic<std::divides>(this).apply(value); }
+Value Value::operator/(const HardCoded &value) { return Arithmetic<std::divides>(this).apply(value); }
Value Value::operator/(double value) { return Arithmetic<std::divides>(this).apply(value); }
/**
@@ -832,6 +872,7 @@ Value Value::operator%(bool value) { return Value(numericValue(
Value Value::operator%(char value) { return Value(numericValue() % value); }
Value Value::operator%(const std::string &value) { return Value(numericValue() % atoi(value.c_str())); }
Value Value::operator%(const char *value) { return Value(numericValue() % atoi(value)); }
+Value Value::operator%(const HardCoded &value) { return Value(numericValue() % atoi(value.buffer())); }
Value Value::operator%(double value) { return Value(numericValue() % (int)value); }
/**
@@ -1355,27 +1396,6 @@ bool Value::isCallable() const
}
/**
- * Is the variable empty?
- * @return bool
- */
-bool Value::isEmpty() const
-{
- switch (type()) {
- case Type::Null: return true;
- case Type::Numeric: return numericValue() == 0;
- case Type::Float: return floatValue() == 0.0;
- case Type::Bool: return boolValue() == false;
- case Type::Array: return size() == 0;
- case Type::Object: return false;
- case Type::String: return size() == 0;
- case Type::Callable: return false;
- default: return false;
- }
-
- // for the time being we consider resources and consts to be not-empty
-}
-
-/**
* Make a clone of the type
* @return Value
*/
@@ -1451,15 +1471,28 @@ std::string Value::stringValue() const
}
/**
- * Retrieve raw string value
+ * Access to the raw buffer
+ * @return char *
+ */
+char *Value::buffer() const
+{
+ // must be a string
+ if (!isString()) return nullptr;
+
+ // already a string?
+ return Z_STRVAL_P(_val);
+}
+
+/**
+ * Access to the raw buffer
* @return const char *
*/
const char *Value::rawValue() const
{
- // already a string?
+ // must be a string
if (isString()) return Z_STRVAL_P(_val);
- // make a clone
+ // make a clone and return that buffer
return clone(Type::String).rawValue();
}