-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared_functor.h
59 lines (51 loc) · 1.28 KB
/
shared_functor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <functional>
#include <memory>
#include <type_traits>
#include <atomic>
#if defined(DEBUG)
#define SAFE_SHARED_FUNCTORS
#endif
namespace kaiu {
namespace detail {
/*
* Allows us to capture a non-copyable in a lambda, then cast the lambda to
* std::function.
*
* If DEBUG or SAFE_SHARED_FUNCTORS is defined it also enforces that the
* callable can only be called once, and will throw an exception if it is called
* subsequent times.
*/
template <typename F>
class shared_functor {
private:
struct functor_state {
#if defined(SAFE_SHARED_FUNCTORS)
std::atomic_flag called{ATOMIC_FLAG_INIT};
#endif
F ptr;
functor_state(F&& f) : ptr(std::move(f)) { }
};
std::shared_ptr<functor_state> state;
public:
explicit shared_functor(F&& f) :
state(std::make_shared<functor_state>(std::move(f)))
{ }
template <typename... Args>
void operator () (Args&&... args)
{
#if defined(SAFE_SHARED_FUNCTORS)
if (state->called.test_and_set(std::memory_order_release)) {
throw std::logic_error("Shared functor called more than once");
}
#endif
state->ptr(std::forward<Args>(args)...);
}
};
template <typename F>
shared_functor<typename std::decay<F>::type> make_shared_functor(F&& functor)
{
return shared_functor<typename std::decay<F>::type>(std::move(functor));
}
}
}