summaryrefslogtreecommitdiff
path: root/include/forcedvalue.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-02 11:33:53 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-02 11:33:53 +0100
commit52fe0c39457421e075959179ee6b64a20b96f0d9 (patch)
treee6dd000114d104bf6286d74682feb694b3cb97a3 /include/forcedvalue.h
parentfa02aa127d2c4261d15123829e44f6d997444abc (diff)
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
Diffstat (limited to 'include/forcedvalue.h')
-rw-r--r--include/forcedvalue.h94
1 files changed, 94 insertions, 0 deletions
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 <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+template <Type TYPE>
+class ForcedValue : public Value
+{
+public:
+ /**
+ * Constructor
+ */
+ ForcedValue() : Value() { setType(TYPE); }
+
+ /**
+ * Copy constructor
+ * @param that
+ */
+ ForcedValue(const ForcedValue<TYPE> &that) : Value(that) {}
+
+ /**
+ * Move constructor
+ * @param that
+ */
+ ForcedValue(ForcedValue<TYPE> &&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<Type::Array>;
+using Object = ForcedValue<Type::Object>;
+
+/**
+ * End of namespace
+ */
+}
+