Skip to content

Commit

Permalink
feat(lazy): add cancellable_lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed May 21, 2024
1 parent fe345a0 commit 417fe4d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/rohrkabel/utils/lazy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,38 @@ namespace pipewire
}
};

template <typename T>
class cancellable_lazy : public lazy<T>
{
std::stop_source stop;

public:
template <typename... Args>
cancellable_lazy(std::stop_source stop, Args &&...args)
: lazy<T>(std::forward<Args>(args)...), stop(std::move(stop))
{
}

[[nodiscard]] std::stop_source stop_source() const
{
return stop;
}
};

template <typename T, typename Function>
requires std::same_as<std::invoke_result_t<Function>, T>
lazy<T> make_lazy(Function &&func)
{
return std::async(std::launch::deferred, std::forward<Function>(func));
}

template <typename T, typename Function>
requires std::same_as<std::invoke_result_t<Function, std::stop_token>, T>
cancellable_lazy<T> make_cancellable_lazy(Function &&func)
{
auto source = std::stop_source{};
auto fut = std::async(std::launch::deferred, std::forward<Function>(func), source.get_token());

return {source, std::move(fut)};
}
} // namespace pipewire

0 comments on commit 417fe4d

Please sign in to comment.