- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
#include <experimental/coroutine>
#include <iostream>
#include <optional>
#include <utility>
template<typename T>
class Maybe
{
std::shared_ptr<std::optional<T>> m_maybe = std::make_shared<std::optional<T>>();
public:
Maybe() = default;
Maybe(const T& t)
: m_maybe { std::make_shared<std::optional<T>>(t) }
{
}
explicit operator bool() const { return static_cast<bool>(*m_maybe); }
T& operator* () { return **m_maybe; }
const T& operator*() const { return **m_maybe; }
void reset() { m_maybe->reset(); }
template<typename U>
void emplace(U&& u) { m_maybe->emplace(std::forward<U>(u)); }
};
template<typename T>
void printMaybe(const Maybe<T>& opt)
{
if (opt)
std::cout << *opt << std::endl;
else
std::cout << "<empty>" << std::endl;
}
template<typename T, typename... Args>
struct std::experimental::coroutine_traits<Maybe<T>, Args...>
{
struct promise_type
{
Maybe<T> m_maybe;
auto get_return_object() { return m_maybe; }
std::experimental::suspend_never initial_suspend() { return {}; }
std::experimental::suspend_never final_suspend() { return {}; }
void unhandled_exception() { m_maybe.reset(); }
template<typename U>
void return_value(U&& u) { m_maybe.emplace(std::forward<U>(u)); }
};
};
template<typename T>
auto operator co_await(const Maybe<T>& maybe)
{
struct Awaiter
{
const Maybe<T>& input;
bool await_ready() { return static_cast<bool>(input); }
auto await_resume() { return *input; }
void await_suspend(std::experimental::coroutine_handle<> coro) { coro.destroy(); }
};
return Awaiter { maybe };
}
Maybe<int> maybeAdd(const Maybe<int>& maybeA, const Maybe<int>& maybeB)
{
auto a = co_await maybeA;
auto b = co_await maybeB;
co_return a + b;
}
int main()
{
/*
printMaybe(maybeAdd({ 1 }, { 2 }));
printMaybe(maybeAdd({}, { 2 }));
printMaybe(maybeAdd({ 1 }, {}));
*/
const auto res = maybeAdd({ 1 }, { 2 });
return res ? *res : 0;
}