]> granicus.if.org Git - icinga2/blob - lib/base/namespace-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / namespace-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
8
9 using namespace icinga;
10
11 static void NamespaceSet(const String& key, const Value& value)
12 {
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);
17 }
18
19 static Value NamespaceGet(const String& key)
20 {
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);
25 }
26
27 static void NamespaceRemove(const String& key)
28 {
29         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
30         Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
31         REQUIRE_NOT_NULL(self);
32         self->Remove(key);
33 }
34
35 static bool NamespaceContains(const String& key)
36 {
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);
41 }
42
43 static Array::Ptr NamespaceKeys()
44 {
45         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46         Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
47         REQUIRE_NOT_NULL(self);
48
49         ArrayData keys;
50         ObjectLock olock(self);
51         for (const Namespace::Pair& kv : self) {
52                 keys.push_back(kv.first);
53         }
54         return new Array(std::move(keys));
55 }
56
57 static Array::Ptr NamespaceValues()
58 {
59         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60         Namespace::Ptr self = static_cast<Namespace::Ptr>(vframe->Self);
61         REQUIRE_NOT_NULL(self);
62
63         ArrayData values;
64         ObjectLock olock(self);
65         for (const Namespace::Pair& kv : self) {
66                 values.push_back(kv.second->Get());
67         }
68         return new Array(std::move(values));
69 }
70
71 Object::Ptr Namespace::GetPrototype()
72 {
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) },
80         });
81
82         return prototype;
83 }
84