Hello all, I want to use juce::Vector3D as a property in my ValueTree. I also want to be able to access/modify the Vector3D representation in the ValueTree via a CachedValue wrapper.
To do this, I need to write a juce::VariantConverter for juce::Vector3D that converts between var and Vector3D. I simply need to store the public members (x, y, and z) of the Vector3D into some var format and be able to read that format back into a var.
So far, I have the following implementation. Does anyone have suggestions for this implementation or notice any mistakes? I may be being too crazy with my constexpr here haha. Thanks!
I implemented a similar template class to store different structures in MemoryBlock saved as var in ValueTree. In my case it’s a kind of array, so I can operate on its elements via typical operators, comparators, UndoableActions etc. It works ok.
After further investigation, here’s a better implementation. @matkatmusic offered me some help on discord by suggesting using Array instead of raw pointers. I also realized var has Array related methods so I have landed on this implementation (using C++ 17 syntax):
Do you suggest Array::set() because it checks the index for out of bounds? My instinct was to use Array::getReference() to eliminate a pass-by-value parameter copy to the newValue parameter of set(), although this optimization is probably negligible for types like float. I think I wouldn’t need to worry about gaining the bonus of out-of-bounds checking contained in set() due to the initialization of Array with templated size. But, I do see that Array::set is more concise.
This is probably unnecessary optimization on my part.