| 1 | //! @file action.cpp |
|---|---|
| 2 | //! @author ryftchen |
| 3 | //! @brief The definitions (action) in the application module. |
| 4 | //! @version 0.1.0 |
| 5 | //! @copyright Copyright (c) 2022-2025 ryftchen. All rights reserved. |
| 6 | |
| 7 | #include "action.hpp" |
| 8 | |
| 9 | namespace application::action |
| 10 | { |
| 11 | std::atomic_bool Awaitable::active = false; |
| 12 | |
| 13 | Awaitable::Awaitable(const std::coroutine_handle<promise_type>& handle) : handle{handle} |
| 14 | { |
| 15 | if (active.load()) |
| 16 | { |
| 17 | throw std::runtime_error{"There can only be one awaitable instance active at any given time."}; |
| 18 | } |
| 19 | active.store(i: true); |
| 20 | } |
| 21 | |
| 22 | Awaitable::~Awaitable() |
| 23 | { |
| 24 | active.store(i: false); |
| 25 | if (handle) |
| 26 | { |
| 27 | handle.destroy(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void Awaitable::resume() const |
| 32 | { |
| 33 | if (handle) |
| 34 | { |
| 35 | handle.resume(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | bool Awaitable::done() const |
| 40 | { |
| 41 | return !handle || handle.done(); |
| 42 | } |
| 43 | } // namespace application::action |
| 44 |