Commit 9f6f11bf authored by Dominik Charousset's avatar Dominik Charousset

Optionally allow operator++ on counters

parent 4dc03681
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#pragma once #pragma once
#include <type_traits>
#include "caf/config.hpp" #include "caf/config.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/telemetry/gauge.hpp" #include "caf/telemetry/gauge.hpp"
...@@ -48,6 +50,10 @@ public: ...@@ -48,6 +50,10 @@ public:
// nop // nop
} }
explicit counter(const std::vector<label>&) noexcept {
// nop
}
// -- modifiers -------------------------------------------------------------- // -- modifiers --------------------------------------------------------------
/// Increments the counter by 1. /// Increments the counter by 1.
...@@ -62,6 +68,13 @@ public: ...@@ -62,6 +68,13 @@ public:
gauge_.inc(amount); gauge_.inc(amount);
} }
/// Increments the counter by 1.
/// @returns The new value of the counter.
template <class T = ValueType>
std::enable_if_t<std::is_same<T, int64_t>::value, T> operator++() noexcept {
return ++gauge_;
}
// -- observers -------------------------------------------------------------- // -- observers --------------------------------------------------------------
/// Returns the current value of the counter. /// Returns the current value of the counter.
......
...@@ -25,25 +25,27 @@ ...@@ -25,25 +25,27 @@
using namespace caf; using namespace caf;
CAF_TEST(double counters can only increment) { CAF_TEST(double counters can only increment) {
telemetry::dbl_counter g; telemetry::dbl_counter c;
CAF_MESSAGE("counters start at 0"); CAF_MESSAGE("counters start at 0");
CAF_CHECK_EQUAL(g.value(), 0.0); CAF_CHECK_EQUAL(c.value(), 0.0);
CAF_MESSAGE("counters are incrementable"); CAF_MESSAGE("counters are incrementable");
g.inc(); c.inc();
g.inc(2.0); c.inc(2.0);
CAF_CHECK_EQUAL(g.value(), 3.0); CAF_CHECK_EQUAL(c.value(), 3.0);
CAF_MESSAGE("users can create counters with custom start values"); CAF_MESSAGE("users can create counters with custom start values");
CAF_CHECK_EQUAL(telemetry::dbl_counter{42.0}.value(), 42.0); CAF_CHECK_EQUAL(telemetry::dbl_counter{42.0}.value(), 42.0);
} }
CAF_TEST(integer counters can only increment) { CAF_TEST(integer counters can only increment) {
telemetry::int_counter g; telemetry::int_counter c;
CAF_MESSAGE("counters start at 0"); CAF_MESSAGE("counters start at 0");
CAF_CHECK_EQUAL(g.value(), 0); CAF_CHECK_EQUAL(c.value(), 0);
CAF_MESSAGE("counters are incrementable"); CAF_MESSAGE("counters are incrementable");
g.inc(); c.inc();
g.inc(2); c.inc(2);
CAF_CHECK_EQUAL(g.value(), 3); CAF_CHECK_EQUAL(c.value(), 3);
CAF_MESSAGE("integer counters also support operator++");
CAF_CHECK_EQUAL(++c, 4);
CAF_MESSAGE("users can create counters with custom start values"); CAF_MESSAGE("users can create counters with custom start values");
CAF_CHECK_EQUAL(telemetry::int_counter{42}.value(), 42); CAF_CHECK_EQUAL(telemetry::int_counter{42}.value(), 42);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment