roar
Loading...
Searching...
No Matches
promise_compat.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <promise-cpp/promise.hpp>
5
6namespace Roar::Detail
7{
8 template <typename T>
9 struct PromiseReferenceWrap : std::reference_wrapper<T>
10 {
11 using std::reference_wrapper<T>::reference_wrapper;
12 using std::reference_wrapper<T>::get;
13 using std::reference_wrapper<T>::operator T&;
14 // dummy function to prevent the compiler from complaining about func traits in promise lib.
15 void operator()() const
16 {}
17 };
18
19 template <typename T>
20 auto ref(T& thing)
21 {
22 return PromiseReferenceWrap<T>{thing};
23 }
24
25 template <typename T>
26 auto cref(T const& thing)
27 {
29 }
30
31 template <typename T>
33 {
34 using type = T;
35 static T& unpack(T& thing)
36 {
37 return thing;
38 }
39 };
40
41 template <typename T>
43 {
44 using type = T&;
46 {
47 return thing.get();
48 }
49 };
50
51 template <typename T>
53 {
54 using type = T const&;
56 {
57 return thing.get();
58 }
59 };
60
61 template <typename... List>
63 {};
64
65 template <typename... List>
67 {};
68
69 template <typename...>
71
73 {
74 public:
75 PromiseTypeBindBase(promise::Promise promise)
76 : promise_(std::move(promise))
77 {}
78 virtual ~PromiseTypeBindBase() = default;
79
80 protected:
81 promise::Promise promise_;
82 };
83
84 template <typename... ThenArgs>
86 {
88
89 auto then(std::function<void(typename UnpackReferenceWrapper<ThenArgs>::type...)> func)
90 {
91 return promise_.then([func = std::move(func)](ThenArgs... args) {
92 func(UnpackReferenceWrapper<ThenArgs>::unpack(args)...);
93 });
94 }
95
96 auto fail(auto&& func)
97 {
98 return promise_.fail(std::forward<decltype(func)>(func));
99 }
100 };
101
102 template <typename... FailArgs>
104 {
106
107 auto then(auto&& func)
108 {
109 return promise_.then(std::forward<decltype(func)>(func));
110 }
111
112 auto fail(std::function<void(typename UnpackReferenceWrapper<FailArgs>::type...)> func)
113 {
114 return promise_.fail([func = std::move(func)](FailArgs... args) {
115 func(UnpackReferenceWrapper<FailArgs>::unpack(args)...);
116 });
117 }
118 };
119
120 template <typename... ThenArgs, typename... FailArgs>
121 struct PromiseTypeBind<PromiseTypeBindThen<ThenArgs...>, PromiseTypeBindFail<FailArgs...>>
122 : public PromiseTypeBindBase
123 {
125
127 then(std::function<void(typename UnpackReferenceWrapper<ThenArgs>::type...)> func)
128 {
129 return promise_.then([func = std::move(func)](ThenArgs... args) {
130 func(UnpackReferenceWrapper<ThenArgs>::unpack(args)...);
131 });
132 }
133
135 fail(std::function<void(typename UnpackReferenceWrapper<FailArgs>::type...)> func)
136 {
137 return promise_.fail([func = std::move(func)](FailArgs... args) {
138 func(UnpackReferenceWrapper<FailArgs>::unpack(args)...);
139 });
140 }
141 };
142}
Definition promise_compat.hpp:73
PromiseTypeBindBase(promise::Promise promise)
Definition promise_compat.hpp:75
promise::Promise promise_
Definition promise_compat.hpp:81
virtual ~PromiseTypeBindBase()=default
Definition range_file_body.hpp:18
auto ref(T &thing)
Definition promise_compat.hpp:20
auto cref(T const &thing)
Definition promise_compat.hpp:26
Definition promise_compat.hpp:10
void operator()() const
Definition promise_compat.hpp:15
Definition promise_compat.hpp:67
Definition promise_compat.hpp:63
auto then(auto &&func)
Definition promise_compat.hpp:107
auto fail(std::function< void(typename UnpackReferenceWrapper< FailArgs >::type...)> func)
Definition promise_compat.hpp:112
PromiseTypeBind< PromiseTypeBindThen< ThenArgs... > > fail(std::function< void(typename UnpackReferenceWrapper< FailArgs >::type...)> func)
Definition promise_compat.hpp:135
PromiseTypeBind< PromiseTypeBindFail< FailArgs... > > then(std::function< void(typename UnpackReferenceWrapper< ThenArgs >::type...)> func)
Definition promise_compat.hpp:127
auto then(std::function< void(typename UnpackReferenceWrapper< ThenArgs >::type...)> func)
Definition promise_compat.hpp:89
auto fail(auto &&func)
Definition promise_compat.hpp:96
Definition promise_compat.hpp:70
static T & unpack(PromiseReferenceWrap< T > thing)
Definition promise_compat.hpp:45
static T const & unpack(PromiseReferenceWrap< T const > thing)
Definition promise_compat.hpp:55
Definition promise_compat.hpp:33
T type
Definition promise_compat.hpp:34
static T & unpack(T &thing)
Definition promise_compat.hpp:35