]> granicus.if.org Git - icinga2/blob - lib/base/configobject.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / configobject.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef CONFIGOBJECT_H
4 #define CONFIGOBJECT_H
5
6 #include "base/i2-base.hpp"
7 #include "base/configobject-ti.hpp"
8 #include "base/object.hpp"
9 #include "base/type.hpp"
10 #include "base/dictionary.hpp"
11 #include <boost/signals2.hpp>
12
13 namespace icinga
14 {
15
16 class ConfigType;
17
18 /**
19  * A dynamic object that can be instantiated from the configuration file.
20  *
21  * @ingroup base
22  */
23 class ConfigObject : public ObjectImpl<ConfigObject>
24 {
25 public:
26         DECLARE_OBJECT(ConfigObject);
27
28         static boost::signals2::signal<void (const ConfigObject::Ptr&)> OnStateChanged;
29
30         bool IsActive() const;
31         bool IsPaused() const;
32
33         void SetExtension(const String& key, const Value& value);
34         Value GetExtension(const String& key);
35         void ClearExtension(const String& key);
36
37         ConfigObject::Ptr GetZone() const;
38
39         void ModifyAttribute(const String& attr, const Value& value, bool updateVersion = true);
40         void RestoreAttribute(const String& attr, bool updateVersion = true);
41         bool IsAttributeModified(const String& attr) const;
42
43         void Register();
44         void Unregister();
45
46         void PreActivate();
47         void Activate(bool runtimeCreated = false);
48         void Deactivate(bool runtimeRemoved = false);
49         void SetAuthority(bool authority);
50
51         void Start(bool runtimeCreated = false) override;
52         void Stop(bool runtimeRemoved = false) override;
53
54         virtual void Pause();
55         virtual void Resume();
56
57         virtual void OnConfigLoaded();
58         virtual void CreateChildObjects(const Type::Ptr& childType);
59         virtual void OnAllConfigLoaded();
60         virtual void OnStateLoaded();
61
62         Dictionary::Ptr GetSourceLocation() const override;
63
64         template<typename T>
65         static intrusive_ptr<T> GetObject(const String& name)
66         {
67                 typedef TypeImpl<T> ObjType;
68                 auto *ptype = static_cast<ObjType *>(T::TypeInstance.get());
69                 return static_pointer_cast<T>(ptype->GetObject(name));
70         }
71
72         static ConfigObject::Ptr GetObject(const String& type, const String& name);
73
74         static void DumpObjects(const String& filename, int attributeTypes = FAState);
75         static void RestoreObjects(const String& filename, int attributeTypes = FAState);
76         static void StopObjects();
77
78         static void DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&)>& callback);
79
80         static Object::Ptr GetPrototype();
81
82 private:
83         ConfigObject::Ptr m_Zone;
84
85         static void RestoreObject(const String& message, int attributeTypes);
86 };
87
88 #define DECLARE_OBJECTNAME(klass)                                               \
89         inline static String GetTypeName()                                      \
90         {                                                                       \
91                 return #klass;                                                  \
92         }                                                                       \
93                                                                                 \
94         inline static intrusive_ptr<klass> GetByName(const String& name)        \
95         {                                                                       \
96                 return ConfigObject::GetObject<klass>(name);                    \
97         }
98
99 }
100
101 #endif /* CONFIGOBJECT_H */