| Directory: | ./ |
|---|---|
| File: | src/Future.h |
| Date: | 2025-02-12 12:41:42 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 35 | 49 | 71.4% |
| Branches: | 12 | 58 | 20.7% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /***************************************************************************** | ||
| 2 | * vim:tw=78 | ||
| 3 | * | ||
| 4 | * Copyright (C) 2022 Bjarne von Horn (vh at igh dot de). | ||
| 5 | * | ||
| 6 | * This file is part of the PdCom library. | ||
| 7 | * | ||
| 8 | * The PdCom library is free software: you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU Lesser General Public License as published by | ||
| 10 | * the Free Software Foundation, either version 3 of the License, or (at your | ||
| 11 | * option) any later version. | ||
| 12 | * | ||
| 13 | * The PdCom library is distributed in the hope that it will be useful, but | ||
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
| 16 | * License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public License | ||
| 19 | * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | * | ||
| 21 | *****************************************************************************/ | ||
| 22 | |||
| 23 | #ifndef PDCOM5_FUTURE_IMPL_H | ||
| 24 | #define PDCOM5_FUTURE_IMPL_H | ||
| 25 | |||
| 26 | #include <functional> | ||
| 27 | #include <memory> | ||
| 28 | #include <pdcom5/Exception.h> | ||
| 29 | #include <pdcom5/Future.h> | ||
| 30 | #include <tuple> | ||
| 31 | #include <type_traits> | ||
| 32 | |||
| 33 | namespace PdCom { | ||
| 34 | namespace impl { | ||
| 35 | template <class Exception> | ||
| 36 | 4 | struct PromiseBase | |
| 37 | { | ||
| 38 | std::function<void(Exception)> reject_; | ||
| 39 | }; | ||
| 40 | |||
| 41 | template <class Exception, class... Result> | ||
| 42 | 4 | struct Promise : public PromiseBase<Exception> | |
| 43 | { | ||
| 44 | std::function<void(Result...)> resolve_; | ||
| 45 | }; | ||
| 46 | } // namespace impl | ||
| 47 | template <class Exception> | ||
| 48 | 10 | class PromiseBase | |
| 49 | { | ||
| 50 | protected: | ||
| 51 | std::weak_ptr<impl::PromiseBase<Exception>> impl_; | ||
| 52 | |||
| 53 | public: | ||
| 54 | using ExceptionType = Exception; | ||
| 55 | |||
| 56 | 2 | PromiseBase(std::weak_ptr<impl::PromiseBase<Exception>> impl) noexcept : | |
| 57 | 2 | impl_(std::move(impl)) | |
| 58 | 2 | {} | |
| 59 | 8 | PromiseBase(PromiseBase &&) = default; | |
| 60 | PromiseBase(PromiseBase const &) = delete; | ||
| 61 | PromiseBase &operator=(PromiseBase &&) = default; | ||
| 62 | PromiseBase &operator=(PromiseBase const &) = delete; | ||
| 63 | |||
| 64 | template <class... Args> | ||
| 65 | bool reject(Args &&...args); | ||
| 66 | }; | ||
| 67 | |||
| 68 | template <class Exception, class... Result> | ||
| 69 | 18 | class Promise : public PromiseBase<Exception> | |
| 70 | { | ||
| 71 | using Impl = impl::Promise<Exception, Result...>; | ||
| 72 | |||
| 73 | public: | ||
| 74 | using ResolveFn = std::function<void(Result...)>; | ||
| 75 | |||
| 76 | |||
| 77 | Promise() noexcept : PromiseBase<Exception>(nullptr) {} | ||
| 78 | 2 | Promise(std::weak_ptr<Impl> impl) noexcept : | |
| 79 | 2 | PromiseBase<Exception>(std::move(impl)) | |
| 80 | 2 | {} | |
| 81 | |||
| 82 | template <class... Args> | ||
| 83 | bool resolve(Args &&...result); | ||
| 84 | |||
| 85 | PromiseBase<Exception> toBase() | ||
| 86 | { | ||
| 87 | return {std::move(PromiseBase<Exception>::impl_)}; | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | template <class Exception, class... Result> | ||
| 92 | ✗ | const Future<Exception, Result...> &Future<Exception, Result...>::then( | |
| 93 | Future<Exception, Result...>::ResolveFn resolve) const & | ||
| 94 | { | ||
| 95 | ✗ | if (!impl_) | |
| 96 | ✗ | throw InvalidArgument("Future is empty"); | |
| 97 | ✗ | impl_->resolve_ = resolve; | |
| 98 | ✗ | return *this; | |
| 99 | } | ||
| 100 | |||
| 101 | template <class Exception, class... Result> | ||
| 102 | const Future<Exception, Result...> & | ||
| 103 | ✗ | Future<Exception, Result...>::handle_exception( | |
| 104 | Future<Exception, Result...>::RejectFn reject) const & | ||
| 105 | { | ||
| 106 | ✗ | if (!impl_) | |
| 107 | ✗ | throw InvalidArgument("Future is empty"); | |
| 108 | ✗ | impl_->reject_ = reject; | |
| 109 | ✗ | return *this; | |
| 110 | } | ||
| 111 | |||
| 112 | template <class Exception, class... Result> | ||
| 113 | 2 | Future<Exception, Result...> Future<Exception, Result...>::then( | |
| 114 | Future<Exception, Result...>::ResolveFn resolve) && | ||
| 115 | { | ||
| 116 |
1/4✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | if (!impl_) |
| 117 | ✗ | throw InvalidArgument("Future is empty"); | |
| 118 | 2 | impl_->resolve_ = resolve; | |
| 119 | 2 | return std::move(*this); | |
| 120 | } | ||
| 121 | |||
| 122 | template <class Exception, class... Result> | ||
| 123 | 2 | Future<Exception, Result...> Future<Exception, Result...>::handle_exception( | |
| 124 | Future<Exception, Result...>::RejectFn reject) && | ||
| 125 | { | ||
| 126 |
1/4✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
|
2 | if (!impl_) |
| 127 | ✗ | throw InvalidArgument("Future is empty"); | |
| 128 | 2 | impl_->reject_ = reject; | |
| 129 | 2 | return std::move(*this); | |
| 130 | } | ||
| 131 | |||
| 132 | template <class Exception, class... Result> | ||
| 133 | template <class... Args> | ||
| 134 | 1 | inline bool Promise<Exception, Result...>::resolve(Args &&...args) | |
| 135 | { | ||
| 136 | 1 | bool ans = false; | |
| 137 |
1/4✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
|
2 | if (const auto impl = std::static_pointer_cast<Impl>( |
| 138 | PromiseBase<Exception>::impl_.lock())) { | ||
| 139 |
1/4✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
1 | if (impl->resolve_) { |
| 140 |
2/6✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
|
1 | impl->resolve_(std::forward<Args>(args)...); |
| 141 | 1 | ans = true; | |
| 142 | } | ||
| 143 | } | ||
| 144 | 1 | PromiseBase<Exception>::impl_.reset(); | |
| 145 | 1 | return ans; | |
| 146 | } | ||
| 147 | |||
| 148 | template <class Exception> | ||
| 149 | template <class... Args> | ||
| 150 | 1 | inline bool PromiseBase<Exception>::reject(Args &&...args) | |
| 151 | { | ||
| 152 |
2/4✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
|
1 | if (const auto impl = impl_.lock()) { |
| 153 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | if (impl->reject_) { |
| 154 |
1/2✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
1 | impl->reject_(std::forward<Args>(args)...); |
| 155 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | return true; |
| 156 | } | ||
| 157 | } | ||
| 158 | ✗ | impl_.reset(); | |
| 159 | ✗ | return false; | |
| 160 | } | ||
| 161 | |||
| 162 | template <class Exception, class... Result> | ||
| 163 | inline std::pair<Future<Exception, Result...>, Promise<Exception, Result...>> | ||
| 164 | 2 | createFuture() | |
| 165 | { | ||
| 166 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
4 | auto ans = std::make_shared<impl::Promise<Exception, Result...>>(); |
| 167 | return {Future<Exception, Result...> {ans}, | ||
| 168 | 4 | Promise<Exception, Result...> {ans}}; | |
| 169 | } | ||
| 170 | |||
| 171 | } // namespace PdCom | ||
| 172 | |||
| 173 | |||
| 174 | #endif // PDCOM5_FUTURE_IMPL_H | ||
| 175 |