]> granicus.if.org Git - icinga2/blob - lib/base/array.hpp
5e0602106db5517b0366e9d36a8f1ade4c59008c
[icinga2] / lib / base / array.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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 #ifndef ARRAY_H
21 #define ARRAY_H
22
23 #include "base/i2-base.hpp"
24 #include "base/value.hpp"
25 #include <boost/range/iterator.hpp>
26 #include <vector>
27
28 namespace icinga
29 {
30
31 /**
32  * An array of Value items.
33  *
34  * @ingroup base
35  */
36 class I2_BASE_API Array : public Object
37 {
38 public:
39         DECLARE_PTR_TYPEDEFS(Array);
40
41         /**
42          * An iterator that can be used to iterate over array elements.
43          */
44         typedef std::vector<Value>::iterator Iterator;
45
46         typedef std::vector<Value>::size_type SizeType;
47
48         Value Get(unsigned int index) const;
49         void Set(unsigned int index, const Value& value);
50         void Add(const Value& value);
51
52         Iterator Begin(void);
53         Iterator End(void);
54
55         size_t GetLength(void) const;
56         bool Contains(const String& value) const;
57
58         void Insert(unsigned int index, const Value& value);
59         void Remove(unsigned int index);
60         void Remove(Iterator it);
61
62         void Resize(size_t new_size);
63         void Clear(void);
64
65         void CopyTo(const Array::Ptr& dest) const;
66         Array::Ptr ShallowClone(void) const;
67
68         static Array::Ptr FromJson(cJSON *json);
69         cJSON *ToJson(void) const;
70
71 private:
72         std::vector<Value> m_Data; /**< The data for the array. */
73 };
74
75 inline Array::Iterator range_begin(Array::Ptr x)
76 {
77         return x->Begin();
78 }
79
80 inline Array::Iterator range_end(Array::Ptr x)
81 {
82         return x->End();
83 }
84
85 }
86
87 namespace boost
88 {
89
90 template<>
91 struct range_mutable_iterator<icinga::Array::Ptr>
92 {
93         typedef icinga::Array::Iterator type;
94 };
95
96 template<>
97 struct range_const_iterator<icinga::Array::Ptr>
98 {
99         typedef icinga::Array::Iterator type;
100 };
101
102 }
103
104 #endif /* ARRAY_H */