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 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 6 | namespace aos::testing { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 7 | |
| 8 | // Simple class to count messages on a channel easily. This only counts |
| 9 | // messages published while running. |
| 10 | template <typename T> |
| 11 | class MessageCounter { |
| 12 | public: |
| 13 | MessageCounter(aos::EventLoop *event_loop, std::string_view name) { |
| 14 | event_loop->MakeNoArgWatcher<T>(name, [this]() { ++count_; }); |
| 15 | } |
| 16 | |
| 17 | // Returns the number of messages seen so far. |
| 18 | size_t count() const { return count_; } |
| 19 | |
| 20 | private: |
| 21 | size_t count_ = 0; |
| 22 | }; |
| 23 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 24 | } // namespace aos::testing |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 25 | |
| 26 | #endif // AOS_EVENTS_MESSAGE_COUNTER_H_ |