libsl3 1.3.53004
A C++ interface for SQLite
Loading...
Searching...
No Matches
container.hpp
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 <cstddef>
13#include <initializer_list>
14#include <iterator>
15#include <string>
16#include <type_traits>
17#include <utility>
18
19#include <sl3/error.hpp>
20
21namespace sl3
22{
23 /**
24 * \brief Wrapper to provide begin, end and random access of a container
25 *
26 * \tparam ContainerType a container like for example std::vector
27 *
28 * A ContainerType has begin, end, random access and size.
29 *
30 * This class makes these methods accessible and hides the others, like a
31 * push_back, erase, ...
32 * A derived class can decide which other methods shall become visible.
33 *
34 */
35 template <typename ContainerType> class Container
36 {
37 public:
38 //@{
39 using container_type = ContainerType;
40 using iterator = typename container_type::iterator;
41 using const_iterator = typename container_type::const_iterator;
42 using value_type = typename container_type::value_type;
43 using reference = typename container_type::reference;
44 using const_reference = typename container_type::const_reference;
45 using size_type = typename container_type::size_type;
46 //@}
47
48 /**
49 * \brief Constructor.
50 *
51 * Create an empty container.
52 *
53 */
54 Container () noexcept {}
55 /**
56 * \brief Constructor
57 *
58 * \param container values
59 */
60 Container (ContainerType container)
61 : _cont (std::move (container))
62 {
63 }
64
65 /**
66 * \brief Constructor
67 *
68 * \param container values
69 */
70 Container (std::initializer_list<value_type> container)
71 : _cont (std::move (container))
72 {
73 }
74
75 /**
76 * \brief Copy Constructor
77 */
78 Container (const Container&) = default;
79
80 /**
81 * \brief Assignment
82 * \return reference to this
83 */
84 Container& operator= (const Container&) = default;
85
86 /**
87 * \brief Move constructor
88 */
89 Container (Container&&) noexcept (
90 std::is_nothrow_move_constructible<ContainerType>::value) = default;
91
92 /**
93 * \brief Move assignment
94 * \return reference to this
95 */
96 Container& operator= (Container&&) = default;
97
98 /**
99 * \brief Destructor
100 */
101 virtual ~Container () noexcept (
102 std::is_nothrow_destructible<ContainerType>::value) = default;
103
104 /**
105 * \brief Iterator access
106 * \return requested iterator
107 */
108 iterator
110 {
111 return std::begin (_cont);
112 }
113
114 /**
115 * \brief Iterator access
116 * \return requested iterator
117 */
118 const_iterator
119 begin () const
120 {
121 return _cont.cbegin ();
122 }
123
124 /**
125 * \brief Iterator access
126 * \return requested iterator
127 */
128 iterator
130 {
131 return std::end (_cont);
132 }
133
134 /**
135 * \brief Iterator access
136 * \return requested iterator
137 */
138 const_iterator
139 end () const
140 {
141 return _cont.cend ();
142 }
143
144 /**
145 * \brief Iterator access
146 * \return requested iterator
147 */
148 const_iterator
149 cbegin () const
150 {
151 return _cont.cbegin ();
152 }
153
154 /**
155 * \brief Iterator access
156 * \return requested iterator
157 */
158 const_iterator
159 cend () const
160 {
161 return _cont.cend ();
162 };
163
164 ///
165
166 /**
167 * \brief Container size
168 * \return number of elements
169 */
170 size_type
171 size () const
172 {
173 return _cont.size ();
174 }
175
176 /**\brief checked random access
177 * \param i index
178 * \throw sl3::ErrOutOfRange if index is invalid
179 * \return reference to element at requested index
180 */
181 reference
182 at (size_t i)
183 {
184 try
185 {
186 return _cont.at (i);
187 }
188 catch (...)
189 {
190 throw ErrOutOfRange ("no data at: " + std::to_string (i));
191 }
192 }
193
194 /** \brief checked random access
195 *
196 * \param i index
197 * \throw sl3::ErrOutOfRange if index is invalid
198 * \return const reference to element at requested index
199 */
200 const_reference
201 at (size_t i) const
202 {
203 try
204 {
205 return _cont.at (i);
206 }
207 catch (...)
208 {
209 throw ErrOutOfRange ("no data at: " + std::to_string (i));
210 }
211 }
212
213 /**\brief unchecked random access
214 * \param i index
215 * behavior is undefined if the given index is invalid
216 * \return
217 * reference to element at requested index
218 */
219 reference
220 operator[] (size_t i)
221 {
222 return _cont[i];
223 }
224 /**\brief unchecked random access
225 * \param i index
226 * behavior is undefined if the given index is invalid
227 * \return
228 * reference to element at requested index
229 */
230 const_reference
231 operator[] (size_t i) const
232 {
233 return _cont[i];
234 }
235
236 protected:
237 /// Container T
238 ContainerType _cont;
239 };
240}
241
242#endif /* SL3_CONTAINER_HPP_ */
const_iterator begin() const
Iterator access.
Definition container.hpp:119
Container(Container &&) noexcept(std::is_nothrow_move_constructible< ContainerType >::value)=default
Move constructor.
size_type size() const
Container size.
Definition container.hpp:171
const_reference at(size_t i) const
checked random access
Definition container.hpp:201
const_iterator end() const
Iterator access.
Definition container.hpp:139
ContainerType _cont
Container T.
Definition container.hpp:238
const_iterator cbegin() const
Iterator access.
Definition container.hpp:149
iterator end()
Iterator access.
Definition container.hpp:129
Container(std::initializer_list< value_type > container)
Constructor.
Definition container.hpp:70
Container(const Container &)=default
Copy Constructor.
Container(ContainerType container)
Constructor.
Definition container.hpp:60
Container() noexcept
Constructor.
Definition container.hpp:54
reference operator[](size_t i)
unchecked random access
Definition container.hpp:220
reference at(size_t i)
checked random access
Definition container.hpp:182
Container & operator=(const Container &)=default
Assignment.
const_iterator cend() const
Iterator access.
Definition container.hpp:159
iterator begin()
Iterator access.
Definition container.hpp:109
Namespace of libSL3.
Definition columns.hpp:18
ErrType< ErrCode::OutOfRange > ErrOutOfRange
thrown if an index op is out of range
Definition error.hpp:114
STL namespace.