Skip to content

Commit 5e81b4d

Browse files
committed
DependencyGraph: use ConfigObject*, not Object*
This saves dynamic_cast<ConfigObject*> + if() on every item of GetChildren().
1 parent 188ba53 commit 5e81b4d

6 files changed

Lines changed: 20 additions & 32 deletions

File tree

lib/base/dependencygraph.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
using namespace icinga;
66

77
std::mutex DependencyGraph::m_Mutex;
8-
std::map<Object *, std::map<Object *, int> > DependencyGraph::m_Dependencies;
8+
std::map<ConfigObject*, std::map<ConfigObject*, int>> DependencyGraph::m_Dependencies;
99

10-
void DependencyGraph::AddDependency(Object* child, Object* parent)
10+
void DependencyGraph::AddDependency(ConfigObject* child, ConfigObject* parent)
1111
{
1212
std::unique_lock<std::mutex> lock(m_Mutex);
1313
m_Dependencies[parent][child]++;
1414
}
1515

16-
void DependencyGraph::RemoveDependency(Object* child, Object* parent)
16+
void DependencyGraph::RemoveDependency(ConfigObject* child, ConfigObject* parent)
1717
{
1818
std::unique_lock<std::mutex> lock(m_Mutex);
1919

@@ -32,16 +32,15 @@ void DependencyGraph::RemoveDependency(Object* child, Object* parent)
3232
m_Dependencies.erase(parent);
3333
}
3434

35-
std::vector<Object::Ptr> DependencyGraph::GetChildren(const Object::Ptr& parent)
35+
std::vector<ConfigObject::Ptr> DependencyGraph::GetChildren(const ConfigObject::Ptr& parent)
3636
{
37-
std::vector<Object::Ptr> objects;
37+
std::vector<ConfigObject::Ptr> objects;
3838

3939
std::unique_lock<std::mutex> lock(m_Mutex);
4040
auto it = m_Dependencies.find(parent.get());
4141

4242
if (it != m_Dependencies.end()) {
43-
typedef std::pair<Object *, int> kv_pair;
44-
for (const kv_pair& kv : it->second) {
43+
for (auto& kv : it->second) {
4544
objects.emplace_back(kv.first);
4645
}
4746
}

lib/base/dependencygraph.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#define DEPENDENCYGRAPH_H
55

66
#include "base/i2-base.hpp"
7-
#include "base/object.hpp"
7+
#include "base/configobject.hpp"
88
#include <map>
99
#include <mutex>
1010

@@ -18,15 +18,15 @@ namespace icinga {
1818
class DependencyGraph
1919
{
2020
public:
21-
static void AddDependency(Object* child, Object* parent);
22-
static void RemoveDependency(Object* child, Object* parent);
23-
static std::vector<Object::Ptr> GetChildren(const Object::Ptr& parent);
21+
static void AddDependency(ConfigObject* child, ConfigObject* parent);
22+
static void RemoveDependency(ConfigObject* child, ConfigObject* parent);
23+
static std::vector<ConfigObject::Ptr> GetChildren(const ConfigObject::Ptr& parent);
2424

2525
private:
2626
DependencyGraph();
2727

2828
static std::mutex m_Mutex;
29-
static std::map<Object *, std::map<Object *, int> > m_Dependencies;
29+
static std::map<ConfigObject*, std::map<ConfigObject*, int>> m_Dependencies;
3030
};
3131

3232
}

lib/base/scriptutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ String ScriptUtils::MsiGetComponentPathShim(const String& component)
520520

521521
Array::Ptr ScriptUtils::TrackParents(const Object::Ptr& child)
522522
{
523-
return Array::FromVector(DependencyGraph::GetChildren(child));
523+
return Array::FromVector(DependencyGraph::GetChildren(dynamic_pointer_cast<ConfigObject>(child)));
524524
}
525525

526526
double ScriptUtils::Ptr(const Object::Ptr& object)

lib/remote/configobjectutility.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,7 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
319319
return false;
320320
}
321321

322-
for (const Object::Ptr& pobj : parents) {
323-
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);
324-
325-
if (!parentObj)
326-
continue;
327-
322+
for (auto& parentObj : parents) {
328323
DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation, cookie);
329324
}
330325

lib/remote/objectqueryhandler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,7 @@ bool ObjectQueryHandler::HandleRequest(
208208
Array::Ptr used_by = new Array();
209209
metaAttrs.emplace_back("used_by", used_by);
210210

211-
for (auto& pobj : DependencyGraph::GetChildren(obj))
212-
{
213-
ConfigObject::Ptr configObj = dynamic_pointer_cast<ConfigObject>(pobj);
214-
215-
if (!configObj)
216-
continue;
217-
211+
for (auto& configObj : DependencyGraph::GetChildren(obj)) {
218212
used_by->Add(new Dictionary({
219213
{ "type", configObj->GetReflectionType()->GetName() },
220214
{ "name", configObj->GetName() }

tools/mkclass/classcompiler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -930,23 +930,23 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
930930
<< "\t" << "}" << std::endl;
931931
} else {
932932
m_Impl << "\t" << "if (!oldValue.IsEmpty())" << std::endl
933-
<< "\t\t" << "DependencyGraph::RemoveDependency(this, ConfigObject::GetObject";
933+
<< "\t\tDependencyGraph::RemoveDependency(";
934934

935935
/* Ew */
936936
if (field.Type.TypeName == "Zone" && m_Library == "base")
937-
m_Impl << "(\"Zone\", ";
937+
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
938938
else
939-
m_Impl << "<" << field.Type.TypeName << ">(";
939+
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
940940

941941
m_Impl << "oldValue).get());" << std::endl
942942
<< "\t" << "if (!newValue.IsEmpty())" << std::endl
943-
<< "\t\t" << "DependencyGraph::AddDependency(this, ConfigObject::GetObject";
943+
<< "\t\tDependencyGraph::AddDependency(";
944944

945945
/* Ew */
946946
if (field.Type.TypeName == "Zone" && m_Library == "base")
947-
m_Impl << "(\"Zone\", ";
947+
m_Impl << "dynamic_cast<ConfigObject*>(this), ConfigObject::GetObject(\"Zone\", ";
948948
else
949-
m_Impl << "<" << field.Type.TypeName << ">(";
949+
m_Impl << "this, ConfigObject::GetObject<" << field.Type.TypeName << ">(";
950950

951951
m_Impl << "newValue).get());" << std::endl;
952952
}

0 commit comments

Comments
 (0)