summaryrefslogtreecommitdiff
path: root/include/array.h
blob: 94d282407843f46e214650713ac361ee33b18888 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 *  Array.h
 *
 *  The Array is a wrapper around the value class that ensures that a 
 *  certain property always is an array
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013, 2014 Copernica BV
 */

/**
 *  Set up namespace
 */
namespace Php {
    
/**
 *  Class definition
 */
class Array : public Value
{
public:
    /**
     *  Constructor
     */
    Array() : Value() { setType(Type::Array); }
    
    /**
     *  Copy constructor from a value object
     *  @param  value
     */
    Array(const Value &value) : Value(value) 
    {
        // type must be valid
        if (value.type() != Type::Array) throw Php::Exception("Assiging a non-array to an array variable");
    }

    /**
     *  Move constructor from a value object
     *  @param  value
     */
    Array(Value &&value) : Value(std::move(value))
    {
        // type must be valid
        if (value.type() != Type::Array) throw Php::Exception("Moving a non-array to an array variable");
    }
    
    /**
     *  Destructor
     */
    virtual ~Array() {}

    /**
     *  Change the internal type of the variable
     *  @param  Type
     */
    virtual Value &setType(Type type) override
    {
        // throw exception if things are going wrong
        if (type != Type::Array) throw Php::Exception("Changing type of a fixed array variable");
        
        // call base
        return Value::setType(Type::Array);
    }
    
    /**
     *  Assignment operator
     *  @param  value
     *  @return Array
     */
    Array &operator=(const Value &value)
    {
        // skip self assignment
        if (this == &value) return *this;
        
        // type must be valid
        if (value.type() != Type::Array) throw Php::Exception("Assiging a non-array to a fixed array variable");
        
        // call base
        Value::operator=(value);

        // done
        return *this;
    }
    
    /**
     *  Move assignment operator
     *  @param  value
     *  @return Array
     */
    Array &operator=(Value &&value)
    {
        // skip self assignment
        if (this == &value) return *this;
        
        // type must be valid
        if (value.type() != Type::Array) throw Php::Exception("Moving a non-array to a fixed array variable");
        
        // call base
        Value::operator=(std::move(value));
        
        // done
        return *this;
    }
};

/**
 *  Define for arrays and objects
 */
using Array     =   Array;
    
/**
 *  End of namespace
 */
}