]> granicus.if.org Git - icinga2/blob - lib/base/array-script.cpp
5ab10360866a0beacaeb888ddf6076331ffb6957
[icinga2] / lib / base / array-script.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://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/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>
26
27 using namespace icinga;
28
29 static double ArrayLen(void)
30 {
31         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
32         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
33         return self->GetLength();
34 }
35
36 static void ArraySet(int index, const Value& value)
37 {
38         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
39         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
40         self->Set(index, value);
41 }
42
43 static void ArrayAdd(const Value& value)
44 {
45         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
47         self->Add(value);
48 }
49
50 static void ArrayRemove(int index)
51 {
52         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
54         self->Remove(index);
55 }
56
57 static bool ArrayContains(const Value& value)
58 {
59         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
61         return self->Contains(value);
62 }
63
64 static void ArrayClear(void)
65 {
66         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
67         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
68         self->Clear();
69 }
70
71 static bool ArraySortCmp(const Function::Ptr& cmp, const Value& a, const Value& b)
72 {
73         std::vector<Value> args;
74         args.push_back(a);
75         args.push_back(b);
76         return cmp->Invoke(args);
77 }
78
79 static Array::Ptr ArraySort(const std::vector<Value>& args)
80 {
81         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
82         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
83
84         Array::Ptr arr = self->ShallowClone();
85
86         if (args.empty()) {
87                 ObjectLock olock(arr);
88                 std::sort(arr->Begin(), arr->End());
89         } else {
90                 ObjectLock olock(arr);
91                 std::sort(arr->Begin(), arr->End(), boost::bind(ArraySortCmp, args[0], _1, _2));
92         }
93
94         return arr;
95 }
96
97 static Array::Ptr ArrayClone(void)
98 {
99         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
100         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
101         return self->ShallowClone();
102 }
103
104 static Value ArrayJoin(const Value& separator)
105 {
106         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
107         Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
108
109         Value result;
110         bool first = true;
111
112         ObjectLock olock(self);
113         BOOST_FOREACH(const Value& item, self) {
114                 if (first) {
115                         first = false;
116                 } else {
117                         result = result + separator;
118                 }
119
120                 result = result + item;
121         }
122
123         return result;
124 }
125
126 Object::Ptr Array::GetPrototype(void)
127 {
128         static Dictionary::Ptr prototype;
129
130         if (!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));
141         }
142
143         return prototype;
144 }
145