]> granicus.if.org Git - icinga2/blob - lib/base/dependencygraph.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / dependencygraph.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/dependencygraph.hpp"
4
5 using namespace icinga;
6
7 boost::mutex DependencyGraph::m_Mutex;
8 std::map<Object *, std::map<Object *, int> > DependencyGraph::m_Dependencies;
9
10 void DependencyGraph::AddDependency(Object *parent, Object *child)
11 {
12         boost::mutex::scoped_lock lock(m_Mutex);
13         m_Dependencies[child][parent]++;
14 }
15
16 void DependencyGraph::RemoveDependency(Object *parent, Object *child)
17 {
18         boost::mutex::scoped_lock lock(m_Mutex);
19
20         auto& refs = m_Dependencies[child];
21         auto it = refs.find(parent);
22
23         if (it == refs.end())
24                 return;
25
26         it->second--;
27
28         if (it->second == 0)
29                 refs.erase(it);
30
31         if (refs.empty())
32                 m_Dependencies.erase(child);
33 }
34
35 std::vector<Object::Ptr> DependencyGraph::GetParents(const Object::Ptr& child)
36 {
37         std::vector<Object::Ptr> objects;
38
39         boost::mutex::scoped_lock lock(m_Mutex);
40         auto it = m_Dependencies.find(child.get());
41
42         if (it != m_Dependencies.end()) {
43                 typedef std::pair<Object *, int> kv_pair;
44                 for (const kv_pair& kv : it->second) {
45                         objects.emplace_back(kv.first);
46                 }
47         }
48
49         return objects;
50 }