diff --git a/lib/base/function.hpp b/lib/base/function.hpp index e0067d226..fa1ce92bd 100644 --- a/lib/base/function.hpp +++ b/lib/base/function.hpp @@ -61,28 +61,28 @@ private: Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \ Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \ nsp->SetAttribute(#name, new ConstEmbeddedNamespaceValue(sf)); \ - }, 10) + }, InitializePriority::RegisterFunctions) #define REGISTER_SAFE_FUNCTION(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \ Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \ nsp->SetAttribute(#name, new ConstEmbeddedNamespaceValue(sf)); \ - }, 10) + }, InitializePriority::RegisterFunctions) #define REGISTER_FUNCTION_NONCONST(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \ Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \ nsp->SetAttribute(#name, new EmbeddedNamespaceValue(sf)); \ - }, 10) + }, InitializePriority::RegisterFunctions) #define REGISTER_SAFE_FUNCTION_NONCONST(ns, name, callback, args) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \ Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \ nsp->SetAttribute(#name, new EmbeddedNamespaceValue(sf)); \ - }, 10) + }, InitializePriority::RegisterFunctions) } diff --git a/lib/base/initialize.cpp b/lib/base/initialize.cpp index e1dbec547..49b653fcf 100644 --- a/lib/base/initialize.cpp +++ b/lib/base/initialize.cpp @@ -5,7 +5,7 @@ using namespace icinga; -bool icinga::InitializeOnceHelper(const std::function& func, int priority) +bool icinga::InitializeOnceHelper(const std::function& func, InitializePriority priority) { Loader::AddDeferredInitializer(func, priority); return true; diff --git a/lib/base/initialize.hpp b/lib/base/initialize.hpp index 446ad69af..1459bd25f 100644 --- a/lib/base/initialize.hpp +++ b/lib/base/initialize.hpp @@ -9,12 +9,25 @@ namespace icinga { +enum class InitializePriority { + CreateNamespaces = 1000, + InitIcingaApplication = 50, + RegisterTypeType = 20, + RegisterObjectType = 20, + RegisterPrimitiveTypes = 15, + RegisterBuiltinTypes = 15, + RegisterFunctions = 10, + RegisterTypes = 10, + EvaluateConfigFragments = 5, + Default = 0, +}; + #define I2_TOKENPASTE(x, y) x ## y #define I2_TOKENPASTE2(x, y) I2_TOKENPASTE(x, y) #define I2_UNIQUE_NAME(prefix) I2_TOKENPASTE2(prefix, __COUNTER__) -bool InitializeOnceHelper(const std::function& func, int priority = 0); +bool InitializeOnceHelper(const std::function& func, InitializePriority priority = InitializePriority::Default); #define INITIALIZE_ONCE(func) \ namespace { namespace I2_UNIQUE_NAME(io) { \ diff --git a/lib/base/loader.cpp b/lib/base/loader.cpp index e969f371d..84427d755 100644 --- a/lib/base/loader.cpp +++ b/lib/base/loader.cpp @@ -25,7 +25,7 @@ void Loader::ExecuteDeferredInitializers() } } -void Loader::AddDeferredInitializer(const std::function& callback, int priority) +void Loader::AddDeferredInitializer(const std::function& callback, InitializePriority priority) { if (!GetDeferredInitializers().get()) GetDeferredInitializers().reset(new std::priority_queue()); diff --git a/lib/base/loader.hpp b/lib/base/loader.hpp index 704a32a09..48b6b3d11 100644 --- a/lib/base/loader.hpp +++ b/lib/base/loader.hpp @@ -4,6 +4,7 @@ #define LOADER_H #include "base/i2-base.hpp" +#include "base/initialize.hpp" #include "base/string.hpp" #include #include @@ -14,7 +15,7 @@ namespace icinga struct DeferredInitializer { public: - DeferredInitializer(std::function callback, int priority) + DeferredInitializer(std::function callback, InitializePriority priority) : m_Callback(std::move(callback)), m_Priority(priority) { } @@ -30,7 +31,7 @@ public: private: std::function m_Callback; - int m_Priority; + InitializePriority m_Priority; }; /** @@ -41,7 +42,7 @@ private: class Loader { public: - static void AddDeferredInitializer(const std::function& callback, int priority = 0); + static void AddDeferredInitializer(const std::function& callback, InitializePriority priority = InitializePriority::Default); static void ExecuteDeferredInitializers(); private: diff --git a/lib/base/objecttype.cpp b/lib/base/objecttype.cpp index 7ab0f2ad8..b871555bf 100644 --- a/lib/base/objecttype.cpp +++ b/lib/base/objecttype.cpp @@ -12,7 +12,7 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { type->SetPrototype(Object::GetPrototype()); Type::Register(type); Object::TypeInstance = type; -}, 20); +}, InitializePriority::RegisterObjectType); String ObjectType::GetName() const { diff --git a/lib/base/primitivetype.hpp b/lib/base/primitivetype.hpp index ae7454e07..439e20f3e 100644 --- a/lib/base/primitivetype.hpp +++ b/lib/base/primitivetype.hpp @@ -37,7 +37,7 @@ private: icinga::Type::Ptr t = new PrimitiveType(#type, "None"); \ t->SetPrototype(prototype); \ icinga::Type::Register(t); \ - }, 15) + }, InitializePriority::RegisterBuiltinTypes) #define REGISTER_PRIMITIVE_TYPE_FACTORY(type, base, prototype, factory) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ @@ -45,7 +45,7 @@ private: t->SetPrototype(prototype); \ icinga::Type::Register(t); \ type::TypeInstance = t; \ - }, 15); \ + }, InitializePriority::RegisterPrimitiveTypes); \ DEFINE_TYPE_INSTANCE(type) #define REGISTER_PRIMITIVE_TYPE(type, base, prototype) \ diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index 2001e8c0b..4454d7733 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -35,11 +35,11 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { l_InternalNS = new Namespace(true); globalNS->SetAttribute("Internal", new ConstEmbeddedNamespaceValue(l_InternalNS)); -}, 1000); +}, InitializePriority::CreateNamespaces); -INITIALIZE_ONCE_WITH_PRIORITY([]() { +INITIALIZE_ONCE([]() { l_InternalNS->Freeze(); -}, 0); +}); ScriptFrame::ScriptFrame(bool allocLocals) : Locals(allocLocals ? new Dictionary() : nullptr), Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0) diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 6e4fd6b57..604a07ae5 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -15,7 +15,7 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() { type->SetPrototype(TypeType::GetPrototype()); Type::TypeInstance = type; Type::Register(type); -}, 20); +}, InitializePriority::RegisterTypeType); String Type::ToString() const { diff --git a/lib/base/type.hpp b/lib/base/type.hpp index f47299560..7b8d1cacb 100644 --- a/lib/base/type.hpp +++ b/lib/base/type.hpp @@ -128,7 +128,7 @@ class TypeImpl icinga::Type::Ptr t = new TypeImpl(); \ type::TypeInstance = t; \ icinga::Type::Register(t); \ - }, 10); \ + }, InitializePriority::RegisterTypes); \ DEFINE_TYPE_INSTANCE(type) #define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \ @@ -137,7 +137,7 @@ class TypeImpl t->SetPrototype(prototype); \ type::TypeInstance = t; \ icinga::Type::Register(t); \ - }, 10); \ + }, InitializePriority::RegisterTypes); \ DEFINE_TYPE_INSTANCE(type) #define DEFINE_TYPE_INSTANCE(type) \ diff --git a/lib/config/configfragment.hpp b/lib/config/configfragment.hpp index 55038f8bf..883aef86c 100644 --- a/lib/config/configfragment.hpp +++ b/lib/config/configfragment.hpp @@ -21,6 +21,6 @@ std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \ icinga::Application::Exit(1); \ } \ - }, 5) + }, icinga::InitializePriority::EvaluateConfigFragments) #endif /* CONFIGFRAGMENT_H */ diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index e98f5e757..7e0312754 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -26,7 +26,7 @@ static Timer::Ptr l_RetentionTimer; REGISTER_TYPE(IcingaApplication); /* Ensure that the priority is lower than the basic System namespace initialization in scriptframe.cpp. */ -INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, 50); +INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, InitializePriority::InitIcingaApplication); void IcingaApplication::StaticInitialize() {