1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
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>
27 using namespace icinga;
29 static double DictionaryLen(void)
31 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
32 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
33 return self->GetLength();
36 static void DictionarySet(const String& key, const Value& value)
38 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
39 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
40 self->Set(key, value);
43 static Value DictionaryGet(const String& key)
45 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
47 return self->Get(key);
50 static void DictionaryRemove(const String& key)
52 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
57 static bool DictionaryContains(const String& key)
59 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
61 return self->Contains(key);
64 static Dictionary::Ptr DictionaryShallowClone(void)
66 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
67 Dictionary::Ptr self = static_cast<Dictionary::Ptr>(vframe->Self);
68 return self->ShallowClone();
71 static Array::Ptr DictionaryKeys(void)
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) {
83 Object::Ptr Dictionary::GetPrototype(void)
85 static Dictionary::Ptr 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));