1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/namespace.hpp"
4 #include "base/objectlock.hpp"
5 #include "base/debug.hpp"
6 #include "base/primitivetype.hpp"
7 #include "base/debuginfo.hpp"
8 #include "base/exception.hpp"
11 using namespace icinga;
13 template class std::map<icinga::String, std::shared_ptr<icinga::NamespaceValue> >;
15 REGISTER_PRIMITIVE_TYPE(Namespace, Object, Namespace::GetPrototype());
17 Namespace::Namespace(NamespaceBehavior *behavior)
18 : m_Behavior(std::unique_ptr<NamespaceBehavior>(behavior))
21 Value Namespace::Get(const String& field) const
23 ObjectLock olock(this);
26 if (!GetOwnField(field, &value))
27 BOOST_THROW_EXCEPTION(ScriptError("Namespace does not contain field '" + field + "'"));
31 bool Namespace::Get(const String& field, Value *value) const
33 ObjectLock olock(this);
35 auto nsVal = GetAttribute(field);
40 *value = nsVal->Get(DebugInfo());
44 void Namespace::Set(const String& field, const Value& value, bool overrideFrozen)
46 ObjectLock olock(this);
48 return SetFieldByName(field, value, overrideFrozen, DebugInfo());
51 bool Namespace::Contains(const String& field) const
53 ObjectLock olock(this);
55 return HasOwnField(field);
58 void Namespace::Remove(const String& field, bool overrideFrozen)
60 ObjectLock olock(this);
62 m_Behavior->Remove(this, field, overrideFrozen);
65 void Namespace::RemoveAttribute(const String& field)
67 ObjectLock olock(this);
69 Namespace::Iterator it;
70 it = m_Data.find(field);
72 if (it == m_Data.end())
78 std::shared_ptr<NamespaceValue> Namespace::GetAttribute(const String& key) const
80 ObjectLock olock(this);
82 auto it = m_Data.find(key);
84 if (it == m_Data.end())
90 void Namespace::SetAttribute(const String& key, const std::shared_ptr<NamespaceValue>& nsVal)
92 ObjectLock olock(this);
97 Value Namespace::GetFieldByName(const String& field, bool, const DebugInfo& debugInfo) const
99 ObjectLock olock(this);
101 auto nsVal = GetAttribute(field);
104 return nsVal->Get(debugInfo);
106 return GetPrototypeField(const_cast<Namespace *>(this), field, false, debugInfo); /* Ignore indexer not found errors similar to the Dictionary class. */
109 void Namespace::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
111 ObjectLock olock(this);
113 auto nsVal = GetAttribute(field);
116 m_Behavior->Register(this, field, value, overrideFrozen, debugInfo);
118 nsVal->Set(value, overrideFrozen, debugInfo);
121 bool Namespace::HasOwnField(const String& field) const
123 ObjectLock olock(this);
125 return GetAttribute(field) != nullptr;
128 bool Namespace::GetOwnField(const String& field, Value *result) const
130 ObjectLock olock(this);
132 auto nsVal = GetAttribute(field);
137 *result = nsVal->Get(DebugInfo());
141 EmbeddedNamespaceValue::EmbeddedNamespaceValue(const Value& value)
145 Value EmbeddedNamespaceValue::Get(const DebugInfo& debugInfo) const
150 void EmbeddedNamespaceValue::Set(const Value& value, bool, const DebugInfo&)
155 void ConstEmbeddedNamespaceValue::Set(const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
158 BOOST_THROW_EXCEPTION(ScriptError("Constant must not be modified.", debugInfo));
160 EmbeddedNamespaceValue::Set(value, overrideFrozen, debugInfo);
163 void NamespaceBehavior::Register(const Namespace::Ptr& ns, const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) const
165 ns->SetAttribute(field, std::make_shared<EmbeddedNamespaceValue>(value));
168 void NamespaceBehavior::Remove(const Namespace::Ptr& ns, const String& field, bool overrideFrozen)
170 if (!overrideFrozen) {
171 auto attr = ns->GetAttribute(field);
173 if (dynamic_pointer_cast<ConstEmbeddedNamespaceValue>(attr))
174 BOOST_THROW_EXCEPTION(ScriptError("Constants must not be removed."));
177 ns->RemoveAttribute(field);
180 void ConstNamespaceBehavior::Register(const Namespace::Ptr& ns, const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo) const
182 if (m_Frozen && !overrideFrozen)
183 BOOST_THROW_EXCEPTION(ScriptError("Namespace is read-only and must not be modified.", debugInfo));
185 ns->SetAttribute(field, std::make_shared<ConstEmbeddedNamespaceValue>(value));
188 void ConstNamespaceBehavior::Remove(const Namespace::Ptr& ns, const String& field, bool overrideFrozen)
190 if (m_Frozen && !overrideFrozen)
191 BOOST_THROW_EXCEPTION(ScriptError("Namespace is read-only and must not be modified."));
193 NamespaceBehavior::Remove(ns, field, overrideFrozen);
196 void ConstNamespaceBehavior::Freeze()
201 Namespace::Iterator Namespace::Begin()
205 return m_Data.begin();
208 Namespace::Iterator Namespace::End()
215 Namespace::Iterator icinga::begin(const Namespace::Ptr& x)
220 Namespace::Iterator icinga::end(const Namespace::Ptr& x)