summaryrefslogtreecommitdiff
path: root/include/hashmember.h
blob: d476b5579623d83a79903f43597edfe69519562f (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
 *  HashMember.h
 *
 *  When you're accessing members in an array or an object, you're
 *  doing this via an internal member object. This is an object that
 *  keeps track of the array to which it belongs, and that will update
 *  the array when the member is modified
 *
 *  You are not supposed to instantiate this class. An instance of it is 
 *  created when you call Value::operator[]
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Set up namespace
 */
namespace Php {
    
/**
 *  Forward definitions
 */
class Value;

/**
 *  Member class
 */
template <typename Type>
class HashMember
{
public:
    /**
     *  Destructor
     */
    virtual ~HashMember() {}

    /**
     *  Assign a value object to the array
     *  @param  value
     *  @return Member
     */
    HashMember &operator=(const Value &value)
    {
        // set property in parent array
        _base.set(_index, value);

        // if there is a parent, it should sets its value too
        if (_parent) _parent->operator=(_base);
        
        // done
        return *this;
    }

    /**
     *  Retrieve the original value
     *  @return Value
     */
    Value value() const
    {
        return _base.get(_index);
    }

    /**
     *  Cast to a value object
     *  @return Value
     */
    operator Value () const
    {
        return _base.get(_index);
    }

    /**
     *  Cast to a integer
     *  @return int16_t
     */
    operator int16_t () const
    {
        return _base.get(_index).numericValue();
    }

    /**
     *  Cast to a integer
     *  @return int32_t
     */
    operator int32_t () const
    {
        return _base.get(_index).numericValue();
    }

    /**
     *  Cast to a integer
     *  @return int64_t
     */
    operator int64_t () const
    {
        return _base.get(_index).numericValue();
    }
    
    /**
     *  Cast to a boolean
     *  @return boolean
     */
    operator bool () const
    {
        return _base.get(_index).boolValue();
    }
    
    /**
     *  Cast to a string
     *  @return string
     */
    operator std::string () const
    {
        return _base.get(_index).stringValue();
    }
    
    /**
     *  Cast to byte array
     *  @return const char *
     */
    operator const char * () const
    {
        return _base.get(_index).rawValue();
    }
    
    /**
     *  Cast to a floating point
     *  @return double
     */
    operator double () const
    {
        return _base.get(_index).decimalValue();
    }
    
    /**
     *  Array access operator
     *  This can be used for accessing arrays
     *  @param  index
     *  @return HashMember
     */
    HashMember operator[](int index)
    {
        return _base.get(_index)[index].add(this);
    }

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return HashMember
     */
    HashMember operator[](const std::string &key)
    {
        return _base.get(_index)[key].add(this);
    }

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return HashMember
     */
    HashMember operator[](const char *key)
    {
        return _base.get(_index)[key].add(this);
    }

private:
    /**
     *  Constructor
     *  @param  base    Base value
     *  @param  index   Index in the array
     */
    HashMember(const Value *base, Type index) : _base(*base), _index(index) {}
    
    /**
     *  Protected copy constructor
     *  @param  value   Other element
     */
    HashMember(const HashMember<Type> &member) : _base(member._base), _index(member._index), _parent(member._parent) {}
    
    /**
     *  Add parent
     *  @param  parent
     *  @return HashMember
     */
    HashMember &add(HashMember *parent)
    {
        _parent = parent;
        return *this;
    }
    
    /**
     *  The original index
     *  @var Type
     */
    Type _index;
    
    /**
     *  Base value
     *  @var Value
     */
    Value _base;
    
    /**
     *  Parent member (in case of nested members)
     *  @var HashMember
     */
    HashMember *_parent = nullptr;
    
    /**
     *  Only value objects may construct members
     */
    friend class Value;
    
};
    
/**
 *  End of namespace
 */
}