]> granicus.if.org Git - icinga2/blob - lib/base/configtype.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / configtype.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/configobject.hpp"
4 #include "base/convert.hpp"
5 #include "base/exception.hpp"
6
7 using namespace icinga;
8
9 ConfigType::~ConfigType()
10 { }
11
12 ConfigObject::Ptr ConfigType::GetObject(const String& name) const
13 {
14         boost::mutex::scoped_lock lock(m_Mutex);
15
16         auto nt = m_ObjectMap.find(name);
17
18         if (nt == m_ObjectMap.end())
19                 return nullptr;
20
21         return nt->second;
22 }
23
24 void ConfigType::RegisterObject(const ConfigObject::Ptr& object)
25 {
26         String name = object->GetName();
27
28         {
29                 boost::mutex::scoped_lock lock(m_Mutex);
30
31                 auto it = m_ObjectMap.find(name);
32
33                 if (it != m_ObjectMap.end()) {
34                         if (it->second == object)
35                                 return;
36
37                         auto *type = dynamic_cast<Type *>(this);
38
39                         BOOST_THROW_EXCEPTION(ScriptError("An object with type '" + type->GetName() + "' and name '" + name + "' already exists (" +
40                                 Convert::ToString(it->second->GetDebugInfo()) + "), new declaration: " + Convert::ToString(object->GetDebugInfo()),
41                                 object->GetDebugInfo()));
42                 }
43
44                 m_ObjectMap[name] = object;
45                 m_ObjectVector.push_back(object);
46         }
47 }
48
49 void ConfigType::UnregisterObject(const ConfigObject::Ptr& object)
50 {
51         String name = object->GetName();
52
53         {
54                 boost::mutex::scoped_lock lock(m_Mutex);
55
56                 m_ObjectMap.erase(name);
57                 m_ObjectVector.erase(std::remove(m_ObjectVector.begin(), m_ObjectVector.end(), object), m_ObjectVector.end());
58         }
59 }
60
61 std::vector<ConfigObject::Ptr> ConfigType::GetObjects() const
62 {
63         boost::mutex::scoped_lock lock(m_Mutex);
64         return m_ObjectVector;
65 }
66
67 std::vector<ConfigObject::Ptr> ConfigType::GetObjectsHelper(Type *type)
68 {
69         return static_cast<TypeImpl<ConfigObject> *>(type)->GetObjects();
70 }
71
72 int ConfigType::GetObjectCount() const
73 {
74         boost::mutex::scoped_lock lock(m_Mutex);
75         return m_ObjectVector.size();
76 }