Skip to content

Commit

Permalink
Use traits to reduce code (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Aug 23, 2018
1 parent 6ba8891 commit 296a013
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
24 changes: 7 additions & 17 deletions src/VariantVisitorAdd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@ namespace monitoring
class VariantVisitorAdd : public boost::static_visitor<boost::variant<int, std::string, double, uint64_t>>
{
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<std::is_arithmetic<T>::value, T>::type
>
T operator()(const T& a, const T& b) const {
return a + b;
}

/// If arguments have different type an exception is raised
/// \throws MonitoringInternalException
template<typename T, typename U>
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");
}
};

Expand Down
13 changes: 5 additions & 8 deletions src/VariantVisitorRate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ class VariantVisitorRate : public boost::static_visitor<boost::variant<int, std:
VariantVisitorRate(int count) : timestampCount(count) {
}

/// 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
template<typename T>
template<
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
>
double operator()(const T& a, const T& b) const {
return (1000*(static_cast<double>(a) - b)) / timestampCount;
}
Expand All @@ -33,7 +30,7 @@ class VariantVisitorRate : public boost::static_visitor<boost::variant<int, std:
/// \throws MonitoringInternalException
template<typename T, typename U>
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");
}
};

Expand Down
14 changes: 14 additions & 0 deletions test/testDerived.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Monitoring/DerivedMetrics.h"
#include "../src/Exceptions/MonitoringInternalException.h"
#include "../src/VariantVisitorAdd.h"
#include <chrono>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -142,6 +143,19 @@ BOOST_AUTO_TEST_CASE(derivedIncrementDouble) {
}
}

BOOST_AUTO_TEST_CASE(testBoostVisitor) {
{
boost::variant<int, std::string, double, uint64_t> a = 10;
boost::variant<int, std::string, double, uint64_t> b = 11;
auto value = boost::apply_visitor(VariantVisitorAdd(), a, b);
}
{
boost::variant<int, std::string, double, uint64_t> a = 10;
boost::variant<int, std::string, double, uint64_t> b = 10.10;
BOOST_CHECK_THROW(boost::apply_visitor(VariantVisitorAdd(), a, b), o2::monitoring::MonitoringInternalException);
}
}

} // namespace Test
} // namespace monitoring
} // namespace o2

0 comments on commit 296a013

Please sign in to comment.