Skip to content

Commit b5b0921

Browse files
committed
Use lambda functions for INITIALIZE_ONCE
fixes #12562
1 parent 5058c5d commit b5b0921

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+185
-354
lines changed

CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,8 @@ include(CheckIncludeFileCXX)
217217

218218
check_symbol_exists(__COUNTER__ "" HAVE_COUNTER_MACRO)
219219

220-
if(NOT HAVE_COUNTER_MACRO AND ICINGA2_UNITY_BUILD)
221-
message(STATUS "Your C/C++ compiler does not support the __COUNTER__ macro. Disabling unity build.")
222-
set(ICINGA2_UNITY_BUILD FALSE)
220+
if(NOT HAVE_COUNTER_MACRO)
221+
message(FATAL_ERROR "Your C/C++ compiler does not support the __COUNTER__ macro.")
223222
endif()
224223

225224
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DI2_DEBUG")

config.h.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef CONFIG_H
22
#define CONFIG_H
33

4-
#cmakedefine HAVE_COUNTER_MACRO
54
#cmakedefine HAVE_BACKTRACE_SYMBOLS
65
#cmakedefine HAVE_PIPE2
76
#cmakedefine HAVE_VFORK

lib/base/console.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@
2323

2424
using namespace icinga;
2525

26-
INITIALIZE_ONCE(&Console::DetectType);
27-
2826
static ConsoleType l_ConsoleType = Console_Dumb;
2927

28+
INITIALIZE_ONCE([]() {
29+
l_ConsoleType = Console_Dumb;
30+
31+
#ifndef _WIN32
32+
if (isatty(1))
33+
l_ConsoleType = Console_VT100;
34+
#else /* _WIN32 */
35+
l_ConsoleType = Console_Windows;
36+
#endif /* _WIN32 */
37+
});
38+
3039
ConsoleColorTag::ConsoleColorTag(int color, ConsoleType consoleType)
3140
: m_Color(color), m_ConsoleType(consoleType)
3241
{ }
@@ -46,18 +55,6 @@ std::ostream& icinga::operator<<(std::ostream& fp, const ConsoleColorTag& cct)
4655
return fp;
4756
}
4857

49-
void Console::DetectType(void)
50-
{
51-
l_ConsoleType = Console_Dumb;
52-
53-
#ifndef _WIN32
54-
if (isatty(1))
55-
l_ConsoleType = Console_VT100;
56-
#else /* _WIN32 */
57-
l_ConsoleType = Console_Windows;
58-
#endif /* _WIN32 */
59-
}
60-
6158
void Console::SetType(std::ostream& fp, ConsoleType type)
6259
{
6360
if (&fp == &std::cout || &fp == &std::cerr)

lib/base/function.hpp

+32-50
Original file line numberDiff line numberDiff line change
@@ -67,66 +67,48 @@ class I2_BASE_API Function : public ObjectImpl<Function>
6767
};
6868

6969
#define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback) \
70-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
71-
void RegisterFunction(void) { \
72-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
73-
ScriptGlobal::Set(#ns "." #name, sf); \
74-
} \
75-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
76-
} } }
70+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
71+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
72+
ScriptGlobal::Set(#ns "." #name, sf); \
73+
}, 10)
7774

7875
#define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \
79-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
80-
void RegisterFunction(void) { \
81-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
82-
ScriptGlobal::Set(#ns "." #name, sf); \
83-
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), false, true); \
84-
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
85-
} \
86-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
87-
} } }
76+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
77+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
78+
ScriptGlobal::Set(#ns "." #name, sf); \
79+
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), false, true); \
80+
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
81+
}, 10)
8882

8983
#define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \
90-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
91-
void RegisterFunction(void) { \
92-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
93-
ScriptGlobal::Set(#ns "." #name, sf); \
94-
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), false, true); \
95-
ScriptGlobal::Set("Deprecated." #name, dsf); \
96-
} \
97-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
98-
} } }
84+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
85+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \
86+
ScriptGlobal::Set(#ns "." #name, sf); \
87+
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), false, true); \
88+
ScriptGlobal::Set("Deprecated." #name, dsf); \
89+
}, 10)
9990

10091
#define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback) \
101-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
102-
void RegisterFunction(void) { \
103-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
104-
ScriptGlobal::Set(#ns "." #name, sf); \
105-
} \
106-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
107-
} } }
92+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
93+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
94+
ScriptGlobal::Set(#ns "." #name, sf); \
95+
}, 10)
10896

10997
#define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \
110-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
111-
void RegisterFunction(void) { \
112-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
113-
ScriptGlobal::Set(#ns "." #name, sf); \
114-
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), true, true); \
115-
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
116-
} \
117-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
118-
} } }
98+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
99+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
100+
ScriptGlobal::Set(#ns "." #name, sf); \
101+
Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), true, true); \
102+
ScriptGlobal::Set("Deprecated.__" #name, dsf); \
103+
}, 10)
119104

120105
#define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \
121-
namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \
122-
void RegisterFunction(void) { \
123-
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
124-
ScriptGlobal::Set(#ns "." #name, sf); \
125-
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), true, true); \
126-
ScriptGlobal::Set("Deprecated." #name, dsf); \
127-
} \
128-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \
129-
} } }
106+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
107+
Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \
108+
ScriptGlobal::Set(#ns "." #name, sf); \
109+
Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), true, true); \
110+
ScriptGlobal::Set("Deprecated." #name, dsf); \
111+
}, 10)
130112

131113
}
132114

lib/base/json-script.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ static String JsonEncodeShim(const Value& value)
3131
return JsonEncode(value);
3232
}
3333

34-
static void InitializeJsonObj(void)
35-
{
34+
INITIALIZE_ONCE([]() {
3635
Dictionary::Ptr jsonObj = new Dictionary();
3736

3837
/* Methods */
3938
jsonObj->Set("encode", new Function("Json#encode", WrapFunction(JsonEncodeShim), true));
4039
jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), true));
4140

4241
ScriptGlobal::Set("Json", jsonObj);
43-
}
44-
45-
INITIALIZE_ONCE(InitializeJsonObj);
46-
42+
});

lib/base/logger.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,20 @@
3131
using namespace icinga;
3232

3333
REGISTER_TYPE(Logger);
34-
INITIALIZE_ONCE(&Logger::StaticInitialize);
3534

3635
std::set<Logger::Ptr> Logger::m_Loggers;
3736
boost::mutex Logger::m_Mutex;
3837
bool Logger::m_ConsoleLogEnabled = true;
3938
bool Logger::m_TimestampEnabled = true;
4039
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
4140

42-
void Logger::StaticInitialize(void)
43-
{
41+
INITIALIZE_ONCE([]() {
4442
ScriptGlobal::Set("LogDebug", LogDebug);
4543
ScriptGlobal::Set("LogNotice", LogNotice);
4644
ScriptGlobal::Set("LogInformation", LogInformation);
4745
ScriptGlobal::Set("LogWarning", LogWarning);
4846
ScriptGlobal::Set("LogCritical", LogCritical);
49-
}
47+
});
5048

5149
/**
5250
* Constructor for the Logger class.

lib/base/logger.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class I2_BASE_API Logger : public ObjectImpl<Logger>
9090
static void SetConsoleLogSeverity(LogSeverity logSeverity);
9191
static LogSeverity GetConsoleLogSeverity(void);
9292

93-
static void StaticInitialize(void);
94-
9593
virtual void ValidateSeverity(const String& value, const ValidationUtils& utils) override;
9694

9795
protected:

lib/base/math-script.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ static double MathSign(double x)
158158
return 0;
159159
}
160160

161-
static void InitializeMathObj(void)
162-
{
161+
INITIALIZE_ONCE([]() {
163162
Dictionary::Ptr mathObj = new Dictionary();
164163

165164
/* Constants */
@@ -196,7 +195,4 @@ static void InitializeMathObj(void)
196195
mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), true));
197196

198197
ScriptGlobal::Set("Math", mathObj);
199-
}
200-
201-
INITIALIZE_ONCE(InitializeMathObj);
202-
198+
});

lib/base/object.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,11 @@ static void TypeInfoTimerHandler(void)
237237
}
238238
}
239239

240-
static void StartTypeInfoTimer(void)
241-
{
240+
INITIALIZE_ONCE([]() {
242241
l_ObjectCountTimer = new Timer();
243242
l_ObjectCountTimer->SetInterval(10);
244243
l_ObjectCountTimer->OnTimerExpired.connect(boost::bind(TypeInfoTimerHandler));
245244
l_ObjectCountTimer->Start();
246-
}
247-
248-
INITIALIZE_ONCE(StartTypeInfoTimer);
245+
});
249246
#endif /* I2_LEAK_DEBUG */
250247

lib/base/objecttype.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222

2323
using namespace icinga;
2424

25-
static void RegisterObjectType(void)
26-
{
25+
INITIALIZE_ONCE_WITH_PRIORITY([]() {
2726
Type::Ptr type = new ObjectType();
2827
type->SetPrototype(Object::GetPrototype());
2928
Type::Register(type);
3029
Object::TypeInstance = type;
31-
}
32-
33-
INITIALIZE_ONCE_WITH_PRIORITY(&RegisterObjectType, 20);
30+
}, 20);
3431

3532
ObjectType::ObjectType(void)
3633
{ }

lib/base/primitivetype.hpp

+11-19
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,19 @@ class I2_BASE_API PrimitiveType : public Type
4949
};
5050

5151
#define REGISTER_BUILTIN_TYPE(type, prototype) \
52-
namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \
53-
void RegisterBuiltinType(void) \
54-
{ \
55-
icinga::Type::Ptr t = new PrimitiveType(#type, "None"); \
56-
t->SetPrototype(prototype); \
57-
icinga::Type::Register(t); \
58-
} \
59-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterBuiltinType, 15); \
60-
} } }
52+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
53+
icinga::Type::Ptr t = new PrimitiveType(#type, "None"); \
54+
t->SetPrototype(prototype); \
55+
icinga::Type::Register(t); \
56+
}, 15)
6157

6258
#define REGISTER_PRIMITIVE_TYPE_FACTORY(type, base, prototype, factory) \
63-
namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \
64-
void RegisterPrimitiveType(void) \
65-
{ \
66-
icinga::Type::Ptr t = new PrimitiveType(#type, #base, factory);\
67-
t->SetPrototype(prototype); \
68-
icinga::Type::Register(t); \
69-
type::TypeInstance = t; \
70-
} \
71-
INITIALIZE_ONCE_WITH_PRIORITY(RegisterPrimitiveType, 15); \
72-
} } } \
59+
INITIALIZE_ONCE_WITH_PRIORITY([]() { \
60+
icinga::Type::Ptr t = new PrimitiveType(#type, #base, factory); \
61+
t->SetPrototype(prototype); \
62+
icinga::Type::Register(t); \
63+
type::TypeInstance = t; \
64+
}, 15); \
7365
DEFINE_TYPE_INSTANCE(type)
7466

7567
#define REGISTER_PRIMITIVE_TYPE(type, base, prototype) \

lib/base/process.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ static std::map<Process::ConsoleHandle, Process::ProcessHandle> l_FDs[IOTHREADS]
5656
#endif /* _WIN32 */
5757
static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT;
5858

59-
INITIALIZE_ONCE(&Process::StaticInitialize);
60-
6159
Process::Process(const Process::Arguments& arguments, const Dictionary::Ptr& extraEnvironment)
6260
: m_Arguments(arguments), m_ExtraEnvironment(extraEnvironment), m_Timeout(600)
6361
#ifdef _WIN32
@@ -76,8 +74,7 @@ Process::~Process(void)
7674
#endif /* _WIN32 */
7775
}
7876

79-
void Process::StaticInitialize(void)
80-
{
77+
INITIALIZE_ONCE([]() {
8178
for (int tid = 0; tid < IOTHREADS; tid++) {
8279
#ifdef _WIN32
8380
l_Events[tid] = CreateEvent(NULL, TRUE, FALSE, NULL);
@@ -104,7 +101,7 @@ void Process::StaticInitialize(void)
104101
# endif /* HAVE_PIPE2 */
105102
#endif /* _WIN32 */
106103
}
107-
}
104+
});
108105

109106
void Process::ThreadInitialize(void)
110107
{

lib/base/process.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class I2_BASE_API Process : public Object
7979

8080
static Arguments PrepareCommand(const Value& command);
8181

82-
static void StaticInitialize(void);
8382
static void ThreadInitialize(void);
8483

8584
static String PrettyPrintArguments(const Arguments& arguments);

lib/base/scriptframe.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ using namespace icinga;
2626
boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
2727
Array::Ptr ScriptFrame::m_Imports;
2828

29-
INITIALIZE_ONCE_WITH_PRIORITY(&ScriptFrame::StaticInitialize, 50);
30-
31-
void ScriptFrame::StaticInitialize(void)
32-
{
29+
INITIALIZE_ONCE_WITH_PRIORITY([]() {
3330
Dictionary::Ptr systemNS = new Dictionary();
3431
ScriptGlobal::Set("System", systemNS);
35-
AddImport(systemNS);
32+
ScriptFrame::AddImport(systemNS);
3633

3734
Dictionary::Ptr typesNS = new Dictionary();
3835
ScriptGlobal::Set("Types", typesNS);
39-
AddImport(typesNS);
36+
ScriptFrame::AddImport(typesNS);
4037

4138
Dictionary::Ptr deprecatedNS = new Dictionary();
4239
ScriptGlobal::Set("Deprecated", deprecatedNS);
43-
AddImport(deprecatedNS);
44-
}
40+
ScriptFrame::AddImport(deprecatedNS);
41+
}, 50);
4542

4643
ScriptFrame::ScriptFrame(void)
4744
: Locals(new Dictionary()), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0)

lib/base/scriptframe.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ struct I2_BASE_API ScriptFrame
4040
ScriptFrame(const Value& self);
4141
~ScriptFrame(void);
4242

43-
static void StaticInitialize(void);
44-
4543
void IncreaseStackDepth(void);
4644
void DecreaseStackDepth(void);
4745

0 commit comments

Comments
 (0)