]> granicus.if.org Git - icinga2/blob - lib/base/dictionary.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / dictionary.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/dictionary.hpp"
4 #include "base/objectlock.hpp"
5 #include "base/debug.hpp"
6 #include "base/primitivetype.hpp"
7 #include "base/configwriter.hpp"
8 #include <sstream>
9
10 using namespace icinga;
11
12 template class std::map<String, Value>;
13
14 REGISTER_PRIMITIVE_TYPE(Dictionary, Object, Dictionary::GetPrototype());
15
16 Dictionary::Dictionary(const DictionaryData& other)
17 {
18         for (const auto& kv : other)
19                 m_Data.insert(kv);
20 }
21
22 Dictionary::Dictionary(DictionaryData&& other)
23 {
24         for (auto& kv : other)
25                 m_Data.insert(std::move(kv));
26 }
27
28 Dictionary::Dictionary(std::initializer_list<Dictionary::Pair> init)
29         : m_Data(init)
30 { }
31
32 /**
33  * Retrieves a value from a dictionary.
34  *
35  * @param key The key whose value should be retrieved.
36  * @returns The value of an empty value if the key was not found.
37  */
38 Value Dictionary::Get(const String& key) const
39 {
40         ObjectLock olock(this);
41
42         auto it = m_Data.find(key);
43
44         if (it == m_Data.end())
45                 return Empty;
46
47         return it->second;
48 }
49
50 /**
51  * Retrieves a value from a dictionary.
52  *
53  * @param key The key whose value should be retrieved.
54  * @param result The value of the dictionary item (only set when the key exists)
55  * @returns true if the key exists, false otherwise.
56  */
57 bool Dictionary::Get(const String& key, Value *result) const
58 {
59         ObjectLock olock(this);
60
61         auto it = m_Data.find(key);
62
63         if (it == m_Data.end())
64                 return false;
65
66         *result = it->second;
67         return true;
68 }
69
70 /**
71  * Sets a value in the dictionary.
72  *
73  * @param key The key.
74  * @param value The value.
75  * @param overrideFrozen Whether to allow modifying frozen dictionaries.
76  */
77 void Dictionary::Set(const String& key, Value value, bool overrideFrozen)
78 {
79         ObjectLock olock(this);
80
81         if (m_Frozen && !overrideFrozen)
82                 BOOST_THROW_EXCEPTION(std::invalid_argument("Value in dictionary must not be modified."));
83
84         m_Data[key] = std::move(value);
85 }
86
87 /**
88  * Returns the number of elements in the dictionary.
89  *
90  * @returns Number of elements.
91  */
92 size_t Dictionary::GetLength() const
93 {
94         ObjectLock olock(this);
95
96         return m_Data.size();
97 }
98
99 /**
100  * Checks whether the dictionary contains the specified key.
101  *
102  * @param key The key.
103  * @returns true if the dictionary contains the key, false otherwise.
104  */
105 bool Dictionary::Contains(const String& key) const
106 {
107         ObjectLock olock(this);
108
109         return (m_Data.find(key) != m_Data.end());
110 }
111
112 /**
113  * Returns an iterator to the beginning of the dictionary.
114  *
115  * Note: Caller must hold the object lock while using the iterator.
116  *
117  * @returns An iterator.
118  */
119 Dictionary::Iterator Dictionary::Begin()
120 {
121         ASSERT(OwnsLock());
122
123         return m_Data.begin();
124 }
125
126 /**
127  * Returns an iterator to the end of the dictionary.
128  *
129  * Note: Caller must hold the object lock while using the iterator.
130  *
131  * @returns An iterator.
132  */
133 Dictionary::Iterator Dictionary::End()
134 {
135         ASSERT(OwnsLock());
136
137         return m_Data.end();
138 }
139
140 /**
141  * Removes the item specified by the iterator from the dictionary.
142  *
143  * @param it The iterator.
144  * @param overrideFrozen Whether to allow modifying frozen dictionaries.
145  */
146 void Dictionary::Remove(Dictionary::Iterator it, bool overrideFrozen)
147 {
148         ASSERT(OwnsLock());
149
150         if (m_Frozen && !overrideFrozen)
151                 BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
152
153         m_Data.erase(it);
154 }
155
156 /**
157  * Removes the specified key from the dictionary.
158  *
159  * @param key The key.
160  * @param overrideFrozen Whether to allow modifying frozen dictionaries.
161  */
162 void Dictionary::Remove(const String& key, bool overrideFrozen)
163 {
164         ObjectLock olock(this);
165
166         if (m_Frozen && !overrideFrozen)
167                 BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
168
169         Dictionary::Iterator it;
170         it = m_Data.find(key);
171
172         if (it == m_Data.end())
173                 return;
174
175         m_Data.erase(it);
176 }
177
178 /**
179  * Removes all dictionary items.
180  *
181  * @param overrideFrozen Whether to allow modifying frozen dictionaries.
182  */
183 void Dictionary::Clear(bool overrideFrozen)
184 {
185         ObjectLock olock(this);
186
187         if (m_Frozen && !overrideFrozen)
188                 BOOST_THROW_EXCEPTION(std::invalid_argument("Dictionary must not be modified."));
189
190         m_Data.clear();
191 }
192
193 void Dictionary::CopyTo(const Dictionary::Ptr& dest) const
194 {
195         ObjectLock olock(this);
196
197         for (const Dictionary::Pair& kv : m_Data) {
198                 dest->Set(kv.first, kv.second);
199         }
200 }
201
202 /**
203  * Makes a shallow copy of a dictionary.
204  *
205  * @returns a copy of the dictionary.
206  */
207 Dictionary::Ptr Dictionary::ShallowClone() const
208 {
209         Dictionary::Ptr clone = new Dictionary();
210         CopyTo(clone);
211         return clone;
212 }
213
214 /**
215  * Makes a deep clone of a dictionary
216  * and its elements.
217  *
218  * @returns a copy of the dictionary.
219  */
220 Object::Ptr Dictionary::Clone() const
221 {
222         DictionaryData dict;
223
224         {
225                 ObjectLock olock(this);
226
227                 dict.reserve(GetLength());
228
229                 for (const Dictionary::Pair& kv : m_Data) {
230                         dict.emplace_back(kv.first, kv.second.Clone());
231                 }
232         }
233
234         return new Dictionary(std::move(dict));
235 }
236
237 /**
238  * Returns an array containing all keys
239  * which are currently set in this directory.
240  *
241  * @returns an array of key names
242  */
243 std::vector<String> Dictionary::GetKeys() const
244 {
245         ObjectLock olock(this);
246
247         std::vector<String> keys;
248
249         for (const Dictionary::Pair& kv : m_Data) {
250                 keys.push_back(kv.first);
251         }
252
253         return keys;
254 }
255
256 String Dictionary::ToString() const
257 {
258         std::ostringstream msgbuf;
259         ConfigWriter::EmitScope(msgbuf, 1, const_cast<Dictionary *>(this));
260         return msgbuf.str();
261 }
262
263 void Dictionary::Freeze()
264 {
265         ObjectLock olock(this);
266         m_Frozen = true;
267 }
268
269 Value Dictionary::GetFieldByName(const String& field, bool, const DebugInfo& debugInfo) const
270 {
271         Value value;
272
273         if (Get(field, &value))
274                 return value;
275         else
276                 return GetPrototypeField(const_cast<Dictionary *>(this), field, false, debugInfo);
277 }
278
279 void Dictionary::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo&)
280 {
281         Set(field, value, overrideFrozen);
282 }
283
284 bool Dictionary::HasOwnField(const String& field) const
285 {
286         return Contains(field);
287 }
288
289 bool Dictionary::GetOwnField(const String& field, Value *result) const
290 {
291         return Get(field, result);
292 }
293
294 Dictionary::Iterator icinga::begin(const Dictionary::Ptr& x)
295 {
296         return x->Begin();
297 }
298
299 Dictionary::Iterator icinga::end(const Dictionary::Ptr& x)
300 {
301         return x->End();
302 }
303