1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/namespace.hpp"
4 #include "base/function.hpp"
5 #include "base/functionwrapper.hpp"
6 #include "base/scriptframe.hpp"
7 #include "base/array.hpp"
9 using namespace icinga;
11 static void NamespaceSet(const String& key, const Value& value)
13 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
14 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
15 REQUIRE_NOT_NULL(self);
16 self->Set(key, value);
19 static Value NamespaceGet(const String& key)
21 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
22 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
23 REQUIRE_NOT_NULL(self);
24 return self->Get(key);
27 static void NamespaceRemove(const String& key)
29 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
30 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
31 REQUIRE_NOT_NULL(self);
35 static bool NamespaceContains(const String& key)
37 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
38 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
39 REQUIRE_NOT_NULL(self);
40 return self->Contains(key);
43 static Array::Ptr NamespaceKeys()
45 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
47 REQUIRE_NOT_NULL(self);
50 ObjectLock olock(self);
51 for (const Namespace::Pair& kv : self) {
52 keys.push_back(kv.first);
54 return new Array(std::move(keys));
57 static Array::Ptr NamespaceValues()
59 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60 Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
61 REQUIRE_NOT_NULL(self);
64 ObjectLock olock(self);
65 for (const Namespace::Pair& kv : self) {
66 values.push_back(kv.second->Get());
68 return new Array(std::move(values));
71 Object::Ptr Namespace::GetPrototype()
73 static Dictionary::Ptr prototype = new Dictionary({
74 { "set", new Function("Namespace#set", NamespaceSet, { "key", "value" }) },
75 { "get", new Function("Namespace#get", NamespaceGet, { "key" }) },
76 { "remove", new Function("Namespace#remove", NamespaceRemove, { "key" }) },
77 { "contains", new Function("Namespace#contains", NamespaceContains, { "key" }, true) },
78 { "keys", new Function("Namespace#keys", NamespaceKeys, {}, true) },
79 { "values", new Function("Namespace#values", NamespaceValues, {}, true) },