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