]> granicus.if.org Git - icinga2/blob - lib/base/array.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / array.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef ARRAY_H
4 #define ARRAY_H
5
6 #include "base/i2-base.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/value.hpp"
9 #include <boost/range/iterator.hpp>
10 #include <vector>
11 #include <set>
12
13 namespace icinga
14 {
15
16 typedef std::vector<Value> ArrayData;
17
18 /**
19  * An array of Value items.
20  *
21  * @ingroup base
22  */
23 class Array final : public Object
24 {
25 public:
26         DECLARE_OBJECT(Array);
27
28         /**
29          * An iterator that can be used to iterate over array elements.
30          */
31         typedef std::vector<Value>::iterator Iterator;
32
33         typedef std::vector<Value>::size_type SizeType;
34
35         Array() = default;
36         Array(const ArrayData& other);
37         Array(ArrayData&& other);
38         Array(std::initializer_list<Value> init);
39
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);
44
45         Iterator Begin();
46         Iterator End();
47
48         size_t GetLength() const;
49         bool Contains(const Value& value) const;
50
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);
54
55         void Resize(SizeType newSize, bool overrideFrozen = false);
56         void Clear(bool overrideFrozen = false);
57
58         void Reserve(SizeType newSize, bool overrideFrozen = false);
59
60         void CopyTo(const Array::Ptr& dest) const;
61         Array::Ptr ShallowClone() const;
62
63         static Object::Ptr GetPrototype();
64
65         template<typename T>
66         static Array::Ptr FromVector(const std::vector<T>& v)
67         {
68                 Array::Ptr result = new Array();
69                 ObjectLock olock(result);
70                 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
71                 return result;
72         }
73
74         template<typename T>
75         std::set<T> ToSet()
76         {
77                 ObjectLock olock(this);
78                 return std::set<T>(Begin(), End());
79         }
80
81         template<typename T>
82         static Array::Ptr FromSet(const std::set<T>& v)
83         {
84                 Array::Ptr result = new Array();
85                 ObjectLock olock(result);
86                 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
87                 return result;
88         }
89
90         Object::Ptr Clone() const override;
91
92         Array::Ptr Reverse() const;
93
94         void Sort(bool overrideFrozen = false);
95
96         String ToString() const override;
97
98         Array::Ptr Unique() const;
99         void Freeze();
100
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;
103
104 private:
105         std::vector<Value> m_Data; /**< The data for the array. */
106         bool m_Frozen{false};
107 };
108
109 Array::Iterator begin(const Array::Ptr& x);
110 Array::Iterator end(const Array::Ptr& x);
111
112 }
113
114 extern template class std::vector<icinga::Value>;
115
116 #endif /* ARRAY_H */