Skip to content

Commit

Permalink
implement unique function to use as base for task
Browse files Browse the repository at this point in the history
  • Loading branch information
daronenko committed Sep 16, 2024
1 parent da27b4c commit dff89ee
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions source/fiber/sched/task/unique_function.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "task.hpp"

#include <memory>
#include <utility>

namespace fiber::sched::task {

template <typename F>
class UniqueFunction : TaskBase {
public:
static TaskBase* New(F&& func) {
return new UniqueFunction(std::move(func));
}

// Movable

UniqueFunction(UniqueFunction&& other) noexcept
: func_(std::move(other.func_)) {
}

UniqueFunction& operator=(UniqueFunction&& other) noexcept {
if (this != &other) {
func_ = std::move(other.func_);
}

return *this;
}

// Non-copyable
UniqueFunction(const UniqueFunction&&) = delete;
UniqueFunction& operator=(const UniqueFunction&&) = delete;

void Run() noexcept override {
func_();
delete this;
}

virtual ~UniqueFunction() = default;

private:
explicit UniqueFunction(F&& func)
: func_(std::move(func)) {
}

private:
F func_;
};

} // namespace fiber::sched::task

0 comments on commit dff89ee

Please sign in to comment.