From 3a09cf72d60d5577daa211ea2a5904224ff82eeb Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 4 Dec 2024 11:19:37 +0100 Subject: [PATCH] DependencyGraph: use ConfigObject*, not Object* This saves dynamic_cast + if() on every item of GetChildren(). --- lib/base/dependencygraph.cpp | 13 ++++++------- lib/base/dependencygraph.hpp | 10 +++++----- lib/base/scriptutils.cpp | 2 +- lib/remote/configobjectutility.cpp | 7 +------ lib/remote/objectqueryhandler.cpp | 8 +------- tools/mkclass/classcompiler.cpp | 24 ++++++++++++------------ 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/lib/base/dependencygraph.cpp b/lib/base/dependencygraph.cpp index 88cb18194..58f685ce0 100644 --- a/lib/base/dependencygraph.cpp +++ b/lib/base/dependencygraph.cpp @@ -5,15 +5,15 @@ using namespace icinga; std::mutex DependencyGraph::m_Mutex; -std::map > DependencyGraph::m_Dependencies; +std::map> DependencyGraph::m_Dependencies; -void DependencyGraph::AddDependency(Object* child, Object* parent) +void DependencyGraph::AddDependency(ConfigObject* child, ConfigObject* parent) { std::unique_lock lock(m_Mutex); m_Dependencies[parent][child]++; } -void DependencyGraph::RemoveDependency(Object* child, Object* parent) +void DependencyGraph::RemoveDependency(ConfigObject* child, ConfigObject* parent) { std::unique_lock lock(m_Mutex); @@ -32,16 +32,15 @@ void DependencyGraph::RemoveDependency(Object* child, Object* parent) m_Dependencies.erase(parent); } -std::vector DependencyGraph::GetChildren(const Object::Ptr& parent) +std::vector DependencyGraph::GetChildren(const ConfigObject::Ptr& parent) { - std::vector objects; + std::vector objects; std::unique_lock lock(m_Mutex); auto it = m_Dependencies.find(parent.get()); if (it != m_Dependencies.end()) { - typedef std::pair kv_pair; - for (const kv_pair& kv : it->second) { + for (auto& kv : it->second) { objects.emplace_back(kv.first); } } diff --git a/lib/base/dependencygraph.hpp b/lib/base/dependencygraph.hpp index 43dd97c08..6afb08fc5 100644 --- a/lib/base/dependencygraph.hpp +++ b/lib/base/dependencygraph.hpp @@ -4,7 +4,7 @@ #define DEPENDENCYGRAPH_H #include "base/i2-base.hpp" -#include "base/object.hpp" +#include "base/configobject.hpp" #include #include @@ -18,15 +18,15 @@ namespace icinga { class DependencyGraph { public: - static void AddDependency(Object* child, Object* parent); - static void RemoveDependency(Object* child, Object* parent); - static std::vector GetChildren(const Object::Ptr& parent); + static void AddDependency(ConfigObject* child, ConfigObject* parent); + static void RemoveDependency(ConfigObject* child, ConfigObject* parent); + static std::vector GetChildren(const ConfigObject::Ptr& parent); private: DependencyGraph(); static std::mutex m_Mutex; - static std::map > m_Dependencies; + static std::map> m_Dependencies; }; } diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 5bf2164a7..3611799ff 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -520,7 +520,7 @@ String ScriptUtils::MsiGetComponentPathShim(const String& component) Array::Ptr ScriptUtils::TrackParents(const Object::Ptr& child) { - return Array::FromVector(DependencyGraph::GetChildren(child)); + return Array::FromVector(DependencyGraph::GetChildren(dynamic_pointer_cast(child))); } double ScriptUtils::Ptr(const Object::Ptr& object) diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index 75dcfab93..9d1958050 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -319,12 +319,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo return false; } - for (const Object::Ptr& pobj : parents) { - ConfigObject::Ptr parentObj = dynamic_pointer_cast(pobj); - - if (!parentObj) - continue; - + for (auto& parentObj : parents) { DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie); } diff --git a/lib/remote/objectqueryhandler.cpp b/lib/remote/objectqueryhandler.cpp index 4c40ef3ed..8b7313789 100644 --- a/lib/remote/objectqueryhandler.cpp +++ b/lib/remote/objectqueryhandler.cpp @@ -208,13 +208,7 @@ bool ObjectQueryHandler::HandleRequest( Array::Ptr used_by = new Array(); metaAttrs.emplace_back("used_by", used_by); - for (auto& pobj : DependencyGraph::GetChildren(obj)) - { - ConfigObject::Ptr configObj = dynamic_pointer_cast(pobj); - - if (!configObj) - continue; - + for (auto& configObj : DependencyGraph::GetChildren(obj)) { used_by->Add(new Dictionary({ { "type", configObj->GetReflectionType()->GetName() }, { "name", configObj->GetName() } diff --git a/tools/mkclass/classcompiler.cpp b/tools/mkclass/classcompiler.cpp index 6082cbed6..693011369 100644 --- a/tools/mkclass/classcompiler.cpp +++ b/tools/mkclass/classcompiler.cpp @@ -903,13 +903,13 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&) m_Impl << "\t" << "if (oldValue) {" << std::endl << "\t\t" << "ObjectLock olock(oldValue);" << std::endl << "\t\t" << "for (const String& ref : oldValue) {" << std::endl - << "\t\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject"; + << "\t\t\tDependencyGraph::RemoveDependency("; /* Ew */ if (field.Type.TypeName == "Zone" && m_Library == "base") - m_Impl << "(\"Zone\", "; + m_Impl << "dynamic_cast(this), ConfigObject::GetObject(\"Zone\", "; else - m_Impl << "<" << field.Type.TypeName << ">("; + m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">("; m_Impl << "ref).get());" << std::endl << "\t\t" << "}" << std::endl @@ -917,36 +917,36 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&) << "\t" << "if (newValue) {" << std::endl << "\t\t" << "ObjectLock olock(newValue);" << std::endl << "\t\t" << "for (const String& ref : newValue) {" << std::endl - << "\t\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject"; + << "\t\t\tDependencyGraph::AddDependency("; /* Ew */ if (field.Type.TypeName == "Zone" && m_Library == "base") - m_Impl << "(\"Zone\", "; + m_Impl << "dynamic_cast(this), ConfigObject::GetObject(\"Zone\", "; else - m_Impl << "<" << field.Type.TypeName << ">("; + m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">("; m_Impl << "ref).get());" << std::endl << "\t\t" << "}" << std::endl << "\t" << "}" << std::endl; } else { m_Impl << "\t" << "if (!oldValue.IsEmpty())" << std::endl - << "\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject"; + << "\t\tDependencyGraph::RemoveDependency("; /* Ew */ if (field.Type.TypeName == "Zone" && m_Library == "base") - m_Impl << "(\"Zone\", "; + m_Impl << "dynamic_cast(this), ConfigObject::GetObject(\"Zone\", "; else - m_Impl << "<" << field.Type.TypeName << ">("; + m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">("; m_Impl << "oldValue).get());" << std::endl << "\t" << "if (!newValue.IsEmpty())" << std::endl - << "\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject"; + << "\t\tDependencyGraph::AddDependency("; /* Ew */ if (field.Type.TypeName == "Zone" && m_Library == "base") - m_Impl << "(\"Zone\", "; + m_Impl << "dynamic_cast(this), ConfigObject::GetObject(\"Zone\", "; else - m_Impl << "<" << field.Type.TypeName << ">("; + m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">("; m_Impl << "newValue).get());" << std::endl; }