compiler error : get_if<> with a variant enum ?

Francine Sauvage francine.sauvage at atos.net
Tue Oct 25 03:31:03 AEDT 2022


Hi Patrick,

Thank you to help me !

I tried :
variant<MyEnum, std::string> 
=> what():  xyz.openbmc_project.sdbusplus.Error.InvalidEnumString:

" 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
                >;                 
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


More information about the openbmc mailing list