Changed dbus add report interface
- metric parameters now take single sensor instead of list
- added interface support for new operation types
Tested:
- All telemetry tests are passing.
Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Change-Id: Id3a41c48e81a287e7d205ae1c747daa36d4cdb29
diff --git a/src/utils/conversion.hpp b/src/utils/conversion.hpp
index 7db4be8..5e8ef81 100644
--- a/src/utils/conversion.hpp
+++ b/src/utils/conversion.hpp
@@ -1,18 +1,57 @@
#pragma once
+#include <algorithm>
+#include <array>
#include <stdexcept>
+#include <string>
namespace utils
{
template <class T, T first, T last>
-inline T toEnum(int x)
+inline T toEnum(std::underlying_type_t<T> x)
{
- if (x < static_cast<decltype(x)>(first) ||
- x > static_cast<decltype(x)>(last))
+ if (x < static_cast<std::underlying_type_t<T>>(first) ||
+ x > static_cast<std::underlying_type_t<T>>(last))
{
throw std::out_of_range("Value is not in range of enum");
}
return static_cast<T>(x);
}
+
+template <class T>
+inline std::underlying_type_t<T> toUnderlying(T value)
+{
+ return static_cast<std::underlying_type_t<T>>(value);
+}
+
+template <class T, size_t N>
+inline T stringToEnum(const std::array<std::pair<std::string_view, T>, N>& data,
+ const std::string& value)
+{
+ auto it = std::find_if(
+ std::begin(data), std::end(data),
+ [&value](const auto& item) { return item.first == value; });
+ if (it == std::end(data))
+ {
+ throw std::out_of_range("Value is not in range of enum");
+ }
+ return it->second;
+}
+
+template <class T, size_t N>
+inline std::string_view
+ enumToString(const std::array<std::pair<std::string_view, T>, N>& data,
+ T value)
+{
+ auto it = std::find_if(
+ std::begin(data), std::end(data),
+ [value](const auto& item) { return item.second == value; });
+ if (it == std::end(data))
+ {
+ throw std::out_of_range("Value is not in range of enum");
+ }
+ return it->first;
+}
+
} // namespace utils