Move LoadVariant into Utils
It seems useful in other daemons and is really a
utility.
Change-Id: Iebf3b0dcf5b2981c5f1832b59014772387a656b8
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/Utils.hpp b/include/Utils.hpp
index b86c8b5..f5f9341 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -42,4 +42,31 @@
// replaces limits if MinReading and MaxReading are found.
void findLimits(std::pair<double, double>& limits,
- const SensorBaseConfiguration* data);
\ No newline at end of file
+ const SensorBaseConfiguration* data);
+
+template <typename T>
+inline T loadVariant(
+ const boost::container::flat_map<std::string, BasicVariantType>& data,
+ const std::string& key)
+{
+ auto it = data.find(key);
+ if (it == data.end())
+ {
+ std::cerr << "Configuration missing " << key << "\n";
+ throw std::invalid_argument("Key Missing");
+ }
+ if constexpr (std::is_same_v<T, double>)
+ {
+ return sdbusplus::message::variant_ns::visit(VariantToDoubleVisitor(),
+ it->second);
+ }
+ else if constexpr (std::is_same_v<T, std::string>)
+ {
+ return sdbusplus::message::variant_ns::visit(VariantToStringVisitor(),
+ it->second);
+ }
+ else
+ {
+ static_assert("Type Not Implemented");
+ }
+}