Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
callback.hh
Go to the documentation of this file.
1#pragma once
3
4#include <cassert>
5#include <future>
6#include <functional>
7
8namespace nix {
9
15template<typename T>
16class Callback
17{
18 std::function<void(std::future<T>)> fun;
19 std::atomic_flag done = ATOMIC_FLAG_INIT;
20
21public:
22
23 Callback(std::function<void(std::future<T>)> fun) : fun(fun) { }
24
25 // NOTE: std::function is noexcept move-constructible since C++20.
26 Callback(Callback && callback) noexcept(std::is_nothrow_move_constructible_v<decltype(fun)>)
27 : fun(std::move(callback.fun))
28 {
29 auto prev = callback.done.test_and_set();
30 if (prev) done.test_and_set();
31 }
32
33 void operator()(T && t) noexcept
34 {
35 auto prev = done.test_and_set();
36 assert(!prev);
37 std::promise<T> promise;
38 promise.set_value(std::move(t));
39 fun(promise.get_future());
40 }
41
42 void rethrow(const std::exception_ptr & exc = std::current_exception()) noexcept
43 {
44 auto prev = done.test_and_set();
45 assert(!prev);
46 std::promise<T> promise;
47 promise.set_exception(exc);
48 fun(promise.get_future());
49 }
50};
51
52}
T t
Definition lexer.l:154