Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame^] | 1 | #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 | |
| 7 | namespace 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. |
| 12 | template <class T> |
| 13 | struct 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. |
| 18 | template <class ClassType, class ReturnType, class A1> |
| 19 | struct watch_message_type_trait<ReturnType (ClassType::*)(A1) const> { |
| 20 | using message_type = typename std::decay<A1>::type; |
| 21 | }; |
| 22 | |
| 23 | template <typename T> |
| 24 | typename Sender<T>::Message Sender<T>::MakeMessage() { |
| 25 | return Message(sender_.get()); |
| 26 | } |
| 27 | |
| 28 | template <typename Watch> |
| 29 | void 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 |