mirror of https://github.com/Icinga/icinga2.git
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:
parent
61285adcae
commit
99bb687350
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) { \
|
||||
|
|
|
@ -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>());
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -21,6 +21,6 @@
|
|||
std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \
|
||||
icinga::Application::Exit(1); \
|
||||
} \
|
||||
}, 5)
|
||||
}, icinga::InitializePriority::EvaluateConfigFragments)
|
||||
|
||||
#endif /* CONFIGFRAGMENT_H */
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue