From e72cd10803a020d762db6a7073420294241f690e Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Wed, 28 Aug 2013 00:32:51 +0200 Subject: Initial implementation of the variable class --- README.md | 1 - include/variable.h | 119 ++++++++++++++++++++-- src/variable.cpp | 286 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 396 insertions(+), 10 deletions(-) create mode 100644 src/variable.cpp diff --git a/README.md b/README.md index aec985d..7f98600 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,4 @@ PHP-CPP is an initiave from Copernica BV. For more information, contact me at emiel.bruijntjes@copernica.com. - Emiel Bruijntjes (25 August 2013) diff --git a/include/variable.h b/include/variable.h index 393239f..fccab9c 100644 --- a/include/variable.h +++ b/include/variable.h @@ -7,6 +7,11 @@ * @copyright 2013 Copernica BV */ +/** + * Forward definitions + */ +struct _zval_struct; + /** * Set up namespace */ @@ -47,6 +52,12 @@ public: */ Variable(double value); + /** + * Wrap object around zval + * @param zval + */ + Variable(struct _zval_struct *zval); + /** * Copy constructor * @param value @@ -64,54 +75,100 @@ public: * @return Variable */ virtual Variable &operator=(const Variable &value); + + /** + * Assignment operator + * @param value + * @return Variable + */ + Variable &operator=(int value); + + /** + * Assignment operator + * @param value + * @return Variable + */ + Variable &operator=(bool value); + + /** + * Assignment operator + * @param value + * @return Variable + */ + Variable &operator=(const std::string &value); + + /** + * Assignment operator + * @param value + * @return Variable + */ + Variable &operator=(double value); + + /** + * The type of object + * @return Type + */ + Type type(); + + /** + * Change the internal type of the variable + * @param Type + */ + void setType(Type type); + + /** + * Is this a NULL value? + * @return bool + */ + bool isNull(); /** * Is this an integer value? * @return bool */ - virtual bool isInt(); + bool isInt(); /** * Is this a boolean value? * @return bool */ - virtual bool isBool(); + bool isBool(); /** * Is this a string value? * @return bool */ - virtual bool isString(); + bool isString(); /** * Is this a decimal value? * @return bool */ - virtual bool isDecimal(); + bool isDecimal(); /** * Is this an object value? * @return bool */ - virtual bool isObject(); + bool isObject(); /** * Is this an array value? * @return bool */ - virtual bool isArray(); + bool isArray(); /** * Retrieve the value as integer * @return int */ - virtual int intValue(); + int intValue(); /** * Retrieve the value as boolean * @return bool */ - virtual bool boolValue(); + bool boolValue(); /** * Retrieve the value as string @@ -123,7 +180,51 @@ public: * Retrieve the value as decimal * @return double */ - virtual double decimalValue(); + double decimalValue(); + + /** + * Cast to an int + * @return int + */ + operator int () + { + return intValue(); + } + + /** + * Cast to a boolean + * @return boolean + */ + operator bool () + { + return boolValue(); + } + + /** + * Cast to a string + * @return string + */ + operator std::string () + { + return stringValue(); + } + + /** + * Cast to a floating point + * @return double + */ + operator double () + { + return decimalValue(); + } + +protected: + /** + * The wrapped zval + * @var struct zval + */ + struct _zval_struct *_val; + }; /** diff --git a/src/variable.cpp b/src/variable.cpp new file mode 100644 index 0000000..4735267 --- /dev/null +++ b/src/variable.cpp @@ -0,0 +1,286 @@ +/** + * Variable.cpp + * + * Implementation for the Variable class, which wraps a PHP userspace + * variable (a 'zval' in Zend's terminology) into a C++ object + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace PhpCpp { + +/** + * Constructor (value = NULL) + */ +Variable::Variable() +{ + // create a null zval + MAKE_STD_ZVAL(_val); + ZVAL_NULL(_val); +} + +/** + * Constructor based on integer value + * @param value + */ +Variable::Variable(int value) +{ + // create an integer zval + MAKE_STD_ZVAL(_val); + ZVAL_LONG(_val, value); +} + +/** + * Constructor based on boolean value + * @param value + */ +Variable::Variable(bool value) +{ + // create a boolean zval + MAKE_STD_ZVAL(_val); + ZVAL_BOOL(_val, value); +} + +/** + * Constructor based on string value + * @param value + */ +Variable::Variable(const std::string &value) +{ + // create a string zval + MAKE_STD_ZVAL(_val); + ZVAL_STRINGL(_val, value.c_str(), value.size(), 1); +} + +/** + * Constructor based on decimal value + * @param value + */ +Variable::Variable(double value) +{ + // create a double zval + MAKE_STD_ZVAL(_val); + ZVAL_DOUBLE(_val, value); +} + +/** + * Wrap object around zval + * @param zval + */ +Variable::Variable(struct _zval_struct *zval) +{ + // just copy the zval into this object + _val = zval; +} + +/** + * Copy constructor + * @param value + */ +Variable::Variable(const Variable &that) +{ + // @todo implementation, what should we do + + +} + +/** + * Destructor + */ +Variable::~Variable() +{ + // @todo implementation + +} + +/** + * Assignment operator + * @param value + * @return Variable + */ +Variable &Variable::operator=(const Variable &value) +{ + // skip self assignment + if (this == &value) return *this; + + // @todo implementation + + + // done + return *this; +} + +/** + * Assignment operator + * @param value + * @return Variable + */ +Variable &Variable::operator=(int value) +{ + // @todo implementation + + // done + return *this; +} + +/** + * Assignment operator + * @param value + * @return Variable + */ +Variable &Variable::operator=(bool value) +{ + // @todo implementation + + + // done + return *this; +} + +/** + * Assignment operator + * @param value + * @return Variable + */ +Variable &Variable::operator=(const std::string &value) +{ + // @todo implementation + + + // done + return *this; +} + +/** + * Assignment operator + * @param value + * @return Variable + */ +Variable &Variable::operator=(double value) +{ + // @todo implementation + + + // done + return *this; +} + +/** + * The type of object + * @return Type + */ +Type Variable::type() +{ + return (Type)Z_TYPE_P(_val); +} + +/** + * Change the internal type + * @param type + */ +void Variable::setType(Type type) +{ + // @todo implementation + +} + +/** + * Is this a NULL value? + * @return bool + */ +bool Variable::isNull() +{ + return Z_TYPE_P(_val) == IS_NULL; +} + +/** + * Is this an integer value? + * @return bool + */ +bool Variable::isInt() +{ + return Z_TYPE_P(_val) == IS_LONG; +} + +/** + * Is this a boolean value? + * @return bool + */ +bool Variable::isBool() +{ + return Z_TYPE_P(_val) == IS_BOOL; +} + +/** + * Is this a string value? + * @return bool + */ +bool Variable::isString() +{ + return Z_TYPE_P(_val) == IS_STRING; +} + +/** + * Is this a decimal value? + * @return bool + */ +bool Variable::isDecimal() +{ + return Z_TYPE_P(_val) == IS_DOUBLE; +} + +/** + * Is this an object value? + * @return bool + */ +bool Variable::isObject() +{ + return Z_TYPE_P(_val) == IS_OBJECT; +} + +/** + * Is this an array value? + * @return bool + */ +bool Variable::isArray() +{ + return Z_TYPE_P(_val) == IS_ARRAY; +} + +/** + * Retrieve the value as integer + * @return int + */ +int Variable::intValue() +{ + // @todo implementation +} + +/** + * Retrieve the value as boolean + * @return bool + */ +bool Variable::boolValue() +{ + // @todo implementation +} + +/** + * Retrieve the value as string + * @return string + */ +std::string Variable::stringValue() +{ + // @todo implementation +} + +/** + * End of namespace + */ +} + -- cgit v1.2.3