StormByte C++ Library 1.2.0
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
iterable.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
5 *
6 * StormByte is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
24
25#include <iterator>
26#include <utility>
27
32namespace StormByte {
46 template <typename Container>
47 class Iterable {
48 protected:
49 Container m_data;
50
51 public:
52 using value_type = typename Container::value_type;
53 using size_type = typename Container::size_type;
54 using difference_type = typename Container::difference_type;
55 using reference = typename Container::reference;
56 using const_reference = typename Container::const_reference;
57 using pointer = typename Container::pointer;
58 using const_pointer = typename Container::const_pointer;
59
64 class Iterator {
65 friend class Iterable;
66 public:
67 using iterator_category = typename std::iterator_traits<typename Container::iterator>::iterator_category;
68 using value_type = typename std::iterator_traits<typename Container::iterator>::value_type;
69 using difference_type = typename std::iterator_traits<typename Container::iterator>::difference_type;
70 using pointer = typename std::iterator_traits<typename Container::iterator>::pointer;
71 using reference = typename std::iterator_traits<typename Container::iterator>::reference;
72
78
84
90
96
102
108
115
122
128
134
139 difference_type operator-(const Iterator& other) const;
140
145 bool operator==(const Iterator& other) const;
146
151 bool operator!=(const Iterator& other) const;
152
153 private:
154 typename Container::iterator m_it;
155
160 Iterator(typename Container::iterator it);
161 };
162
168 friend class Iterable;
169 public:
170 using iterator_category = typename std::iterator_traits<typename Container::const_iterator>::iterator_category;
171 using value_type = typename std::iterator_traits<typename Container::const_iterator>::value_type;
172 using difference_type = typename std::iterator_traits<typename Container::const_iterator>::difference_type;
173 using pointer = typename std::iterator_traits<typename Container::const_iterator>::pointer;
174 using reference = typename std::iterator_traits<typename Container::const_iterator>::reference;
175
181
187
193
199
205
211
218
225
231
237
243
248 bool operator==(const ConstIterator& other) const;
249
254 bool operator!=(const ConstIterator& other) const;
255
256 private:
257 typename Container::const_iterator m_it;
258
263 ConstIterator(typename Container::const_iterator it);
264 };
265
268 using reverse_iterator = std::reverse_iterator<iterator>;
269 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
270
274 Iterable() = default;
275
290 template<typename C>
291 explicit Iterable(C&& data)
292 requires Type::SameAs<C, Container> && (
295 );
296
306 Iterable(const Iterable& other);
307
311 Iterable(Iterable&&) requires Type::MoveConstructible<Container> = default;
312
316 virtual ~Iterable() = default;
317
324 Iterable& operator=(const Iterable& other);
325
330 Iterable& operator=(Iterable&&) requires Type::MoveAssignable<Container> = default;
331
336 bool operator==(const Iterable& other) const;
337
342 bool operator!=(const Iterable& other) const;
343
347 iterator begin() noexcept;
348
352 const_iterator begin() const noexcept;
353
357 iterator end() noexcept;
358
362 const_iterator end() const noexcept;
363
367 const_iterator cbegin() const noexcept;
368
372 const_iterator cend() const noexcept;
373
378
383
388
392 const_reverse_iterator rend() const noexcept;
393
398
403
407 size_type size() const noexcept;
408
412 bool empty() const noexcept;
413
422 reference operator[](size_type i);
423
430 template<typename K>
431 auto operator[](K const& key) -> decltype(auto)
432 requires (Type::HasMappedType<Container>);
433
441 template<typename K>
442 auto operator[](K const& key) const -> decltype(auto)
443 requires (Type::HasMappedType<const Container>);
444
451 const_reference operator[](size_type i) const;
452
467 template<typename T>
468 void add(T&& value)
469 requires (
470 (Type::LvalueReference<T> && Type::CopyConstructible<value_type>) ||
471 (!Type::LvalueReference<T> && Type::MoveConstructible<value_type>)
472 );
473
481 void add(value_type value)
482 requires Type::MoveConstructible<value_type>;
483
489 bool has_item(const value_type& value) const;
490
497 template<typename M>
498 bool has_item(M const& value) const
499 requires Type::HasMappedType<const Container> && std::convertible_to<M, typename Container::mapped_type>;
500
507 template<typename K>
508 bool has_key(const K& key) const
509 requires Type::HasMappedType<const Container>;
510 };
511}
512
513#include <StormByte/iterable.txx>
514
Const iterator adapter over Container::const_iterator.
Definition iterable.hxx:167
ConstIterator operator++(int)
Post-increment.
ConstIterator & operator-=(difference_type n)
Retreat in place.
ConstIterator & operator--()
Pre-decrement.
typename std::iterator_traits< typename Container::const_iterator >::iterator_category iterator_category
Matches the underlying container iterator; not assumed random-access.
Definition iterable.hxx:170
ConstIterator operator-(difference_type n) const
Iterator moved back by n.
typename std::iterator_traits< typename Container::const_iterator >::value_type value_type
Definition iterable.hxx:171
reference operator*() const
Dereference.
pointer operator->() const
Member access.
typename std::iterator_traits< typename Container::const_iterator >::pointer pointer
Definition iterable.hxx:173
ConstIterator operator+(difference_type n) const
Iterator advanced by n.
bool operator!=(const ConstIterator &other) const
Inequality.
ConstIterator & operator++()
Pre-increment.
ConstIterator operator--(int)
Post-decrement.
bool operator==(const ConstIterator &other) const
Equality.
typename std::iterator_traits< typename Container::const_iterator >::difference_type difference_type
Definition iterable.hxx:172
typename std::iterator_traits< typename Container::const_iterator >::reference reference
Definition iterable.hxx:174
difference_type operator-(const ConstIterator &other) const
Distance to other.
ConstIterator & operator+=(difference_type n)
Advance in place.
Mutable iterator adapter over Container::iterator.
Definition iterable.hxx:64
Iterator & operator++()
Pre-increment.
bool operator!=(const Iterator &other) const
Inequality.
typename std::iterator_traits< typename Container::iterator >::difference_type difference_type
Definition iterable.hxx:69
reference operator*()
Dereference.
typename std::iterator_traits< typename Container::iterator >::value_type value_type
Definition iterable.hxx:68
Iterator & operator+=(difference_type n)
Advance in place.
typename std::iterator_traits< typename Container::iterator >::pointer pointer
Definition iterable.hxx:70
Iterator operator++(int)
Post-increment.
Iterator operator--(int)
Post-decrement.
Iterator & operator-=(difference_type n)
Retreat in place.
Iterator operator+(difference_type n) const
Iterator advanced by n.
bool operator==(const Iterator &other) const
Equality.
Iterator & operator--()
Pre-decrement.
Iterator operator-(difference_type n) const
Iterator moved back by n.
typename std::iterator_traits< typename Container::iterator >::reference reference
Definition iterable.hxx:71
typename std::iterator_traits< typename Container::iterator >::iterator_category iterator_category
Matches the underlying container iterator; not assumed random-access.
Definition iterable.hxx:67
pointer operator->()
Member access.
difference_type operator-(const Iterator &other) const
Distance to other.
Wrapper that adds a uniform iteration and mutation API around a standard container.
Definition iterable.hxx:47
Iterable(C &&data)
Builds from a container.
const_reverse_iterator crbegin() const noexcept
Const reverse begin.
const_iterator cend() const noexcept
Const end.
typename Container::size_type size_type
Container::size_type.
Definition iterable.hxx:53
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse adapter.
Definition iterable.hxx:268
reverse_iterator rend() noexcept
Mutable reverse end.
Iterable(Iterable &&)=default
Move constructor.
bool has_key(const K &key) const
Key lookup for associative containers.
bool has_item(const value_type &value) const
Linear search for an equal element.
iterator end() noexcept
Mutable end.
const_iterator cbegin() const noexcept
Const begin.
reverse_iterator rbegin() noexcept
Mutable reverse begin.
Iterable()=default
Empty iterable.
typename Container::value_type value_type
Container::value_type.
Definition iterable.hxx:52
bool empty() const noexcept
Whether the container is empty.
Iterable(const Iterable &other)
Copy constructor.
void add(T &&value)
Inserts value via push_back, else push_front, else associative insert.
typename Container::const_pointer const_pointer
Container::const_pointer.
Definition iterable.hxx:58
typename Container::pointer pointer
Container::pointer.
Definition iterable.hxx:57
size_type size() const noexcept
Element count.
Container m_data
Underlying container.
Definition iterable.hxx:49
typename Container::reference reference
Container::reference.
Definition iterable.hxx:55
typename Container::difference_type difference_type
Container::difference_type.
Definition iterable.hxx:54
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse adapter.
Definition iterable.hxx:269
iterator begin() noexcept
Mutable begin.
const_reverse_iterator crend() const noexcept
Const reverse end.
typename Container::const_reference const_reference
Container::const_reference.
Definition iterable.hxx:56
Type that can actually be copy-constructed.
Definition object_semantics.hxx:136
Lvalue reference type (std::is_lvalue_reference).
Definition categories.hxx:65
Type that can be move-constructed.
Definition object_semantics.hxx:189
Same type after stripping cv and references from both sides.
Definition relations.hxx:65
Root namespace of the StormByte suite.
STL namespace.