]> granicus.if.org Git - icinga2/blob - lib/base/array.hpp
62bcf7e2ee9d76e22820d0b56dec963a0be168e8
[icinga2] / lib / base / array.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef ARRAY_H
21 #define ARRAY_H
22
23 #include "base/i2-base.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/value.hpp"
26 #include <boost/range/iterator.hpp>
27 #include <vector>
28 #include <set>
29
30 namespace icinga
31 {
32
33 typedef std::vector<Value> ArrayData;
34
35 /**
36  * An array of Value items.
37  *
38  * @ingroup base
39  */
40 class Array final : public Object
41 {
42 public:
43         DECLARE_OBJECT(Array);
44
45         /**
46          * An iterator that can be used to iterate over array elements.
47          */
48         typedef std::vector<Value>::iterator Iterator;
49
50         typedef std::vector<Value>::size_type SizeType;
51
52         Array() = default;
53         Array(const ArrayData& other);
54         Array(ArrayData&& other);
55         Array(std::initializer_list<Value> init);
56
57         Value Get(SizeType index) const;
58         void Set(SizeType index, const Value& value);
59         void Set(SizeType index, Value&& value);
60         void Add(Value value);
61
62         Iterator Begin();
63         Iterator End();
64
65         size_t GetLength() const;
66         bool Contains(const Value& value) const;
67
68         void Insert(SizeType index, Value value);
69         void Remove(SizeType index);
70         void Remove(Iterator it);
71
72         void Resize(SizeType newSize);
73         void Clear();
74
75         void Reserve(SizeType newSize);
76
77         void CopyTo(const Array::Ptr& dest) const;
78         Array::Ptr ShallowClone() const;
79
80         static Object::Ptr GetPrototype();
81
82         template<typename T>
83         static Array::Ptr FromVector(const std::vector<T>& v)
84         {
85                 Array::Ptr result = new Array();
86                 ObjectLock olock(result);
87                 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
88                 return result;
89         }
90
91         template<typename T>
92         std::set<T> ToSet()
93         {
94                 ObjectLock olock(this);
95                 return std::set<T>(Begin(), End());
96         }
97
98         template<typename T>
99         static Array::Ptr FromSet(const std::set<T>& v)
100         {
101                 Array::Ptr result = new Array();
102                 ObjectLock olock(result);
103                 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
104                 return result;
105         }
106
107         Object::Ptr Clone() const override;
108
109         Array::Ptr Reverse() const;
110
111         void Sort();
112
113         String ToString() const override;
114
115         Array::Ptr Unique() const;
116         void Freeze();
117
118         Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
119         void SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo) override;
120
121 private:
122         std::vector<Value> m_Data; /**< The data for the array. */
123         bool m_Frozen{false};
124 };
125
126 Array::Iterator begin(const Array::Ptr& x);
127 Array::Iterator end(const Array::Ptr& x);
128
129 }
130
131 extern template class std::vector<icinga::Value>;
132
133 #endif /* ARRAY_H */