compiler error : get_if<> with a variant enum ?

Francine Sauvage francine.sauvage at atos.net
Tue Oct 25 20:47:17 AEDT 2022


Hi Patrick,

Yes, I understood yesterday that a fix exists in the latest code.
I will try it when the devOps guy come back (tomorrow), and I' ll let you know if it solve my issue,

Kind Regards,

Francine SAUVAGE
Team Leader - R&D, BDS
Atos France
Mobile: +33 6 21 43 11 21
R&D, BDS
Avenue Jean Jaurès - France
atos.net
     
TT: [Wednesday or Thurday(2nd week of month) ] - Friday] 
OoO : October 31


-----Message d'origine-----
De : Patrick Williams <patrick at stwcx.xyz> 
Envoyé : lundi 24 octobre 2022 22:11
À : Francine Sauvage <francine.sauvage at atos.net>
Cc : openbmc at lists.ozlabs.org
Objet : Re: compiler error : get_if<> with a variant enum ?

On Mon, Oct 24, 2022 at 04:31:03PM +0000, Francine Sauvage wrote:
> Hi Patrick,
> 
> Thank you to help me !
> 
> I tried :
> variant<MyEnum, std::string>
> => what():  xyz.openbmc_project.sdbusplus.Error.InvalidEnumString:

Can you confirm you are using the latest code?  This symptom sounds like something that was fixed over a year ago now:

https://github.com/openbmc/sdbusplus/commit/a22dbf40a115d5cd133e67500afa387b317cac14

We even have test cases that verify this exact scenario:

https://github.com/openbmc/sdbusplus/blob/293c8a26901f39fa3c9a34be9b4ad530c5592f90/test/server/message_variant.cpp

> 
> " I think you could unpack a `variant<YourEnumType, std::string>` from the message (or dbus call result) and if it is your enumeration it'll be interpreted as so but if not it will fall back to the string. "
> I agree, theorically, but the "read" fails (even if callMethod is OK) :
> ----------------------------------------------------------------------
> --------------------------------------
> using FlexDbusVariantType = std::variant<
>                 sdbusplus::xyz::openbmc_project::Inventory::Item::server::FlexibleModule::ModuleTypes,  <==== here is MyEnum
>                 std::vector<std::string>, std::vector<double>, std::string,
>                 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
>                 uint16_t, uint8_t, bool
>                 >;                 

What happens if you drop the ModuleTypes out of here?  Does it run successfully, but parse as a string?

Is it possible you're missing some other type out of your variant list?

> using FlexDBusPropertiesMap = boost::container::flat_map<std::string, 
> FlexDbusVariantType>; using FlexDBusInterfacesMap =  
> boost::container::flat_map<std::string, FlexDBusPropertiesMap>; using 
> FlexManagedObjectType = std::vector<std::pair<sdbusplus::message::object_path, FlexDBusInterfacesMap>>; .......................
> 
>     std::cout << "Before calling callMethod" << std::endl;
> 
>     auto respMsg = bull::dbus::utility::callMethod(
>         bus, 
>         "xyz.openbmc_project.Inventory.Manager", 
>         root_obj, 
>         ORG_DBUS_OBJECT_MAN,
>         "GetManagedObjects");
> 
>     std::cout << "After calling callMethod" << std::endl;
> 
>     FlexManagedObjectType managed_objects;
>     
>     respMsg.read(managed_objects);  ,  <==== here it fails
> 
>     std::cout << "After reading" << std::endl;
> 
> ----------------------------------------------------------------------
> ---------------------------------------
> 
> If I don't declare MyEnum in the variant, I cannot use get_if<MyEnum> 
> If I declare MyEnum in the variant, I always get a runtime error => 
> what():  xyz.openbmc_project.sdbusplus.Error.InvalidEnumString:
> 
> If I remove MyEnum from variant, it works, but I cannot compare strictly the Enum, I need to convert.
> get_if< MyEnum > is not possible if my Enum is not in the variant,
> 
> I will be pleased if you have any suggestion,
> 
> Kind Regards,
> 
> Francine SAUVAGE
> Team Leader - R&D, BDS
> Atos France
> Mobile: +33 6 21 43 11 21
> R&D, BDS
> Avenue Jean Jaurès - France
> atos.net
>      
> TT: [Wednesday or Thurday(2nd week of month) ] - Friday] OoO : October 
> 31
> 
> 
> -----Message d'origine-----
> De : Patrick Williams <patrick at stwcx.xyz> Envoyé : vendredi 21 octobre 
> 2022 22:40 À : Francine Sauvage <francine.sauvage at atos.net> Cc : 
> openbmc at lists.ozlabs.org Objet : Re: compiler error : get_if<> with a 
> variant enum ?
> 
> On Thu, Oct 20, 2022 at 04:32:40PM +0000, Francine Sauvage wrote:
> > But, I get a runtime error as all my objects are different:
> > 
> > what():  xyz.openbmc_project.sdbusplus.Error.InvalidEnumString: An enumeration mapping was attempted for which no valid enumeration value exists.
> > 
> > Is there a way to add enum types to the variant of a ManagedObjects that potentially are NOT in the object interfaces/Properties ?
> > 
> > Or is there a way to filter the objects in ManagedObjects dbus calls ?
> 
> I'm sorry, I'm having trouble figuring out what you're trying to accomplish or where it is having trouble.
> 
> I think you could unpack a `variant<YourEnumType, std::string>` from the message (or dbus call result) and if it is your enumeration it'll be interpreted as so but if not it will fall back to the string.  You can even do `variant<Enum1, Enum2, Enum3>` and it'll turn into the correct one.
> 
> The important thing is that you need to pass all possible types into the variant when you attempt to unpack it.
> 
> Patrick Williams

--
Patrick Williams


More information about the openbmc mailing list