]> granicus.if.org Git - icinga2/blob - lib/base/dictionary.hpp
Update copyright headers for 2016
[icinga2] / lib / base / dictionary.hpp
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 #ifndef DICTIONARY_H
21 #define DICTIONARY_H
22
23 #include "base/i2-base.hpp"
24 #include "base/object.hpp"
25 #include "base/value.hpp"
26 #include <boost/range/iterator.hpp>
27 #include <map>
28 #include <vector>
29
30 namespace icinga
31 {
32
33 /**
34  * A container that holds key-value pairs.
35  *
36  * @ingroup base
37  */
38 class I2_BASE_API Dictionary : public Object
39 {
40 public:
41         DECLARE_OBJECT(Dictionary);
42
43         /**
44          * An iterator that can be used to iterate over dictionary elements.
45          */
46         typedef std::map<String, Value>::iterator Iterator;
47
48         typedef std::map<String, Value>::size_type SizeType;
49
50         typedef std::pair<String, Value> Pair;
51
52         inline Dictionary(void)
53         { }
54
55         inline ~Dictionary(void)
56         { }
57
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;
62
63         /**
64          * Returns an iterator to the beginning of the dictionary.
65          *
66          * Note: Caller must hold the object lock while using the iterator.
67          *
68          * @returns An iterator.
69          */
70         inline Iterator Begin(void)
71         {
72                 ASSERT(OwnsLock());
73
74                 return m_Data.begin();
75         }
76
77         /**
78          * Returns an iterator to the end of the dictionary.
79          *
80          * Note: Caller must hold the object lock while using the iterator.
81          *
82          * @returns An iterator.
83          */
84         inline Iterator End(void)
85         {
86                 ASSERT(OwnsLock());
87
88                 return m_Data.end();
89         }
90
91         size_t GetLength(void) const;
92
93         void Remove(const String& key);
94
95         /**
96          * Removes the item specified by the iterator from the dictionary.
97          *
98          * @param it The iterator.
99          */
100         inline void Remove(Iterator it)
101         {
102                 ASSERT(OwnsLock());
103
104                 m_Data.erase(it);
105         }
106
107         void Clear(void);
108
109         void CopyTo(const Dictionary::Ptr& dest) const;
110         Dictionary::Ptr ShallowClone(void) const;
111
112         std::vector<String> GetKeys(void) const;
113
114         static Object::Ptr GetPrototype(void);
115         
116         virtual Object::Ptr Clone(void) const override;
117
118         virtual String ToString(void) const override;
119
120 private:
121         std::map<String, Value> m_Data; /**< The data for the dictionary. */
122 };
123
124 inline Dictionary::Iterator range_begin(Dictionary::Ptr x)
125 {
126         return x->Begin();
127 }
128
129 inline Dictionary::Iterator range_end(Dictionary::Ptr x)
130 {
131         return x->End();
132 }
133
134 }
135
136 namespace boost
137 {
138
139 template<>
140 struct range_mutable_iterator<icinga::Dictionary::Ptr>
141 {
142         typedef icinga::Dictionary::Iterator type;
143 };
144
145 template<>
146 struct range_const_iterator<icinga::Dictionary::Ptr>
147 {
148         typedef icinga::Dictionary::Iterator type;
149 };
150
151 }
152
153 #endif /* DICTIONARY_H */