summaryrefslogtreecommitdiff
path: root/include/forcedvalue.h
blob: 29e630a24f960345b1d16cbfe74508781f98aa48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
 */
}