message: read: move enum exception higher
In order to better handle trying the next enumeration when
parsing variants with multiple enum types, switch the usage
of string-to-enum to the one returning a std::optional. This
requires adding the exception (for bad enum parsing) into the
higher level 'read_single' implementation for enums.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: If90e9b8a9fbbfd6988a7c18116cd014571bed973
diff --git a/include/sdbusplus/message/read.hpp b/include/sdbusplus/message/read.hpp
index 2e01ec8..92a2839 100644
--- a/include/sdbusplus/message/read.hpp
+++ b/include/sdbusplus/message/read.hpp
@@ -50,7 +50,7 @@
* template function prototype for the conversion from string functions.
*/
template <typename T>
-auto convert_from_string(const std::string&) = delete;
+auto convert_from_string(const std::string&) noexcept = delete;
/** @struct can_read_multiple
* @brief Utility to identify C++ types that may not be grouped into a
@@ -167,7 +167,12 @@
std::string value{};
sdbusplus::message::read(intf, m, value);
- t = convert_from_string<Td<T>>(value);
+ auto r = convert_from_string<Td<T>>(value);
+ if (!r)
+ {
+ throw sdbusplus::exception::InvalidEnumString();
+ }
+ t = *r;
}
};
diff --git a/tools/sdbusplus/templates/interface.server.hpp.mako b/tools/sdbusplus/templates/interface.server.hpp.mako
index db8d110..1a3d25e 100644
--- a/tools/sdbusplus/templates/interface.server.hpp.mako
+++ b/tools/sdbusplus/templates/interface.server.hpp.mako
@@ -223,9 +223,9 @@
% for e in interface.enums:
template <>
inline auto convert_from_string<${interface.cppNamespace()}::${e.name}>(
- const std::string& value)
+ const std::string& value) noexcept
{
- return ${interface.cppNamespace()}::convert${e.name}FromString(value);
+ return ${interface.cppNamespace()}::convertStringTo${e.name}(value);
}
template <>