Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_MESSAGE_COUNTER_H_ |
| 2 | #define AOS_EVENTS_MESSAGE_COUNTER_H_ |
| 3 | |
| 4 | #include "aos/events/event_loop.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | namespace testing { |
| 8 | |
| 9 | // Simple class to count messages on a channel easily. This only counts |
| 10 | // messages published while running. |
| 11 | template <typename T> |
| 12 | class MessageCounter { |
| 13 | public: |
| 14 | MessageCounter(aos::EventLoop *event_loop, std::string_view name) { |
| 15 | event_loop->MakeNoArgWatcher<T>(name, [this]() { ++count_; }); |
| 16 | } |
| 17 | |
| 18 | // Returns the number of messages seen so far. |
| 19 | size_t count() const { return count_; } |
| 20 | |
| 21 | private: |
| 22 | size_t count_ = 0; |
| 23 | }; |
| 24 | |
| 25 | } // namespace testing |
| 26 | } // namespace aos |
| 27 | |
| 28 | #endif // AOS_EVENTS_MESSAGE_COUNTER_H_ |