]> granicus.if.org Git - icinga2/blob - lib/base/array.cpp
Update copyright headers for 2016
[icinga2] / lib / base / array.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
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 #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>
27
28 using namespace icinga;
29
30 REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
31
32 /**
33  * Restrieves a value from an array.
34  *
35  * @param index The index.
36  * @returns The value.
37  */
38 Value Array::Get(unsigned int index) const
39 {
40         ObjectLock olock(this);
41
42         return m_Data.at(index);
43 }
44
45 /**
46  * Sets a value in the array.
47  *
48  * @param index The index.
49  * @param value The value.
50  */
51 void Array::Set(unsigned int index, const Value& value)
52 {
53         ObjectLock olock(this);
54
55         m_Data.at(index) = value;
56 }
57
58 /**
59  * Adds a value to the array.
60  *
61  * @param value The value.
62  */
63 void Array::Add(const Value& value)
64 {
65         ObjectLock olock(this);
66
67         m_Data.push_back(value);
68 }
69
70 /**
71  * Returns the number of elements in the array.
72  *
73  * @returns Number of elements.
74  */
75 size_t Array::GetLength(void) const
76 {
77         ObjectLock olock(this);
78
79         return m_Data.size();
80 }
81
82 /**
83  * Checks whether the array contains the specified value.
84  *
85  * @param value The value.
86  * @returns true if the array contains the value, false otherwise.
87  */
88 bool Array::Contains(const Value& value) const
89 {
90         ObjectLock olock(this);
91
92         return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
93 }
94
95 /**
96  * Insert the given value at the specified index
97  *
98  * @param index The index
99  * @param value The value to add
100  */
101 void Array::Insert(unsigned int index, const Value& value)
102 {
103         ObjectLock olock(this);
104
105         ASSERT(index <= m_Data.size());
106
107         m_Data.insert(m_Data.begin() + index, value);
108 }
109
110 /**
111  * Removes the specified index from the array.
112  *
113  * @param index The index.
114  */
115 void Array::Remove(unsigned int index)
116 {
117         ObjectLock olock(this);
118
119         m_Data.erase(m_Data.begin() + index);
120 }
121
122 /**
123  * Removes the item specified by the iterator from the array.
124  *
125  * @param it The iterator.
126  */
127 void Array::Remove(Array::Iterator it)
128 {
129         ASSERT(OwnsLock());
130
131         m_Data.erase(it);
132 }
133
134 void Array::Resize(size_t new_size)
135 {
136         ObjectLock olock(this);
137
138         m_Data.resize(new_size);
139 }
140
141 void Array::Clear(void)
142 {
143         ObjectLock olock(this);
144
145         m_Data.clear();
146 }
147
148 void Array::Reserve(size_t new_size)
149 {
150         ObjectLock olock(this);
151
152         m_Data.reserve(new_size);
153 }
154
155 void Array::CopyTo(const Array::Ptr& dest) const
156 {
157         ObjectLock olock(this);
158         ObjectLock xlock(dest);
159
160         std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(dest->m_Data));
161 }
162
163 /**
164  * Makes a shallow copy of an array.
165  *
166  * @returns a copy of the array.
167  */
168 Array::Ptr Array::ShallowClone(void) const
169 {
170         Array::Ptr clone = new Array();
171         CopyTo(clone);
172         return clone;
173 }
174
175 /**
176  * Makes a deep clone of an array
177  * and its elements.
178  * 
179  * @returns a copy of the array.
180  */
181 Object::Ptr Array::Clone(void) const
182 {
183         Array::Ptr arr = new Array();
184         
185         ObjectLock olock(this);
186         BOOST_FOREACH(const Value& val, m_Data) {
187                 arr->Add(val.Clone());
188         }
189         
190         return arr;
191 }
192
193 Array::Ptr Array::Reverse(void) const
194 {
195         Array::Ptr result = new Array();
196
197         ObjectLock olock(this);
198         ObjectLock xlock(result);
199
200         std::copy(m_Data.rbegin(), m_Data.rend(), std::back_inserter(result->m_Data));
201
202         return result;
203 }
204
205 String Array::ToString(void) const
206 {
207         std::ostringstream msgbuf;
208         ConfigWriter::EmitArray(msgbuf, 1, const_cast<Array *>(this));
209         return msgbuf.str();
210 }