#pragma once #include "sys/threadpool.hpp" #include #include #include #include // This macro helps keep things a bit easier to read and cuts down on repetition. #define TASK_FINISH_RETURN(x) \ x->complete(); \ return namespace sys { /// @brief Class that runs tasks in a thread and automatically deactivates when finished. class Task { public: // clang-format off struct DataStruct : sys::threadpool::DataStruct { sys::Task *task{}; }; // clang-format on /// @brief This makes some things easier to type and read in other places. using TaskData = std::shared_ptr; /// @brief Default constructor. Task(); /// @brief Starts a new task. Task(sys::threadpool::JobFunction function, sys::Task::TaskData taskData); /// @brief Returns if the thread has signaled it's finished running. /// @return True if the thread is still running. False if it isn't. bool is_running() const noexcept; /// @brief Allows thread to signal it's finished. /// @note Spawned task threads must call this when their work is finished. void complete() noexcept; /// @brief Sets the task/threads current status string. Thread safe. void set_status(std::string_view status); /// @brief Moves the status string instead of creating a copy. void set_status(std::string &status); /// @brief Returns the status string. Thread safe. /// @return Copy of the status string. std::string get_status() noexcept; protected: // Whether task is still running. std::atomic m_isRunning{}; private: // Status string the thread can set that the main thread can display. std::string m_status{}; // Mutex so that string doesn't get messed up. std::mutex m_statusLock{}; }; } // namespace sys