diff --git a/src/VariantVisitorAdd.h b/src/VariantVisitorAdd.h index 84e1734d3..77b688756 100644 --- a/src/VariantVisitorAdd.h +++ b/src/VariantVisitorAdd.h @@ -7,23 +7,13 @@ namespace monitoring class VariantVisitorAdd : public boost::static_visitor> { public: - /// Overloads operator() to avoid operating on strings - /// \throws MonitoringInternalException - double operator()(const std::string&, const std::string&) const { - throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on string values"); - } - - /// Calculates rate only when two arguments of the same type are passed - /// \return calculated rate in Hz - int operator()(const int& a, const int& b) const { - return a + b; - } - - double operator()(const double& a, const double& b) const { - return a + b; - } - uint64_t operator()(const uint64_t& a, const uint64_t& b) const { + /// Overloads operator() that sums numeric values + template< + typename T, + typename = typename std::enable_if::value, T>::type + > + T operator()(const T& a, const T& b) const { return a + b; } @@ -31,7 +21,7 @@ class VariantVisitorAdd : public boost::static_visitor double operator()(const T&, const U&) const { - throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on different types"); + throw MonitoringInternalException("DerivedMetrics/Visitor", "Cannot operate on different or non-numeric types"); } }; diff --git a/src/VariantVisitorRate.h b/src/VariantVisitorRate.h index 558758df6..7ad3d9a62 100644 --- a/src/VariantVisitorRate.h +++ b/src/VariantVisitorRate.h @@ -16,15 +16,12 @@ class VariantVisitorRate : public boost::static_visitor + template< + typename T, + typename = typename std::enable_if::value, T>::type + > double operator()(const T& a, const T& b) const { return (1000*(static_cast(a) - b)) / timestampCount; } @@ -33,7 +30,7 @@ class VariantVisitorRate : public boost::static_visitor double operator()(const T&, const U&) const { - throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on different types"); + throw MonitoringInternalException("DerivedMetrics/Visitor", "Cannot operate on different or non-numeric types"); } }; diff --git a/test/testDerived.cxx b/test/testDerived.cxx index 110f17c67..6c18fadb6 100644 --- a/test/testDerived.cxx +++ b/test/testDerived.cxx @@ -1,5 +1,6 @@ #include "Monitoring/DerivedMetrics.h" #include "../src/Exceptions/MonitoringInternalException.h" +#include "../src/VariantVisitorAdd.h" #include #include #include @@ -142,6 +143,19 @@ BOOST_AUTO_TEST_CASE(derivedIncrementDouble) { } } +BOOST_AUTO_TEST_CASE(testBoostVisitor) { + { + boost::variant a = 10; + boost::variant b = 11; + auto value = boost::apply_visitor(VariantVisitorAdd(), a, b); + } + { + boost::variant a = 10; + boost::variant b = 10.10; + BOOST_CHECK_THROW(boost::apply_visitor(VariantVisitorAdd(), a, b), o2::monitoring::MonitoringInternalException); + } +} + } // namespace Test } // namespace monitoring } // namespace o2