Label getting automatically updated by Value changes but should internally use text

I want to have a Label which content gets automatically updated when a Value connected to it changed. Obviously I do the following:

label.getTextValue().referTo(my_value_of_somekind);

my_value_of_somekind should be int or double, but should still show as proper numbers in the Label. This is due to two reasons:

  1. I need to use those values in realtime parts of the software.
  2. I want to save the ValueTree where those values are into a file without them being there in text format.

How do I achieve that? I assume that Label::getTextValue() works only with text so when I actually get that value from ValueTree, it’ll do some kind of slow text to int/double conversions? How to avoid that?

Also, when I use ValueTree::setProperty to create an initial value which uses int/double, it won’t help. So what’s going on? Is there a way to ensure the Value uses internally something else than String to contain the current value?

I also tested what gets saved into the file if I don’t do that “referTo()” part at all. The result was that the saved file contained proper binary data instead of a string. I really want to keep that data as binary data also in the memory at all times.

Value and ValueTree are poor choices for use on the realtime thread. They don’t make any guarantees about what operations might cause allocations, and it’s a bad idea to allocate on the audio thread. Additionally, it’s difficult to safely synchronise access to ValueTrees from multiple threads because multiple Values might all be referencing the same shared state. If you’re accessing the ValueTree from the UI thread and the audio thread simultaneously, you’ll almost certainly end up with data races.

The label needs to display the value as text, so some sort of text<->numeric conversion is unavoidable. I think in order to keep the ValueTree entry stored as a numeric value rather than text, you’d have to write your own conversion. That is, instead of using label.getTextValue().referTo() you can use a Label::Listener to detect when the label text changes, convert it to a number, and store it into the ValueTree; and you can also listen to that Value and update the Label’s text appropriately when the Value changes.

Thanks! I ended up doing the following:

valueTreePropertyChanged() → for those occasions when the value is changed somewhere else in the software. Updates the Label’s text by converting int/double to text.

Label::onTextChange() → lambda callback when label’s text is edited by the user by typing on keyboard. Does String to int/double conversion.

The idea is that now the Value and Label are completely separated from each other. That seems to work.

I will also make sure I won’t be using Values in the realtime side. I’ll convert them into regular variables whenever those Values get changed.