1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://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/array.hpp"
21 #include "base/function.hpp"
22 #include "base/functionwrapper.hpp"
23 #include "base/scriptframe.hpp"
24 #include "base/objectlock.hpp"
25 #include <boost/foreach.hpp>
27 using namespace icinga;
29 static double ArrayLen(void)
31 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
32 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
33 return self->GetLength();
36 static void ArraySet(int index, const Value& value)
38 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
39 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
40 self->Set(index, value);
43 static void ArrayAdd(const Value& value)
45 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
50 static void ArrayRemove(int index)
52 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
57 static bool ArrayContains(const Value& value)
59 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
61 return self->Contains(value);
64 static void ArrayClear(void)
66 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
67 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
71 static bool ArraySortCmp(const Function::Ptr& cmp, const Value& a, const Value& b)
73 std::vector<Value> args;
76 return cmp->Invoke(args);
79 static Array::Ptr ArraySort(const std::vector<Value>& args)
81 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
82 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
84 Array::Ptr arr = self->ShallowClone();
87 ObjectLock olock(arr);
88 std::sort(arr->Begin(), arr->End());
90 ObjectLock olock(arr);
91 std::sort(arr->Begin(), arr->End(), boost::bind(ArraySortCmp, args[0], _1, _2));
97 static Array::Ptr ArrayClone(void)
99 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
100 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
101 return self->ShallowClone();
104 static Value ArrayJoin(const Value& separator)
106 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
107 Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
112 ObjectLock olock(self);
113 BOOST_FOREACH(const Value& item, self) {
117 result = result + separator;
120 result = result + item;
126 Object::Ptr Array::GetPrototype(void)
128 static Dictionary::Ptr prototype;
131 prototype = new Dictionary();
132 prototype->Set("len", new Function(WrapFunction(ArrayLen), true));
133 prototype->Set("set", new Function(WrapFunction(ArraySet)));
134 prototype->Set("add", new Function(WrapFunction(ArrayAdd)));
135 prototype->Set("remove", new Function(WrapFunction(ArrayRemove)));
136 prototype->Set("contains", new Function(WrapFunction(ArrayContains), true));
137 prototype->Set("clear", new Function(WrapFunction(ArrayClear)));
138 prototype->Set("sort", new Function(WrapFunction(ArraySort), true));
139 prototype->Set("clone", new Function(WrapFunction(ArrayClone), true));
140 prototype->Set("join", new Function(WrapFunction(ArrayJoin), true));