1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
6 #include "base/i2-base.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/value.hpp"
9 #include <boost/range/iterator.hpp>
16 typedef std::vector<Value> ArrayData;
19 * An array of Value items.
23 class Array final : public Object
26 DECLARE_OBJECT(Array);
29 * An iterator that can be used to iterate over array elements.
31 typedef std::vector<Value>::iterator Iterator;
33 typedef std::vector<Value>::size_type SizeType;
36 Array(const ArrayData& other);
37 Array(ArrayData&& other);
38 Array(std::initializer_list<Value> init);
40 Value Get(SizeType index) const;
41 void Set(SizeType index, const Value& value, bool overrideFrozen = false);
42 void Set(SizeType index, Value&& value, bool overrideFrozen = false);
43 void Add(Value value, bool overrideFrozen = false);
48 size_t GetLength() const;
49 bool Contains(const Value& value) const;
51 void Insert(SizeType index, Value value, bool overrideFrozen = false);
52 void Remove(SizeType index, bool overrideFrozen = false);
53 void Remove(Iterator it, bool overrideFrozen = false);
55 void Resize(SizeType newSize, bool overrideFrozen = false);
56 void Clear(bool overrideFrozen = false);
58 void Reserve(SizeType newSize, bool overrideFrozen = false);
60 void CopyTo(const Array::Ptr& dest) const;
61 Array::Ptr ShallowClone() const;
63 static Object::Ptr GetPrototype();
66 static Array::Ptr FromVector(const std::vector<T>& v)
68 Array::Ptr result = new Array();
69 ObjectLock olock(result);
70 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
77 ObjectLock olock(this);
78 return std::set<T>(Begin(), End());
82 static Array::Ptr FromSet(const std::set<T>& v)
84 Array::Ptr result = new Array();
85 ObjectLock olock(result);
86 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
90 Object::Ptr Clone() const override;
92 Array::Ptr Reverse() const;
94 void Sort(bool overrideFrozen = false);
96 String ToString() const override;
98 Array::Ptr Unique() const;
101 Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
102 void SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
105 std::vector<Value> m_Data; /**< The data for the array. */
106 bool m_Frozen{false};
109 Array::Iterator begin(const Array::Ptr& x);
110 Array::Iterator end(const Array::Ptr& x);
114 extern template class std::vector<icinga::Value>;