1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
22 using namespace icinga;
24 DynamicType::DynamicType(const String& name, const DynamicType::ObjectFactory& factory)
25 : m_Name(name), m_ObjectFactory(factory)
28 DynamicType::Ptr DynamicType::GetByName(const String& name)
30 DynamicType::TypeMap::const_iterator tt = GetTypes().find(name);
32 if (tt == GetTypes().end())
33 return DynamicType::Ptr();
38 DynamicType::TypeMap& DynamicType::GetTypes(void)
40 static DynamicType::TypeMap types;
44 DynamicType::NameMap& DynamicType::GetObjects(void)
49 String DynamicType::GetName(void) const
54 void DynamicType::RegisterObject(const DynamicObject::Ptr& object)
56 m_Objects[object->GetName()] = object;
59 void DynamicType::UnregisterObject(const DynamicObject::Ptr& object)
61 m_Objects.erase(object->GetName());
64 DynamicObject::Ptr DynamicType::GetObject(const String& name) const
66 DynamicType::NameMap::const_iterator nt = m_Objects.find(name);
68 if (nt == m_Objects.end())
69 return DynamicObject::Ptr();
74 void DynamicType::RegisterType(const DynamicType::Ptr& type)
76 if (GetByName(type->GetName()))
77 BOOST_THROW_EXCEPTION(runtime_error("Cannot register class for type '" +
78 type->GetName() + "': Objects of this type already exist."));
80 GetTypes()[type->GetName()] = type;
83 DynamicObject::Ptr DynamicType::CreateObject(const Dictionary::Ptr& serializedUpdate) const
85 DynamicObject::Ptr obj = m_ObjectFactory(serializedUpdate);
87 /* register attributes */
89 DynamicAttributeType type;
90 BOOST_FOREACH(tuples::tie(name, type), m_Attributes)
91 obj->RegisterAttribute(name, type);
93 /* apply the object's non-config attributes */
94 obj->ApplyUpdate(serializedUpdate, Attribute_All & ~Attribute_Config);
99 bool DynamicType::TypeExists(const String& name)
101 return (GetByName(name));
104 void DynamicType::AddAttribute(const String& name, DynamicAttributeType type)
106 m_Attributes[name] = type;
109 void DynamicType::RemoveAttribute(const String& name)
111 m_Attributes.erase(name);
114 bool DynamicType::HasAttribute(const String& name)
116 return (m_Attributes.find(name) != m_Attributes.end());
119 void DynamicType::AddAttributes(const AttributeDescription *attributes, int attributeCount)
121 for (int i = 0; i < attributeCount; i++)
122 AddAttribute(attributes[i].Name, attributes[i].Type);