CachedValue referTo not working


#1

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


#2

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


#3

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


#4

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


#5

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


#6

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

You need to do
complex = newcomplex;


#7

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!