]> granicus.if.org Git - icinga2/blob - lib/base/namespace.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / namespace.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef NAMESPACE_H
4 #define NAMESPACE_H
5
6 #include "base/i2-base.hpp"
7 #include "base/object.hpp"
8 #include "base/value.hpp"
9 #include "base/debuginfo.hpp"
10 #include <map>
11 #include <vector>
12 #include <memory>
13
14 namespace icinga
15 {
16
17 struct NamespaceValue
18 {
19         virtual Value Get(const DebugInfo& debugInfo = DebugInfo()) const = 0;
20         virtual void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo = DebugInfo()) = 0;
21 };
22
23 struct EmbeddedNamespaceValue : public NamespaceValue
24 {
25         EmbeddedNamespaceValue(const Value& value);
26
27         Value Get(const DebugInfo& debugInfo) const override;
28         void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
29
30 private:
31         Value m_Value;
32 };
33
34 struct ConstEmbeddedNamespaceValue : public EmbeddedNamespaceValue
35 {
36         using EmbeddedNamespaceValue::EmbeddedNamespaceValue;
37
38         void Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
39 };
40
41 class Namespace;
42
43 struct NamespaceBehavior
44 {
45         virtual void Register(const boost::intrusive_ptr<Namespace>& ns, const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) const;
46         virtual void Remove(const boost::intrusive_ptr<Namespace>& ns, const String& field, bool overrideFrozen);
47 };
48
49 struct ConstNamespaceBehavior : public NamespaceBehavior
50 {
51         void Register(const boost::intrusive_ptr<Namespace>& ns, const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) const override;
52         void Remove(const boost::intrusive_ptr<Namespace>& ns, const String& field, bool overrideFrozen) override;
53         void Freeze();
54
55 private:
56         bool m_Frozen;
57 };
58
59 /**
60  * A namespace.
61  *
62  * @ingroup base
63  */
64 class Namespace final : public Object
65 {
66 public:
67         DECLARE_OBJECT(Namespace);
68
69         typedef std::map<String, std::shared_ptr<NamespaceValue> >::iterator Iterator;
70
71         typedef std::map<String, std::shared_ptr<NamespaceValue> >::value_type Pair;
72
73         Namespace(NamespaceBehavior *behavior = new NamespaceBehavior);
74
75         Value Get(const String& field) const;
76         bool Get(const String& field, Value *value) const;
77         void Set(const String& field, const Value& value, bool overrideFrozen = false);
78         bool Contains(const String& field) const;
79         void Remove(const String& field, bool overrideFrozen = false);
80
81         std::shared_ptr<NamespaceValue> GetAttribute(const String& field) const;
82         void SetAttribute(const String& field, const std::shared_ptr<NamespaceValue>& nsVal);
83         void RemoveAttribute(const String& field);
84
85         Iterator Begin();
86         Iterator End();
87
88         Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
89         void SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) override;
90         bool HasOwnField(const String& field) const override;
91         bool GetOwnField(const String& field, Value *result) const override;
92
93         static Object::Ptr GetPrototype();
94
95 private:
96         std::map<String, std::shared_ptr<NamespaceValue> > m_Data;
97         std::unique_ptr<NamespaceBehavior> m_Behavior;
98 };
99
100 Namespace::Iterator begin(const Namespace::Ptr& x);
101 Namespace::Iterator end(const Namespace::Ptr& x);
102
103 }
104
105 extern template class std::map<icinga::String, std::shared_ptr<icinga::NamespaceValue> >;
106
107 #endif /* NAMESPACE_H */