1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/value.hpp"
26 #include <boost/range/iterator.hpp>
33 typedef std::vector<Value> ArrayData;
36 * An array of Value items.
40 class Array final : public Object
43 DECLARE_OBJECT(Array);
46 * An iterator that can be used to iterate over array elements.
48 typedef std::vector<Value>::iterator Iterator;
50 typedef std::vector<Value>::size_type SizeType;
53 Array(const ArrayData& other);
54 Array(ArrayData&& other);
55 Array(std::initializer_list<Value> init);
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);
65 size_t GetLength() const;
66 bool Contains(const Value& value) const;
68 void Insert(SizeType index, Value value);
69 void Remove(SizeType index);
70 void Remove(Iterator it);
72 void Resize(SizeType newSize);
75 void Reserve(SizeType newSize);
77 void CopyTo(const Array::Ptr& dest) const;
78 Array::Ptr ShallowClone() const;
80 static Object::Ptr GetPrototype();
83 static Array::Ptr FromVector(const std::vector<T>& v)
85 Array::Ptr result = new Array();
86 ObjectLock olock(result);
87 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
94 ObjectLock olock(this);
95 return std::set<T>(Begin(), End());
99 static Array::Ptr FromSet(const std::set<T>& v)
101 Array::Ptr result = new Array();
102 ObjectLock olock(result);
103 std::copy(v.begin(), v.end(), std::back_inserter(result->m_Data));
107 Object::Ptr Clone() const override;
109 Array::Ptr Reverse() const;
113 String ToString() const override;
115 Array::Ptr Unique() const;
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;
122 std::vector<Value> m_Data; /**< The data for the array. */
123 bool m_Frozen{false};
126 Array::Iterator begin(const Array::Ptr& x);
127 Array::Iterator end(const Array::Ptr& x);
131 extern template class std::vector<icinga::Value>;