From 3dab46623b50f81bdb2581c42af20a4d671b8eba Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Tue, 13 Dec 2022 15:28:24 +0100 Subject: [PATCH] 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. --- lib/base/scriptframe.cpp | 6 +----- lib/base/type.cpp | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/base/scriptframe.cpp b/lib/base/scriptframe.cpp index b9725cf60..5d0807d72 100644 --- a/lib/base/scriptframe.cpp +++ b/lib/base/scriptframe.cpp @@ -11,7 +11,7 @@ using namespace icinga; boost::thread_specific_ptr > 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); diff --git a/lib/base/type.cpp b/lib/base/type.cpp index 64a442d98..14794cb91 100644 --- a/lib/base/type.cpp +++ b/lib/base/type.cpp @@ -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()); + } +}, 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()) + if (!l_TypesNS->Get(name, &ptype)) return nullptr; return ptype;