onChange = [this](){
case IdComboBox::Save:
{
mPresetManager.setStateCopied(file.withFileExtension("xml"));
break;
}
}
void MyPresetManager::setStateCopied(const juce::File& inFile) noexcept
{
const juce::ValueTree state = mParameters.getStateCopied();
std::unique_ptr<juce::XmlElement> xml = state.createXml();
}
const juce::ValueTree MyParameters::getStateCopied() const noexcept
{
return mApvts.copyState();
}
juce::ValueTree& mApvts.copyState()
copyState() returns a copied ValueTree by value, so receiving it as a reference would bind to a temporary object. Referencing a temporary object is unsafe because its lifetime is not guaranteed.
setStateCopied(const juce::File& inFile)
{
if(xml == nullptr)
{
return;
}
}
setStateCopied(const juce::File& inFile)
{
if(xml->writeTo(inFile)==false)
{
return;
}
}
[1] User selects "Save" from the combo box (id == IdComboBox::Save)
↓
[2] onChange lambda triggers
↓
[3] launchAsync callback is called with FileChooser result
↓
[4] case IdComboBox::Save:
→ mPresetManager.setStateCopied(file)
↓
[5] MyPresetManager::setStateCopied()
→ mParameters.getStateCopied()
↓
[6] MyParameters::getStateCopied()
→ returns mApvts.copyState() → ValueTree state
↓
[7] ValueTree::createXml()
→ returns std::unique_ptr<XmlElement>
(internally uses new XmlElement)
↓
[8] Check if xml == nullptr
→ return if XML creation failed
↓
[9] xml->writeTo(inFile)
→ writes XML data to the file path
→ returns true if successful, false otherwise
↓
[10] Check if writeTo() == false
→ return if file write failed