CachedValue referTo not working

Hi!

I’m trying chachedvalues and the variantconverter to save data serially in a valuetree.
Here is my code:

struct Complex
{
    float re;
    float im;
};

template<>
struct VariantConverter<Complex>
{
    static Complex fromVar(const var& v)
    {
        Complex c;
        StringArray result = StringArray::fromTokens(v.toString(),";",String());
        c.re = result[0].getIntValue();
        c.im = result[1].getIntValue();
        return c;
    }
    
    static var toVar(const Complex& c)
    {
        String result;
        result += (String)c.re;
        result += ";";
        result += (String)c.im;
        return result;
    }
};

ValueTree myTree("MyValueTree");

CachedValue<Complex> complex;
complex.referTo(myTree,"ComplexNumber",nullptr);
complex->re = 10;
complex->im = 20;

DBG(myTree.toXmlString());

There is no data from the complex number printed inside the “MyValueTree” section.
It seems that the “Complex” property isn’t added to it at all.

Who can help me?

Thank you,
Jelle Bakker

Are you sure that this cast works?
result += (String)c.re;
I think what you want to do ist a conversion to a decimal String
result += String(c.re);

BTW: A var can save floats in binary form, so no need to save it as a decimal String (which always can add rounding errors), unless you want a human readable form

Thank you, good advice.
But the toVar method isn’t called at all (when I use DBG to print something).

i guess you have to use the setValue method, without there is no chance that the cachedValue notices the change

BTW: i guess the numbers are floats, so getIntValue() will round them

This is an example from a presenation from Daniel at the ADC last week.

This uses the =operator of Complex, not the
one from CachedValue

You need to do
complex = newcomplex;

I figured out the problem.
I had to create a Complex object and assign it to the cachedvalue.
I also had to overload the != operator in the Complex struct.

Thank you chkn for your help!