Make Value a polymorfic type?


#1

Hi,
For some reason I need to hide an index within my instances of the Value type (not to have to search a vast array to find out which of the values changed in my valueChanged callback). Well, I thought the solution would be simple and just subclassed Value and added the index parameter. But then I found out that’s not going to work because I cannot dynamic_cast the my child class to the parent Value class because there is not a single virtual function within Value and thus the type is not polymorphic. Well, is it for performance reasons or just because nobody ever needed that?

Thanks!


#2

The Value wraps a var aka variant, which can be of various types. The Value adds callbacks to react to canges using Listeners.
So if you want to figure out, what type the Value is, you can use

Value v (5);
v.getValue().isInt();    // = true
v.getValue().isObject(); // = false

Value o (new DynamicObject());
o.getValue().isInt();    // = false
o.getValue().isObject(); // = true

And many operators for var are specialised, so

int x = v.getValue();              // sets x = 5
DynamicObject* obj = v.getValue(); // sets o = nullptr
obj = o.getValue();                // retrieves the object created into o

HTH

BTW. I think this won’t work, will it?

auto x = v.getValue();  // which operator=() will be used...?

#3

Thanks for your reply but it seems you didn’t get my point. I know which type the value object contains. But that’s not the point. My listener has a vast number of values it listens to. In it’s callback it needs to decide which value has caused the callback. Now I could go through all of them and call refersToSameSourceAs() for each of them and once it returns true I know I have found the correct one. BUT I don’t want to go through all of them as that would be very ineffective. Instead I wanted to embed the index of the value in the object itself and after casting it to the child component I would be able to get that value without searching the array. But I cannot because Value is not polymorphic.


#4

Ah I see, sorry. I didn’t get it indeed.

Maybe you could use a ValueTree instead? You can put your ID alongside the property and listen to ValueTree::Listener::valueTreePropertyChanged (ValueTree & treeWhosePropertyHasChanged, const Identifier & property).