libsl3 1.3.51003
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 <sl3/error.hpp>
13
14namespace 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 Container () noexcept {}
48 /**
49 * \brief Constructor
50 *
51 * \param container values
52 */
53 Container (ContainerType container) noexcept (
54 std::is_nothrow_move_constructible<ContainerType>::value)
55 : _cont (std::move (container))
56 {
57 }
58
59 /**
60 * \brief Constructor
61 *
62 * \param container values
63 */
64 Container (std::initializer_list<value_type> container)
65 : _cont (std::move (container))
66 {
67 }
68
69 /**
70 * \brief Copy Constructor
71 */
72 Container (const Container&) = default;
73
74 /**
75 * \brief Assignment
76 * \return reference to this
77 */
78 Container& operator= (const Container&) = default;
79
80 /**
81 * \brief Move constructor
82 */
83 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 Container& operator= (Container&&) = default;
92
93 /**
94 * \brief Destructor
95 */
96 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
106 {
107 return std::begin (_cont);
108 }
109
110 /**
111 * \brief Iterator access
112 * \return requested iterator
113 */
114 const_iterator
115 begin () const
116 {
117 return _cont.cbegin ();
118 }
119
120 /**
121 * \brief Iterator access
122 * \return requested iterator
123 */
124 iterator
126 {
127 return std::end (_cont);
128 }
129
130 /**
131 * \brief Iterator access
132 * \return requested iterator
133 */
134 const_iterator
135 end () const
136 {
137 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 size () const
168 {
169 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 at (size_t i)
179 {
180 try
181 {
182 return _cont.at (i);
183 }
184 catch (...)
185 {
186 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 at (size_t i) const
198 {
199 try
200 {
201 return _cont.at (i);
202 }
203 catch (...)
204 {
205 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 operator[] (size_t i)
217 {
218 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 operator[] (size_t i) const
228 {
229 return _cont[i];
230 }
231
232 protected:
233 /// Container T
234 ContainerType _cont;
235 };
236}
237
238#endif /* SL3_CONTAINER_HPP_ */
const_iterator begin() const
Iterator access.
Definition container.hpp:115
Container(Container &&) noexcept(std::is_nothrow_move_constructible< ContainerType >::value)=default
Move constructor.
Container(ContainerType container) noexcept(std::is_nothrow_move_constructible< ContainerType >::value)
Constructor.
Definition container.hpp:53
size_type size() const
Container size.
Definition container.hpp:167
const_reference at(size_t i) const
checked random access
Definition container.hpp:197
const_iterator end() const
Iterator access.
Definition container.hpp:135
ContainerType _cont
Container T.
Definition container.hpp:234
const_iterator cbegin() const
Iterator access.
Definition container.hpp:145
iterator end()
Iterator access.
Definition container.hpp:125
Container(std::initializer_list< value_type > container)
Constructor.
Definition container.hpp:64
Container(const Container &)=default
Copy Constructor.
Container() noexcept
Constructor.
Definition container.hpp:47
reference operator[](size_t i)
unchecked random access
Definition container.hpp:216
reference at(size_t i)
checked random access
Definition container.hpp:178
Container & operator=(const Container &)=default
Assignment.
const_iterator cend() const
Iterator access.
Definition container.hpp:155
iterator begin()
Iterator access.
Definition container.hpp:105
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.