From 52fe0c39457421e075959179ee6b64a20b96f0d9 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sun, 2 Mar 2014 11:33:53 +0100 Subject: types are not a C++11 class, introduced FixedValue class that can not change type, and implemented both Object and Array to make use of that type, implemented - but not yet tested - Base::value() method --- include/array.h | 83 ------------------------------------------- include/base.h | 48 +++++++++---------------- include/classbase.h | 13 +++++-- include/forcedvalue.h | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ include/hiddenpointer.h | 20 +++++++++++ include/type.h | 26 +++++++------- include/value.h | 14 ++++---- 7 files changed, 161 insertions(+), 137 deletions(-) delete mode 100644 include/array.h create mode 100644 include/forcedvalue.h (limited to 'include') diff --git a/include/array.h b/include/array.h deleted file mode 100644 index 6956fee..0000000 --- a/include/array.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Array.h - * - * An array is an extension to the Value class. It extends the Value class - * to initialize the variable as an array, instead of a null pointer - * - * @author Emiel Bruijntjes - * @copyright 2013 Copernica BV - */ - -/** - * Set up namespace - */ -namespace Php { - -/** - * Class definition - */ -class Array : public Value -{ -public: - /** - * Constructor - */ - Array() : Value() { setType(arrayType); } - - /** - * Copy constructor - * @param array - */ - Array(const Array &array) : Value(array) {} - - /** - * Move constructor - * @param array - */ - Array(Array &&that) : Value(std::move(that)) {} - - /** - * Copy constructor from a value object - * @param value - */ - Array(const Value &value) : Value(value) { setType(arrayType); } - - /** - * Destructor - */ - virtual ~Array() {} - - /** - * Change the internal type of the variable - * @param Type - */ - virtual Value &setType(Type type) override - { - // only possible for arrays - if (type != arrayType) return *this; - - // call base - return Value::setType(type); - } - -protected: - /** - * Validate the object - * @return Value - */ - virtual Value &validate() override - { - // make sure the value object is an array - setType(arrayType); - - // call base - return Value::validate(); - } - -}; - -/** - * End of namespace - */ -} - diff --git a/include/base.h b/include/base.h index df4076b..93ee66e 100644 --- a/include/base.h +++ b/include/base.h @@ -15,59 +15,43 @@ namespace Php { */ class Base { -public: +protected: /** * Constructor */ Base() {} +public: /** * Virtual destructor */ virtual ~Base() {} /** - * The pseudo constructor that is called from PHP after the object is constructed - * @param parameters + * Convert the object to a Php::Value object (how it is used externally) + * @return Value */ - virtual void __construct(const Parameters ¶meters) - { - // call all other possible implementations - __construct(); - } + Value value() const; - /** - * The pseudo constructor that is called from PHP after the object is constructed - */ - virtual void __construct() {} - - /** - * The pseudo destructor that is called from PHP right before the object is destructed - */ - virtual void __destruct() {} - /** * Get access to a property by name * @param string - * @return Property + * @return Value */ -// Property operator[](const char *name); - + Value operator[](const char *name) const + { + return value()[name]; + } + /** * Alternative way to access a property * @param string - * @return Property + * @return Value */ -// Property operator[](const std::string &name); - -protected: - /** - * All properties of the object - * @var Properties - */ -// Properties _properties; - -private: + Value operator[](const std::string &name) const + { + return value()[name]; + } }; diff --git a/include/classbase.h b/include/classbase.h index ad501ea..249122a 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -54,14 +54,17 @@ public: * @todo prefer move */ ClassBase(const ClassBase &that) : - _name(that._name), _type(that._type), _methods(that._methods), _members(that._members) {} + _name(that._name), _type(that._type), _methods(that._methods), + _members(that._members), _entry(nullptr) {} /** * Move constructor * @param that */ ClassBase(ClassBase &&that) : - _type(that._type), _methods(std::move(that._methods)), _members(std::move(that._members)), _entry(that._entry) + _name(std::move(that._name)), _type(that._type), + _methods(std::move(that._methods)), _members(std::move(that._members)), + _entry(that._entry) { // other entry are invalid now (not that it is used..., class objects are // only moved during extension setup, when the entry pointer has not yet @@ -164,6 +167,12 @@ private: */ std::string _name; + /** + * The comment for reflexion, with a stored pointer to ourselves + * @var char* + */ + char *_comment = nullptr; + /** * The class type (this can be values like Php::Abstract and Php::Final) * @var ClassType diff --git a/include/forcedvalue.h b/include/forcedvalue.h new file mode 100644 index 0000000..29e630a --- /dev/null +++ b/include/forcedvalue.h @@ -0,0 +1,94 @@ +/** + * ForcedValue.h + * + * The ForcedValue is a wrapper around the value class that ensures that a + * certain property always has a certain type. + * + * @author Emiel Bruijntjes + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +template +class ForcedValue : public Value +{ +public: + /** + * Constructor + */ + ForcedValue() : Value() { setType(TYPE); } + + /** + * Copy constructor + * @param that + */ + ForcedValue(const ForcedValue &that) : Value(that) {} + + /** + * Move constructor + * @param that + */ + ForcedValue(ForcedValue &&that) : Value(std::move(that)) {} + + /** + * Copy constructor from a value object + * @param value + */ + ForcedValue(const Value &value) : Value(value) { setType(TYPE); } + + /** + * Wrap object around zval + * @param zval Zval to wrap + * @param ref Force this to be a reference + */ + ForcedValue(struct _zval_struct *zval, bool ref = false) : Value(zval, ref) { setType(TYPE); } + + /** + * Destructor + */ + virtual ~ForcedValue() {} + + /** + * Change the internal type of the variable + * @param Type + */ + virtual Value &setType(Type type) override + { + // call base + return Value::setType(TYPE); + } + +protected: + /** + * Validate the object + * @return Value + */ + virtual Value &validate() override + { + // make sure the object has a valid type + setType(TYPE); + + // call base + return Value::validate(); + } + +}; + +/** + * Define for arrays and objects + */ +using Array = ForcedValue; +using Object = ForcedValue; + +/** + * End of namespace + */ +} + diff --git a/include/hiddenpointer.h b/include/hiddenpointer.h index 93fdc7c..cf6bea0 100644 --- a/include/hiddenpointer.h +++ b/include/hiddenpointer.h @@ -80,6 +80,16 @@ public: } } + /** + * Move constructor + * @param that + */ + HiddenPointer(HiddenPointer &&that) : _allocated(that._allocated), _buffer(that._buffer) + { + // the other object is no longer allocated + that._allocated = false; + } + /** * Destructor */ @@ -141,6 +151,16 @@ public: return _buffer + sizeof(Type *); } + /** + * Derefence the pointer + * @return Type* + */ + Type *operator->() const + { + // type is stored in front of the buffer + return *((Type **)_buffer); + } + private: /** * Buffer that holds both the pointer and the text - the text starts diff --git a/include/type.h b/include/type.h index 717ae90..bec0fd5 100644 --- a/include/type.h +++ b/include/type.h @@ -17,19 +17,19 @@ namespace Php { * Supported types for variables * The values are the same as the ones used internally in Zend */ -typedef enum _Type { - nullType = 0, - numericType = 1, - floatType = 2, - boolType = 3, - arrayType = 4, - objectType = 5, - stringType = 6, - resourceType = 7, - constantType = 8, - constantArrayType = 9, - callableType = 10 -} Type; +enum class Type : unsigned char { + Null = 0, + Numeric = 1, + Float = 2, + Bool = 3, + Array = 4, + Object = 5, + String = 6, + Resource = 7, + Constant = 8, + ConstantArray = 9, + Callable = 10 +}; /** * End of namespace diff --git a/include/value.h b/include/value.h index d4fae0b..fc6e4fe 100644 --- a/include/value.h +++ b/include/value.h @@ -285,13 +285,13 @@ public: * Check if the value is of a certain type * @return bool */ - bool isNull() const { return type() == nullType; } - bool isNumeric() const { return type() == numericType; } - bool isBool() const { return type() == boolType; } - bool isString() const { return type() == stringType; } - bool isFloat() const { return type() == floatType; } - bool isObject() const { return type() == objectType; } - bool isArray() const { return type() == arrayType; } + bool isNull() const { return type() == Type::Null; } + bool isNumeric() const { return type() == Type::Numeric; } + bool isBool() const { return type() == Type::Bool; } + bool isString() const { return type() == Type::String; } + bool isFloat() const { return type() == Type::Float; } + bool isObject() const { return type() == Type::Object; } + bool isArray() const { return type() == Type::Array; } bool isCallable() const; /** -- cgit v1.2.3