Skip to content

Commit 296a013

Browse files
authored
Use traits to reduce code (#65)
1 parent 6ba8891 commit 296a013

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/VariantVisitorAdd.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,21 @@ namespace monitoring
77
class VariantVisitorAdd : public boost::static_visitor<boost::variant<int, std::string, double, uint64_t>>
88
{
99
public:
10-
/// Overloads operator() to avoid operating on strings
11-
/// \throws MonitoringInternalException
12-
double operator()(const std::string&, const std::string&) const {
13-
throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on string values");
14-
}
15-
16-
/// Calculates rate only when two arguments of the same type are passed
17-
/// \return calculated rate in Hz
18-
int operator()(const int& a, const int& b) const {
19-
return a + b;
20-
}
21-
22-
double operator()(const double& a, const double& b) const {
23-
return a + b;
24-
}
2510

26-
uint64_t operator()(const uint64_t& a, const uint64_t& b) const {
11+
/// Overloads operator() that sums numeric values
12+
template<
13+
typename T,
14+
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
15+
>
16+
T operator()(const T& a, const T& b) const {
2717
return a + b;
2818
}
2919

3020
/// If arguments have different type an exception is raised
3121
/// \throws MonitoringInternalException
3222
template<typename T, typename U>
3323
double operator()(const T&, const U&) const {
34-
throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on different types");
24+
throw MonitoringInternalException("DerivedMetrics/Visitor", "Cannot operate on different or non-numeric types");
3525
}
3626
};
3727

src/VariantVisitorRate.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ class VariantVisitorRate : public boost::static_visitor<boost::variant<int, std:
1616
VariantVisitorRate(int count) : timestampCount(count) {
1717
}
1818

19-
/// Overloads operator() to avoid operating on strings
20-
/// \throws MonitoringInternalException
21-
double operator()(const std::string&, const std::string&) const {
22-
throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on string values");
23-
}
24-
2519
/// Calculates rate only when two arguments of the same type are passed
2620
/// \return calculated rate in Hz
27-
template<typename T>
21+
template<
22+
typename T,
23+
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
24+
>
2825
double operator()(const T& a, const T& b) const {
2926
return (1000*(static_cast<double>(a) - b)) / timestampCount;
3027
}
@@ -33,7 +30,7 @@ class VariantVisitorRate : public boost::static_visitor<boost::variant<int, std:
3330
/// \throws MonitoringInternalException
3431
template<typename T, typename U>
3532
double operator()(const T&, const U&) const {
36-
throw MonitoringInternalException("DerivedMetrics/VariantRateVisitor", "Cannot operate on different types");
33+
throw MonitoringInternalException("DerivedMetrics/Visitor", "Cannot operate on different or non-numeric types");
3734
}
3835
};
3936

test/testDerived.cxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Monitoring/DerivedMetrics.h"
22
#include "../src/Exceptions/MonitoringInternalException.h"
3+
#include "../src/VariantVisitorAdd.h"
34
#include <chrono>
45
#include <thread>
56
#include <vector>
@@ -142,6 +143,19 @@ BOOST_AUTO_TEST_CASE(derivedIncrementDouble) {
142143
}
143144
}
144145

146+
BOOST_AUTO_TEST_CASE(testBoostVisitor) {
147+
{
148+
boost::variant<int, std::string, double, uint64_t> a = 10;
149+
boost::variant<int, std::string, double, uint64_t> b = 11;
150+
auto value = boost::apply_visitor(VariantVisitorAdd(), a, b);
151+
}
152+
{
153+
boost::variant<int, std::string, double, uint64_t> a = 10;
154+
boost::variant<int, std::string, double, uint64_t> b = 10.10;
155+
BOOST_CHECK_THROW(boost::apply_visitor(VariantVisitorAdd(), a, b), o2::monitoring::MonitoringInternalException);
156+
}
157+
}
158+
145159
} // namespace Test
146160
} // namespace monitoring
147161
} // namespace o2

0 commit comments

Comments
 (0)