StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
pair.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
45
46#include <compare>
47#include <cstddef>
48#include <functional>
49#include <span>
50#include <tuple>
51#include <type_traits>
52#include <utility>
53
58namespace StormByte {
63 namespace Safe {
72 template<Type::SafeValue First, Type::SafeValue Second>
74 public:
75 using first_type = First;
76 using second_type = Second;
77
78 First first;
79 Second second;
80
84 Pair(): first(), second() {}
85
91 Pair(const First& firstValue, const Second& secondValue);
92
98 Pair(First&& firstValue, Second&& secondValue) noexcept(
99 std::is_nothrow_move_constructible_v<First> &&
100 std::is_nothrow_move_constructible_v<Second>);
101
109 template<class U, class V>
112 Pair(U&& firstValue, V&& secondValue);
113
122 template<class... FirstArgs, class... SecondArgs>
123 Pair(std::piecewise_construct_t tag, std::tuple<FirstArgs...> firstArgs, std::tuple<SecondArgs...> secondArgs);
124
131 template<class OtherFirst, class OtherSecond>
135 Pair(const std::pair<OtherFirst, OtherSecond>& other);
136
143 template<class OtherFirst, class OtherSecond>
147 Pair(std::pair<OtherFirst, OtherSecond>&& other);
148
155 template<class OtherFirst, class OtherSecond>
160
167 template<class OtherFirst, class OtherSecond>
172
177 Pair(const Pair& other) = default;
178
183 Pair(Pair&& other) noexcept(std::is_nothrow_move_constructible_v<First> &&
184 std::is_nothrow_move_constructible_v<Second>) = default;
185
189 ~Pair() = default;
190
196 Pair& operator=(const Pair& other) = default;
197
203 Pair& operator=(Pair&& other) noexcept(std::is_nothrow_move_assignable_v<First> &&
204 std::is_nothrow_move_assignable_v<Second>) = default;
205
213 template<class OtherFirst, class OtherSecond>
217
225 template<class OtherFirst, class OtherSecond>
229
237 template<class OtherFirst, class OtherSecond>
240 Pair& operator=(const std::pair<OtherFirst, OtherSecond>& other);
241
249 template<class OtherFirst, class OtherSecond>
252 Pair& operator=(std::pair<OtherFirst, OtherSecond>&& other);
253
259
265 friend void swap(Pair& left, Pair& right) noexcept(noexcept(left.swap(right))) {
266 left.swap(right);
267 }
268
275
282
287 STORMBYTE_FORCE_INLINE explicit operator std::pair<First, Second>() const {
288 return {first, second};
289 }
290
291 private:
301 template<class... FirstArgs, class... SecondArgs, std::size_t... FirstIndexes, std::size_t... SecondIndexes>
302 Pair(std::piecewise_construct_t tag, std::tuple<FirstArgs...>& firstArgs, std::tuple<SecondArgs...>& secondArgs,
303 std::index_sequence<FirstIndexes...>, std::index_sequence<SecondIndexes...>);
304 };
305
313 template<Type::SafeValue First, Type::SafeValue Second, class SecondReference>
314 class PairReference final {
315 public:
317
318 const First first;
319 SecondReference second;
320
327 PairReference(const First& firstValue, const Second& secondValue, SecondReference secondReference);
328
333 operator const value_type&() const;
334
339 PairReference(const PairReference& other) = default;
340
345 PairReference(PairReference&& other) = default;
346
353 template<std::size_t Index>
354 requires (Index == 0)
355 friend const First& get(const PairReference& reference) noexcept {
356 return reference.first;
357 }
358
365 template<std::size_t Index>
366 requires (Index == 1)
367 friend SecondReference& get(PairReference& reference) noexcept {
368 return reference.second;
369 }
370
377 template<std::size_t Index>
378 requires (Index == 0)
379 friend const First&& get(PairReference&& reference) noexcept {
380 return std::move(reference.first);
381 }
382
389 template<std::size_t Index>
390 requires (Index == 1)
391 friend SecondReference get(PairReference&& reference) noexcept {
392 return std::move(reference.second);
393 }
394
401 template<std::size_t Index>
402 requires (Index == 0)
403 friend const First&& get(const PairReference&& reference) noexcept {
404 return std::move(reference.first);
405 }
406
413 template<std::size_t Index>
414 requires (Index == 1)
415 friend SecondReference get(const PairReference&& reference) {
416 return reference.second;
417 }
418
419 private:
420 mutable value_type m_snapshot;
421 };
422
429 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
430 requires (Index == 0)
431 constexpr First& get(Pair<First, Second>& pair) noexcept;
432
439 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
440 requires (Index == 1)
441 constexpr Second& get(Pair<First, Second>& pair) noexcept;
442
449 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
450 requires (Index == 0)
451 constexpr const First& get(const Pair<First, Second>& pair) noexcept;
452
459 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
460 requires (Index == 1)
461 constexpr const Second& get(const Pair<First, Second>& pair) noexcept;
462
469 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
470 requires (Index == 0)
471 constexpr First&& get(Pair<First, Second>&& pair) noexcept;
472
479 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
480 requires (Index == 1)
481 constexpr Second&& get(Pair<First, Second>&& pair) noexcept;
482
489 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
490 requires (Index == 0)
491 constexpr const First&& get(const Pair<First, Second>&& pair) noexcept;
492
499 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
500 requires (Index == 1)
501 constexpr const Second&& get(const Pair<First, Second>&& pair) noexcept;
502 }
503
508 namespace Type {
514 template<SafeValue First, SafeValue Second>
515 requires IsSafe<First>::value && IsSafe<Second>::value
516 struct IsSafe<Safe::Pair<First, Second>>: std::true_type {};
517
523 template<SafeValue First, SafeValue Second>
526
532 template<SafeValue First, SafeValue Second>
533 struct IsSafeValue<Safe::Pair<First, Second>>: std::true_type {};
534 }
535}
536
542template<StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second>
543struct std::tuple_size<StormByte::Safe::Pair<First, Second>>: std::integral_constant<std::size_t, 2> {};
544
551template<StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second, class SecondReference>
552struct std::tuple_size<StormByte::Safe::PairReference<First, Second, SecondReference>>:
553 std::integral_constant<std::size_t, 2> {};
554
561template<std::size_t Index, StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second>
562requires (Index < 2)
563struct std::tuple_element<Index, StormByte::Safe::Pair<First, Second>> {
564 using type = std::conditional_t<Index == 0, First, Second>;
565};
566
574template<std::size_t Index, StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second, class SecondReference>
575requires (Index < 2)
577 using type = std::conditional_t<Index == 0, const First, SecondReference>;
578};
579
585template<StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second>
593 return HashCombine(Hash<First>{}(value.first), Hash<Second>{}(value.second));
594 }
595};
596
602template<StormByte::Type::SafeValue First, StormByte::Type::SafeValue Second>
603struct std::hash<StormByte::Safe::Pair<First, Second>> {
609 std::size_t operator()(const StormByte::Safe::Pair<First, Second>& value) const noexcept {
611 }
612};
613
Tuple-like proxy for an immutable first value and callback-backed second value.
Definition pair.hxx:314
friend const First && get(const PairReference &&reference) noexcept
Read the first component of a const temporary proxy.
Definition pair.hxx:403
SecondReference second
Callback-backed second value proxy.
Definition pair.hxx:319
Pair< First, Second > value_type
Copied entry value type.
Definition pair.hxx:316
friend SecondReference get(const PairReference &&reference)
Read the second component of a const temporary proxy.
Definition pair.hxx:415
PairReference(PairReference &&other)=default
Move a pair-reference proxy.
friend const First && get(PairReference &&reference) noexcept
Move-access the first component of a temporary proxy.
Definition pair.hxx:379
const First first
Immutable first value copy.
Definition pair.hxx:318
friend SecondReference get(PairReference &&reference) noexcept
Move-access the second component of a temporary proxy.
Definition pair.hxx:391
PairReference(const PairReference &other)=default
Copy a pair-reference proxy.
friend const First & get(const PairReference &reference) noexcept
Access the first component for structured bindings.
Definition pair.hxx:355
PairReference(const First &firstValue, const Second &secondValue, SecondReference secondReference)
Bind the proxy to copied values and a mutable second-value proxy.
friend SecondReference & get(PairReference &reference) noexcept
Access the second component for structured bindings.
Definition pair.hxx:367
std::pair whose members keep their own Safe storage.
Definition pair.hxx:73
Pair(U &&firstValue, V &&secondValue)
Construct both members from convertible values.
First first_type
Definition pair.hxx:75
Pair(std::piecewise_construct_t tag, std::tuple< FirstArgs... > firstArgs, std::tuple< SecondArgs... > secondArgs)
Construct both members from argument tuples.
Second second
Definition pair.hxx:79
STORMBYTE_FORCE_INLINE auto operator<=>(const Pair &other) const
Order by the first member, then the second.
First first
Definition pair.hxx:78
Pair(Pair< OtherFirst, OtherSecond > &&other)
Move a compatible Safe pair.
Pair()
Value-initialize both members.
Definition pair.hxx:84
Pair(const Pair< OtherFirst, OtherSecond > &other)
Copy a compatible Safe pair.
Pair(First &&firstValue, Second &&secondValue) noexcept(std::is_nothrow_move_constructible_v< First > &&std::is_nothrow_move_constructible_v< Second >)
Move two values into the pair.
friend void swap(Pair &left, Pair &right) noexcept(noexcept(left.swap(right)))
Exchange two pairs.
Definition pair.hxx:265
Pair(const Pair &other)=default
Copy a pair.
Pair(std::pair< OtherFirst, OtherSecond > &&other)
Move a caller-owned STL pair.
Pair(const First &firstValue, const Second &secondValue)
Copy two values into the pair.
Pair & operator=(std::pair< OtherFirst, OtherSecond > &&other)
Move-assign a caller-owned STL pair.
Pair & operator=(const std::pair< OtherFirst, OtherSecond > &other)
Copy-assign a caller-owned STL pair.
Second second_type
Definition pair.hxx:76
Pair & operator=(Pair< OtherFirst, OtherSecond > &&other)
Move-assign a compatible Safe pair.
Pair(const std::pair< OtherFirst, OtherSecond > &other)
Copy a caller-owned STL pair.
void swap(Pair &other) noexcept(Type::Swappable< First > &&Type::Swappable< Second >)
Exchange both members.
Pair & operator=(const Pair &other)=default
Copy-assign a pair.
Pair(Pair &&other) noexcept(std::is_nothrow_move_constructible_v< First > &&std::is_nothrow_move_constructible_v< Second >)=default
Move a pair.
bool operator==(const Pair &other) const
Compare both members.
Pair & operator=(Pair &&other) noexcept(std::is_nothrow_move_assignable_v< First > &&std::is_nothrow_move_assignable_v< Second >)=default
Move-assign a pair.
~Pair()=default
Destroy both members through their own Safe operations.
Pair & operator=(const Pair< OtherFirst, OtherSecond > &other)
Copy-assign a compatible Safe pair.
T can be assigned from U (std::assignable_from).
Definition object_semantics.hxx:372
Type constructible from Args (std::constructible_from).
Definition object_semantics.hxx:356
From is implicitly convertible to To (std::is_convertible).
Definition conversions.hxx:74
Type whose == and != both yield something convertible to bool.
Definition comparison.hxx:81
Recognizes a conditional Safe component after stripping cv and references.
Definition safe.hxx:301
Type with accessible first and second members.
Definition wrappers.hxx:215
Type that can be swapped with std::swap (std::is_swappable).
Definition object_semantics.hxx:328
Type whose <=> operator is well-formed.
Definition comparison.hxx:97
Owned values that cross a DLL without the caller's CRT.
STORMBYTE_FORCE_INLINE std::size_t HashCombine(std::size_t seed, std::size_t next) noexcept
Mix an existing hash with another.
Definition hash.hxx:83
constexpr First & get(Pair< First, Second > &pair) noexcept
Access the first pair member.
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STL namespace.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
STORMBYTE_FORCE_INLINE std::size_t operator()(const StormByte::Safe::Pair< First, Second > &value) const noexcept
Hash value.
Definition pair.hxx:592
Cross-module hash.
Definition hash.hxx:95
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
std::size_t operator()(const StormByte::Safe::Pair< First, Second > &value) const noexcept
Hash value.
Definition pair.hxx:609
std::conditional_t< Index==0, const First, SecondReference > type
Definition pair.hxx:577
std::conditional_t< Index==0, First, Second > type
Definition pair.hxx:564
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57