summaryrefslogtreecommitdiff
path: root/include/member.h
blob: 49ac246307ef98032fa9df0791f96ac51f0fe2f7 (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
/**
 *	Member.h
 *
 *	When you're accessing members in an array or an object, you're
 *	doing so via an internal member object. This is an object that
 *	keeps track of the array to which it belonged, and that updates
 *	the class when the object is modified
 *
 *	This is an abstract class. You are not supposed to instantiate it
 *	yourself. 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 PhpCpp {
	
/**
 *  Forward definitions
 */
class Value;

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

	/**
	 *  Assign a value object to the array
	 *  @param	value
	 *  @return Member
	 */
	Member &operator=(const Value &value)
	{
		// set property in parent array
		_value->set(_index, value);
		
		// leap out if this is not a nested array access
		//if (!_parent) return *this;
		
		// nested array access, we need to update the parent too
		//_parent->operator=(*_value);
		
		// done
		return *this;
	}

	/**
	 *  Retrieve the original value
	 *  @return	Value
	 */
	Value value() const
	{
		return _value->get(_index);
	}

	/**
	 *  Cast to a value object
	 *  @return	Value
	 */
	operator Value () const
	{
		return _value->get(_index);
	}

    /**
     *  Cast to a long
     *  @return long
     */
    operator long () const
    {
		return _value->get(_index).longValue();
	}
    
    /**
     *  Cast to a boolean
     *  @return boolean
     */
    operator bool () const
    {
		return _value->get(_index).boolValue();
	}
    
    /**
     *  Cast to a string
     *  @return string
     */
    operator std::string () const
    {
		return _value->get(_index).stringValue();
	}
    
    /**
     *  Cast to byte array
     *  @return const char *
     */
    operator const char * () const
    {
		return _value->get(_index).rawValue();
	}
    
    /**
     *  Cast to a floating point
     *  @return double
     */
    operator double () const
    {
		return _value->get(_index).decimalValue();
	}
	
    /**
     *  Array access operator
     *  This can be used for accessing arrays
     *  @param  index
     *  @return Member
     */
    Member operator[](int index)
    {
		return _value->get(_index)[index];
	}

    /**
     *  Array access operator
     *  This can be used for accessing associative arrays
     *  @param  key
     *  @return Member
     */
    Member operator[](const std::string &key)
    {
		return _value->get(_index)[key];
	}

	/**
	 *  Array access operator
	 *  This can be used for accessing associative arrays
	 *  @param	key
	 *  @return	Member
	 */
	Member operator[](const char *key)
	{
		return _value->get(_index)[key];
	}

private:
	/**
	 *  Constructor
	 *	@param	value	Parent element
	 *  @param	index	Index in the array
	 */
	Member(Value *value, Type index) : _value(value), _index(index) {}
	
	/**
	 *  Private copy constructor
	 *  @param	value	Other element
	 */
	Member(const Member<Type> &member) : _value(member._value), _index(member._index) {}
	
	/**
	 *  Set the member
	 *  @param	member
	 */
	Member<Type> &add(Member *parent)
	{
		_parent = parent;
		return *this;
	}
	
	/**
	 *  The array of which this is a member
	 *	@var Value
	 */
	Value *_value;
	
	/**
	 *  The original index
	 *  @var Type
	 */
	Type _index;
	
	/**
	 *  Parent member
	 * 
	 *  When accessing nested arrays a["a"]["b"] = 'true', the member
	 *  object that represents the "b" entry holds a pointer to the member
	 *  object that represents "a", so that it can tell its parent to
	 *  store itself in the top array too
	 * 
	 *  @var Member
	 */
	Member *_parent = nullptr;
	
	/**
	 *  Value objects may create members
	 */
	friend class Value;
};
	
/**
 *	End of namespace
 */
}