StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
expected.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 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
44
45#include <expected>
46#include <format>
47#include <type_traits>
48#include <memory>
49#include <string>
50
55namespace StormByte {
61 template <typename T, class E>
62 using Expected = std::conditional_t<
64 std::expected<std::reference_wrapper<std::remove_reference_t<T>>, Shared<E>>,
65 std::expected<T, Shared<E>>
66 >;
67
74 template <typename E>
75 auto Unexpected(Shared<E> error) {
76 return std::unexpected<Shared<E>>(std::move(error));
77 }
78
83 template <typename E>
84 auto Unexpected(std::shared_ptr<E>) = delete;
85
92 template <typename E>
93 requires (
94 !requires { typename std::remove_cvref_t<E>::element_type; } ||
95 !Type::SameAs<std::remove_cvref_t<E>, Shared<typename std::remove_cvref_t<E>::element_type>>
96 )
97 auto Unexpected(E&& error) {
98 using Error = std::decay_t<E>;
99 return std::unexpected<Shared<Error>>(
100 Heap::MakeShared<Error>(std::forward<E>(error))
101 );
102 }
103
111 template <typename Base, typename Derived>
112 auto Unexpected(Derived&& error) -> std::unexpected<Shared<Base>>
115 {
116 using DerivedT = std::decay_t<Derived>;
117 return std::unexpected<Shared<Base>>(
118 Shared<Base>::template MakePointer<DerivedT>(std::forward<Derived>(error))
119 );
120 }
121
133 template <typename E, typename... Args>
134 auto Unexpected(const std::string& fmt, Args&&... args) {
135 std::string formatted_message;
136
137 if constexpr (sizeof...(Args) == 0) {
138 formatted_message = fmt;
139 } else {
140 auto format_args = std::make_format_args(args...);
141 formatted_message = std::vformat(fmt, format_args);
142 }
143
144 return std::unexpected<Shared<E>>(
145 Heap::MakeShared<E>(std::move(formatted_message))
146 );
147 }
148}
Shared owner of a T allocated on Base's heap.
Definition safe_pointers.hxx:201
Derived derives from Base (std::is_base_of).
Definition relations.hxx:113
Lvalue or rvalue reference type.
Definition categories.hxx:81
Same type after stripping cv and references from both sides.
Definition relations.hxx:94
Root namespace of the StormByte suite.
auto Unexpected(Shared< E > error)
Forwards an error already stored by Expected.
Definition expected.hxx:75
std::conditional_t< Type::Reference< T >, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, Shared< E > >, std::expected< T, Shared< E > > > Expected
std::expected alias with reference and shared-error handling.
Definition expected.hxx:66
DLL-safe shared and unique owners.