-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_flow.hpp
46 lines (40 loc) · 1.04 KB
/
control_flow.hpp
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
#pragma once
#include <type_traits>
#include <utility>
namespace base
{
// This enum is used to control the flow of ForEach invocations.
enum class ControlFlow
{
Break,
Continue
};
// A wrapper that calls |fn| with arguments |args|.
// To avoid excessive calls, |fn| may signal the end of execution via its return value,
// which should then be checked by the wrapper's user.
template <typename Fn>
class ControlFlowWrapper
{
public:
template <typename Gn>
explicit ControlFlowWrapper(Gn && gn) : m_fn(std::forward<Gn>(gn))
{
}
template <typename... Args>
std::enable_if_t<std::is_same<std::result_of_t<Fn(Args...)>, base::ControlFlow>::value,
base::ControlFlow>
operator()(Args &&... args)
{
return m_fn(std::forward<Args>(args)...);
}
template <typename... Args>
std::enable_if_t<std::is_same<std::result_of_t<Fn(Args...)>, void>::value, base::ControlFlow>
operator()(Args &&... args)
{
m_fn(std::forward<Args>(args)...);
return ControlFlow::Continue;
}
private:
Fn m_fn;
};
} // namespace base