1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://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 <boost/foreach.hpp>
25 using namespace icinga;
28 * Restrieves a value from an array.
30 * @param index The index..
33 Value Array::Get(unsigned int index) const
36 ObjectLock olock(this);
38 return m_Data.at(index);
42 * Sets a value in the array.
44 * @param index The index.
45 * @param value The value.
47 void Array::Set(unsigned int index, const Value& value)
50 ObjectLock olock(this);
52 m_Data.at(index) = value;
56 * Adds a value to the array.
58 * @param value The value.
60 void Array::Add(const Value& value)
63 ObjectLock olock(this);
65 m_Data.push_back(value);
69 * Returns an iterator to the beginning of the array.
71 * Note: Caller must hold the object lock while using the iterator.
73 * @returns An iterator.
75 Array::Iterator Array::Begin(void)
79 return m_Data.begin();
83 * Returns an iterator to the end of the array.
85 * Note: Caller must hold the object lock while using the iterator.
87 * @returns An iterator.
89 Array::Iterator Array::End(void)
97 * Returns the number of elements in the array.
99 * @returns Number of elements.
101 size_t Array::GetLength(void) const
104 ObjectLock olock(this);
106 return m_Data.size();
110 * Checks whether the array contains the specified value.
112 * @param value The value.
113 * @returns true if the array contains the value, false otherwise.
115 bool Array::Contains(const String& value) const
118 ObjectLock olock(this);
120 return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
124 * Insert the given value at the specified index
126 * @param index The index
127 * @param value The value to add
129 void Array::Insert(unsigned int index, const Value& value)
132 ObjectLock olock(this);
134 ASSERT(index <= m_Data.size());
136 m_Data.insert(m_Data.begin() + index, value);
140 * Removes the specified index from the array.
142 * @param index The index.
144 void Array::Remove(unsigned int index)
147 ObjectLock olock(this);
149 m_Data.erase(m_Data.begin() + index);
153 * Removes the item specified by the iterator from the array.
155 * @param it The iterator.
157 void Array::Remove(Array::Iterator it)
164 void Array::Resize(size_t new_size)
167 ObjectLock olock(this);
169 m_Data.resize(new_size);
172 void Array::Clear(void)
175 ObjectLock olock(this);
180 void Array::CopyTo(const Array::Ptr& dest) const
183 ObjectLock olock(this);
184 ObjectLock xlock(dest);
186 std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(dest->m_Data));
190 * Makes a shallow copy of an array.
192 * @returns a copy of the array.
194 Array::Ptr Array::ShallowClone(void) const
196 Array::Ptr clone = make_shared<Array>();