]> granicus.if.org Git - icinga2/blob - lib/base/dynamictype.cpp
Use BOOST_THROW_EXCEPTION instead of boost::throw_exception()
[icinga2] / lib / base / dynamictype.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-base.h"
21
22 using namespace icinga;
23
24 DynamicType::DynamicType(const String& name, const DynamicType::ObjectFactory& factory)
25         : m_Name(name), m_ObjectFactory(factory)
26 { }
27
28 DynamicType::Ptr DynamicType::GetByName(const String& name)
29 {
30         DynamicType::TypeMap::const_iterator tt = GetTypes().find(name);
31
32         if (tt == GetTypes().end())
33                 return DynamicType::Ptr();
34
35         return tt->second;
36 }
37
38 DynamicType::TypeMap& DynamicType::GetTypes(void)
39 {
40         static DynamicType::TypeMap types;
41         return types;
42 }
43
44 DynamicType::NameMap& DynamicType::GetObjects(void)
45 {
46         return m_Objects;
47 }
48
49 String DynamicType::GetName(void) const
50 {
51         return m_Name;
52 }
53
54 void DynamicType::RegisterObject(const DynamicObject::Ptr& object)
55 {
56         m_Objects[object->GetName()] = object;
57 }
58
59 void DynamicType::UnregisterObject(const DynamicObject::Ptr& object)
60 {
61         m_Objects.erase(object->GetName());
62 }
63
64 DynamicObject::Ptr DynamicType::GetObject(const String& name) const
65 {
66         DynamicType::NameMap::const_iterator nt = m_Objects.find(name);
67
68         if (nt == m_Objects.end())
69                 return DynamicObject::Ptr();
70
71         return nt->second;
72 }
73
74 void DynamicType::RegisterType(const DynamicType::Ptr& type)
75 {
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."));
79
80         GetTypes()[type->GetName()] = type;
81 }
82
83 DynamicObject::Ptr DynamicType::CreateObject(const Dictionary::Ptr& serializedUpdate) const
84 {
85         DynamicObject::Ptr obj = m_ObjectFactory(serializedUpdate);
86
87         /* register attributes */
88         String name;
89         DynamicAttributeType type;
90         BOOST_FOREACH(tuples::tie(name, type), m_Attributes)
91                 obj->RegisterAttribute(name, type);
92
93         /* apply the object's non-config attributes */
94         obj->ApplyUpdate(serializedUpdate, Attribute_All & ~Attribute_Config);
95
96         return obj;
97 }
98
99 bool DynamicType::TypeExists(const String& name)
100 {
101         return (GetByName(name));
102 }
103
104 void DynamicType::AddAttribute(const String& name, DynamicAttributeType type)
105 {
106         m_Attributes[name] = type;
107 }
108
109 void DynamicType::RemoveAttribute(const String& name)
110 {
111         m_Attributes.erase(name);
112 }
113
114 bool DynamicType::HasAttribute(const String& name)
115 {
116         return (m_Attributes.find(name) != m_Attributes.end());
117 }
118
119 void DynamicType::AddAttributes(const AttributeDescription *attributes, int attributeCount)
120 {
121         for (int i = 0; i < attributeCount; i++)
122                 AddAttribute(attributes[i].Name, attributes[i].Type);
123 }