23 #include <unordered_map>
34 template <
typename... Types>
36 template <
typename... Types>
72 void WhenAll(std::function<
void()> func)
const;
76 const unsigned int id;
78 SyncEntry(
int c) : count(c) {}
81 std::function<void()> stored_func;
83 static std::map<unsigned int, SyncEntry*> entries;
84 static unsigned int id_counter;
88 std::function<void()>
bind_tuple(std::function<
void(T)> f, std::tuple<T> t){
89 return std::bind(f, std::get<0>(t));
91 template <
typename T,
typename S>
92 std::function<void()>
bind_tuple(std::function<
void(T, S)> f, std::tuple<T, S> t){
93 return std::bind(f, std::get<0>(t), std::get<1>(t));
95 template <
typename T,
typename S,
typename R>
96 std::function<void()>
bind_tuple(std::function<
void(T, S, R)> f, std::tuple<T, S, R> t){
97 return std::bind(f, std::get<0>(t), std::get<1>(t), std::get<2>(t));
99 inline std::function<void()>
bind_tuple(std::function<
void()> f, std::tuple<>){
111 static std::map<int, LateReturnEntryBase*>
entries;
120 template <
typename... Types>
129 std::function<void()> f =
bind_tuple(stored_func, stored_args);
133 std::cout <<
"Exception while invoking a latereturn continuation" << std::endl;
137 std::function<void(Types...)> stored_func;
139 std::tuple<Types...> stored_args;
141 std::unordered_map<std::type_index, std::function<void(std::shared_ptr<Exceptions::Exception>)>> catchers;
143 std::shared_ptr<Exceptions::Exception> stored_exception;
145 std::function<void(std::shared_ptr<Exceptions::Exception>)> default_catcher;
236 template <
typename... Types>
249 std::cout <<
"ERROR: LateReturn Then called, but it is not in the base!" << std::endl;
253 if(!entry->triggered){
254 entry->stored_func = f;
255 entry->stored =
true;
258 entry->stored_func = f;
271 template<
typename Ex>
272 const LateReturn&
Catch(std::function<
void(std::shared_ptr<Exceptions::Exception>)> func)
const{
279 if(entry->stored_exception){
280 std::cout <<
"There is a stored exception already" << std::endl;
281 func(entry->stored_exception);
283 entry->catchers[
typeid(Ex)] = func;
294 template<
typename Ex>
302 if(entry->default_catcher){
303 std::cout <<
"ERROR: Cannot add another default cather to the same latereturn" << std::endl;
306 if(entry->stored_exception){
307 std::cout <<
"There is a stored exception already" << std::endl;
308 func(entry->stored_exception);
310 entry->default_catcher = func;
320 template<
typename... X>
328 if(entry->default_catcher){
329 std::cout <<
"ERROR: Cannot add another default cather to the same latereturn" << std::endl;
332 if(entry->stored_exception){
333 std::cout <<
"There is a stored exception already" << std::endl;
338 entry->default_catcher = [r](std::shared_ptr<Exceptions::Exception> ex){
350 Then([=](Types...)mutable{
361 Then([r](Types... result){
425 template <
typename... Types>
441 std::cout <<
"ERROR: Return() used on the same relay " <<
id <<
" twice!" << std::endl;
442 std::cout <<
"Did you remember to capture the relay by-value?" << std::endl;
447 entry->stored_args = std::tuple<Types...>(args...);
452 entry->stored_args = std::tuple<Types...>(args...);
453 entry->triggered =
true;
471 template<
typename Ex,
typename... ConstructArgs>
473 std::shared_ptr<Ex> exception = std::make_shared<Ex>(args...);
483 auto it2 = entry->catchers.find(
typeid(*ex));
484 if(it2 != entry->catchers.end()){
488 if(entry->default_catcher){
490 (entry->default_catcher)(ex);
493 entry->stored_exception = ex;
500 std::cout <<
"WARNING: A LateThrow cannot be caught and is ignored: " << ex->what() << std::endl;
505 std::cout <<
"ERROR: A relay may not PassException(...) after Return(...)ing." << std::endl;
517 Relay(
int i) : id(i) {};
524 template <
typename T>
527 lr.Then([r,&to_set](T val)
mutable{
539 template <
typename... X>
544 template <
typename... X,
typename Y>
547 first.Then([=](X... args){
548 r.Return( then(args...) );
553 template <
typename... X,
typename... Y>
556 first.Then([=](X... args){
557 then(args...).ThenReturn(r);
564 #endif // LATERETURN_HPP
LateReturn(const Relay< Types...> &r)
Definition: LateReturn.hpp:367
LateReturn< Types...> GetLateReturn() const
Definition: LateReturn.hpp:510
const LateReturn & Catch(const Relay< X...> &r) const
Definition: LateReturn.hpp:321
const Relay & LateThrow(ConstructArgs...args) const
Definition: LateReturn.hpp:472
virtual ~LateReturnEntryBase()
Definition: LateReturn.hpp:108
const LateReturn & ThenReturn(Relay< Types...> r) const
Definition: LateReturn.hpp:360
LateReturn LateAssign(T &to_set, LateReturn< T > lr)
Definition: LateReturn.hpp:525
void operator>>=(LateReturn< X...> &&first, typename identity< std::function< void(X...)>>::type then)
Definition: LateReturn.hpp:540
Definition: LateReturn.hpp:62
const Relay & Return(Types...args) const
Definition: LateReturn.hpp:437
Relay()
Definition: LateReturn.hpp:429
Definition: LateReturn.hpp:37
Definition: LateReturn.hpp:121
static std::map< int, LateReturnEntryBase * > entries
Definition: LateReturn.hpp:111
const LateReturn & Catch(std::function< void(std::shared_ptr< Exceptions::Exception >)> func) const
Definition: LateReturn.hpp:272
Definition: LateReturn.hpp:106
const LateReturn & ThenSync(Sync &s) const
Definition: LateReturn.hpp:349
int GetID() const
Definition: LateReturn.hpp:514
void WhenAll(std::function< void()> func) const
Definition: Alertable.hpp:26
bool triggered
Definition: LateReturn.hpp:109
LateReturn(LateReturn &&other)
Definition: LateReturn.hpp:370
static int id_counter
Definition: LateReturn.hpp:112
LateReturn & operator=(const LateReturn &other)=delete
LateReturn & operator=(LateReturn &&other)
Definition: LateReturn.hpp:371
Definition: LateReturn.hpp:35
const Relay & PassException(std::shared_ptr< Exceptions::Exception > ex) const
Definition: LateReturn.hpp:478
std::function< void()> bind_tuple(std::function< void(T)> f, std::tuple< T > t)
Definition: LateReturn.hpp:88
const LateReturn & CatchAll(std::function< void(std::shared_ptr< Exceptions::Exception >)> func) const
Definition: LateReturn.hpp:295
T type
Definition: LateReturn.hpp:537
const LateReturn & Then(std::function< void(Types...)> f) const
Definition: LateReturn.hpp:246
Definition: LateReturn.hpp:535