mp-coro main
Coroutine support tools
storage.h
Go to the documentation of this file.
1// The MIT License (MIT)
2//
3// Copyright (c) 2021 Mateusz Pusz
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#include <exception>
26#include <type_traits>
27#include <variant>
28
29namespace mp_coro::detail {
30
31/// If rethrow contains an exception, rethrow it.
32template <typename... Args>
33void check_and_rethrow(const std::variant<Args...> &result) {
34 if (std::holds_alternative<std::exception_ptr>(result))
35 std::rethrow_exception(std::get<std::exception_ptr>(std::move(result)));
36}
37
38/// Storage class that can either contain a value, an exception, or be empty.
39template <typename T>
41 protected:
42 std::variant<std::monostate, std::exception_ptr, T> result;
43
44 public:
45 template <std::convertible_to<T> U>
46 void set_value(U &&value) noexcept(
47 std::is_nothrow_constructible_v<T, decltype(std::forward<U>(value))>) {
48 result.template emplace<T>(std::forward<U>(value));
49 }
50
51 [[nodiscard]] const T &get() const & {
52 check_and_rethrow(this->result);
53 return std::get<T>(this->result);
54 }
55
56 [[nodiscard]] T &&get() && {
57 check_and_rethrow(this->result);
58 return std::get<T>(std::move(this->result));
59 }
60};
61
62/// Storage class that can either contain a reference to a value, an exception,
63/// or be empty.
64template <typename T>
65class storage_base<T &> {
66 protected:
67 std::variant<std::monostate, std::exception_ptr, T *> result;
68
69 public:
70 void set_value(T &value) noexcept { result = std::addressof(value); }
71
72 [[nodiscard]] T &get() const {
73 check_and_rethrow(this->result);
74 return *std::get<T *>(this->result);
75 }
76};
77
78/// Storage class that can either an exception, or be empty.
79template <>
80class storage_base<void> {
81 protected:
82 std::variant<std::monostate, std::exception_ptr> result;
83
84 public:
85 void get() const { check_and_rethrow(this->result); }
86};
87
88/// Storage class that can either contain a value, an exception, or be empty.
89template <typename T>
90class storage : public storage_base<T> {
91 public:
92 using value_type = T;
93 void set_exception(std::exception_ptr ptr) noexcept { this->result = std::move(ptr); }
94};
95
96} // namespace mp_coro::detail
void set_value(T &value) noexcept
Definition: storage.h:70
std::variant< std::monostate, std::exception_ptr, T * > result
Definition: storage.h:67
std::variant< std::monostate, std::exception_ptr > result
Definition: storage.h:82
Storage class that can either contain a value, an exception, or be empty.
Definition: storage.h:40
const T & get() const &
Definition: storage.h:51
std::variant< std::monostate, std::exception_ptr, T > result
Definition: storage.h:42
void set_value(U &&value) noexcept(std::is_nothrow_constructible_v< T, decltype(std::forward< U >(value))>)
Definition: storage.h:46
Storage class that can either contain a value, an exception, or be empty.
Definition: storage.h:90
void set_exception(std::exception_ptr ptr) noexcept
Definition: storage.h:93
void check_and_rethrow(const std::variant< Args... > &result)
If rethrow contains an exception, rethrow it.
Definition: storage.h:33