Branch data Line data Source code
1 : : /******************************************************************************
2 : : ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z ---------------
3 : : ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------
4 : : ---- This Source Code Form is subject to the terms of the Mozilla Public -----
5 : : ---- License, v. 2.0. If a copy of the MPL was not distributed with this -----
6 : : ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------
7 : : ******************************************************************************/
8 : :
9 : : #ifndef SL3_CONTAINER_HPP_
10 : : #define SL3_CONTAINER_HPP_
11 : :
12 : : #include <sl3/error.hpp>
13 : :
14 : : namespace sl3
15 : : {
16 : : /**
17 : : * \brief Wrapper to provide begin, end and random access of a container
18 : : *
19 : : * \tparam ContainerType a container like for example std::vector
20 : : *
21 : : * A ContainerType has begin, end, random access and size.
22 : : *
23 : : * This class makes these methods accessible and hides the others, like a
24 : : * push_back, erase, ...
25 : : * A derived class can decide which other methods shall become visible.
26 : : *
27 : : */
28 : : template <typename ContainerType> class Container
29 : : {
30 : : public:
31 : : //@{
32 : : using container_type = ContainerType;
33 : : using iterator = typename container_type::iterator;
34 : : using const_iterator = typename container_type::const_iterator;
35 : : using value_type = typename container_type::value_type;
36 : : using reference = typename container_type::reference;
37 : : using const_reference = typename container_type::const_reference;
38 : : using size_type = typename container_type::size_type;
39 : : //@}
40 : :
41 : : /**
42 : : * \brief Constructor.
43 : : *
44 : : * Create an empty container.
45 : : *
46 : : */
47 : 251 : Container () noexcept {}
48 : : /**
49 : : * \brief Constructor
50 : : *
51 : : * \param container values
52 : : */
53 : 109 : Container (ContainerType container) noexcept (
54 : : std::is_nothrow_move_constructible<ContainerType>::value)
55 : 109 : : _cont (std::move (container))
56 : : {
57 : 109 : }
58 : :
59 : : /**
60 : : * \brief Constructor
61 : : *
62 : : * \param container values
63 : : */
64 : 83 : Container (std::initializer_list<value_type> container)
65 : 166 : : _cont (std::move (container))
66 : : {
67 : 83 : }
68 : :
69 : : /**
70 : : * \brief Copy Constructor
71 : : */
72 : 60 : Container (const Container&) = default;
73 : :
74 : : /**
75 : : * \brief Assignment
76 : : * \return reference to this
77 : : */
78 : 1 : Container& operator= (const Container&) = default;
79 : :
80 : : /**
81 : : * \brief Move constructor
82 : : */
83 : 150 : Container (Container&&) noexcept (
84 : : std::is_nothrow_move_constructible<ContainerType>::value)
85 : : = default;
86 : :
87 : : /**
88 : : * \brief Move assignment
89 : : * \return reference to this
90 : : */
91 : 4 : Container& operator= (Container&&) = default;
92 : :
93 : : /**
94 : : * \brief Destructor
95 : : */
96 : 653 : virtual ~Container () noexcept (
97 : : std::is_nothrow_destructible<ContainerType>::value)
98 : : = default;
99 : :
100 : : /**
101 : : * \brief Iterator access
102 : : * \return requested iterator
103 : : */
104 : : iterator
105 : 127 : begin ()
106 : : {
107 : 254 : return std::begin (_cont);
108 : : }
109 : :
110 : : /**
111 : : * \brief Iterator access
112 : : * \return requested iterator
113 : : */
114 : : const_iterator
115 : 19 : begin () const
116 : : {
117 : 19 : return _cont.cbegin ();
118 : : }
119 : :
120 : : /**
121 : : * \brief Iterator access
122 : : * \return requested iterator
123 : : */
124 : : iterator
125 : 127 : end ()
126 : : {
127 : 254 : return std::end (_cont);
128 : : }
129 : :
130 : : /**
131 : : * \brief Iterator access
132 : : * \return requested iterator
133 : : */
134 : : const_iterator
135 : 19 : end () const
136 : : {
137 : 19 : return _cont.cend ();
138 : : }
139 : :
140 : : /**
141 : : * \brief Iterator access
142 : : * \return requested iterator
143 : : */
144 : : const_iterator
145 : : cbegin () const
146 : : {
147 : : return _cont.cbegin ();
148 : : }
149 : :
150 : : /**
151 : : * \brief Iterator access
152 : : * \return requested iterator
153 : : */
154 : : const_iterator
155 : : cend () const
156 : : {
157 : : return _cont.cend ();
158 : : };
159 : :
160 : : ///
161 : :
162 : : /**
163 : : * \brief Container size
164 : : * \return number of elements
165 : : */
166 : : size_type
167 : 473 : size () const
168 : : {
169 : 473 : return _cont.size ();
170 : : }
171 : :
172 : : /**\brief checked random access
173 : : * \param i index
174 : : * \throw sl3::ErrOutOfRange if index is invalid
175 : : * \return reference to element at requested index
176 : : */
177 : : reference
178 : 44 : at (size_t i)
179 : : {
180 : : try
181 : : {
182 : 44 : return _cont.at (i);
183 : : }
184 : 2 : catch (...)
185 : : {
186 : 1 : throw ErrOutOfRange ("no data at: " + std::to_string (i));
187 : : }
188 : : }
189 : :
190 : : /** \brief checked random access
191 : : *
192 : : * \param i index
193 : : * \throw sl3::ErrOutOfRange if index is invalid
194 : : * \return const reference to element at requested index
195 : : */
196 : : const_reference
197 : 46 : at (size_t i) const
198 : : {
199 : : try
200 : : {
201 : 46 : return _cont.at (i);
202 : : }
203 : 2 : catch (...)
204 : : {
205 : 1 : throw ErrOutOfRange ("no data at: " + std::to_string (i));
206 : : }
207 : : }
208 : :
209 : : /**\brief unchecked random access
210 : : * \param i index
211 : : * behavior is undefined if the given index is invalid
212 : : * \return
213 : : * reference to element at requested index
214 : : */
215 : : reference
216 : 263 : operator[] (size_t i)
217 : : {
218 : 263 : return _cont[i];
219 : : }
220 : : /**\brief unchecked random access
221 : : * \param i index
222 : : * behavior is undefined if the given index is invalid
223 : : * \return
224 : : * reference to element at requested index
225 : : */
226 : : const_reference
227 : 236 : operator[] (size_t i) const
228 : : {
229 : 236 : return _cont[i];
230 : : }
231 : :
232 : : protected:
233 : : /// Container T
234 : : ContainerType _cont;
235 : : };
236 : : }
237 : :
238 : : #endif /* SL3_CONTAINER_HPP_ */
|