StormByte C++ Library: Database module 0.0.9999
StormByte-Database is a StormByte library module for handling database connections
Loading...
Searching...
No Matches
row.hxx
1#pragma once
2
3#include <StormByte/database/value.hxx>
4
5#include <iterator>
6#include <utility>
7#include <vector>
8
13namespace StormByte::Database {
18 class STORMBYTE_DATABASE_PUBLIC Row {
19 public:
24 class STORMBYTE_DATABASE_PUBLIC Iterator {
25 public:
26 using iterator_category = std::random_access_iterator_tag;
27 using value_type = Value;
28 using difference_type = std::ptrdiff_t;
29 using pointer = Value*;
30 using reference = Value&;
31
37 return (*m_row)[m_index];
38 }
39
45 return &(*m_row)[m_index];
46 }
47
52 inline Iterator& operator++() {
53 ++m_index;
54 return *this;
55 }
56
61 inline Iterator operator++(int) {
62 Iterator tmp = *this;
63 ++(*this);
64 return tmp;
65 }
66
71 inline Iterator& operator--() noexcept {
72 --m_index;
73 return *this;
74 }
75
80 inline Iterator operator--(int) noexcept {
81 Iterator tmp = *this;
82 --(*this);
83 return tmp;
84 }
85
91 inline Iterator& operator+=(difference_type n) noexcept {
92 m_index = static_cast<std::size_t>(static_cast<difference_type>(m_index) + n);
93 return *this;
94 }
95
101 inline Iterator& operator-=(difference_type n) noexcept {
102 m_index = static_cast<std::size_t>(static_cast<difference_type>(m_index) - n);
103 return *this;
104 }
105
111 inline Iterator operator+(difference_type n) const noexcept {
112 Iterator tmp = *this;
113 tmp += n;
114 return tmp;
115 }
116
122 inline Iterator operator-(difference_type n) const noexcept {
123 Iterator tmp = *this;
124 tmp -= n;
125 return tmp;
126 }
127
133 inline difference_type operator-(const Iterator& other) const noexcept {
134 return static_cast<difference_type>(m_index) - static_cast<difference_type>(other.m_index);
135 }
136
142 inline bool operator==(const Iterator& other) const noexcept {
143 return m_row == other.m_row && m_index == other.m_index;
144 }
145
151 inline bool operator!=(const Iterator& other) const noexcept {
152 return !(*this == other);
153 }
154
155 private:
156 friend class Row;
157
163 Iterator(Row* row, std::size_t index) noexcept:
164 m_row(row), m_index(index) {}
165
166 Row* m_row;
167 std::size_t m_index;
168 };
169
170 class STORMBYTE_DATABASE_PUBLIC ConstIterator {
171 public:
172 using iterator_category = std::random_access_iterator_tag;
173 using value_type = const Value;
174 using difference_type = std::ptrdiff_t;
175 using pointer = const Value*;
176 using reference = const Value&;
177
182 inline reference operator*() const {
183 return (*m_row)[m_index];
184 }
185
190 inline pointer operator->() const {
191 return &(*m_row)[m_index];
192 }
193
199 ++m_index;
200 return *this;
201 }
202
208 ConstIterator tmp = *this;
209 ++(*this);
210 return tmp;
211 }
212
217 inline ConstIterator& operator--() noexcept {
218 --m_index;
219 return *this;
220 }
221
226 inline ConstIterator operator--(int) noexcept {
227 ConstIterator tmp = *this;
228 --(*this);
229 return tmp;
230 }
231
237 inline ConstIterator& operator+=(difference_type n) noexcept {
238 m_index = static_cast<std::size_t>(static_cast<difference_type>(m_index) + n);
239 return *this;
240 }
241
247 inline ConstIterator& operator-=(difference_type n) noexcept {
248 m_index = static_cast<std::size_t>(static_cast<difference_type>(m_index) - n);
249 return *this;
250 }
251
257 inline ConstIterator operator+(difference_type n) const noexcept {
258 ConstIterator tmp = *this;
259 tmp += n;
260 return tmp;
261 }
262
268 inline ConstIterator operator-(difference_type n) const noexcept {
269 ConstIterator tmp = *this;
270 tmp -= n;
271 return tmp;
272 }
273
279 inline difference_type operator-(const ConstIterator& other) const noexcept {
280 return static_cast<difference_type>(m_index) - static_cast<difference_type>(other.m_index);
281 }
282
286 inline reference operator[](difference_type n) const {
287 return (*m_row)[m_index + static_cast<std::size_t>(n)];
288 }
289
295 inline bool operator==(const ConstIterator& other) const noexcept {
296 return m_row == other.m_row && m_index == other.m_index;
297 }
298
304 inline bool operator!=(const ConstIterator& other) const noexcept {
305 return !(*this == other);
306 }
307
308 private:
309 friend class Row;
310
316 ConstIterator(const Row* row, std::size_t index) noexcept:
317 m_row(row), m_index(index) {}
318
319 const Row* m_row;
320 std::size_t m_index;
321 };
322
323 using iterator = Iterator;
324 using const_iterator = ConstIterator;
325 using reverse_iterator = std::reverse_iterator<iterator>;
326 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
327
331 Row() noexcept = default;
332
337 Row(const Row& other) = default;
338
343 Row(Row&& other) noexcept = default;
344
348 ~Row() noexcept = default;
349
355 Row& operator=(const Row& other) = default;
356
362 Row& operator=(Row&& other) noexcept = default;
363
370 const Value& operator[](std::size_t index) const &;
371
378 Value& operator[](std::size_t index) &;
379
386 Value operator[](std::size_t index) &&;
387
394 const Value& operator[](const std::string& columnName) const &;
395
402 Value& operator[](const std::string& columnName) &;
403
410 Value operator[](const std::string& columnName) &&;
411
417 inline void Add(std::string&& columnName, Value&& value) {
418 m_values.emplace_back(std::move(columnName), std::move(value));
419 }
420
425 inline iterator begin() noexcept {
426 return Iterator(this, 0);
427 }
428
433 inline iterator end() noexcept {
434 return Iterator(this, m_values.size());
435 }
436
441 inline const_iterator begin() const noexcept {
442 return ConstIterator(this, 0);
443 }
444
449 inline const_iterator end() const noexcept {
450 return ConstIterator(this, m_values.size());
451 }
452
457 inline const_iterator cbegin() const noexcept {
458 return ConstIterator(this, 0);
459 }
460
465 inline const_iterator cend() const noexcept {
466 return ConstIterator(this, m_values.size());
467 }
468
473 inline reverse_iterator rbegin() noexcept {
474 return reverse_iterator(end());
475 }
476
481 inline reverse_iterator rend() noexcept {
482 return reverse_iterator(begin());
483 }
484
489 inline const_reverse_iterator rbegin() const noexcept {
490 return const_reverse_iterator(end());
491 }
492
497 inline const_reverse_iterator rend() const noexcept {
498 return const_reverse_iterator(begin());
499 }
500
505 inline std::size_t Count() const noexcept {
506 return m_values.size();
507 }
508
509 private:
510 std::vector<std::pair<std::string, Value>> m_values;
511 };
512}
ConstIterator & operator--() noexcept
Pre-decrement operator.
Definition row.hxx:217
ConstIterator & operator++()
Pre-increment operator.
Definition row.hxx:198
bool operator==(const ConstIterator &other) const noexcept
Equality operator.
Definition row.hxx:295
ConstIterator & operator+=(difference_type n) noexcept
Addition assignment operator.
Definition row.hxx:237
pointer operator->() const
Arrow operator.
Definition row.hxx:190
ConstIterator & operator-=(difference_type n) noexcept
Subtraction assignment operator.
Definition row.hxx:247
bool operator!=(const ConstIterator &other) const noexcept
Inequality operator.
Definition row.hxx:304
difference_type operator-(const ConstIterator &other) const noexcept
Difference operator.
Definition row.hxx:279
ConstIterator operator--(int) noexcept
Post-decrement operator.
Definition row.hxx:226
ConstIterator operator+(difference_type n) const noexcept
Addition operator.
Definition row.hxx:257
reference operator*() const
Dereference operator.
Definition row.hxx:182
ConstIterator operator-(difference_type n) const noexcept
Subtraction operator.
Definition row.hxx:268
ConstIterator operator++(int)
Post-increment operator.
Definition row.hxx:207
reference operator[](difference_type n) const
Definition row.hxx:286
Iterator class for Row.
Definition row.hxx:24
Iterator & operator--() noexcept
Pre-decrement operator.
Definition row.hxx:71
Iterator operator--(int) noexcept
Post-decrement operator.
Definition row.hxx:80
Iterator operator-(difference_type n) const noexcept
Subtraction operator.
Definition row.hxx:122
bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition row.hxx:142
reference operator*()
Dereference operator.
Definition row.hxx:36
Iterator & operator++()
Pre-increment operator.
Definition row.hxx:52
Iterator operator+(difference_type n) const noexcept
Addition operator.
Definition row.hxx:111
pointer operator->()
Arrow operator.
Definition row.hxx:44
difference_type operator-(const Iterator &other) const noexcept
Difference operator.
Definition row.hxx:133
Iterator & operator-=(difference_type n) noexcept
Subtraction assignment operator.
Definition row.hxx:101
Iterator & operator+=(difference_type n) noexcept
Addition assignment operator.
Definition row.hxx:91
bool operator!=(const Iterator &other) const noexcept
Inequality operator.
Definition row.hxx:151
Iterator operator++(int)
Post-increment operator.
Definition row.hxx:61
Row class for databases.
Definition row.hxx:18
reverse_iterator rbegin() noexcept
Gets reverse begin iterator.
Definition row.hxx:473
std::size_t Count() const noexcept
Gets number of columns in the Row.
Definition row.hxx:505
const_iterator begin() const noexcept
Gets const begin iterator.
Definition row.hxx:441
const_reverse_iterator rend() const noexcept
Gets const reverse end iterator.
Definition row.hxx:497
reverse_iterator rend() noexcept
Gets reverse end iterator.
Definition row.hxx:481
const_iterator cbegin() const noexcept
Gets const begin iterator.
Definition row.hxx:457
const_iterator end() const noexcept
Gets const end iterator.
Definition row.hxx:449
iterator begin() noexcept
Gets begin iterator.
Definition row.hxx:425
iterator end() noexcept
Gets end iterator.
Definition row.hxx:433
const_iterator cend() const noexcept
Gets const end iterator.
Definition row.hxx:465
Row() noexcept=default
Default Constructor.
const_reverse_iterator rbegin() const noexcept
Gets const reverse begin iterator.
Definition row.hxx:489
Value class for databases.
Definition value.hxx:20