]> granicus.if.org Git - icinga2/blob - lib/base/loader.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / loader.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef LOADER_H
4 #define LOADER_H
5
6 #include "base/i2-base.hpp"
7 #include "base/string.hpp"
8 #include <boost/thread/tss.hpp>
9 #include <queue>
10
11 namespace icinga
12 {
13
14 struct DeferredInitializer
15 {
16 public:
17         DeferredInitializer(std::function<void ()> callback, int priority)
18                 : m_Callback(std::move(callback)), m_Priority(priority)
19         { }
20
21         bool operator<(const DeferredInitializer& other) const
22         {
23                 return m_Priority < other.m_Priority;
24         }
25
26         void operator()()
27         {
28                 m_Callback();
29         }
30
31 private:
32         std::function<void ()> m_Callback;
33         int m_Priority;
34 };
35
36 /**
37  * Loader helper functions.
38  *
39  * @ingroup base
40  */
41 class Loader
42 {
43 public:
44         static void AddDeferredInitializer(const std::function<void ()>& callback, int priority = 0);
45         static void ExecuteDeferredInitializers();
46
47 private:
48         Loader();
49
50         static boost::thread_specific_ptr<std::priority_queue<DeferredInitializer> >& GetDeferredInitializers();
51 };
52
53 }
54
55 #endif /* LOADER_H */