sdbus++: interface: add non-throwing enum conversion

Add a variant of the enum conversion function that returns
a std::optional instead of throwing.  In the future this
will be used in the tuple-parsing to handle tuples containing
multiple enumerations.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I2325e1dd7299baedf24e78f6ca65ad16df9d1d32
diff --git a/tools/sdbusplus/templates/interface.server.cpp.mako b/tools/sdbusplus/templates/interface.server.cpp.mako
index ddb7537..202d409 100644
--- a/tools/sdbusplus/templates/interface.server.cpp.mako
+++ b/tools/sdbusplus/templates/interface.server.cpp.mako
@@ -104,8 +104,8 @@
 
 } // anonymous namespace
 
-auto ${classname}::convert${e.name}FromString(const std::string& s) ->
-        ${e.name}
+auto ${classname}::convertStringTo${e.name}(const std::string& s) noexcept ->
+        std::optional<${e.name}>
 {
     auto i = std::find_if(
             std::begin(mapping${classname}${e.name}),
@@ -113,7 +113,7 @@
             [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
     if (std::end(mapping${classname}${e.name}) == i)
     {
-        throw sdbusplus::exception::InvalidEnumString();
+        return std::nullopt;
     }
     else
     {
@@ -121,6 +121,21 @@
     }
 }
 
+auto ${classname}::convert${e.name}FromString(const std::string& s) ->
+        ${e.name}
+{
+    auto r = convertStringTo${e.name}(s);
+
+    if (!r)
+    {
+        throw sdbusplus::exception::InvalidEnumString();
+    }
+    else
+    {
+        return *r;
+    }
+}
+
 std::string ${classname}::convert${e.name}ToString(${classname}::${e.name} v)
 {
     auto i = std::find_if(
diff --git a/tools/sdbusplus/templates/interface.server.hpp.mako b/tools/sdbusplus/templates/interface.server.hpp.mako
index 0fc1849..db8d110 100644
--- a/tools/sdbusplus/templates/interface.server.hpp.mako
+++ b/tools/sdbusplus/templates/interface.server.hpp.mako
@@ -1,6 +1,7 @@
 #pragma once
 #include <limits>
 #include <map>
+#include <optional>
 #include <sdbusplus/sdbus.hpp>
 #include <sdbusplus/server.hpp>
 #include <sdbusplus/utility/dedup_variant.hpp>
@@ -122,9 +123,19 @@
          *  @param[in] s - The string to convert in the form of
          *                 "${interface.name}.<value name>"
          *  @return - The enum value.
+         *
+         *  @note Throws if string is not a valid mapping.
          */
         static ${e.name} convert${e.name}FromString(const std::string& s);
 
+        /** @brief Convert a string to an appropriate enum value.
+         *  @param[in] s - The string to convert in the form of
+         *                 "${interface.name}.<value name>"
+         *  @return - The enum value or std::nullopt
+         */
+        static std::optional<${e.name}> convertStringTo${e.name}(
+                const std::string& s) noexcept;
+
         /** @brief Convert an enum value to a string.
          *  @param[in] e - The enum to convert to a string.
          *  @return - The string conversion in the form of