diff options
-rw-r--r-- | include/hardcoded.h | 89 | ||||
-rw-r--r-- | include/hashmember.h | 10 | ||||
-rw-r--r-- | include/value.h | 30 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/arithmetic.h | 11 | ||||
-rw-r--r-- | src/includes.h | 1 | ||||
-rw-r--r-- | src/value.cpp | 76 |
7 files changed, 43 insertions, 175 deletions
diff --git a/include/hardcoded.h b/include/hardcoded.h deleted file mode 100644 index f48fc61..0000000 --- a/include/hardcoded.h +++ /dev/null @@ -1,89 +0,0 @@ -/** - * HardCoded.h - * - * Small class that can be wrapped around a "hardcoded string". Normally, the - * Value object always makes a full copy of a string, because the value object - * may exist for a longer period than the pointer-to-a-string that is wrapped - * in it. - * - * However, in some situations it is already certain that the buffer in - * which the original string is stored will outlive the Value object. This is - * for example true for hardcoded strings. Such const-char* can be wrapped into - * a Php::HardCoded instance before they are assigned to a Php::Value object - * - * This class is called HardCoded because it is often used for hardcoded - * strings, but you can use it for other values as well. - * - * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @copyright 2014 Copernica BV - */ - -/** - * Php namespace - */ -namespace Php { - -/** - * Class definition - */ -class HardCoded -{ -public: - /** - * Constructors - * - * The class has a regular constructor with a size or not, a copy constructor - * and move constructor. - * - * @param buffer - * @param size - * @param that - */ - HardCoded(const char *buffer, size_t size) : _buffer(buffer), _size(size) {} - HardCoded(const char *buffer) : _buffer(buffer), _size(strlen(buffer)) {} - HardCoded(const char buffer) : _buffer(&buffer), _size(1) {} - HardCoded(const HardCoded &that) : _buffer(that._buffer), _size(that._size) {} - HardCoded(HardCoded &&that) : _buffer(that._buffer), _size(that._size) {} - - /** - * Destructor - */ - virtual ~HardCoded() {} - - /** - * Method to get access to the buffer - * @return const char * - */ - const char *buffer() const - { - return _buffer; - } - - /** - * Size of the buffer - * @return size_t - */ - size_t size() const - { - return _size; - } - -private: - /** - * The actual buffer - * @var const char * - */ - const char *_buffer; - - /** - * Size of the buffer - * @var size_t - */ - size_t _size; -}; - -/** - * End namespace - */ -} - diff --git a/include/hashmember.h b/include/hashmember.h index d61e5c9..b8f4dcd 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -189,7 +189,6 @@ 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); } /** @@ -205,7 +204,6 @@ 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); } /** @@ -221,7 +219,6 @@ 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); } /** @@ -237,7 +234,6 @@ 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); } /** @@ -253,7 +249,6 @@ 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); } /** @@ -269,7 +264,6 @@ 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; } /** @@ -285,7 +279,6 @@ 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; } /** @@ -301,7 +294,6 @@ 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; } /** @@ -317,7 +309,6 @@ 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; } /** @@ -333,7 +324,6 @@ 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/value.h b/include/value.h index 13cea38..ed0367c 100644 --- a/include/value.h +++ b/include/value.h @@ -57,7 +57,6 @@ public: Value(char value); Value(const std::string &value); Value(const char *value, int size = -1); - Value(const HardCoded &value); Value(double value); /** @@ -162,7 +161,6 @@ 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); /** @@ -178,7 +176,6 @@ 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); /** @@ -194,7 +191,6 @@ 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); /** @@ -210,7 +206,6 @@ 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); /** @@ -226,7 +221,6 @@ 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); /** @@ -242,7 +236,6 @@ 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); /** @@ -258,7 +251,6 @@ 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); /** @@ -274,7 +266,6 @@ 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); /** @@ -290,7 +281,6 @@ 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); /** @@ -306,7 +296,6 @@ 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); /** @@ -322,7 +311,6 @@ 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); /** @@ -388,12 +376,28 @@ public: /** * 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 + * variables - other variables return nullptr. + * + * If you are going to write to the buffer, make sure that you first call + * the reserve() method to ensure that the buffer is big enough. + * * @return char * */ char *buffer() const; /** + * Reserve enough space in the buffer. If you want to write directly to + * the buffer (which is returned by the buffer() method), you should first + * reserve enough space in it. This can be done with this reserve() method. + * This will also turn the Value object into a string (if it was not + * already a string). The writable buffer is returned. + * + * @param size + * @return char* + */ + char *reserve(size_t size); + + /** * Get access to the raw buffer for read operationrs. * @return const char * */ @@ -27,7 +27,6 @@ */ #include <phpcpp/exception.h> #include <phpcpp/type.h> -#include <phpcpp/hardcoded.h> #include <phpcpp/value.h> #include <phpcpp/array.h> #include <phpcpp/object.h> diff --git a/src/arithmetic.h b/src/arithmetic.h index 00d5641..20e346e 100644 --- a/src/arithmetic.h +++ b/src/arithmetic.h @@ -144,17 +144,6 @@ public: * @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 - * @param value - * @return Value - */ Value apply(double value) { return Value(F<double>()(_value->floatValue(), value)); diff --git a/src/includes.h b/src/includes.h index 55aa103..0c00df3 100644 --- a/src/includes.h +++ b/src/includes.h @@ -46,7 +46,6 @@ */ #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 7ce49fe..7787538 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -22,7 +22,7 @@ * * * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @copyright 2013 Copernica BV + * @copyright 2013, 2014 Copernica BV */ #include "includes.h" @@ -130,17 +130,6 @@ 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 */ @@ -192,6 +181,7 @@ Value::Value(HashTable *ht) Z_TYPE_P(_val) = IS_ARRAY; // add a reference + // @todo this may be wrong Z_ADDREF_P(_val); } @@ -240,6 +230,8 @@ Value::Value(const Value &that) // to the variable but have to allocate a new variable ALLOC_ZVAL(_val); INIT_PZVAL_COPY(_val, that._val); + + // we have to call the copy constructor to copy the entire other zval zval_copy_ctor(_val); } else @@ -288,11 +280,8 @@ Value::Value(const Value &that) * Move constructor * @param value */ -Value::Value(Value &&that) +Value::Value(Value &&that) : _val(that._val) { - // just copy the zval - _val = that._val; - // clear the other object that._val = nullptr; } @@ -309,7 +298,7 @@ Value::~Value() // and only one reference will remain, the object will then impossible be // a reference if (Z_REFCOUNT_P(_val) <= 2) Z_UNSET_ISREF_P(_val); - + // destruct the zval (this function will decrement the reference counter, // and only destruct if there are no other references left) zval_ptr_dtor(&_val); @@ -417,9 +406,6 @@ Value &Value::operator=(Value &&value) // the current refcount int refcount = Z_REFCOUNT_P(_val); - // clean up the current zval (but keep the zval structure) - zval_dtor(_val); - // make the copy *_val = *value._val; @@ -674,26 +660,6 @@ 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 @@ -727,7 +693,6 @@ 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); } /** @@ -743,7 +708,6 @@ 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); } /** @@ -759,7 +723,6 @@ 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); } /** @@ -775,7 +738,6 @@ 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); } /** @@ -792,7 +754,6 @@ 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); } /** @@ -808,7 +769,6 @@ 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); } /** @@ -824,7 +784,6 @@ 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); } /** @@ -840,7 +799,6 @@ 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); } /** @@ -856,7 +814,6 @@ 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); } /** @@ -872,7 +829,6 @@ 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); } /** @@ -1484,6 +1440,26 @@ char *Value::buffer() const } /** + * Reserve enough space + * @param size + * @return char* + */ +char *Value::reserve(size_t size) +{ + // must be a string + setType(Type::String); + + // leap ouf it the size if already big enough + if (Z_STRLEN_P(_val) >= (int)size) return Z_STRVAL_P(_val); + + // is there already a buffer? + if (!Z_STRVAL_P(_val)) return Z_STRVAL_P(_val) = (char *)emalloc(size); + + // reallocate an existing buffer + return Z_STRVAL_P(_val) = (char *)erealloc(Z_STRVAL_P(_val), size); +} + +/** * Access to the raw buffer * @return const char * */ |