Move Types namespace into type.cpp and simplify Type::GetByName()

This commit moves the initialization of the globals.Types namespace to type.cpp
in order to keep a pointer to the Namespace object in Type::m_Namespace and
simplify Type::GetByName() using it.

The dynamic type check is moved into an assertion after freezing the namespace.
This commit is contained in:
Julian Brost 2022-12-13 15:28:24 +01:00
parent 11e37a0bd1
commit 3dab46623b
2 changed files with 17 additions and 13 deletions

View File

@ -11,7 +11,7 @@ using namespace icinga;
boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
static Namespace::Ptr l_SystemNS, l_TypesNS, l_StatsNS, l_InternalNS;
static Namespace::Ptr l_SystemNS, l_StatsNS, l_InternalNS;
/* Ensure that this gets called with highest priority
* and wins against other static initializers in lib/icinga, etc.
@ -33,9 +33,6 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() {
l_SystemNS->Set("Configuration", new Configuration());
l_TypesNS = new Namespace(true);
globalNS->Set("Types", l_TypesNS, true);
l_StatsNS = new Namespace(true);
globalNS->Set("StatsFunctions", l_StatsNS, true);
@ -45,7 +42,6 @@ INITIALIZE_ONCE_WITH_PRIORITY([]() {
INITIALIZE_ONCE_WITH_PRIORITY([]() {
l_SystemNS->Freeze();
l_TypesNS->Freeze();
l_StatsNS->Freeze();
l_InternalNS->Freeze();
}, InitializePriority::FreezeNamespaces);

View File

@ -9,6 +9,21 @@ using namespace icinga;
Type::Ptr Type::TypeInstance;
static Namespace::Ptr l_TypesNS = new Namespace(true);
INITIALIZE_ONCE_WITH_PRIORITY([]() {
ScriptGlobal::GetGlobals()->Set("Types", l_TypesNS, true);
}, InitializePriority::CreateNamespaces);
INITIALIZE_ONCE_WITH_PRIORITY([]() {
l_TypesNS->Freeze();
ObjectLock olock (l_TypesNS);
for (const auto& t : l_TypesNS) {
VERIFY(t.second.Val.IsObjectType<Type>());
}
}, InitializePriority::FreezeNamespaces);
/* Ensure that the priority is lower than the basic namespace initialization in scriptframe.cpp. */
INITIALIZE_ONCE_WITH_PRIORITY([]() {
Type::Ptr type = new TypeType();
@ -29,16 +44,9 @@ void Type::Register(const Type::Ptr& type)
Type::Ptr Type::GetByName(const String& name)
{
Namespace::Ptr typesNS = ScriptGlobal::Get("Types", &Empty);
if (!typesNS)
return nullptr;
Value ptype;
if (!typesNS->Get(name, &ptype))
return nullptr;
if (!ptype.IsObjectType<Type>())
if (!l_TypesNS->Get(name, &ptype))
return nullptr;
return ptype;