blob: 418c1eb6d8c2e638db3115dee3d50eb6f6e39f20 [file] [log] [blame]
Austin Schuh01b4c352020-09-21 23:09:39 -07001#ifndef AOS_EVENTS_MESSAGE_COUNTER_H_
2#define AOS_EVENTS_MESSAGE_COUNTER_H_
3
4#include "aos/events/event_loop.h"
5
6namespace aos {
7namespace testing {
8
9// Simple class to count messages on a channel easily. This only counts
10// messages published while running.
11template <typename T>
12class 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_