Add struct to a VariantMap

I want to pass data within an event using a struct (or similar if needed). VariantMap sent with the event seems to only accept specific classes.

Maybe use void* to save custom struct in Variant?

struct s {} a_s;
Variant v(static_cast<void*>(&a_s));
s * recv = static_cast<s*>(v.GetVoidPtr());

That’s plausible and is what I’m actually doing, but I was hoping to find another solution

Is there any valid reason why you need a custom type ?

I want to pass more than one type of data (string, image, etc) all in one structure. A struct looks (to me) as the best way.

You could use VariantMap

Also this :

Is not a good idea to use. Once your a_s goes out of scope your void* becomes invalid. you can dynamically allocate a_s but then you have to track all your structs to prevent memory leaking by deleting them manually when they become useless

I would recommend adding your data to the VariantMap directly instead of first packing it into a struct.

VariantMap data;
data["string"] = String("bla");
data["image"] = myImagePtr;
// etc
SendEvent(E_WHATEVER, data);
2 Likes

Urho has support for custom variant types, but I have not toyed with it.
Variants tend to do (almost) everything I expect, with the exception of providing compare operators for equivalent types, or even acknowledging that type equivalency is potentially possible.
https://urho3d.github.io/documentation/HEAD/class_urho3_d_1_1_custom_variant_value.html

3 Likes

As @Leith mentioned you can use CustomVariantValue. I just used it the first time and it works as follows (at least it works for JSONObject) Use MakeCustomValue (which is located in Variant.h) to envelope your object:

put:

        VariantMap map;
        map[P_DATA]=MakeCustomValue(yourvalue);

And use GetCustom() to get it out again…

get:


    // eventData is the incoming VariantMap
    JSONObject data  =  eventData[P_DATA].GetCustom<YOURTYPE>();

Hope that helps

3 Likes