1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/dictionary.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 double DictionaryLen()
13 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
14 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
15 REQUIRE_NOT_NULL(self);
16 return self->GetLength();
19 static void DictionarySet(const String& key, const Value& value)
21 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
22 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
23 REQUIRE_NOT_NULL(self);
24 self->Set(key, value);
27 static Value DictionaryGet(const String& key)
29 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
30 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
31 REQUIRE_NOT_NULL(self);
32 return self->Get(key);
35 static void DictionaryRemove(const String& key)
37 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
38 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
39 REQUIRE_NOT_NULL(self);
43 static void DictionaryClear()
45 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
47 REQUIRE_NOT_NULL(self);
51 static bool DictionaryContains(const String& key)
53 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
54 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
55 REQUIRE_NOT_NULL(self);
56 return self->Contains(key);
59 static Dictionary::Ptr DictionaryShallowClone()
61 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
62 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
63 REQUIRE_NOT_NULL(self);
64 return self->ShallowClone();
67 static Array::Ptr DictionaryKeys()
69 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
70 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
71 REQUIRE_NOT_NULL(self);
74 ObjectLock olock(self);
75 for (const Dictionary::Pair& kv : self) {
76 keys.push_back(kv.first);
78 return new Array(std::move(keys));
81 static Array::Ptr DictionaryValues()
83 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
84 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
85 REQUIRE_NOT_NULL(self);
88 ObjectLock olock(self);
89 for (const Dictionary::Pair& kv : self) {
90 values.push_back(kv.second);
92 return new Array(std::move(values));
95 static void DictionaryFreeze()
97 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
98 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
102 Object::Ptr Dictionary::GetPrototype()
104 static Dictionary::Ptr prototype = new Dictionary({
105 { "len", new Function("Dictionary#len", DictionaryLen, {}, true) },
106 { "set", new Function("Dictionary#set", DictionarySet, { "key", "value" }) },
107 { "get", new Function("Dictionary#get", DictionaryGet, { "key" }) },
108 { "remove", new Function("Dictionary#remove", DictionaryRemove, { "key" }) },
109 { "clear", new Function("Dictionary#clear", DictionaryClear, {}) },
110 { "contains", new Function("Dictionary#contains", DictionaryContains, { "key" }, true) },
111 { "shallow_clone", new Function("Dictionary#shallow_clone", DictionaryShallowClone, {}, true) },
112 { "keys", new Function("Dictionary#keys", DictionaryKeys, {}, true) },
113 { "values", new Function("Dictionary#values", DictionaryValues, {}, true) },
114 { "freeze", new Function("Dictionary#freeze", DictionaryFreeze, {}) }