1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/configobject.hpp"
4 #include "base/convert.hpp"
5 #include "base/exception.hpp"
7 using namespace icinga;
9 ConfigType::~ConfigType()
12 ConfigObject::Ptr ConfigType::GetObject(const String& name) const
14 boost::mutex::scoped_lock lock(m_Mutex);
16 auto nt = m_ObjectMap.find(name);
18 if (nt == m_ObjectMap.end())
24 void ConfigType::RegisterObject(const ConfigObject::Ptr& object)
26 String name = object->GetName();
29 boost::mutex::scoped_lock lock(m_Mutex);
31 auto it = m_ObjectMap.find(name);
33 if (it != m_ObjectMap.end()) {
34 if (it->second == object)
37 auto *type = dynamic_cast<Type *>(this);
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()));
44 m_ObjectMap[name] = object;
45 m_ObjectVector.push_back(object);
49 void ConfigType::UnregisterObject(const ConfigObject::Ptr& object)
51 String name = object->GetName();
54 boost::mutex::scoped_lock lock(m_Mutex);
56 m_ObjectMap.erase(name);
57 m_ObjectVector.erase(std::remove(m_ObjectVector.begin(), m_ObjectVector.end(), object), m_ObjectVector.end());
61 std::vector<ConfigObject::Ptr> ConfigType::GetObjects() const
63 boost::mutex::scoped_lock lock(m_Mutex);
64 return m_ObjectVector;
67 std::vector<ConfigObject::Ptr> ConfigType::GetObjectsHelper(Type *type)
69 return static_cast<TypeImpl<ConfigObject> *>(type)->GetObjects();
72 int ConfigType::GetObjectCount() const
74 boost::mutex::scoped_lock lock(m_Mutex);
75 return m_ObjectVector.size();