| 1 | // Aseprite |
| 2 | // Copyright (C) 2019-2020 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifndef APP_TASK_H_INCLUDED |
| 8 | #define APP_TASK_H_INCLUDED |
| 9 | #pragma once |
| 10 | |
| 11 | #include "base/task.h" |
| 12 | |
| 13 | #include <functional> |
| 14 | #include <mutex> |
| 15 | |
| 16 | namespace app { |
| 17 | |
| 18 | class Task { |
| 19 | public: |
| 20 | Task(); |
| 21 | ~Task(); |
| 22 | |
| 23 | void run(base::task::func_t&& func); |
| 24 | void wait(); |
| 25 | |
| 26 | // Returns true when the task is completed (whether it was |
| 27 | // canceled or not) |
| 28 | bool completed() const { |
| 29 | return m_task.completed(); |
| 30 | } |
| 31 | |
| 32 | bool running() const { |
| 33 | return m_task.running(); |
| 34 | } |
| 35 | |
| 36 | bool canceled() const { |
| 37 | std::lock_guard lock(m_token_mutex); |
| 38 | if (m_token) |
| 39 | return m_token->canceled(); |
| 40 | else |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | float progress() const { |
| 45 | std::lock_guard lock(m_token_mutex); |
| 46 | if (m_token) |
| 47 | return m_token->progress(); |
| 48 | else |
| 49 | return 0.0f; |
| 50 | } |
| 51 | |
| 52 | void cancel() { |
| 53 | std::lock_guard lock(m_token_mutex); |
| 54 | if (m_token) |
| 55 | m_token->cancel(); |
| 56 | } |
| 57 | |
| 58 | void set_progress(float progress) { |
| 59 | std::lock_guard lock(m_token_mutex); |
| 60 | if (m_token) |
| 61 | m_token->set_progress(progress); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | base::task m_task; |
| 66 | mutable std::mutex m_token_mutex; |
| 67 | base::task_token* m_token; |
| 68 | }; |
| 69 | |
| 70 | } // namespace app |
| 71 | |
| 72 | #endif |
| 73 | |