summaryrefslogtreecommitdiffstats
path: root/lib/base/array.cpp
blob: 08e06fad2d2d310fabd7f36f6be6f89bbd93304a (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/array.hpp"
#include "base/objectlock.hpp"
#include "base/debug.hpp"
#include "base/primitivetype.hpp"
#include "base/dictionary.hpp"
#include "base/configwriter.hpp"
#include "base/convert.hpp"
#include "base/exception.hpp"

using namespace icinga;

template class std::vector<Value>;

REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());

Array::Array(const ArrayData& other)
	: m_Data(other)
{ }

Array::Array(ArrayData&& other)
	: m_Data(std::move(other))
{ }

Array::Array(std::initializer_list<Value> init)
	: m_Data(init)
{ }

/**
 * Restrieves a value from an array.
 *
 * @param index The index.
 * @returns The value.
 */
Value Array::Get(SizeType index) const
{
	ObjectLock olock(this);

	return m_Data.at(index);
}

/**
 * Sets a value in the array.
 *
 * @param index The index.
 * @param value The value.
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Set(SizeType index, const Value& value, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Value in array must not be modified."));

	m_Data.at(index) = value;
}

/**
 * Sets a value in the array.
 *
 * @param index The index.
 * @param value The value.
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Set(SizeType index, Value&& value, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.at(index).Swap(value);
}

/**
 * Adds a value to the array.
 *
 * @param value The value.
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Add(Value value, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.push_back(std::move(value));
}

/**
 * Returns an iterator to the beginning of the array.
 *
 * Note: Caller must hold the object lock while using the iterator.
 *
 * @returns An iterator.
 */
Array::Iterator Array::Begin()
{
	ASSERT(OwnsLock());

	return m_Data.begin();
}

/**
 * Returns an iterator to the end of the array.
 *
 * Note: Caller must hold the object lock while using the iterator.
 *
 * @returns An iterator.
 */
Array::Iterator Array::End()
{
	ASSERT(OwnsLock());

	return m_Data.end();
}

/**
 * Returns the number of elements in the array.
 *
 * @returns Number of elements.
 */
size_t Array::GetLength() const
{
	ObjectLock olock(this);

	return m_Data.size();
}

/**
 * Checks whether the array contains the specified value.
 *
 * @param value The value.
 * @returns true if the array contains the value, false otherwise.
 */
bool Array::Contains(const Value& value) const
{
	ObjectLock olock(this);

	return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
}

/**
 * Insert the given value at the specified index
 *
 * @param index The index
 * @param value The value to add
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Insert(SizeType index, Value value, bool overrideFrozen)
{
	ObjectLock olock(this);

	ASSERT(index <= m_Data.size());

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.insert(m_Data.begin() + index, std::move(value));
}

/**
 * Removes the specified index from the array.
 *
 * @param index The index.
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Remove(SizeType index, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	if (index >= m_Data.size())
		BOOST_THROW_EXCEPTION(std::invalid_argument("Index to remove must be within bounds."));

	m_Data.erase(m_Data.begin() + index);
}

/**
 * Removes the item specified by the iterator from the array.
 *
 * @param it The iterator.
 * @param overrideFrozen Whether to allow modifying frozen arrays.
 */
void Array::Remove(Array::Iterator it, bool overrideFrozen)
{
	ASSERT(OwnsLock());

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.erase(it);
}

void Array::Resize(SizeType newSize, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.resize(newSize);
}

void Array::Clear(bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.clear();
}

void Array::Reserve(SizeType newSize, bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	m_Data.reserve(newSize);
}

void Array::CopyTo(const Array::Ptr& dest) const
{
	ObjectLock olock(this);
	ObjectLock xlock(dest);

	if (dest->m_Frozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(dest->m_Data));
}

/**
 * Makes a shallow copy of an array.
 *
 * @returns a copy of the array.
 */
Array::Ptr Array::ShallowClone() const
{
	Array::Ptr clone = new Array();
	CopyTo(clone);
	return clone;
}

/**
 * Makes a deep clone of an array
 * and its elements.
 *
 * @returns a copy of the array.
 */
Object::Ptr Array::Clone() const
{
	ArrayData arr;

	ObjectLock olock(this);
	for (const Value& val : m_Data) {
		arr.push_back(val.Clone());
	}

	return new Array(std::move(arr));
}

Array::Ptr Array::Reverse() const
{
	Array::Ptr result = new Array();

	ObjectLock olock(this);
	ObjectLock xlock(result);

	std::copy(m_Data.rbegin(), m_Data.rend(), std::back_inserter(result->m_Data));

	return result;
}

void Array::Sort(bool overrideFrozen)
{
	ObjectLock olock(this);

	if (m_Frozen && !overrideFrozen)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Array must not be modified."));

	std::sort(m_Data.begin(), m_Data.end());
}

String Array::ToString() const
{
	std::ostringstream msgbuf;
	ConfigWriter::EmitArray(msgbuf, 1, const_cast<Array *>(this));
	return msgbuf.str();
}

Value Array::Join(const Value& separator) const
{
	Value result;
	bool first = true;

	ObjectLock olock(this);

	for (const Value& item : m_Data) {
		if (first) {
			first = false;
		} else {
			result = result + separator;
		}

		result = result + item;
	}

	return result;
}

Array::Ptr Array::Unique() const
{
	std::set<Value> result;

	ObjectLock olock(this);

	for (const Value& item : m_Data) {
		result.insert(item);
	}

	return Array::FromSet(result);
}

void Array::Freeze()
{
	ObjectLock olock(this);
	m_Frozen = true;
}

Value Array::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
{
	int index;

	try {
		index = Convert::ToLong(field);
	} catch (...) {
		return Object::GetFieldByName(field, sandboxed, debugInfo);
	}

	ObjectLock olock(this);

	if (index < 0 || static_cast<size_t>(index) >= GetLength())
		BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));

	return Get(index);
}

void Array::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
{
	ObjectLock olock(this);

	int index = Convert::ToLong(field);

	if (index < 0)
		BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));

	if (static_cast<size_t>(index) >= GetLength())
		Resize(index + 1, overrideFrozen);

	Set(index, value, overrideFrozen);
}

Array::Iterator icinga::begin(const Array::Ptr& x)
{
	return x->Begin();
}

Array::Iterator icinga::end(const Array::Ptr& x)
{
	return x->End();
}