Program Listing for File task.hpp

Return to documentation for file (include/concore/task.hpp)

#pragma once

#include "task_group.hpp"

#include <functional>

namespace concore {

namespace detail {
class exec_context;
}

inline namespace v1 {

using task_function = std::function<void()>;

class task {
public:
    task() = default;

    template <typename T>
    explicit task(T ftor)
        : fun_(std::move(ftor)) {}

    template <typename T>
    task(T ftor, task_group grp)
        : fun_(std::move(ftor))
        , task_group_(grp) {
        detail::task_group_access::on_task_created(grp);
    }
    template <typename T>
    task& operator=(T ftor) {
        fun_ = std::move(ftor);
        return *this;
    }

    ~task() { detail::task_group_access::on_task_destroyed(task_group_); }

    task(task&&) = default;
    task& operator=(task&&) = default;

    task(const task&) = delete;
    task& operator=(const task&) = delete;

    void swap(task& other) {
        fun_.swap(other.fun_);
        std::swap(task_group_, other.task_group_);
    }

    explicit operator bool() const noexcept { return static_cast<bool>(fun_); }

    void operator()() { fun_(); }

    const task_group& get_task_group() const { return task_group_; }

private:
    task_function fun_;

    task_group task_group_;
};
} // namespace v1

} // namespace concore