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/dictionary.h"
21 #include "base/objectlock.h"
22 #include "base/debug.h"
24 #include <boost/make_shared.hpp>
25 #include <boost/foreach.hpp>
27 using namespace icinga;
30 * Compares dictionary keys using the less operator.
32 struct DictionaryKeyLessComparer
37 * @param a The first key.
38 * @param b The second key.
39 * @returns true if the first key is less than the second key, false
42 bool operator()(const std::pair<String, Value>& a, const char *b)
50 * @param a The first key.
51 * @param b The second key.
52 * @returns true if the first key is less than the second key, false
55 bool operator()(const char *a, const std::pair<String, Value>& b)
62 * Retrieves a value from a dictionary.
64 * @param key The key whose value should be retrieved.
65 * @returns The value of an empty value if the key was not found.
67 Value Dictionary::Get(const char *key) const
70 ObjectLock olock(this);
72 std::map<String, Value>::const_iterator it;
74 it = std::lower_bound(m_Data.begin(), m_Data.end(), key, DictionaryKeyLessComparer());
76 if (it == m_Data.end() || DictionaryKeyLessComparer()(key, *it))
83 * Retrieves a value from the dictionary.
85 * @param key The key whose value should be retrieved.
86 * @returns The value or an empty value if the key was not found.
88 Value Dictionary::Get(const String& key) const
90 return Get(key.CStr());
94 * Sets a value in the dictionary.
97 * @param value The value.
99 void Dictionary::Set(const String& key, const Value& value)
101 if (value.IsEmpty()) {
107 ObjectLock olock(this);
109 std::pair<std::map<String, Value>::iterator, bool> ret;
110 ret = m_Data.insert(std::make_pair(key, value));
112 ret.first->second = value;
116 * Returns an iterator to the beginning of the dictionary.
118 * Note: Caller must hold the object lock while using the iterator.
120 * @returns An iterator.
122 Dictionary::Iterator Dictionary::Begin(void)
126 return m_Data.begin();
130 * Returns an iterator to the end of the dictionary.
132 * Note: Caller must hold the object lock while using the iterator.
134 * @returns An iterator.
136 Dictionary::Iterator Dictionary::End(void)
144 * Returns the number of elements in the dictionary.
146 * @returns Number of elements.
148 size_t Dictionary::GetLength(void) const
151 ObjectLock olock(this);
153 return m_Data.size();
157 * Checks whether the dictionary contains the specified key.
159 * @param key The key.
160 * @returns true if the dictionary contains the key, false otherwise.
162 bool Dictionary::Contains(const String& key) const
165 ObjectLock olock(this);
167 return (m_Data.find(key) != m_Data.end());
171 * Removes the specified key from the dictionary.
173 * @param key The key.
175 void Dictionary::Remove(const String& key)
178 ObjectLock olock(this);
180 Dictionary::Iterator it;
181 it = m_Data.find(key);
183 if (it == m_Data.end())
190 * Removes the item specified by the iterator from the dictionary.
192 * @param it The iterator.
194 void Dictionary::Remove(Dictionary::Iterator it)
201 void Dictionary::CopyTo(const Dictionary::Ptr& dest) const
204 ObjectLock olock(this);
206 BOOST_FOREACH(const Dictionary::Pair& kv, m_Data) {
207 dest->Set(kv.first, kv.second);
212 * Makes a shallow copy of a dictionary.
214 * @returns a copy of the dictionary.
216 Dictionary::Ptr Dictionary::ShallowClone(void) const
218 Dictionary::Ptr clone = make_shared<Dictionary>();
224 * Converts a JSON object to a dictionary.
226 * @param json The JSON object.
227 * @returns A dictionary that is equivalent to the JSON object.
229 Dictionary::Ptr Dictionary::FromJson(cJSON *json)
231 Dictionary::Ptr dictionary = make_shared<Dictionary>();
233 ASSERT(json->type == cJSON_Object);
235 for (cJSON *i = json->child; i != NULL; i = i->next) {
236 dictionary->Set(i->string, Value::FromJson(i));
243 * Converts this dictionary to a JSON object.
245 * @returns A JSON object that is equivalent to the dictionary. Values that
246 * cannot be represented in JSON are omitted.
248 cJSON *Dictionary::ToJson(void) const
250 cJSON *json = cJSON_CreateObject();
253 ObjectLock olock(this);
255 BOOST_FOREACH(const Dictionary::Pair& kv, m_Data) {
256 cJSON_AddItemToObject(json, kv.first.CStr(), kv.second.ToJson());