Bug 1436680 - Allow non-templated uses of AutoCounter. r?chutten draft
authorJeremy Lempereur <jeremy.lempereur@gmail.com>
Tue, 13 Feb 2018 19:08:34 +0100
changeset 765952 347ad94c176e95a28a581c879ca66f90f5bc6477
parent 765950 d6e2730c400b211a5df5c0eb6a7d49a2bf02170c
push id102190
push userbmo:jeremy.lempereur@gmail.com
push dateSun, 11 Mar 2018 13:27:06 +0000
reviewerschutten
bugs1436680
milestone60.0a1
Bug 1436680 - Allow non-templated uses of AutoCounter. r?chutten In order to stay consistent with the AutoTimer api, allowed non-templates uses or AutoCounter. MozReview-Commit-ID: 9qnAeQTIY9T
toolkit/components/telemetry/Telemetry.h
toolkit/components/telemetry/docs/collection/histograms.rst
--- a/toolkit/components/telemetry/Telemetry.h
+++ b/toolkit/components/telemetry/Telemetry.h
@@ -300,16 +300,51 @@ public:
   }
 
 private:
   const TimeStamp start;
   const nsCString key;
   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
 };
 
+class MOZ_RAII RuntimeAutoCounter
+{
+public:
+  explicit RuntimeAutoCounter(
+    HistogramID aId,
+    uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
+    : id(aId)
+    , counter(counterStart)
+  {
+    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
+  }
+
+  ~RuntimeAutoCounter()
+  {
+    Accumulate(id, counter);
+  }
+
+  // Prefix increment only, to encourage good habits.
+  void operator++()
+  {
+    ++counter;
+  }
+
+  // Chaining doesn't make any sense, don't return anything.
+  void operator+=(int increment)
+  {
+    counter += increment;
+  }
+
+private:
+  HistogramID id;
+  uint32_t counter;
+  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
+};
+
 template<HistogramID id>
 class MOZ_RAII AutoCounter {
 public:
   explicit AutoCounter(uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
     : counter(counterStart)
   {
     MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   }
--- a/toolkit/components/telemetry/docs/collection/histograms.rst
+++ b/toolkit/components/telemetry/docs/collection/histograms.rst
@@ -317,21 +317,31 @@ The ``Telemetry.h`` header also declares
   nsresult
   nsPluginHost::StopPluginInstance(nsNPAPIPluginInstance* aInstance)
   {
     Telemetry::AutoTimer<Telemetry::PLUGIN_SHUTDOWN_MS> timer;
     ...
     return NS_OK;
   }
 
-If the HistogramID is not known at compile time, one can use the ``RuntimeAutoTimer`` class, which behaves like the template parameterized ``AutoTimer``.
+If the HistogramID is not known at compile time, one can use the ``RuntimeAutoTimer`` and ``RuntimeAutoCounter`` classes, which behave like the template parameterized ``AutoTimer`` and ``AutoCounter`` ones.
 
 .. code-block:: cpp
 
   void
   FunctionWithTiming(Telemetry::HistogramID aTelemetryID)
   {
     ...
     Telemetry::RuntimeAutoTimer timer(aTelemetryID);
     ...
   }
 
+  int32_t
+  FunctionWithCounter(Telemetry::HistogramID aTelemetryID)
+  {
+    ...
+    Telemetry::RuntimeAutoCounter myCounter(aTelemetryID);
+    ++myCounter;
+    myCounter += 42;
+    ...
+  }
+
 Prefer using the template parameterized ``AutoTimer`` and ``AutoCounter`` on hot paths, if possible.