INITIALIZE_ONCE_WITH_PRIORITY: use enum for priority values

Change the type of the priority values from int to a new enum. By replacing the
magic int values throughout the code base with named values, there is now a
single place where all priority values are defined and you get an overview over
the initialization order.
This commit is contained in:
Julian Brost 2022-12-12 12:32:59 +01:00
parent 61285adcae
commit 99bb687350
12 changed files with 35 additions and 21 deletions

View File

@ -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)
}

View File

@ -5,7 +5,7 @@
using namespace icinga;
bool icinga::InitializeOnceHelper(const std::function<void()>& func, int priority)
bool icinga::InitializeOnceHelper(const std::function<void()>& func, InitializePriority priority)
{
Loader::AddDeferredInitializer(func, priority);
return true;

View File

@ -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<void()>& func, int priority = 0);
bool InitializeOnceHelper(const std::function<void()>& func, InitializePriority priority = InitializePriority::Default);
#define INITIALIZE_ONCE(func) \
namespace { namespace I2_UNIQUE_NAME(io) { \

View File

@ -25,7 +25,7 @@ void Loader::ExecuteDeferredInitializers()
}
}
void Loader::AddDeferredInitializer(const std::function<void()>& callback, int priority)
void Loader::AddDeferredInitializer(const std::function<void()>& callback, InitializePriority priority)
{
if (!GetDeferredInitializers().get())
GetDeferredInitializers().reset(new std::priority_queue<DeferredInitializer>());

View File

@ -4,6 +4,7 @@
#define LOADER_H
#include "base/i2-base.hpp"
#include "base/initialize.hpp"
#include "base/string.hpp"
#include <boost/thread/tss.hpp>
#include <queue>
@ -14,7 +15,7 @@ namespace icinga
struct DeferredInitializer
{
public:
DeferredInitializer(std::function<void ()> callback, int priority)
DeferredInitializer(std::function<void ()> callback, InitializePriority priority)
: m_Callback(std::move(callback)), m_Priority(priority)
{ }
@ -30,7 +31,7 @@ public:
private:
std::function<void ()> m_Callback;
int m_Priority;
InitializePriority m_Priority;
};
/**
@ -41,7 +42,7 @@ private:
class Loader
{
public:
static void AddDeferredInitializer(const std::function<void ()>& callback, int priority = 0);
static void AddDeferredInitializer(const std::function<void ()>& callback, InitializePriority priority = InitializePriority::Default);
static void ExecuteDeferredInitializers();
private:

View File

@ -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
{

View File

@ -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) \

View File

@ -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)

View File

@ -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
{

View File

@ -128,7 +128,7 @@ class TypeImpl
icinga::Type::Ptr t = new TypeImpl<type>(); \
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) \

View File

@ -21,6 +21,6 @@
std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \
icinga::Application::Exit(1); \
} \
}, 5)
}, icinga::InitializePriority::EvaluateConfigFragments)
#endif /* CONFIGFRAGMENT_H */

View File

@ -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()
{