]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Introduce Counters::{get,set}_raw
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 11 Sep 2020 07:41:11 +0000 (09:41 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 14 Sep 2020 17:40:06 +0000 (19:40 +0200)
Casting an integer to an enum class value is undefined behavior so let’s
strive not to do that.

src/Counters.cpp
src/Counters.hpp
src/stats.cpp

index 01db7356c03a7aba087aab120d0f27e261c6bfb6..1ec52e2e050516d51d2e555da5a0e38e8f6dac20 100644 (file)
@@ -29,19 +29,33 @@ Counters::Counters() : m_counters(static_cast<size_t>(Statistic::END))
 uint64_t
 Counters::get(Statistic statistic) const
 {
-  assert(static_cast<size_t>(statistic) < static_cast<size_t>(Statistic::END));
-  const size_t i = static_cast<size_t>(statistic);
-  return i < m_counters.size() ? m_counters[i] : 0;
+  const auto index = static_cast<size_t>(statistic);
+  assert(index < static_cast<size_t>(Statistic::END));
+  return index < m_counters.size() ? m_counters[index] : 0;
 }
 
 void
 Counters::set(Statistic statistic, uint64_t value)
 {
-  const auto i = static_cast<size_t>(statistic);
-  if (i >= m_counters.size()) {
-    m_counters.resize(i + 1);
+  const auto index = static_cast<size_t>(statistic);
+  assert(index < static_cast<size_t>(Statistic::END));
+  m_counters[index] = value;
+}
+
+uint64_t
+Counters::get_raw(size_t index) const
+{
+  assert(index < size());
+  return m_counters[index];
+}
+
+void
+Counters::set_raw(size_t index, uint64_t value)
+{
+  if (index >= m_counters.size()) {
+    m_counters.resize(index + 1);
   }
-  m_counters[i] = value;
+  m_counters[index] = value;
 }
 
 void
@@ -54,7 +68,6 @@ Counters::increment(Statistic statistic, int64_t value)
   auto& counter = m_counters[i];
   counter =
     std::max(static_cast<int64_t>(0), static_cast<int64_t>(counter + value));
-  }
 }
 
 size_t
index cc8177b9c35c5889a2c72864e74e239c4c23028a..44dc0b7265adbfe4e7de0689a48855b46ad8d7fc 100644 (file)
@@ -33,6 +33,10 @@ public:
 
   uint64_t get(Statistic statistic) const;
   void set(Statistic statistic, uint64_t value);
+
+  uint64_t get_raw(size_t index) const;
+  void set_raw(size_t index, uint64_t value);
+
   void increment(Statistic statistic, int64_t value = 1);
 
   size_t size() const;
index 70350f82884a299a5f3f3ef88333a5cbbaa2569c..133da430f4b7cf62a3625755468a90d796f32aea 100644 (file)
@@ -173,7 +173,7 @@ stats_write(const std::string& path, const Counters& counters)
 {
   AtomicFile file(path, AtomicFile::Mode::text);
   for (size_t i = 0; i < counters.size(); ++i) {
-    file.write(fmt::format("{}\n", counters.get(static_cast<Statistic>(i))));
+    file.write(fmt::format("{}\n", counters.get_raw(i)));
   }
   try {
     file.commit();