StormByte 3.0.0
C++26 foundation of the StormByte suite
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 is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <StormByte/size.hxx>
45
46#include <cstddef>
47#include <iterator>
48#include <utility>
49
54namespace StormByte {
59 namespace Safe {
67 template<Type::SafeComponent Component>
68 requires requires(Component& component, const Component& readonly) {
69 typename Component::value_type;
70 { component.begin() };
71 { component.end() };
72 { readonly.begin() };
73 { readonly.end() };
74 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
75 }
77 using InnerIterator = decltype(std::declval<Component&>().begin());
78 using InnerConstIterator = decltype(std::declval<const Component&>().begin());
79
80 public:
81 using container_type = Component;
82 using value_type = typename container_type::value_type;
83 using size_type = Size;
84 using difference_type = std::ptrdiff_t;
85 static constexpr bool random_access = std::random_access_iterator<InnerIterator>;
86
92 template<bool IsConst>
93 class BasicIterator final {
94 using Inner = std::conditional_t<IsConst, InnerConstIterator, InnerIterator>;
95
96 public:
97 using iterator_category = std::conditional_t<random_access, std::random_access_iterator_tag, std::bidirectional_iterator_tag>;
100 using difference_type = std::ptrdiff_t;
101 using reference = decltype(*std::declval<Inner>());
102 using pointer = void;
103
107 BasicIterator() = default;
108
113 template<bool OtherConst>
114 requires IsConst && (!OtherConst)
116
122
128
134
140
146
153
160
167
174
181 friend BasicIterator operator+(difference_type offset, const BasicIterator& cursor) requires random_access {
182 return cursor + offset;
183 }
184
191
197 template<bool OtherConst>
199
205 template<bool OtherConst>
206 bool operator==(const BasicIterator<OtherConst>& other) const;
207
213 template<bool OtherConst>
214 bool operator<(const BasicIterator<OtherConst>& other) const requires random_access;
215
221 template<bool OtherConst>
222 bool operator>(const BasicIterator<OtherConst>& other) const requires random_access;
223
229 template<bool OtherConst>
230 bool operator<=(const BasicIterator<OtherConst>& other) const requires random_access;
231
237 template<bool OtherConst>
238 bool operator>=(const BasicIterator<OtherConst>& other) const requires random_access;
239
240 private:
241 friend class Iterable;
242 template<bool> friend class BasicIterator;
243
248 explicit BasicIterator(Inner current) noexcept;
249
250 Inner m_current{};
251 };
252
255
259 Iterable() = default;
260
265 Iterable(const Iterable& other) = default;
266
271 Iterable(Iterable&& other) noexcept = default;
272
277 explicit Iterable(container_type container) noexcept;
278
282 ~Iterable() = default;
283
289 Iterable& operator=(const Iterable& other) = default;
290
296 Iterable& operator=(Iterable&& other) noexcept = default;
297
302 iterator begin() noexcept;
303
308 iterator end() noexcept;
309
314 const_iterator begin() const noexcept;
315
320 const_iterator end() const noexcept;
321
326 const_iterator cbegin() const noexcept;
327
332 const_iterator cend() const noexcept;
333
338 bool empty() const noexcept;
339
344 size_type size() const noexcept;
345
346 protected:
352
357 const container_type& Container() const noexcept;
358
360 };
361 }
362
367 namespace Type {
372 template<SafeComponent Component>
373 requires IsSafe<std::remove_const_t<Component>>::value
374 struct IsSafe<Safe::Iterable<Component>>: std::true_type {};
375
380 template<SafeComponent Component>
381 requires MaybeSafe<Component>
382 struct IsMaybeSafe<Safe::Iterable<Component>>: std::true_type {};
383
388 template<SafeComponent Component>
389 struct IsSafeValue<Safe::Iterable<Component>>: std::true_type {};
390 }
391}
392
Suite cursor.
Definition iterable.hxx:93
bool operator<=(const BasicIterator< OtherConst > &other) const
Order two cursors.
BasicIterator()=default
Construct a singular cursor.
difference_type operator-(const BasicIterator< OtherConst > &other) const
Return the distance to another cursor.
BasicIterator & operator+=(difference_type offset)
Advance by an offset.
BasicIterator & operator--()
Move to the previous element.
bool operator>=(const BasicIterator< OtherConst > &other) const
Order two cursors.
reference operator*() const
Return the current element.
typename Iterable::value_type value_type
Element type.
Definition iterable.hxx:99
friend BasicIterator operator+(difference_type offset, const BasicIterator &cursor)
Advance a cursor from the offset.
Definition iterable.hxx:181
bool operator==(const BasicIterator< OtherConst > &other) const
Compare cursor position.
friend class BasicIterator
Definition iterable.hxx:242
std::conditional_t< random_access, std::random_access_iterator_tag, std::bidirectional_iterator_tag > iterator_category
Iterator category.
Definition iterable.hxx:97
bool operator>(const BasicIterator< OtherConst > &other) const
Order two cursors.
void pointer
The cursor does not expose a raw component pointer.
Definition iterable.hxx:102
bool operator<(const BasicIterator< OtherConst > &other) const
Order two cursors.
&&!OtherConst BasicIterator(const BasicIterator< OtherConst > &other) noexcept
Convert a mutable cursor to a read-only cursor.
BasicIterator operator+(difference_type offset) const
Return the cursor advanced by an offset.
BasicIterator operator++(int)
Advance one element.
reference operator[](difference_type offset) const
Return the element at an offset.
std::ptrdiff_t difference_type
Cursor distance type.
Definition iterable.hxx:100
iterator_category iterator_concept
C++20 iterator concept.
Definition iterable.hxx:98
BasicIterator & operator++()
Advance one element.
BasicIterator & operator-=(difference_type offset)
Retreat by an offset.
friend class Iterable
Definition iterable.hxx:241
BasicIterator operator-(difference_type offset) const
Return the cursor retreated by an offset.
decltype(*std::declval< Inner >()) reference
Element reference.
Definition iterable.hxx:101
BasicIterator operator--(int)
Move to the previous element.
~Iterable()=default
Destroy the component through its Safe operations.
static constexpr bool random_access
Whether the suite selects the index cursor.
Definition iterable.hxx:85
const_iterator cbegin() const noexcept
Return a read-only cursor to the first element.
BasicIterator< false > iterator
Mutable cursor.
Definition iterable.hxx:253
Iterable(Iterable &&other) noexcept=default
Move the component.
BasicIterator< true > const_iterator
Read-only cursor.
Definition iterable.hxx:254
size_type size() const noexcept
Return the element count.
Iterable(const Iterable &other)=default
Copy the component.
iterator end() noexcept
Return the mutable end cursor.
const_iterator cend() const noexcept
Return the read-only end cursor.
container_type m_container
Stored Safe component. This is the only storage.
Definition iterable.hxx:359
typename container_type::value_type value_type
Element type.
Definition iterable.hxx:82
container_type & Container() noexcept
Return the stored component.
Iterable(container_type container) noexcept
Take ownership of an existing component.
Iterable & operator=(const Iterable &other)=default
Copy-assign the component.
bool empty() const noexcept
Test whether the component has no elements.
Component container_type
Wrapped Safe component.
Definition iterable.hxx:81
iterator begin() noexcept
Return a mutable cursor to the first element.
Iterable()=default
Construct an empty component.
Iterable & operator=(Iterable &&other) noexcept=default
Move-assign the component.
Size size_type
Element count. Size, not std::size_t.
Definition iterable.hxx:83
std::ptrdiff_t difference_type
Cursor distance type.
Definition iterable.hxx:84
Count of abstract units (uint64_t storage).
Definition size.hxx:107
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
Owned values that cross a DLL without the caller's CRT.
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
Host-width-stable count (uint64_t), implicit only to std::size_t.
Recognizes types whose DLL-boundary safety depends on documented provider requirements.
Definition safe.hxx:262
Recognizes collection values with Base-controlled lifetime.
Definition safe.hxx:347
Recognizes Base Safe components, not universal ABI compatibility.
Definition safe.hxx:138
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57