-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert.hpp
39 lines (33 loc) · 1.64 KB
/
convert.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
#pragma once
#include <utility>
/// tarsier is a collection of event handlers.
namespace tarsier {
/// convert maps a type to another.
template <typename Event, typename EventToConvertedEvent, typename HandleConvertedEvent>
class convert {
public:
convert(EventToConvertedEvent&& event_to_converted_event, HandleConvertedEvent&& handle_converted_event) :
_event_to_converted_event(std::forward<EventToConvertedEvent>(event_to_converted_event)),
_handle_converted_event(std::forward<HandleConvertedEvent>(handle_converted_event)) {}
convert(const convert&) = delete;
convert(convert&&) = default;
convert& operator=(const convert&) = delete;
convert& operator=(convert&&) = default;
virtual ~convert() = default;
/// operator() handles an event.
virtual void operator()(Event event) {
_handle_converted_event(_event_to_converted_event(event));
}
protected:
EventToConvertedEvent _event_to_converted_event;
HandleConvertedEvent _handle_converted_event;
};
/// make_convert creates a convert from functors.
template <typename Event, typename EventToConvertedEvent, typename HandleConvertedEvent>
inline convert<Event, EventToConvertedEvent, HandleConvertedEvent>
make_convert(EventToConvertedEvent&& event_to_converted_event, HandleConvertedEvent&& handle_converted_event) {
return convert<Event, EventToConvertedEvent, HandleConvertedEvent>(
std::forward<EventToConvertedEvent>(event_to_converted_event),
std::forward<HandleConvertedEvent>(handle_converted_event));
}
}