1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://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 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include "base/value.hpp"
26 #include <boost/range/iterator.hpp>
34 * A container that holds key-value pairs.
38 class I2_BASE_API Dictionary : public Object
41 DECLARE_OBJECT(Dictionary);
44 * An iterator that can be used to iterate over dictionary elements.
46 typedef std::map<String, Value>::iterator Iterator;
48 typedef std::map<String, Value>::size_type SizeType;
50 typedef std::pair<String, Value> Pair;
52 inline Dictionary(void)
55 inline ~Dictionary(void)
58 Value Get(const String& key) const;
59 bool Get(const String& key, Value *result) const;
60 void Set(const String& key, const Value& value);
61 bool Contains(const String& key) const;
64 * Returns an iterator to the beginning of the dictionary.
66 * Note: Caller must hold the object lock while using the iterator.
68 * @returns An iterator.
70 inline Iterator Begin(void)
74 return m_Data.begin();
78 * Returns an iterator to the end of the dictionary.
80 * Note: Caller must hold the object lock while using the iterator.
82 * @returns An iterator.
84 inline Iterator End(void)
91 size_t GetLength(void) const;
93 void Remove(const String& key);
96 * Removes the item specified by the iterator from the dictionary.
98 * @param it The iterator.
100 inline void Remove(Iterator it)
109 void CopyTo(const Dictionary::Ptr& dest) const;
110 Dictionary::Ptr ShallowClone(void) const;
112 std::vector<String> GetKeys(void) const;
114 static Object::Ptr GetPrototype(void);
116 virtual Object::Ptr Clone(void) const override;
118 virtual String ToString(void) const override;
121 std::map<String, Value> m_Data; /**< The data for the dictionary. */
124 inline Dictionary::Iterator range_begin(Dictionary::Ptr x)
129 inline Dictionary::Iterator range_end(Dictionary::Ptr x)
140 struct range_mutable_iterator<icinga::Dictionary::Ptr>
142 typedef icinga::Dictionary::Iterator type;
146 struct range_const_iterator<icinga::Dictionary::Ptr>
148 typedef icinga::Dictionary::Iterator type;
153 #endif /* DICTIONARY_H */