[JUCE API] Preset ComboBox To Xml File (2)

SangHoon You·2025년 3월 27일
0

JUCE API

목록 보기
2/17

1. Calling the XML generation function

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();
}
  • User selects "Save" from the combo box, the onChange callback is triggered
  • The callback executes and passes the selected path to setStateCopied()
  • This function copies the current parameter state as a ValueTree, then converts it to an XML format using a smart pointer

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.



2. Checking for file errors

setStateCopied(const juce::File& inFile)
{
    if(xml == nullptr)
    {
        return;
    }
}
  • It checks whether the XML object was not created successfully
setStateCopied(const juce::File& inFile)
{
    if(xml->writeTo(inFile)==false)
    {
        return;
    }
}
  • It saves the contents of the XML object as an .xml file at the inFile path, and also checks whether the save operation failed.


3. Final Flow

[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 == nullptrreturn 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() == falsereturn if file write failed
profile
Audio Plugin Developer

0개의 댓글