From 1e12112baf56f9a8ec6dbf7fb409c84f35fff1e8 Mon Sep 17 00:00:00 2001 From: James Feist Date: Tue, 31 Jul 2018 11:44:09 -0700 Subject: Use visitor instead of pulling values directly Sensor thresholds pull out an int64_t and immediately assign it to a double. Use a visitor instead to avoid the intermediate and add flexibility / saftey to type readings. Tested-By: Verifed sensor list still worked. Change-Id: If49bf54ec1c0636b3549d433b86ecdbd1ea99b0d Signed-off-by: James Feist --- sensorhandler.cpp | 12 ++++++++---- utils.hpp | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/sensorhandler.cpp b/sensorhandler.cpp index ee62e17..609fdfd 100644 --- a/sensorhandler.cpp +++ b/sensorhandler.cpp @@ -619,8 +619,10 @@ void getSensorThresholds(uint8_t sensorNum, info.sensorPath, warningThreshIntf); - double warnLow = warnThresholds["WarningLow"].get(); - double warnHigh = warnThresholds["WarningHigh"].get(); + double warnLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(), + warnThresholds["WarningLow"]); + double warnHigh = mapbox::util::apply_visitor( + ipmi::VariantToDoubleVisitor(), warnThresholds["WarningHigh"]); if (warnLow != 0) { @@ -644,8 +646,10 @@ void getSensorThresholds(uint8_t sensorNum, service, info.sensorPath, criticalThreshIntf); - double critLow = critThresholds["CriticalLow"].get(); - double critHigh = critThresholds["CriticalHigh"].get(); + double critLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(), + critThresholds["CriticalLow"]); + double critHigh = mapbox::util::apply_visitor( + ipmi::VariantToDoubleVisitor(), critThresholds["CriticalHigh"]); if (critLow != 0) { diff --git a/utils.hpp b/utils.hpp index 75a3252..535ea66 100644 --- a/utils.hpp +++ b/utils.hpp @@ -208,6 +208,27 @@ ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path, InterfaceList&& interfaces); +/** @struct VariantToDoubleVisitor + * @brief Visitor to convert variants to doubles + * @details Performs a static cast on the underlying type + */ +struct VariantToDoubleVisitor +{ + template + std::enable_if_t::value, double> + operator()(const T &t) const + { + return static_cast(t); + } + + template + std::enable_if_t::value, double> + operator()(const T &t) const + { + throw std::invalid_argument("Cannot translate type to double"); + } +}; + namespace method_no_args { -- cgit v1.2.1