A call to getPlayHead()->getPosition()->getPpqPosition()
returns a Optional type
I’m not familiar with the optional structure, how do I fetch the float or double contained within?
EDIT:
thanks y’all!
that’s really an unintuitive piece in the juce code, but I’ll manage now 
Hi Mate,
This will get you the value.
*getPlayHead()->getPosition()->getPpqPosition();
Just be careful. The docs around the playhead advise to code defensively. so, wrap it around some checks to ensure the DAW isn’t giving you nonsense or not setting the playhead.
The return type of that function ( which I allready provided in my question) is optional<double>
My question is: how do I cast it to a normal double variable?
You just dereference it after making sure that it contains a value:
float x { someDefaultValue };
auto opt { someFuncReturningOptional () };
if (opt.hasValue)
x = *opt;
The docs are not great here, check the unit test code in the JUCE source to see more use cases.
3 Likes
That’s pretty much it but I think you left out the () on the hasValue method call.
You can also do:
if (opt)
x = *opt;
because Optional can cast to a boolean.
2 Likes
Correct on both – 1st cup of coffee hasn’t kicked in yet.
Strikes me as odd that the intent here was to mimic the std::optional type but not include the value() member to make things pin-compatible. I assume that the team will at some point replace this with the standard lib version now that c++17 is old news.
1 Like
It’s wrapping up a private member std::optional in the juce::Optional, but I agree with the deferencing instead of value() being a bit weird. This was a temporary work around, but I’ve no idea if they intend to deprecate it soon.
1 Like