blob: 4c53be358d202a93c73c0b7eb18b7752ffe841fc [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#ifndef _AOS_EVENTS_EVENT_LOOP_TMPL_H_
2#define _AOS_EVENTS_EVENT_LOOP_TMPL_H_
3
4#include <type_traits>
5#include "aos/events/event-loop.h"
6
7namespace aos {
8
9// From a watch functor, this will extract the message type of the argument.
10// This is the template forward declaration, and it extracts the call operator
11// as a PTMF to be used by the following specialization.
12template <class T>
13struct watch_message_type_trait
14 : watch_message_type_trait<decltype(&T::operator())> {};
15
16// From a watch functor, this will extract the message type of the argument.
17// This is the template specialization.
18template <class ClassType, class ReturnType, class A1>
19struct watch_message_type_trait<ReturnType (ClassType::*)(A1) const> {
20 using message_type = typename std::decay<A1>::type;
21};
22
23template <typename T>
24typename Sender<T>::Message Sender<T>::MakeMessage() {
25 return Message(sender_.get());
26}
27
28template <typename Watch>
29void EventLoop::MakeWatcher(const std::string &path, Watch &&w) {
30 using T = typename watch_message_type_trait<Watch>::message_type;
31
32 return MakeRawWatcher(path, QueueTypeInfo::Get<T>(),
33 [w](const Message *message) {
34 w(*reinterpret_cast<const T *>(message));
35 });
36}
37
38} // namespace aos
39
40#endif // _AOS_EVENTS_EVENT_LOOP_TMPL_H