kokkos-utils 0.0.5
Loading...
Searching...
No Matches
TimerListener.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_UTILS_CALLBACKS_TIMERLISTENER_HPP
2#define KOKKOS_UTILS_CALLBACKS_TIMERLISTENER_HPP
3
6
8{
9
10template <typename TimerType>
11concept Startable = requires (TimerType& timer) {
12 { timer.start() } -> std::same_as<void>;
13};
14
15template <typename TimerType, typename EventType>
16concept StartableWithEvent = requires (TimerType& timer, const EventType& event) {
17 { timer.start(event) } -> std::same_as<void>;
18};
19
20template <typename TimerType>
21concept Stoppable = requires (TimerType& timer) {
22 { timer.stop() } -> std::same_as<void>;
23};
24
25template <typename TimerType, typename EventType>
26concept StoppableWithEvent = requires (TimerType& timer, const EventType& event) {
27 { timer.stop(event) } -> std::same_as<void>;
28};
29
41template <
42 Matcher BeginEndMatcherType,
43 typename TimerType
44>
46{
47 template <Event EventType> requires MatcherFor<BeginEndMatcherType, EventType>
48 void operator()(const EventType& event)
49 {
50 if(closed()) return;
51
52 const bool matching = matcher(event);
53
55 if(!this->matched_begin && matching)
56 {
57 this->matched_begin = true;
58
59 if constexpr (Startable<TimerType>) {
60 this->timer.start();
61 } else if constexpr (StartableWithEvent<TimerType, EventType>) {
62 this->timer.start(event);
63 }
64 }
66 else if(this->matched_begin && ! this->matched_end && matching)
67 {
68 this->matched_end = true;
69
70 if constexpr (Stoppable<TimerType>) {
71 this->timer.stop();
72 } else if constexpr (StoppableWithEvent<TimerType, EventType>) {
73 this->timer.stop(event);
74 }
75 }
76 }
77
79 bool connected() const { return matched_begin && !matched_end; }
80
82 bool closed() const { return matched_begin && matched_end; }
83
85 void reset() {
86 matched_begin = matched_end = false;
87 }
88
89 BeginEndMatcherType matcher {};
90 TimerType timer {};
91 bool matched_begin = false;
92 bool matched_end = false;
93};
94
95} // namespace Kokkos::utils::callbacks
96
97#endif // KOKKOS_UTILS_CALLBACKS_TIMERLISTENER_HPP
Check that Callable is a matcher for each event in EventTypes.
Definition Matcher.hpp:35
Callable is a matcher if it is invocable with at least one event type from Kokkos::utils::callbacks::...
Definition Matcher.hpp:31
Listener that starts a timer when a begin event is received and stops it when a corresponding end eve...
void reset()
Reset the listener so that it can be reused to match a begin event again.
void operator()(const EventType &event)
bool connected() const
When the listener has matched the begin, but not yet the end event, it is said to be "connected".