diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-15 18:31:42 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-15 18:31:42 +0100 |
commit | 3bcd6e21d1142b5ec35f99c4bdcd925bf7ae5083 (patch) | |
tree | 08903d9b7e7b2bd57fa1a01fc873d618a17b0a8f /include | |
parent | 38b866988761f4da01eab769dc660b06b07e97be (diff) |
added empty() function, and added HardCoded class
Diffstat (limited to 'include')
-rw-r--r-- | include/call.h | 10 | ||||
-rw-r--r-- | include/hashmember.h | 20 | ||||
-rw-r--r-- | include/init.h | 23 | ||||
-rw-r--r-- | include/value.h | 57 |
4 files changed, 78 insertions, 32 deletions
diff --git a/include/call.h b/include/call.h index bae0a0a..7cf68bb 100644 --- a/include/call.h +++ b/include/call.h @@ -44,8 +44,14 @@ Value call(const char *name, Params&&... params) * make the code run on the highway), it is not expected that these functions * are going to be used very often anyway. */ -inline Value array_keys(const Value &value) { return call("array_keys", value); } -inline Value array_values(const Value &value) { return call("array_values", value); } +inline Value array_keys(const Value &value) { return call("array_keys", value); } +inline Value array_values(const Value &value) { return call("array_values", value); } +inline Value empty(const Value &value) { return call("empty", value); } +inline Value empty(const HashMember<std::string> &member) { return !member.exists() || empty(member.value()); } +inline Value empty(const HashMember<int> &member) { return !member.exists() || empty(member.value()); } +//inline Value isset(const Value &value) { return call("isset", value); } +//inline Value isset(const HashMember<std::string> &member) { return member.exists() && isset(member.value()); } +//inline Value isset(const HashMember<int> &member) { return member.exists() && isset(member.value()); } /** * End of namespace diff --git a/include/hashmember.h b/include/hashmember.h index 41c40d3..d61e5c9 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -53,6 +53,16 @@ public: } /** + * Is this an existing hash member (true) or only one that is ready + * to be assigned a new value to, but that is not yet in the hashtable + * @return bool + */ + bool exists() const + { + return _base->contains(_index); + } + + /** * Retrieve the original value * @return Value */ @@ -179,6 +189,7 @@ public: HashMember &operator+=(char value) { return operator=(this->value() + value); } HashMember &operator+=(const std::string &value) { return operator=(this->value() + value); } HashMember &operator+=(const char *value) { return operator=(this->value() + value); } + HashMember &operator+=(const HardCoded &value) { return operator=(this->value() + value); } HashMember &operator+=(double value) { return operator=(this->value() + value); } /** @@ -194,6 +205,7 @@ public: HashMember &operator-=(char value) { return operator=(this->value() - value); } HashMember &operator-=(const std::string &value) { return operator=(this->value() - value); } HashMember &operator-=(const char *value) { return operator=(this->value() - value); } + HashMember &operator-=(const HardCoded &value) { return operator=(this->value() - value); } HashMember &operator-=(double value) { return operator=(this->value() - value); } /** @@ -209,6 +221,7 @@ public: HashMember &operator*=(char value) { return operator=(this->value() * value); } HashMember &operator*=(const std::string &value) { return operator=(this->value() * value); } HashMember &operator*=(const char *value) { return operator=(this->value() * value); } + HashMember &operator*=(const HardCoded &value) { return operator=(this->value() * value); } HashMember &operator*=(double value) { return operator=(this->value() * value); } /** @@ -224,6 +237,7 @@ public: HashMember &operator/=(char value) { return operator=(this->value() / value); } HashMember &operator/=(const std::string &value) { return operator=(this->value() / value); } HashMember &operator/=(const char *value) { return operator=(this->value() / value); } + HashMember &operator/=(const HardCoded &value) { return operator=(this->value() / value); } HashMember &operator/=(double value) { return operator=(this->value() / value); } /** @@ -239,6 +253,7 @@ public: HashMember &operator%=(char value) { return operator=(this->value() % value); } HashMember &operator%=(const std::string &value) { return operator=(this->value() % value); } HashMember &operator%=(const char *value) { return operator=(this->value() % value); } + HashMember &operator%=(const HardCoded &value) { return operator=(this->value() % value); } HashMember &operator%=(double value) { return operator=(this->value() % value); } /** @@ -254,6 +269,7 @@ public: Value operator+(char value) { return this->value() + value; } Value operator+(const std::string &value) { return this->value() + value; } Value operator+(const char *value) { return this->value() + value; } + Value operator+(const HardCoded &value) { return this->value() + value; } Value operator+(double value) { return this->value() + value; } /** @@ -269,6 +285,7 @@ public: Value operator-(char value) { return this->value() - value; } Value operator-(const std::string &value) { return this->value() - value; } Value operator-(const char *value) { return this->value() - value; } + Value operator-(const HardCoded &value) { return this->value() - value; } Value operator-(double value) { return this->value() - value; } /** @@ -284,6 +301,7 @@ public: Value operator*(char value) { return this->value() * value; } Value operator*(const std::string &value) { return this->value() * value; } Value operator*(const char *value) { return this->value() * value; } + Value operator*(const HardCoded &value) { return this->value() * value; } Value operator*(double value) { return this->value() * value; } /** @@ -299,6 +317,7 @@ public: Value operator/(char value) { return this->value() / value; } Value operator/(const std::string &value) { return this->value() / value; } Value operator/(const char *value) { return this->value() / value; } + Value operator/(const HardCoded &value) { return this->value() / value; } Value operator/(double value) { return this->value() / value; } /** @@ -314,6 +333,7 @@ public: Value operator%(char value) { return this->value() % value; } Value operator%(const std::string &value) { return this->value() % value; } Value operator%(const char *value) { return this->value() % value; } + Value operator%(const HardCoded &value) { return this->value() % value; } Value operator%(double value) { return this->value() % value; } /** diff --git a/include/init.h b/include/init.h index ca6ff65..8f914f5 100644 --- a/include/init.h +++ b/include/init.h @@ -14,26 +14,23 @@ namespace Php { /** - * The way how PHP C API deals with "global" variables is stupid. + * The way how PHP C API deals with "global" variables is peculiar. * - * This is supposed to turn into a structure that is going to be - * instantiated for each parallel running request, and for which the - * PHP engine allocates a certain amount of memory, and a magic - * pointer that is passed and should be forwarded to every thinkable - * PHP function. + * The following macros are supposed to turn into a structure that is going + * to be instantiated for each parallel running request, and for which the + * PHP engine allocates a certain amount of memory, and a magic pointer that + * is passed and should be forwarded to every thinkable PHP function. * - * We don't like this architecture. We have our own environment object - * that makes much more sense, and that we use. However, we need - * to assign this object somewhere, so that's what we do in this - * one and only global variable + * We don't use this architecture. We have our own environment object + * that makes much more sense, and that we use. However, the Zend engine + * expects this structure and this structure to exist. */ ZEND_BEGIN_MODULE_GLOBALS(phpcpp) ZEND_END_MODULE_GLOBALS(phpcpp) /** - * And now we're going to define a macro. This also is a ridiculous - * architecture from PHP to get access to a variable from the - * structure above. + * And now we're going to define a macro. This also is a uncommon architecture + * from PHP to get access to a variable from the structure above. */ #ifdef ZTS #define PHPCPP_G(v) TSRMG(phpcpp_globals_id, phpcpp_globals *, v) diff --git a/include/value.h b/include/value.h index fce8750..13cea38 100644 --- a/include/value.h +++ b/include/value.h @@ -57,6 +57,7 @@ public: Value(char value); Value(const std::string &value); Value(const char *value, int size = -1); + Value(const HardCoded &value); Value(double value); /** @@ -161,6 +162,7 @@ public: Value &operator=(char value); Value &operator=(const std::string &value); Value &operator=(const char *value); + Value &operator=(const HardCoded &value); Value &operator=(double value); /** @@ -176,6 +178,7 @@ public: Value &operator+=(char value); Value &operator+=(const std::string &value); Value &operator+=(const char *value); + Value &operator+=(const HardCoded &value); Value &operator+=(double value); /** @@ -191,6 +194,7 @@ public: Value &operator-=(char value); Value &operator-=(const std::string &value); Value &operator-=(const char *value); + Value &operator-=(const HardCoded &value); Value &operator-=(double value); /** @@ -206,6 +210,7 @@ public: Value &operator*=(char value); Value &operator*=(const std::string &value); Value &operator*=(const char *value); + Value &operator*=(const HardCoded &value); Value &operator*=(double value); /** @@ -221,6 +226,7 @@ public: Value &operator/=(char value); Value &operator/=(const std::string &value); Value &operator/=(const char *value); + Value &operator/=(const HardCoded &value); Value &operator/=(double value); /** @@ -236,6 +242,7 @@ public: Value &operator%=(char value); Value &operator%=(const std::string &value); Value &operator%=(const char *value); + Value &operator%=(const HardCoded &value); Value &operator%=(double value); /** @@ -251,6 +258,7 @@ public: Value operator+(char value); Value operator+(const std::string &value); Value operator+(const char *value); + Value operator+(const HardCoded &value); Value operator+(double value); /** @@ -266,6 +274,7 @@ public: Value operator-(char value); Value operator-(const std::string &value); Value operator-(const char *value); + Value operator-(const HardCoded &value); Value operator-(double value); /** @@ -281,6 +290,7 @@ public: Value operator*(char value); Value operator*(const std::string &value); Value operator*(const char *value); + Value operator*(const HardCoded &value); Value operator*(double value); /** @@ -296,6 +306,7 @@ public: Value operator/(char value); Value operator/(const std::string &value); Value operator/(const char *value); + Value operator/(const HardCoded &value); Value operator/(double value); /** @@ -311,18 +322,19 @@ public: Value operator%(char value); Value operator%(const std::string &value); Value operator%(const char *value); + Value operator%(const HardCoded &value); Value operator%(double value); /** * Comparison operators for hardcoded strings * @param value */ - bool operator==(const char *value) const { return stringValue() == value; } - bool operator!=(const char *value) const { return stringValue() != value; } - bool operator<=(const char *value) const { return stringValue() <= value; } - bool operator>=(const char *value) const { return stringValue() >= value; } - bool operator< (const char *value) const { return stringValue() < value; } - bool operator> (const char *value) const { return stringValue() > value; } + bool operator==(const char *value) const { return ::strcmp(rawValue(), value) == 0; } + bool operator!=(const char *value) const { return ::strcmp(rawValue(), value) != 0; } + bool operator<=(const char *value) const { return ::strcmp(rawValue(), value) <= 0; } + bool operator>=(const char *value) const { return ::strcmp(rawValue(), value) >= 0; } + bool operator< (const char *value) const { return ::strcmp(rawValue(), value) < 0; } + bool operator> (const char *value) const { return ::strcmp(rawValue(), value) > 0; } /** * Comparison operators @@ -372,12 +384,20 @@ public: bool isObject() const { return type() == Type::Object; } bool isArray() const { return type() == Type::Array; } bool isCallable() const; + + /** + * Get access to the raw buffer - you can use this for direct reading and + * writing to and from the buffer. Note that this only works for string + * variables - other variables return nullptr + * @return char * + */ + char *buffer() const; /** - * Is the variable empty? - * @return bool + * Get access to the raw buffer for read operationrs. + * @return const char * */ - bool isEmpty() const; + const char *rawValue() const; /** * Retrieve the value as number @@ -398,14 +418,6 @@ public: bool boolValue() const; /** - * Retrieve the raw string value - * Warning: Only use this for NULL terminated strings, or use it in combination - * with the string size to prevent that you access data outside the buffer - * @return const char * - */ - const char *rawValue() const; - - /** * Retrieve the value as a string * @return string */ @@ -529,6 +541,17 @@ public: * @return bool */ bool contains(const char *key, int size) const; + + /** + * Is a certain key set in the array + * @param key + * @param size + * @return bool + */ + bool contains(const char *key) const + { + return contains(key, strlen(key)); + } /** * Cast to a number |