Uint8 to and from XML

I’m working on getStateInformation and setStateInformation for my plugin, and have run into a little snag when converting to and from XML:

my note velocities are uint8, and when converting back from XML in the setStateInformation method, the compiler is complaining about the conversion of juce::var to uint8 being ambiguous.

i tried doing a static_cast and that didn’t work either.

i can think of a couple of workarounds (saving my values as regular int, etc) but just wanted to check if this is something that others have encountered, and what’s the best way to deal with it?

this works:

    Identifier velocityName = sequenceName + "Velocity" + std::to_string (n);
    int vel = mainValueTree.state.getProperty (velocityName);
    sequence.parameters[n].velocity = static_cast<uint8> (vel);

but this doesn’t:

    Identifier velocityName = sequenceName + "Velocity" + std::to_string (n);
    sequence.parameters[n].velocity = static_cast<uint8> (mainValueTree.state.getProperty (velocityName));

The uint8 cast operator insn’t implemented in the var class. You probably need to cast to int then cast that to uint8 (which is effectively what happens in your first example).

I guess it depends on what else you’re doing but maybe provide accessors to your mainValueTree object’s properties (instread of having the ValueTree as a public member?). Then you can hide away the nasty double cast in the short accessor functions.

thanks for the clarification, Martin.

i think i’ll just leave the double cast for the moment.