Refactor ErrorCounter out from timing report code

This is helpful to allow other applications to track error counts in a
similar manner.

Change-Id: Ifc7127578c08757febc6acdc3a79e42ad7b7cce5
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/BUILD b/aos/events/BUILD
index 6abc075..0f23d5a 100644
--- a/aos/events/BUILD
+++ b/aos/events/BUILD
@@ -24,10 +24,8 @@
     name = "event_loop_fbs",
     srcs = ["event_loop.fbs"],
     gen_reflections = 1,
-    includes = [
-        "//aos:configuration_fbs_includes",
-    ],
     target_compatible_with = ["@platforms//os:linux"],
+    deps = ["//aos:configuration_fbs"],
 )
 
 cc_static_flatbuffer(
@@ -341,6 +339,7 @@
     deps = [
         ":event_loop_fbs",
         "//aos:configuration",
+        "//aos/util:error_counter",
         "@com_github_google_glog//:glog",
     ],
 )
diff --git a/aos/events/event_loop.cc b/aos/events/event_loop.cc
index 09a3834..257313b 100644
--- a/aos/events/event_loop.cc
+++ b/aos/events/event_loop.cc
@@ -389,19 +389,13 @@
 
   // Pre-fill in the defaults for senders.
   std::vector<flatbuffers::Offset<timing::Sender>> sender_offsets;
-  for (const RawSender *sender : senders_) {
+  for (RawSender *sender : senders_) {
     flatbuffers::Offset<timing::Statistic> size_offset =
         timing::CreateStatistic(fbb);
 
-    std::vector<flatbuffers::Offset<timing::SendErrorCount>>
-        error_count_offsets;
-    for (size_t ii = 0; ii < internal::RawSenderTiming::kNumErrors; ++ii) {
-      error_count_offsets.push_back(timing::CreateSendErrorCount(
-          fbb, timing::EnumValuesSendError()[ii], 0));
-    }
     const flatbuffers::Offset<
         flatbuffers::Vector<flatbuffers::Offset<timing::SendErrorCount>>>
-        error_counts_offset = fbb.CreateVector(error_count_offsets);
+        error_counts_offset = sender->timing_.error_counter.Initialize(&fbb);
 
     timing::Sender::Builder sender_builder(fbb);
 
diff --git a/aos/events/timing_statistics.cc b/aos/events/timing_statistics.cc
index fbc49f4..c89bebd 100644
--- a/aos/events/timing_statistics.cc
+++ b/aos/events/timing_statistics.cc
@@ -28,8 +28,10 @@
   sender = new_sender;
   if (!sender) {
     size.set_statistic(nullptr);
+    error_counter.InvalidateBuffer();
   } else {
     size.set_statistic(sender->mutable_size());
+    error_counter.set_mutable_vector(sender->mutable_error_counts());
   }
 }
 
@@ -40,19 +42,14 @@
 
   size.Reset();
   sender->mutate_count(0);
-  for (size_t ii = 0; ii < kNumErrors; ++ii) {
-    sender->mutable_error_counts()->GetMutableObject(ii)->mutate_count(0);
-  }
+  error_counter.ResetCounts();
 }
 
 void RawSenderTiming::IncrementError(timing::SendError error) {
   if (!sender) {
     return;
   }
-  const size_t index = static_cast<size_t>(error);
-  timing::SendErrorCount *counter =
-      sender->mutable_error_counts()->GetMutableObject(index);
-  counter->mutate_count(counter->count() + 1);
+  error_counter.IncrementError(error);
 }
 
 void TimerTiming::set_timing_report(timing::Timer *new_timer) {
diff --git a/aos/events/timing_statistics.h b/aos/events/timing_statistics.h
index e9edff2..3c8c741 100644
--- a/aos/events/timing_statistics.h
+++ b/aos/events/timing_statistics.h
@@ -4,6 +4,7 @@
 #include <cmath>
 
 #include "aos/events/event_loop_generated.h"
+#include "aos/util/error_counter.h"
 
 namespace aos {
 namespace internal {
@@ -79,9 +80,9 @@
 
 // Class to hold timing information for a raw sender.
 struct RawSenderTiming {
-  static constexpr size_t kNumErrors =
-      static_cast<int>(timing::SendError::MAX) -
-      static_cast<int>(timing::SendError::MIN) + 1;
+  typedef util::ErrorCounter<timing::SendError, timing::SendErrorCount>
+      ErrorCounter;
+  static constexpr size_t kNumErrors = ErrorCounter::kNumErrors;
 
   RawSenderTiming(int new_channel_index) : channel_index(new_channel_index) {}
 
@@ -90,8 +91,6 @@
   void IncrementError(timing::SendError error);
   // Sanity check that the enum values are such that we can just use the enum
   // values themselves as array indices without anything weird happening.
-  static_assert(0 == static_cast<int>(timing::SendError::MIN),
-                "Expected error enum values to start at zero.");
   static_assert(
       sizeof(std::invoke_result<decltype(timing::EnumValuesSendError)>::type) /
               sizeof(timing::SendError) ==
@@ -101,6 +100,7 @@
   const int channel_index;
   timing::Sender *sender = nullptr;
   internal::TimingStatistic size;
+  ErrorCounter error_counter;
 };
 
 // Class to hold timing information for timers.