]> granicus.if.org Git - icinga2/blob - lib/base/dictionary.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / dictionary.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef DICTIONARY_H
4 #define DICTIONARY_H
5
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include "base/value.hpp"
9 #include <boost/range/iterator.hpp>
10 #include <map>
11 #include <vector>
12
13 namespace icinga
14 {
15
16 typedef std::vector<std::pair<String, Value> > DictionaryData;
17
18 /**
19  * A container that holds key-value pairs.
20  *
21  * @ingroup base
22  */
23 class Dictionary final : public Object
24 {
25 public:
26         DECLARE_OBJECT(Dictionary);
27
28         /**
29          * An iterator that can be used to iterate over dictionary elements.
30          */
31         typedef std::map<String, Value>::iterator Iterator;
32
33         typedef std::map<String, Value>::size_type SizeType;
34
35         typedef std::map<String, Value>::value_type Pair;
36
37         Dictionary() = default;
38         Dictionary(const DictionaryData& other);
39         Dictionary(DictionaryData&& other);
40         Dictionary(std::initializer_list<Pair> init);
41
42         Value Get(const String& key) const;
43         bool Get(const String& key, Value *result) const;
44         void Set(const String& key, Value value, bool overrideFrozen = false);
45         bool Contains(const String& key) const;
46
47         Iterator Begin();
48         Iterator End();
49
50         size_t GetLength() const;
51
52         void Remove(const String& key, bool overrideFrozen = false);
53
54         void Remove(Iterator it, bool overrideFrozen = false);
55
56         void Clear(bool overrideFrozen = false);
57
58         void CopyTo(const Dictionary::Ptr& dest) const;
59         Dictionary::Ptr ShallowClone() const;
60
61         std::vector<String> GetKeys() const;
62
63         static Object::Ptr GetPrototype();
64
65         Object::Ptr Clone() const override;
66
67         String ToString() const override;
68
69         void Freeze();
70
71         Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
72         void SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
73         bool HasOwnField(const String& field) const override;
74         bool GetOwnField(const String& field, Value *result) const override;
75
76 private:
77         std::map<String, Value> m_Data; /**< The data for the dictionary. */
78         bool m_Frozen{false};
79 };
80
81 Dictionary::Iterator begin(const Dictionary::Ptr& x);
82 Dictionary::Iterator end(const Dictionary::Ptr& x);
83
84 }
85
86 extern template class std::map<icinga::String, icinga::Value>;
87
88 #endif /* DICTIONARY_H */