Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [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 | #include "glog/logging.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | |
| 10 | // From a watch functor, this will extract the message type of the argument. |
| 11 | // This is the template forward declaration, and it extracts the call operator |
| 12 | // as a PTMF to be used by the following specialization. |
| 13 | template <class T> |
| 14 | struct watch_message_type_trait |
| 15 | : watch_message_type_trait<decltype(&T::operator())> {}; |
| 16 | |
| 17 | // From a watch functor, this will extract the message type of the argument. |
| 18 | // This is the template specialization. |
| 19 | template <class ClassType, class ReturnType, class A1> |
| 20 | struct watch_message_type_trait<ReturnType (ClassType::*)(A1) const> { |
| 21 | using message_type = typename std::decay<A1>::type; |
| 22 | }; |
| 23 | |
| 24 | template <typename T> |
| 25 | typename Sender<T>::Builder Sender<T>::MakeBuilder() { |
| 26 | return Builder(sender_.get(), sender_->data(), sender_->size()); |
| 27 | } |
| 28 | |
| 29 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame^] | 30 | void EventLoop::MakeWatcher(const std::string_view channel_name, Watch &&w) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | using T = typename watch_message_type_trait<Watch>::message_type; |
| 32 | const Channel *channel = configuration::GetChannel( |
| 33 | configuration_, channel_name, T::GetFullyQualifiedName(), name()); |
| 34 | |
| 35 | CHECK(channel != nullptr) |
| 36 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 37 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 38 | |
| 39 | return MakeRawWatcher( |
| 40 | channel, [this, w](const Context &context, const void *message) { |
| 41 | context_ = context; |
| 42 | w(*flatbuffers::GetRoot<T>(reinterpret_cast<const char *>(message))); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | } // namespace aos |
| 47 | |
| 48 | #endif // AOS_EVENTS_EVENT_LOOP_TMPL_H |