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