summaryrefslogtreecommitdiff
path: root/include/array.h
blob: 126cd58e92802a833cc4e162a37f5552cfbad9b5 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 *  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 PHPCPP_EXPORT Array : public Value
{
public:
    /**
     *  Constructor
     */
    Array() : Value(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 FatalError("Assigning 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 FatalError("Moving a non-array to an array variable");
    }

    /**
     *  Constructors from a vector (this will create an array)
     *  @param  value
     */
    template <typename T>
    Array(const std::vector<T> &input) : Value(input) {}

    /**
     *  Constructor from a map (this will create an associative array)
     *  @param  value
     */
    template <typename T>
    Array(const std::map<std::string,T> &value) : Value(value) {}

// old visual c++ environments have no support for initializer lists
#   if !defined(_MSC_VER) || _MSC_VER >= 1800

    /**
     *  Constructor from an initializer list
     *  @param  value
     */
    Array(const std::initializer_list<Value> &value) : Value(value) {}

// end of visual c++ check
#   endif

    /**
     *  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 FatalError("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 FatalError("Assigning 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 FatalError("Moving a non-array to a fixed array variable");

        // call base
        Value::operator=(std::move(value));

        // done
        return *this;
    }
};

/**
 *  End of namespace
 */
}