1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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 ******************************************************************************/
20 #include "base/array.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/debug.hpp"
23 #include "base/primitivetype.hpp"
24 #include "base/dictionary.hpp"
25 #include "base/configwriter.hpp"
26 #include <boost/foreach.hpp>
28 using namespace icinga;
30 REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
33 * Restrieves a value from an array.
35 * @param index The index.
38 Value Array::Get(unsigned int index) const
40 ObjectLock olock(this);
42 return m_Data.at(index);
46 * Sets a value in the array.
48 * @param index The index.
49 * @param value The value.
51 void Array::Set(unsigned int index, const Value& value)
53 ObjectLock olock(this);
55 m_Data.at(index) = value;
59 * Adds a value to the array.
61 * @param value The value.
63 void Array::Add(const Value& value)
65 ObjectLock olock(this);
67 m_Data.push_back(value);
71 * Returns the number of elements in the array.
73 * @returns Number of elements.
75 size_t Array::GetLength(void) const
77 ObjectLock olock(this);
83 * Checks whether the array contains the specified value.
85 * @param value The value.
86 * @returns true if the array contains the value, false otherwise.
88 bool Array::Contains(const Value& value) const
90 ObjectLock olock(this);
92 return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
96 * Insert the given value at the specified index
98 * @param index The index
99 * @param value The value to add
101 void Array::Insert(unsigned int index, const Value& value)
103 ObjectLock olock(this);
105 ASSERT(index <= m_Data.size());
107 m_Data.insert(m_Data.begin() + index, value);
111 * Removes the specified index from the array.
113 * @param index The index.
115 void Array::Remove(unsigned int index)
117 ObjectLock olock(this);
119 m_Data.erase(m_Data.begin() + index);
123 * Removes the item specified by the iterator from the array.
125 * @param it The iterator.
127 void Array::Remove(Array::Iterator it)
134 void Array::Resize(size_t new_size)
136 ObjectLock olock(this);
138 m_Data.resize(new_size);
141 void Array::Clear(void)
143 ObjectLock olock(this);
148 void Array::Reserve(size_t new_size)
150 ObjectLock olock(this);
152 m_Data.reserve(new_size);
155 void Array::CopyTo(const Array::Ptr& dest) const
157 ObjectLock olock(this);
158 ObjectLock xlock(dest);
160 std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(dest->m_Data));
164 * Makes a shallow copy of an array.
166 * @returns a copy of the array.
168 Array::Ptr Array::ShallowClone(void) const
170 Array::Ptr clone = new Array();
176 * Makes a deep clone of an array
179 * @returns a copy of the array.
181 Object::Ptr Array::Clone(void) const
183 Array::Ptr arr = new Array();
185 ObjectLock olock(this);
186 BOOST_FOREACH(const Value& val, m_Data) {
187 arr->Add(val.Clone());
193 Array::Ptr Array::Reverse(void) const
195 Array::Ptr result = new Array();
197 ObjectLock olock(this);
198 ObjectLock xlock(result);
200 std::copy(m_Data.rbegin(), m_Data.rend(), std::back_inserter(result->m_Data));
205 String Array::ToString(void) const
207 std::ostringstream msgbuf;
208 ConfigWriter::EmitArray(msgbuf, 1, const_cast<Array *>(this));