diff --git a/CMakeLists.txt b/CMakeLists.txt index dbb3e7dbf..6abd47f79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,9 +217,8 @@ include(CheckIncludeFileCXX) check_symbol_exists(__COUNTER__ "" HAVE_COUNTER_MACRO) -if(NOT HAVE_COUNTER_MACRO AND ICINGA2_UNITY_BUILD) - message(STATUS "Your C/C++ compiler does not support the __COUNTER__ macro. Disabling unity build.") - set(ICINGA2_UNITY_BUILD FALSE) +if(NOT HAVE_COUNTER_MACRO) +message(FATAL_ERROR "Your C/C++ compiler does not support the __COUNTER__ macro.") endif() set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DI2_DEBUG") diff --git a/config.h.cmake b/config.h.cmake index 7d67840b4..1903b287c 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -1,7 +1,6 @@ #ifndef CONFIG_H #define CONFIG_H -#cmakedefine HAVE_COUNTER_MACRO #cmakedefine HAVE_BACKTRACE_SYMBOLS #cmakedefine HAVE_PIPE2 #cmakedefine HAVE_VFORK diff --git a/lib/base/console.cpp b/lib/base/console.cpp index aa47017f6..7d4257c19 100644 --- a/lib/base/console.cpp +++ b/lib/base/console.cpp @@ -23,10 +23,19 @@ using namespace icinga; -INITIALIZE_ONCE(&Console::DetectType); - static ConsoleType l_ConsoleType = Console_Dumb; +INITIALIZE_ONCE([]() { + l_ConsoleType = Console_Dumb; + +#ifndef _WIN32 + if (isatty(1)) + l_ConsoleType = Console_VT100; +#else /* _WIN32 */ + l_ConsoleType = Console_Windows; +#endif /* _WIN32 */ +}); + ConsoleColorTag::ConsoleColorTag(int color, ConsoleType consoleType) : m_Color(color), m_ConsoleType(consoleType) { } @@ -46,18 +55,6 @@ std::ostream& icinga::operator<<(std::ostream& fp, const ConsoleColorTag& cct) return fp; } -void Console::DetectType(void) -{ - l_ConsoleType = Console_Dumb; - -#ifndef _WIN32 - if (isatty(1)) - l_ConsoleType = Console_VT100; -#else /* _WIN32 */ - l_ConsoleType = Console_Windows; -#endif /* _WIN32 */ -} - void Console::SetType(std::ostream& fp, ConsoleType type) { if (&fp == &std::cout || &fp == &std::cerr) diff --git a/lib/base/function.hpp b/lib/base/function.hpp index 32033c630..dbf277f09 100644 --- a/lib/base/function.hpp +++ b/lib/base/function.hpp @@ -67,66 +67,48 @@ private: }; #define REGISTER_SCRIPTFUNCTION_NS(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + }, 10) #define REGISTER_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), false, true); \ - ScriptGlobal::Set("Deprecated.__" #name, dsf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), false, true); \ + ScriptGlobal::Set("Deprecated.__" #name, dsf); \ + }, 10) #define REGISTER_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), false, true); \ - ScriptGlobal::Set("Deprecated." #name, dsf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), false); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), false, true); \ + ScriptGlobal::Set("Deprecated." #name, dsf); \ + }, 10) #define REGISTER_SAFE_SCRIPTFUNCTION_NS(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + }, 10) #define REGISTER_SAFE_SCRIPTFUNCTION_NS_PREFIX(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), true, true); \ - ScriptGlobal::Set("Deprecated.__" #name, dsf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + Function::Ptr dsf = new icinga::Function("Deprecated#__" #name " (deprecated)", WrapFunction(callback), true, true); \ + ScriptGlobal::Set("Deprecated.__" #name, dsf); \ + }, 10) #define REGISTER_SAFE_SCRIPTFUNCTION_NS_DEPRECATED(ns, name, callback) \ - namespace { namespace UNIQUE_NAME(sf) { namespace sf ## ns ## name { \ - void RegisterFunction(void) { \ - Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ - ScriptGlobal::Set(#ns "." #name, sf); \ - Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), true, true); \ - ScriptGlobal::Set("Deprecated." #name, dsf); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterFunction, 10); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + Function::Ptr sf = new icinga::Function(#ns "#" #name, WrapFunction(callback), true); \ + ScriptGlobal::Set(#ns "." #name, sf); \ + Function::Ptr dsf = new icinga::Function("Deprecated#" #name " (deprecated)", WrapFunction(callback), true, true); \ + ScriptGlobal::Set("Deprecated." #name, dsf); \ + }, 10) } diff --git a/lib/base/json-script.cpp b/lib/base/json-script.cpp index 9cf68b35b..d6de15053 100644 --- a/lib/base/json-script.cpp +++ b/lib/base/json-script.cpp @@ -31,8 +31,7 @@ static String JsonEncodeShim(const Value& value) return JsonEncode(value); } -static void InitializeJsonObj(void) -{ +INITIALIZE_ONCE([]() { Dictionary::Ptr jsonObj = new Dictionary(); /* Methods */ @@ -40,7 +39,4 @@ static void InitializeJsonObj(void) jsonObj->Set("decode", new Function("Json#decode", WrapFunction(JsonDecode), true)); ScriptGlobal::Set("Json", jsonObj); -} - -INITIALIZE_ONCE(InitializeJsonObj); - +}); diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 3c132800e..d0778339d 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -31,7 +31,6 @@ using namespace icinga; REGISTER_TYPE(Logger); -INITIALIZE_ONCE(&Logger::StaticInitialize); std::set Logger::m_Loggers; boost::mutex Logger::m_Mutex; @@ -39,14 +38,13 @@ bool Logger::m_ConsoleLogEnabled = true; bool Logger::m_TimestampEnabled = true; LogSeverity Logger::m_ConsoleLogSeverity = LogInformation; -void Logger::StaticInitialize(void) -{ +INITIALIZE_ONCE([]() { ScriptGlobal::Set("LogDebug", LogDebug); ScriptGlobal::Set("LogNotice", LogNotice); ScriptGlobal::Set("LogInformation", LogInformation); ScriptGlobal::Set("LogWarning", LogWarning); ScriptGlobal::Set("LogCritical", LogCritical); -} +}); /** * Constructor for the Logger class. diff --git a/lib/base/logger.hpp b/lib/base/logger.hpp index 36e727d0e..c3e780e3f 100644 --- a/lib/base/logger.hpp +++ b/lib/base/logger.hpp @@ -90,8 +90,6 @@ public: static void SetConsoleLogSeverity(LogSeverity logSeverity); static LogSeverity GetConsoleLogSeverity(void); - static void StaticInitialize(void); - virtual void ValidateSeverity(const String& value, const ValidationUtils& utils) override; protected: diff --git a/lib/base/math-script.cpp b/lib/base/math-script.cpp index 6c5101eef..5623a4ef6 100644 --- a/lib/base/math-script.cpp +++ b/lib/base/math-script.cpp @@ -158,8 +158,7 @@ static double MathSign(double x) return 0; } -static void InitializeMathObj(void) -{ +INITIALIZE_ONCE([]() { Dictionary::Ptr mathObj = new Dictionary(); /* Constants */ @@ -196,7 +195,4 @@ static void InitializeMathObj(void) mathObj->Set("sign", new Function("Math#sign", WrapFunction(MathSign), true)); ScriptGlobal::Set("Math", mathObj); -} - -INITIALIZE_ONCE(InitializeMathObj); - +}); diff --git a/lib/base/object.cpp b/lib/base/object.cpp index 4d149006f..3ac220df3 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -237,14 +237,11 @@ static void TypeInfoTimerHandler(void) } } -static void StartTypeInfoTimer(void) -{ +INITIALIZE_ONCE([]() { l_ObjectCountTimer = new Timer(); l_ObjectCountTimer->SetInterval(10); l_ObjectCountTimer->OnTimerExpired.connect(boost::bind(TypeInfoTimerHandler)); l_ObjectCountTimer->Start(); -} - -INITIALIZE_ONCE(StartTypeInfoTimer); +}); #endif /* I2_LEAK_DEBUG */ diff --git a/lib/base/objecttype.cpp b/lib/base/objecttype.cpp index 764fdbaf9..e10972e21 100644 --- a/lib/base/objecttype.cpp +++ b/lib/base/objecttype.cpp @@ -22,15 +22,12 @@ using namespace icinga; -static void RegisterObjectType(void) -{ +INITIALIZE_ONCE_WITH_PRIORITY([]() { Type::Ptr type = new ObjectType(); type->SetPrototype(Object::GetPrototype()); Type::Register(type); Object::TypeInstance = type; -} - -INITIALIZE_ONCE_WITH_PRIORITY(&RegisterObjectType, 20); +}, 20); ObjectType::ObjectType(void) { } diff --git a/lib/base/primitivetype.hpp b/lib/base/primitivetype.hpp index 1d80b0e1e..a7f65f89d 100644 --- a/lib/base/primitivetype.hpp +++ b/lib/base/primitivetype.hpp @@ -49,27 +49,19 @@ private: }; #define REGISTER_BUILTIN_TYPE(type, prototype) \ - namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \ - void RegisterBuiltinType(void) \ - { \ - icinga::Type::Ptr t = new PrimitiveType(#type, "None"); \ - t->SetPrototype(prototype); \ - icinga::Type::Register(t); \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterBuiltinType, 15); \ - } } } + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + icinga::Type::Ptr t = new PrimitiveType(#type, "None"); \ + t->SetPrototype(prototype); \ + icinga::Type::Register(t); \ + }, 15) #define REGISTER_PRIMITIVE_TYPE_FACTORY(type, base, prototype, factory) \ - namespace { namespace UNIQUE_NAME(prt) { namespace prt ## type { \ - void RegisterPrimitiveType(void) \ - { \ - icinga::Type::Ptr t = new PrimitiveType(#type, #base, factory);\ - t->SetPrototype(prototype); \ - icinga::Type::Register(t); \ - type::TypeInstance = t; \ - } \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterPrimitiveType, 15); \ - } } } \ + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + icinga::Type::Ptr t = new PrimitiveType(#type, #base, factory); \ + t->SetPrototype(prototype); \ + icinga::Type::Register(t); \ + type::TypeInstance = t; \ + }, 15); \ DEFINE_TYPE_INSTANCE(type) #define REGISTER_PRIMITIVE_TYPE(type, base, prototype) \ diff --git a/lib/base/process.cpp b/lib/base/process.cpp index 6e90efc56..1b1bb690b 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -56,8 +56,6 @@ static std::map l_FDs[IOTHREADS] #endif /* _WIN32 */ static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT; -INITIALIZE_ONCE(&Process::StaticInitialize); - Process::Process(const Process::Arguments& arguments, const Dictionary::Ptr& extraEnvironment) : m_Arguments(arguments), m_ExtraEnvironment(extraEnvironment), m_Timeout(600) #ifdef _WIN32 @@ -76,8 +74,7 @@ Process::~Process(void) #endif /* _WIN32 */ } -void Process::StaticInitialize(void) -{ +INITIALIZE_ONCE([]() { for (int tid = 0; tid < IOTHREADS; tid++) { #ifdef _WIN32 l_Events[tid] = CreateEvent(NULL, TRUE, FALSE, NULL); @@ -104,7 +101,7 @@ void Process::StaticInitialize(void) # endif /* HAVE_PIPE2 */ #endif /* _WIN32 */ } -} +}); void Process::ThreadInitialize(void) { diff --git a/lib/base/process.hpp b/lib/base/process.hpp index 2fe1ff3d4..bcc629d4e 100644 --- a/lib/base/process.hpp +++ b/lib/base/process.hpp @@ -79,7 +79,6 @@ public: static Arguments PrepareCommand(const Value& command); - static void StaticInitialize(void); static void ThreadInitialize(void); static String PrettyPrintArguments(const Arguments& arguments); diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index 9e21cd4d8..d4f620980 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -26,22 +26,19 @@ using namespace icinga; boost::thread_specific_ptr > ScriptFrame::m_ScriptFrames; Array::Ptr ScriptFrame::m_Imports; -INITIALIZE_ONCE_WITH_PRIORITY(&ScriptFrame::StaticInitialize, 50); - -void ScriptFrame::StaticInitialize(void) -{ +INITIALIZE_ONCE_WITH_PRIORITY([]() { Dictionary::Ptr systemNS = new Dictionary(); ScriptGlobal::Set("System", systemNS); - AddImport(systemNS); + ScriptFrame::AddImport(systemNS); Dictionary::Ptr typesNS = new Dictionary(); ScriptGlobal::Set("Types", typesNS); - AddImport(typesNS); + ScriptFrame::AddImport(typesNS); Dictionary::Ptr deprecatedNS = new Dictionary(); ScriptGlobal::Set("Deprecated", deprecatedNS); - AddImport(deprecatedNS); -} + ScriptFrame::AddImport(deprecatedNS); +}, 50); ScriptFrame::ScriptFrame(void) : Locals(new Dictionary()), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0) diff --git a/lib/base/scriptframe.hpp b/lib/base/scriptframe.hpp index baf15b9ff..b40d154b6 100644 --- a/lib/base/scriptframe.hpp +++ b/lib/base/scriptframe.hpp @@ -40,8 +40,6 @@ struct I2_BASE_API ScriptFrame ScriptFrame(const Value& self); ~ScriptFrame(void); - static void StaticInitialize(void); - void IncreaseStackDepth(void); void DecreaseStackDepth(void); diff --git a/lib/base/stacktrace.cpp b/lib/base/stacktrace.cpp index a33480224..ecbc3d896 100644 --- a/lib/base/stacktrace.cpp +++ b/lib/base/stacktrace.cpp @@ -27,8 +27,6 @@ using namespace icinga; -INITIALIZE_ONCE(&StackTrace::StaticInitialize); - #ifdef _MSC_VER # pragma optimize("", off) #endif /* _MSC_VER */ @@ -85,13 +83,12 @@ StackTrace::StackTrace(PEXCEPTION_POINTERS exi) } #endif /* _WIN32 */ -void StackTrace::StaticInitialize(void) -{ #ifdef _WIN32 +INITIALIZE_ONCE([]() { (void) SymSetOptions(SYMOPT_UNDNAME | SYMOPT_LOAD_LINES); (void) SymInitialize(GetCurrentProcess(), NULL, TRUE); +}); #endif /* _WIN32 */ -} /** * Prints a stacktrace to the specified stream. diff --git a/lib/base/statsfunction.hpp b/lib/base/statsfunction.hpp index 3cbc909c8..f14b1f216 100644 --- a/lib/base/statsfunction.hpp +++ b/lib/base/statsfunction.hpp @@ -62,14 +62,10 @@ public: }; #define REGISTER_STATSFUNCTION(name, callback) \ - namespace { namespace UNIQUE_NAME(stf) { namespace stf ## name { \ - void RegisterStatsFunction(void) \ - { \ - StatsFunction::Ptr stf = new StatsFunction(callback); \ - StatsFunctionRegistry::GetInstance()->Register(#name, stf); \ - } \ - INITIALIZE_ONCE(RegisterStatsFunction); \ - } } } + INITIALIZE_ONCE([]() { \ + StatsFunction::Ptr stf = new StatsFunction(callback); \ + StatsFunctionRegistry::GetInstance()->Register(#name, stf); \ + }) } diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 6fc72cd11..e687b5a5e 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -25,15 +25,12 @@ using namespace icinga; Type::Ptr Type::TypeInstance; -static void RegisterTypeType(void) -{ +INITIALIZE_ONCE_WITH_PRIORITY([]() { Type::Ptr type = new TypeType(); type->SetPrototype(TypeType::GetPrototype()); Type::TypeInstance = type; Type::Register(type); -} - -INITIALIZE_ONCE_WITH_PRIORITY(RegisterTypeType, 20); +}, 20); String Type::ToString(void) const { diff --git a/lib/base/type.hpp b/lib/base/type.hpp index cb2b50f41..312a1e9e3 100644 --- a/lib/base/type.hpp +++ b/lib/base/type.hpp @@ -138,30 +138,20 @@ class I2_BASE_API TypeImpl }; #define REGISTER_TYPE(type) \ - namespace { namespace UNIQUE_NAME(rt) { \ - void RegisterType ## type(void) \ - { \ - icinga::Type::Ptr t = new TypeImpl(); \ - type::TypeInstance = t; \ - icinga::Type::Register(t); \ - } \ - \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \ - } } \ + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + icinga::Type::Ptr t = new TypeImpl(); \ + type::TypeInstance = t; \ + icinga::Type::Register(t); \ + }, 10); \ DEFINE_TYPE_INSTANCE(type) #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \ - namespace { namespace UNIQUE_NAME(rt) { \ - void RegisterType ## type(void) \ - { \ - icinga::Type::Ptr t = new TypeImpl(); \ - t->SetPrototype(prototype); \ - type::TypeInstance = t; \ - icinga::Type::Register(t); \ - } \ - \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \ - } } \ + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + icinga::Type::Ptr t = new TypeImpl(); \ + t->SetPrototype(prototype); \ + type::TypeInstance = t; \ + icinga::Type::Register(t); \ + }, 10); \ DEFINE_TYPE_INSTANCE(type) #define DEFINE_TYPE_INSTANCE(type) \ diff --git a/lib/base/visibility.hpp b/lib/base/visibility.hpp index 8a2afc25d..574eb8c5a 100644 --- a/lib/base/visibility.hpp +++ b/lib/base/visibility.hpp @@ -32,10 +32,6 @@ #define TOKENPASTE(x, y) x ## y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) -#ifdef HAVE_COUNTER_MACRO -# define UNIQUE_NAME(prefix) TOKENPASTE2(prefix, __COUNTER__) -#else /* HAVE_COUNTER_MACRO */ -# define UNIQUE_NAME(prefix) prefix -#endif /* HAVE_COUNTER_MACRO */ +#define UNIQUE_NAME(prefix) TOKENPASTE2(prefix, __COUNTER__) #endif /* VISIBILITY_H */ diff --git a/lib/cli/clicommand.hpp b/lib/cli/clicommand.hpp index 7663adcf6..d1adad28f 100644 --- a/lib/cli/clicommand.hpp +++ b/lib/cli/clicommand.hpp @@ -88,15 +88,11 @@ private: }; #define REGISTER_CLICOMMAND(name, klass) \ - namespace { namespace UNIQUE_NAME(cli) { \ - void RegisterCommand(void) \ - { \ - std::vector vname; \ - boost::algorithm::split(vname, name, boost::is_any_of("/")); \ - CLICommand::Register(vname, new klass()); \ - } \ - INITIALIZE_ONCE(RegisterCommand); \ - } } + INITIALIZE_ONCE([]() { \ + std::vector vname; \ + boost::algorithm::split(vname, name, boost::is_any_of("/")); \ + CLICommand::Register(vname, new klass()); \ + }) } diff --git a/lib/cli/nodeblackandwhitelistcommand.hpp b/lib/cli/nodeblackandwhitelistcommand.hpp index ffc4928f9..cb34f7dbd 100644 --- a/lib/cli/nodeblackandwhitelistcommand.hpp +++ b/lib/cli/nodeblackandwhitelistcommand.hpp @@ -57,26 +57,22 @@ private: }; #define REGISTER_BLACKANDWHITELIST_CLICOMMAND(type) \ - namespace { namespace UNIQUE_NAME(blackandwhitelist) { namespace blackandwhitelist ## type { \ - void RegisterCommand(void) \ - { \ - String ltype = #type; \ - boost::algorithm::to_lower(ltype); \ + INITIALIZE_ONCE([]() { \ + String ltype = #type; \ + boost::algorithm::to_lower(ltype); \ \ - std::vector name; \ - name.push_back("node"); \ - name.push_back(ltype); \ - name.push_back("add"); \ - CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandAdd)); \ + std::vector name; \ + name.push_back("node"); \ + name.push_back(ltype); \ + name.push_back("add"); \ + CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandAdd)); \ \ - name[2] = "remove"; \ - CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandRemove)); \ + name[2] = "remove"; \ + CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandRemove)); \ \ - name[2] = "list"; \ - CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandList)); \ - } \ - INITIALIZE_ONCE(RegisterCommand); \ - } } } + name[2] = "list"; \ + CLICommand::Register(name, new BlackAndWhitelistCommand(#type, BlackAndWhitelistCommandList)); \ + }) } diff --git a/lib/cli/repositoryobjectcommand.hpp b/lib/cli/repositoryobjectcommand.hpp index 33007573d..4bad83623 100644 --- a/lib/cli/repositoryobjectcommand.hpp +++ b/lib/cli/repositoryobjectcommand.hpp @@ -61,26 +61,22 @@ private: }; #define REGISTER_REPOSITORY_CLICOMMAND(type) \ - namespace { namespace UNIQUE_NAME(repositoryobject) { namespace repositoryobject ## type { \ - void RegisterCommand(void) \ - { \ - String ltype = #type; \ - boost::algorithm::to_lower(ltype); \ + INITIALIZE_ONCE([]() { \ + String ltype = #type; \ + boost::algorithm::to_lower(ltype); \ \ - std::vector name; \ - name.push_back("repository"); \ - name.push_back(ltype); \ - name.push_back("add"); \ - CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandAdd)); \ + std::vector name; \ + name.push_back("repository"); \ + name.push_back(ltype); \ + name.push_back("add"); \ + CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandAdd)); \ \ - name[2] = "remove"; \ - CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandRemove)); \ + name[2] = "remove"; \ + CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandRemove)); \ \ - name[2] = "list"; \ - CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandList)); \ - } \ - INITIALIZE_ONCE(RegisterCommand); \ - } } } + name[2] = "list"; \ + CLICommand::Register(name, new RepositoryObjectCommand(#type, RepositoryCommandList)); \ + }) } diff --git a/lib/config/configfragment.hpp b/lib/config/configfragment.hpp index ad3efba22..2d1c46637 100644 --- a/lib/config/configfragment.hpp +++ b/lib/config/configfragment.hpp @@ -27,22 +27,17 @@ #include "base/application.hpp" #define REGISTER_CONFIG_FRAGMENT(id, name, fragment) \ - namespace { \ - void RegisterConfigFragment(void) \ - { \ - icinga::Expression *expression = icinga::ConfigCompiler::CompileText(name, fragment); \ - VERIFY(expression); \ - try { \ - icinga::ScriptFrame frame; \ - expression->Evaluate(frame); \ - } catch (const std::exception& ex) { \ - std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \ - icinga::Application::Exit(1); \ - } \ - delete expression; \ + INITIALIZE_ONCE_WITH_PRIORITY([]() { \ + icinga::Expression *expression = icinga::ConfigCompiler::CompileText(name, fragment); \ + VERIFY(expression); \ + try { \ + icinga::ScriptFrame frame; \ + expression->Evaluate(frame); \ + } catch (const std::exception& ex) { \ + std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \ + icinga::Application::Exit(1); \ } \ - \ - INITIALIZE_ONCE_WITH_PRIORITY(RegisterConfigFragment, 5); \ - } + delete expression; \ + }, 5) #endif /* CONFIGFRAGMENT_H */ diff --git a/lib/db_ido/dbtype.hpp b/lib/db_ido/dbtype.hpp index d160e947b..287c1e9c1 100644 --- a/lib/db_ido/dbtype.hpp +++ b/lib/db_ido/dbtype.hpp @@ -97,14 +97,10 @@ intrusive_ptr DbObjectFactory(const DbType::Ptr& type, const String& name1, c } #define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \ - namespace { namespace UNIQUE_NAME(ido) { namespace ido ## name { \ - void RegisterDbType(void) \ - { \ - DbType::Ptr dbtype = new DbType(#name, table, tid, idcolumn, DbObjectFactory); \ - DbType::RegisterType(dbtype); \ - } \ - INITIALIZE_ONCE(RegisterDbType); \ - } } } + INITIALIZE_ONCE([]() { \ + DbType::Ptr dbtype = new DbType(#name, table, tid, idcolumn, DbObjectFactory); \ + DbType::RegisterType(dbtype); \ + }) } diff --git a/lib/db_ido/endpointdbobject.cpp b/lib/db_ido/endpointdbobject.cpp index be0ff646f..a24a2401f 100644 --- a/lib/db_ido/endpointdbobject.cpp +++ b/lib/db_ido/endpointdbobject.cpp @@ -30,7 +30,6 @@ using namespace icinga; - REGISTER_DBTYPE(Endpoint, "endpoint", DbObjectTypeEndpoint, "endpoint_object_id", EndpointDbObject); INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize); diff --git a/lib/hello/helloapplication.cpp b/lib/hello/helloapplication.cpp index 181f96e0f..79346b247 100644 --- a/lib/hello/helloapplication.cpp +++ b/lib/hello/helloapplication.cpp @@ -27,12 +27,9 @@ using namespace icinga; REGISTER_TYPE(HelloApplication); -INITIALIZE_ONCE(&HelloApplication::StaticInitialize); - -void HelloApplication::StaticInitialize(void) -{ +INITIALIZE_ONCE([]() { ScriptGlobal::Set("ApplicationType", "HelloApplication"); -} +}); /** * The entry point for the hello application. diff --git a/lib/hello/helloapplication.hpp b/lib/hello/helloapplication.hpp index 695b1fa5a..0934bd4c5 100644 --- a/lib/hello/helloapplication.hpp +++ b/lib/hello/helloapplication.hpp @@ -36,8 +36,6 @@ public: DECLARE_OBJECT(HelloApplication); DECLARE_OBJECTNAME(HelloApplication); - static void StaticInitialize(void); - virtual int Main(void) override; }; diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index 9b3351b8f..7615776af 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -30,15 +30,12 @@ using namespace icinga; -INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler); - -void Dependency::RegisterApplyRuleHandler(void) -{ +INITIALIZE_ONCE([]() { std::vector targets; targets.push_back("Host"); targets.push_back("Service"); ApplyRule::RegisterType("Dependency", targets); -} +}); bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule) { diff --git a/lib/icinga/dependency.hpp b/lib/icinga/dependency.hpp index a2fa00781..7f055c6a0 100644 --- a/lib/icinga/dependency.hpp +++ b/lib/icinga/dependency.hpp @@ -49,8 +49,6 @@ public: bool IsAvailable(DependencyType dt) const; - static void RegisterApplyRuleHandler(void); - virtual void ValidateStates(const Array::Ptr& value, const ValidationUtils& utils) override; static void EvaluateApplyRules(const intrusive_ptr& host); diff --git a/lib/icinga/hostgroup.cpp b/lib/icinga/hostgroup.cpp index 36758850e..30226757e 100644 --- a/lib/icinga/hostgroup.cpp +++ b/lib/icinga/hostgroup.cpp @@ -31,12 +31,9 @@ using namespace icinga; REGISTER_TYPE(HostGroup); -INITIALIZE_ONCE(&HostGroup::RegisterObjectRuleHandler); - -void HostGroup::RegisterObjectRuleHandler(void) -{ +INITIALIZE_ONCE([]() { ObjectRule::RegisterType("HostGroup"); -} +}); bool HostGroup::EvaluateObjectRule(const Host::Ptr& host, const ConfigItem::Ptr& group) { diff --git a/lib/icinga/hostgroup.hpp b/lib/icinga/hostgroup.hpp index 82c945178..d96e4a9e0 100644 --- a/lib/icinga/hostgroup.hpp +++ b/lib/icinga/hostgroup.hpp @@ -46,8 +46,6 @@ public: bool ResolveGroupMembership(const Host::Ptr& host, bool add = true, int rstack = 0); - static void RegisterObjectRuleHandler(void); - static void EvaluateObjectRules(const Host::Ptr& host); private: diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index e7940a1a3..af77c2285 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -30,15 +30,12 @@ using namespace icinga; -INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler); - -void Notification::RegisterApplyRuleHandler(void) -{ +INITIALIZE_ONCE([]() { std::vector targets; targets.push_back("Host"); targets.push_back("Service"); ApplyRule::RegisterType("Notification", targets); -} +}); bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule) { diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp index 33f42a569..211a21b9d 100644 --- a/lib/icinga/notification.hpp +++ b/lib/icinga/notification.hpp @@ -104,8 +104,6 @@ public: static boost::signals2::signal OnNextNotificationChanged; - static void RegisterApplyRuleHandler(void); - virtual void Validate(int types, const ValidationUtils& utils) override; virtual void ValidateStates(const Array::Ptr& value, const ValidationUtils& utils) override; diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index e05777785..6729587e5 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -29,15 +29,12 @@ using namespace icinga; -INITIALIZE_ONCE(&ScheduledDowntime::RegisterApplyRuleHandler); - -void ScheduledDowntime::RegisterApplyRuleHandler(void) -{ +INITIALIZE_ONCE([]() { std::vector targets; targets.push_back("Host"); targets.push_back("Service"); ApplyRule::RegisterType("ScheduledDowntime", targets); -} +}); bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule) { diff --git a/lib/icinga/scheduleddowntime.hpp b/lib/icinga/scheduleddowntime.hpp index 5190157a7..15aa3cd81 100644 --- a/lib/icinga/scheduleddowntime.hpp +++ b/lib/icinga/scheduleddowntime.hpp @@ -48,8 +48,6 @@ public: Checkable::Ptr GetCheckable(void) const; - static void RegisterApplyRuleHandler(void); - static void EvaluateApplyRules(const intrusive_ptr& host); static void EvaluateApplyRules(const intrusive_ptr& service); diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index ba89d71e9..ac34327a2 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -29,14 +29,11 @@ using namespace icinga; -INITIALIZE_ONCE(&Service::RegisterApplyRuleHandler); - -void Service::RegisterApplyRuleHandler(void) -{ +INITIALIZE_ONCE([]() { std::vector targets; targets.push_back("Host"); ApplyRule::RegisterType("Service", targets); -} +}); bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule) { diff --git a/lib/icinga/service.hpp b/lib/icinga/service.hpp index 7b2684c31..5292eb9ec 100644 --- a/lib/icinga/service.hpp +++ b/lib/icinga/service.hpp @@ -54,8 +54,6 @@ public: static StateType StateTypeFromString(const String& state); static String StateTypeToString(StateType state); - static void RegisterApplyRuleHandler(void); - static void EvaluateApplyRules(const Host::Ptr& host); protected: diff --git a/lib/icinga/servicegroup.cpp b/lib/icinga/servicegroup.cpp index ac75565e8..66ae7f8f8 100644 --- a/lib/icinga/servicegroup.cpp +++ b/lib/icinga/servicegroup.cpp @@ -31,12 +31,9 @@ using namespace icinga; REGISTER_TYPE(ServiceGroup); -INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler); - -void ServiceGroup::RegisterObjectRuleHandler(void) -{ +INITIALIZE_ONCE([]() { ObjectRule::RegisterType("ServiceGroup"); -} +}); bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group) { diff --git a/lib/icinga/servicegroup.hpp b/lib/icinga/servicegroup.hpp index 7c3d1ec78..71e7e021a 100644 --- a/lib/icinga/servicegroup.hpp +++ b/lib/icinga/servicegroup.hpp @@ -46,8 +46,6 @@ public: bool ResolveGroupMembership(const Service::Ptr& service, bool add = true, int rstack = 0); - static void RegisterObjectRuleHandler(void); - static void EvaluateObjectRules(const Service::Ptr& service); private: diff --git a/lib/icinga/usergroup.cpp b/lib/icinga/usergroup.cpp index f48a19b04..43bde69b7 100644 --- a/lib/icinga/usergroup.cpp +++ b/lib/icinga/usergroup.cpp @@ -31,12 +31,9 @@ using namespace icinga; REGISTER_TYPE(UserGroup); -INITIALIZE_ONCE(&UserGroup::RegisterObjectRuleHandler); - -void UserGroup::RegisterObjectRuleHandler(void) -{ +INITIALIZE_ONCE([]() { ObjectRule::RegisterType("UserGroup"); -} +}); bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr& group) { diff --git a/lib/icinga/usergroup.hpp b/lib/icinga/usergroup.hpp index 7fbe87a2c..a265296b8 100644 --- a/lib/icinga/usergroup.hpp +++ b/lib/icinga/usergroup.hpp @@ -46,8 +46,6 @@ public: bool ResolveGroupMembership(const User::Ptr& user, bool add = true, int rstack = 0); - static void RegisterObjectRuleHandler(void); - static void EvaluateObjectRules(const User::Ptr& user); private: diff --git a/lib/remote/apiaction.hpp b/lib/remote/apiaction.hpp index c6455cbc6..1faccfad1 100644 --- a/lib/remote/apiaction.hpp +++ b/lib/remote/apiaction.hpp @@ -73,20 +73,16 @@ public: }; #define REGISTER_APIACTION(name, types, callback) \ - namespace { namespace UNIQUE_NAME(apia) { namespace apia ## name { \ - void RegisterAction(void) \ - { \ - String registerName = #name; \ - boost::algorithm::replace_all(registerName, "_", "-"); \ - std::vector registerTypes; \ - String typeNames = types; \ - if (!typeNames.IsEmpty()) \ - boost::algorithm::split(registerTypes, typeNames, boost::is_any_of(";")); \ - ApiAction::Ptr action = new ApiAction(registerTypes, callback); \ - ApiActionRegistry::GetInstance()->Register(registerName, action); \ - } \ - INITIALIZE_ONCE(RegisterAction); \ - } } } + INITIALIZE_ONCE([]() { \ + String registerName = #name; \ + boost::algorithm::replace_all(registerName, "_", "-"); \ + std::vector registerTypes; \ + String typeNames = types; \ + if (!typeNames.IsEmpty()) \ + boost::algorithm::split(registerTypes, typeNames, boost::is_any_of(";")); \ + ApiAction::Ptr action = new ApiAction(registerTypes, callback); \ + ApiActionRegistry::GetInstance()->Register(registerName, action); \ + }) } diff --git a/lib/remote/apifunction.hpp b/lib/remote/apifunction.hpp index 923de76c6..401ccf655 100644 --- a/lib/remote/apifunction.hpp +++ b/lib/remote/apifunction.hpp @@ -67,14 +67,10 @@ public: }; #define REGISTER_APIFUNCTION(name, ns, callback) \ - namespace { namespace UNIQUE_NAME(apif) { namespace apif ## name { \ - void RegisterFunction(void) \ - { \ - ApiFunction::Ptr func = new ApiFunction(callback); \ - ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \ - } \ - INITIALIZE_ONCE(RegisterFunction); \ - } } } + INITIALIZE_ONCE([]() { \ + ApiFunction::Ptr func = new ApiFunction(callback); \ + ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \ + }) } diff --git a/lib/remote/apilistener-configsync.cpp b/lib/remote/apilistener-configsync.cpp index 20f08230b..23f91e23b 100644 --- a/lib/remote/apilistener-configsync.cpp +++ b/lib/remote/apilistener-configsync.cpp @@ -31,17 +31,13 @@ using namespace icinga; -INITIALIZE_ONCE(&ApiListener::StaticInitialize); - REGISTER_APIFUNCTION(UpdateObject, config, &ApiListener::ConfigUpdateObjectAPIHandler); REGISTER_APIFUNCTION(DeleteObject, config, &ApiListener::ConfigDeleteObjectAPIHandler); - -void ApiListener::StaticInitialize(void) -{ +INITIALIZE_ONCE([]() { ConfigObject::OnActiveChanged.connect(&ApiListener::ConfigUpdateObjectHandler); ConfigObject::OnVersionChanged.connect(&ApiListener::ConfigUpdateObjectHandler); -} +}); void ApiListener::ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie) { diff --git a/lib/remote/apilistener.hpp b/lib/remote/apilistener.hpp index 9cb09fc31..cbfbbd150 100644 --- a/lib/remote/apilistener.hpp +++ b/lib/remote/apilistener.hpp @@ -55,8 +55,6 @@ public: DECLARE_OBJECT(ApiListener); DECLARE_OBJECTNAME(ApiListener); - static void StaticInitialize(void); - static boost::signals2::signal OnMasterChanged; ApiListener(void); diff --git a/lib/remote/authority.cpp b/lib/remote/authority.cpp index daca94ab5..5d22d2387 100644 --- a/lib/remote/authority.cpp +++ b/lib/remote/authority.cpp @@ -24,11 +24,6 @@ using namespace icinga; -static bool ObjectNameLessComparer(const ConfigObject::Ptr& a, const ConfigObject::Ptr& b) -{ - return a->GetName() < b->GetName(); -} - void ApiListener::UpdateObjectAuthority(void) { Zone::Ptr my_zone = Zone::GetLocalZone(); @@ -55,7 +50,11 @@ void ApiListener::UpdateObjectAuthority(void) if (num_total > 1 && endpoints.size() <= 1 && (mainTime == 0 || Utility::GetTime() - mainTime < 60)) return; - std::sort(endpoints.begin(), endpoints.end(), ObjectNameLessComparer); + std::sort(endpoints.begin(), endpoints.end(), + [](const ConfigObject::Ptr& a, const ConfigObject::Ptr& b) { + return a->GetName() < b->GetName(); + } + ); } for (const Type::Ptr& type : Type::GetAllTypes()) { diff --git a/lib/remote/consolehandler.cpp b/lib/remote/consolehandler.cpp index 69e7698b7..3b1a7a195 100644 --- a/lib/remote/consolehandler.cpp +++ b/lib/remote/consolehandler.cpp @@ -57,15 +57,12 @@ static void ScriptFrameCleanupHandler(void) l_ApiScriptFrames.erase(key); } -static void InitScriptFrameCleanup(void) -{ +INITIALIZE_ONCE([]() { l_FrameCleanupTimer = new Timer(); l_FrameCleanupTimer->OnTimerExpired.connect(boost::bind(ScriptFrameCleanupHandler)); l_FrameCleanupTimer->SetInterval(30); l_FrameCleanupTimer->Start(); -} - -INITIALIZE_ONCE(InitScriptFrameCleanup); +}); bool ConsoleHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response, const Dictionary::Ptr& params) { diff --git a/lib/remote/httphandler.hpp b/lib/remote/httphandler.hpp index de01969e2..9c22a501e 100644 --- a/lib/remote/httphandler.hpp +++ b/lib/remote/httphandler.hpp @@ -61,15 +61,11 @@ public: }; #define REGISTER_URLHANDLER(url, klass) \ - namespace { namespace UNIQUE_NAME(apif) { namespace apif ## name { \ - void RegisterHandler(void) \ - { \ - Url::Ptr uurl = new Url(url); \ - HttpHandler::Ptr handler = new klass(); \ - HttpHandler::Register(uurl, handler); \ - } \ - INITIALIZE_ONCE(RegisterHandler); \ - } } } + INITIALIZE_ONCE([]() { \ + Url::Ptr uurl = new Url(url); \ + HttpHandler::Ptr handler = new klass(); \ + HttpHandler::Register(uurl, handler); \ + }) } diff --git a/lib/remote/jsonrpcconnection-heartbeat.cpp b/lib/remote/jsonrpcconnection-heartbeat.cpp index 587cb875c..36a02a0d7 100644 --- a/lib/remote/jsonrpcconnection-heartbeat.cpp +++ b/lib/remote/jsonrpcconnection-heartbeat.cpp @@ -31,15 +31,12 @@ REGISTER_APIFUNCTION(Heartbeat, event, &JsonRpcConnection::HeartbeatAPIHandler); static Timer::Ptr l_HeartbeatTimer; -static void StartHeartbeatTimer(void) -{ +INITIALIZE_ONCE([]() { l_HeartbeatTimer = new Timer(); l_HeartbeatTimer->OnTimerExpired.connect(boost::bind(&JsonRpcConnection::HeartbeatTimerHandler)); l_HeartbeatTimer->SetInterval(10); l_HeartbeatTimer->Start(); -} - -INITIALIZE_ONCE(StartHeartbeatTimer); +}); void JsonRpcConnection::HeartbeatTimerHandler(void) {