icinga2/lib/base/dependencygraph.cpp
Alexander A. Klimov 188ba53b74 DependencyGraph: switch "parent" and "child" terminology
The .ti files call `DependencyGraph::AddDependency(this, service.get())`. Obviously, `service.get()` is the parent and `this` (Downtime, Notification, ...) is the child. The DependencyGraph terminology should reflect this not to confuse its future users.
2024-12-04 10:57:30 +01:00

51 lines
1.1 KiB
C++

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/dependencygraph.hpp"
using namespace icinga;
std::mutex DependencyGraph::m_Mutex;
std::map<Object *, std::map<Object *, int> > DependencyGraph::m_Dependencies;
void DependencyGraph::AddDependency(Object* child, Object* parent)
{
std::unique_lock<std::mutex> lock(m_Mutex);
m_Dependencies[parent][child]++;
}
void DependencyGraph::RemoveDependency(Object* child, Object* parent)
{
std::unique_lock<std::mutex> lock(m_Mutex);
auto& refs = m_Dependencies[parent];
auto it = refs.find(child);
if (it == refs.end())
return;
it->second--;
if (it->second == 0)
refs.erase(it);
if (refs.empty())
m_Dependencies.erase(parent);
}
std::vector<Object::Ptr> DependencyGraph::GetChildren(const Object::Ptr& parent)
{
std::vector<Object::Ptr> objects;
std::unique_lock<std::mutex> lock(m_Mutex);
auto it = m_Dependencies.find(parent.get());
if (it != m_Dependencies.end()) {
typedef std::pair<Object *, int> kv_pair;
for (const kv_pair& kv : it->second) {
objects.emplace_back(kv.first);
}
}
return objects;
}