]> granicus.if.org Git - icinga2/blob - lib/base/dictionary-script.cpp
Update copyright headers for 2016
[icinga2] / lib / base / dictionary-script.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/dictionary.hpp"
21 #include "base/function.hpp"
22 #include "base/functionwrapper.hpp"
23 #include "base/scriptframe.hpp"
24 #include "base/array.hpp"
25 #include <boost/foreach.hpp>
26
27 using namespace icinga;
28
29 static double DictionaryLen(void)
30 {
31         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
32         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
33         return self->GetLength();
34 }
35
36 static void DictionarySet(const String& key, const Value& value)
37 {
38         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
39         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
40         self->Set(key, value);
41 }
42
43 static Value DictionaryGet(const String& key)
44 {
45         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
47         return self->Get(key);
48 }
49
50 static void DictionaryRemove(const String& key)
51 {
52         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
54         self->Remove(key);
55 }
56
57 static bool DictionaryContains(const String& key)
58 {
59         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
61         return self->Contains(key);
62 }
63
64 static Dictionary::Ptr DictionaryShallowClone(void)
65 {
66         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
67         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
68         return self->ShallowClone();
69 }
70
71 static Array::Ptr DictionaryKeys(void)
72 {
73         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
74         Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
75         Array::Ptr keys = new Array();
76         ObjectLock olock(self);
77         BOOST_FOREACH(const Dictionary::Pair& kv, self) {
78                 keys->Add(kv.first);
79         }
80         return keys;
81 }
82
83 Object::Ptr Dictionary::GetPrototype(void)
84 {
85         static Dictionary::Ptr prototype;
86
87         if (!prototype) {
88                 prototype = new Dictionary();
89                 prototype->Set("len", new Function(WrapFunction(DictionaryLen), true));
90                 prototype->Set("set", new Function(WrapFunction(DictionarySet)));
91                 prototype->Set("get", new Function(WrapFunction(DictionaryGet)));
92                 prototype->Set("remove", new Function(WrapFunction(DictionaryRemove)));
93                 prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true));
94                 prototype->Set("shallow_clone", new Function(WrapFunction(DictionaryShallowClone), true));
95                 prototype->Set("keys", new Function(WrapFunction(DictionaryKeys), true));
96         }
97
98         return prototype;
99 }
100