cesium-native 0.44.2
Loading...
Searching...
No Matches
CatchFunction.h
1#pragma once
2
3#include "unwrapFuture.h"
4
5namespace CesiumAsync {
6namespace CesiumImpl {
7// Begin omitting doxgen warnings for Impl namespace
9
10template <
11 typename Func,
12 typename T,
13 typename Scheduler,
14 typename TaskParameter = async::task<T>&&>
15struct CatchFunction {
16 Scheduler& scheduler;
17 Func f;
18
19 async::task<T> operator()(TaskParameter t) {
20 try {
21 return async::make_task(t.get());
22 } catch (...) {
23 // Make an exception_ptr task, then scheduler to a wrapper around f that
24 // throws it, catches it, and calls f with a reference to it.
25 auto ptrToException = [f = std::move(f)](std::exception_ptr&& e) mutable {
26 try {
27 std::rethrow_exception(e);
28 } catch (std::exception& e) {
29 return f(std::move(e));
30 } catch (...) {
31 return f(std::runtime_error("Unknown exception"));
32 }
33 };
34 return async::make_task(std::current_exception())
35 .then(
36 scheduler,
37 unwrapFuture<decltype(ptrToException), std::exception_ptr>(
38 std::move(ptrToException)));
39 }
40 }
41};
42
43template <typename Func, typename Scheduler, typename TaskParameter>
44struct CatchFunction<Func, void, Scheduler, TaskParameter> {
45 Scheduler& scheduler;
46 Func f;
47
48 async::task<void> operator()(TaskParameter t) {
49 try {
50 t.get();
51 return async::make_task();
52 } catch (...) {
53 // Make an exception_ptr task, then scheduler to a wrapper around f that
54 // throws it, catches it, and calls f with a reference to it.
55 auto ptrToException = [f = std::move(f)](std::exception_ptr&& e) mutable {
56 try {
57 std::rethrow_exception(e);
58 } catch (std::exception& e) {
59 return f(std::move(e));
60 } catch (...) {
61 return f(std::runtime_error("Unknown exception"));
62 }
63 };
64 return async::make_task(std::current_exception())
65 .then(
66 scheduler,
67 unwrapFuture<decltype(ptrToException), std::exception_ptr>(
68 std::move(ptrToException)));
69 }
70 }
71};
72
74// End omitting doxgen warnings for Impl namespace
75} // namespace CesiumImpl
76} // namespace CesiumAsync
Classes that support asynchronous operations.