]> granicus.if.org Git - icinga2/blob - lib/base/dictionary-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / dictionary-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
8
9 using namespace icinga;
10
11 static double DictionaryLen()
12 {
13         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
14         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
15         REQUIRE_NOT_NULL(self);
16         return self->GetLength();
17 }
18
19 static void DictionarySet(const String& key, const Value& value)
20 {
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);
25 }
26
27 static Value DictionaryGet(const String& key)
28 {
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);
33 }
34
35 static void DictionaryRemove(const String& key)
36 {
37         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
38         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
39         REQUIRE_NOT_NULL(self);
40         self->Remove(key);
41 }
42
43 static void DictionaryClear()
44 {
45         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
47         REQUIRE_NOT_NULL(self);
48         self->Clear();
49 }
50
51 static bool DictionaryContains(const String& key)
52 {
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);
57 }
58
59 static Dictionary::Ptr DictionaryShallowClone()
60 {
61         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
62         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
63         REQUIRE_NOT_NULL(self);
64         return self->ShallowClone();
65 }
66
67 static Array::Ptr DictionaryKeys()
68 {
69         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
70         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
71         REQUIRE_NOT_NULL(self);
72
73         ArrayData keys;
74         ObjectLock olock(self);
75         for (const Dictionary::Pair& kv : self) {
76                 keys.push_back(kv.first);
77         }
78         return new Array(std::move(keys));
79 }
80
81 static Array::Ptr DictionaryValues()
82 {
83         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
84         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
85         REQUIRE_NOT_NULL(self);
86
87         ArrayData values;
88         ObjectLock olock(self);
89         for (const Dictionary::Pair& kv : self) {
90                 values.push_back(kv.second);
91         }
92         return new Array(std::move(values));
93 }
94
95 static void DictionaryFreeze()
96 {
97         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
98         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
99         self->Freeze();
100 }
101
102 Object::Ptr Dictionary::GetPrototype()
103 {
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, {}) }
115         });
116
117         return prototype;
118 }
119