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
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 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
23
24#include <expected>
25#include <format>
26#include <type_traits>
27#include <memory>
28#include <string>
29
34namespace StormByte {
40 template <typename T, class E>
41 using Expected = std::conditional_t<
43 std::expected<std::reference_wrapper<std::remove_reference_t<T>>, std::shared_ptr<E>>,
44 std::expected<T, std::shared_ptr<E>>
45 >;
46
53 template <typename E>
54 auto Unexpected(std::shared_ptr<E> error_ptr) {
55 return std::unexpected<std::shared_ptr<E>>(std::move(error_ptr));
56 }
57
64 template <typename E>
65 auto Unexpected(E&& error) {
66 return std::unexpected<std::shared_ptr<std::decay_t<E>>>(
67 std::make_shared<std::decay_t<E>>(std::forward<E>(error))
68 );
69 }
70
78 template <typename Base, typename Derived>
79 auto Unexpected(Derived&& error) -> std::unexpected<std::shared_ptr<Base>>
80 requires std::is_base_of_v<Base, std::decay_t<Derived>> &&
81 (!std::same_as<Base, std::decay_t<Derived>>)
82 {
83 using DerivedT = std::decay_t<Derived>;
84 return std::unexpected<std::shared_ptr<Base>>(std::static_pointer_cast<Base>(std::make_shared<DerivedT>(std::forward<Derived>(error))));
85 }
86
95 template <typename E, typename... Args>
96 auto Unexpected(const std::string& fmt, Args&&... args) {
97 std::string formatted_message;
98
99 if constexpr (sizeof...(Args) == 0) {
100 formatted_message = fmt;
101 } else {
102 auto format_args = std::make_format_args(args...);
103 formatted_message = std::vformat(fmt, format_args);
104 }
105
106 return std::unexpected<std::shared_ptr<E>>(
107 std::make_shared<E>(std::move(formatted_message))
108 );
109 }
110}
Lvalue or rvalue reference type.
Definition categories.hxx:49
Root namespace of the StormByte suite.
auto Unexpected(std::shared_ptr< E > error_ptr)
Builds std::unexpected from an existing error pointer.
Definition expected.hxx:54
std::conditional_t< Type::Reference< T >, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, std::shared_ptr< E > >, std::expected< T, std::shared_ptr< E > > > Expected
std::expected alias with reference and shared-error handling.
Definition expected.hxx:45