]> granicus.if.org Git - icinga2/blob - lib/base/loader.hpp
Update copyright headers for 2016
[icinga2] / lib / base / loader.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 LOADER_H
21 #define LOADER_H
22
23 #include "base/i2-base.hpp"
24 #include "base/string.hpp"
25 #include <boost/thread/tss.hpp>
26 #include <boost/function.hpp>
27 #include <queue>
28
29 namespace icinga
30 {
31
32 struct DeferredInitializer
33 {
34 public:
35         DeferredInitializer(const boost::function<void (void)>& callback, int priority)
36             : m_Callback(callback), m_Priority(priority)
37         { }
38
39         inline bool operator<(const DeferredInitializer& other) const
40         {
41                 return m_Priority < other.m_Priority;
42         }
43
44         inline void operator()(void)
45         {
46                 m_Callback();
47         }
48
49 private:
50         boost::function<void (void)> m_Callback;
51         int m_Priority;
52 };
53
54 /**
55  * Loader helper functions.
56  *
57  * @ingroup base
58  */
59 class I2_BASE_API Loader
60 {
61 public:
62         static void LoadExtensionLibrary(const String& library);
63
64         static void AddDeferredInitializer(const boost::function<void(void)>& callback, int priority = 0);
65         static void ExecuteDeferredInitializers(void);
66
67 private:
68         Loader(void);
69
70         static boost::thread_specific_ptr<std::priority_queue<DeferredInitializer> >& GetDeferredInitializers(void);
71
72 };
73
74 }
75
76 #endif /* LOADER_H */