diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/member.h | 236 | ||||
-rw-r--r-- | include/value.h | 204 |
2 files changed, 402 insertions, 38 deletions
diff --git a/include/member.h b/include/member.h new file mode 100644 index 0000000..86e3554 --- /dev/null +++ b/include/member.h @@ -0,0 +1,236 @@ +/** + * Member.h + * + * When you're accessing members in an array or an object, you're + * doing so via an internal member object. This is an object that + * keeps track of the array to which it belonged, and that updates + * the class when the object is modified + * + * This is an abstract class. You are not supposed to instantiate it + * yourself. An instance of it is created when you call Value::operator[] + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Forward definitions + */ +class Value; + +/** + * Member class + */ +template <class Type> +class Member +{ +public: + /** + * Destructor + */ + virtual ~Member() {} + + /** + * Assign a numeric value + * @param value + * @return Member + */ + Member &operator=(long value) + { + _value->set(_index, value); + return *this; + } + + /** + * Assign a numeric value + * @param value + * @return Member + */ + Member &operator=(int value) + { + _value->set(_index, value); + return *this; + } + + /** + * Assign a double value + * @param value + * @return Member + */ + Member &operator=(double value) + { + _value->set(_index, value); + return *this; + } + + /** + * Assign a boolean value + * @param value + * @return Member + */ + Member &operator=(bool value) + { + _value->set(_index, value); + return *this; + } + + /** + * Assign a string value + * @param value + * @return Member + */ + Member &operator=(const std::string &value) + { + _value->set(_index, value); + return *this; + } + + /** + * Assign a byte array value + * @param value + * @return Member + */ + Member &operator=(const char *value) + { + _value->set(_index, value); + return *this; + } + + /** + * Retrieve the original value + * @return Value + */ + Value value() const + { + return _value->get(_index); + } + + /** + * Cast to a value object + * @return Value + */ + operator Value () const + { + return _value->get(_index); + } + + /** + * Cast to a long + * @return long + */ + operator long () const + { + return _value->get(_index).longValue(); + } + + /** + * Cast to a boolean + * @return boolean + */ + operator bool () const + { + return _value->get(_index).boolValue(); + } + + /** + * Cast to a string + * @return string + */ + operator std::string () const + { + return _value->get(_index).stringValue(); + } + + /** + * Cast to byte array + * @return const char * + */ + operator const char * () const + { + return _value->get(_index).rawValue(); + } + + /** + * Cast to a floating point + * @return double + */ + operator double () const + { + return _value->get(_index).decimalValue(); + } + + /** + * Array access operator + * This can be used for accessing arrays + * @param index + * @return Member + */ + Member operator[](int index) + { + return _value->get(_index)[index]; + } + + /** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Member + */ + Member operator[](const std::string &key) + { + return _value->get(_index)[key]; + } + + /** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Member + */ + Member operator[](const char *key) + { + return _value->get(_index)[key]; + } + +private: + /** + * Constructor + * @param value Parent element + * @param index Index in the array + */ + Member(Value *value, Type index) : _value(value), _index(index) {} + + /** + * Private copy constructor + * @param value Other element + */ + Member(const Member<Type> &member) : _value(member._value), _index(member._index) {} + + /** + * The array of which this is a member + * @var Value + */ + Value *_value; + + /** + * The original index + * @var Type + */ + Type _index; + + /** + * Value objects may create members + */ + friend class Value; +}; + +/** + * End of namespace + */ +} + diff --git a/include/value.h b/include/value.h index bc25eab..6619dcb 100644 --- a/include/value.h +++ b/include/value.h @@ -29,6 +29,11 @@ struct _zval_struct; namespace PhpCpp { /** + * Forward definitions + */ +template <class Type> class Member; + +/** * Class definition */ class Value @@ -38,7 +43,7 @@ public: * Empty constructor (value = NULL) */ Value(); - + /** * Constructor based on integer value * @param value @@ -46,6 +51,12 @@ public: Value(int value); /** + * Constructor based on integer value + * @param value + */ + Value(long value); + + /** * Constructor based on boolean value * @param value */ @@ -58,6 +69,13 @@ public: Value(const std::string &value); /** + * Constructor based on byte buffer + * @param value + * @param size + */ + Value(const char *value, int size = -1); + + /** * Constructor based on decimal value * @param value */ @@ -93,6 +111,13 @@ public: * @param value * @return Value */ + Value &operator=(long value); + + /** + * Assignment operator + * @param value + * @return Value + */ Value &operator=(int value); /** @@ -114,13 +139,20 @@ public: * @param value * @return Value */ + Value &operator=(const char *value); + + /** + * Assignment operator + * @param value + * @return Value + */ Value &operator=(double value); /** * The type of object * @return Type */ - Type type(); + Type type() const; /** * Change the internal type of the variable @@ -132,79 +164,87 @@ public: * Is this a NULL value? * @return bool */ - bool isNull(); + bool isNull() const; /** * Is this an integer value? * @return bool */ - bool isInt(); + bool isLong() const; /** * Is this a boolean value? * @return bool */ - bool isBool(); + bool isBool() const; /** * Is this a string value? * @return bool */ - bool isString(); + bool isString() const; /** * Is this a decimal value? * @return bool */ - bool isDecimal(); + bool isDecimal() const; /** * Is this an object value? * @return bool */ - bool isObject(); + bool isObject() const; /** * Is this an array value? * @return bool */ - bool isArray(); + bool isArray() const; /** * Retrieve the value as integer * @return int */ - int intValue(); + long longValue() const; /** * Retrieve the value as boolean * @return bool */ - bool boolValue(); + bool boolValue() const; /** - * Retrieve the value as string + * 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 */ - std::string stringValue(); + std::string stringValue() const; /** * Retrieve the value as decimal * @return double */ - double decimalValue(); + double decimalValue() const; /** * The number of members in case of an array or object * @return int */ - int size(); + int size() const; /** * The number of members in case of an array or object * @return int */ - int count() + int count() const { return size(); } @@ -213,25 +253,47 @@ public: * The number of members in case of an array or object * @return int */ - int length() + int length() const { return size(); } /** - * Cast to an int - * @return int + * Is a certain index set in the array + * @param index + * @return bool + */ + bool contains(int index) const; + + /** + * Is a certain key set in the array + * @param key + * @return bool + */ + bool contains(const std::string &key) const; + + /** + * Is a certain key set in the array + * @param key + * @param size + * @return bool + */ + bool contains(const char *key, int size) const; + + /** + * Cast to a long + * @return long */ - operator int () + operator long () const { - return intValue(); + return longValue(); } /** * Cast to a boolean * @return boolean */ - operator bool () + operator bool () const { return boolValue(); } @@ -240,46 +302,112 @@ public: * Cast to a string * @return string */ - operator std::string () + operator std::string () const { return stringValue(); } /** + * Cast to byte array + * @return const char * + */ + operator const char * () const + { + return rawValue(); + } + + /** * Cast to a floating point * @return double */ - operator double () + operator double () const { return decimalValue(); } /** + * Get access to a certain array member + * @param index + * @return Value + */ + Value get(int index) const; + + /** + * Get access to a certain assoc member + * @param key + * @param size + * @return Value + */ + Value get(const char *key, int size=-1) const; + + /** + * Get access to a certain assoc member + * @param key + * @return Value + */ + Value get(const std::string &key) const + { + return get(key.c_str(), key.size()); + } + + /** + * Set a certain property + * @param index + * @param value + */ + void set(int index, const Value &value); + + /** + * Set a certain property + * @param key + * @param value + */ + void set(const char *key, int size, const Value &value); + + /** + * Set a certain property + * @param key + * @param size + * @param value + */ + void set(const char *key, const Value &value) + { + set(key, strlen(key), value); + } + + /** + * Set a certain property + * @param key + * @param value + */ + void set(const std::string &key, const Value &value) + { + set(key.c_str(), key.size(), value); + } + + /** * Array access operator * This can be used for accessing arrays - * Be aware: if the 'this' object is not already an array, it will be converted into one! * @param index - * @return Value + * @return Member */ - Value operator[](int index); + Member<int> operator[](int index); /** * Array access operator * This can be used for accessing associative arrays - * Be aware: if the 'this' object is not already an array, it will be converted into one! * @param key - * @return Value + * @return Member */ - Value operator[](const std::string &key); + Member<std::string> operator[](const std::string &key); - /** - * Array access operator - * This can be used for adding a record to the array - * Be aware: if the 'this' object is not already an array, it will be converted into one! - * @param key - * @return Value - */ - //Value operator[](); + /** + * Array access operator + * This can be used for accessing associative arrays + * @param key + * @return Member + */ + Member<std::string> operator[](const char *key); protected: |