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