StormByte C++ Library 0.0.9999
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
1#pragma once
2
3#include <StormByte/type_traits.hxx>
4
5#include <expected>
6#include <format>
7#include <memory>
8#include <string>
9
17namespace StormByte {
27 template <typename T, class E>
28 using Expected = std::conditional_t<
30 std::expected<std::reference_wrapper<std::remove_reference_t<T>>, std::shared_ptr<E>>,
31 std::expected<T, std::shared_ptr<E>>
32 >;
33
42 template <typename E>
43 auto Unexpected(std::shared_ptr<E> error_ptr) {
44 return std::unexpected<std::shared_ptr<E>>(std::move(error_ptr));
45 }
46
55 template <typename E>
56 auto Unexpected(E&& error) {
57 return std::unexpected<std::shared_ptr<std::decay_t<E>>>(
58 std::make_shared<std::decay_t<E>>(std::forward<E>(error))
59 );
60 }
61
73 template <typename E, typename... Args>
74 auto Unexpected(const std::string& fmt, Args&&... args) {
75 std::string formatted_message;
76
77 if constexpr (sizeof...(Args) == 0) {
78 // No arguments provided, use the format string directly
80 } else {
81 // Create lvalue references for all arguments
82 auto format_args = std::make_format_args(args...);
83
84 // Use std::vformat for runtime formatting
85 formatted_message = std::vformat(fmt, format_args);
86 }
87
88 // Construct the error instance with the formatted message
89 return std::unexpected<std::shared_ptr<E>>(
90 std::make_shared<E>(std::move(formatted_message))
91 );
92 }
93}
Main namespace for the StormByte library.
auto Unexpected(std::shared_ptr< E > error_ptr)
Creates an std::unexpected with a shared pointer to the error.
Definition expected.hxx:43
std::conditional_t< is_reference< T >::value, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, std::shared_ptr< E > >, std::expected< T, std::shared_ptr< E > > > Expected
Expected type with support for reference types.
Definition expected.hxx:32
Type traits for checking if a type is a reference.
Definition type_traits.hxx:71