From 9b959bd2234354414895c437d53ca29a9d69804d Mon Sep 17 00:00:00 2001 From: Oliver James Date: Fri, 15 Nov 2024 20:00:41 +0000 Subject: [PATCH] AudioProcessor: Use std::optional in TrackProperties --- BREAKING_CHANGES.md | 19 +++++++++++++++++++ examples/Plugins/AudioPluginDemo.h | 4 ++-- .../juce_audio_plugin_client_AAX.cpp | 2 +- .../juce_audio_plugin_client_AU_1.mm | 2 +- .../juce_audio_plugin_client_AUv3.mm | 2 +- .../juce_audio_plugin_client_VST3.cpp | 6 +++--- .../juce_AudioUnitPluginFormat.mm | 4 ++-- .../format_types/juce_VST3PluginFormat.cpp | 16 +++++++++++----- .../processors/juce_AudioProcessor.h | 4 ++-- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 7025879b1e..403b6bace7 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -24,6 +24,25 @@ The previous behavior, where hasStreamFinished never returned true, was incorrect. This update ensures the method works as intended. +## Change + +AudioProcessor::TrackProperties now uses std::optional. + +**Possible Issues** + +Code that accessed TrackProperties properties directly will no longer compile. + +**Workaround** + +Use std::optional::has_value() to check if a property is set. Or Access the +property value safely using std::optional::value() or operator*. + +**Rationale** + +Previously, it was not possible to distinguish whether a TrackProperty was +explicitly set or if the default value was being used. + + ## Change Support for Arm32 in Projucer has been removed for Windows targets. diff --git a/examples/Plugins/AudioPluginDemo.h b/examples/Plugins/AudioPluginDemo.h index c1f95821ab..888660fa01 100644 --- a/examples/Plugins/AudioPluginDemo.h +++ b/examples/Plugins/AudioPluginDemo.h @@ -470,8 +470,8 @@ private: auto trackColour = getProcessor().getTrackProperties().colour; auto& lf = getLookAndFeel(); - backgroundColour = (trackColour == Colour() ? lf.findColour (ResizableWindow::backgroundColourId) - : trackColour.withAlpha (1.0f).withBrightness (0.266f)); + backgroundColour = (trackColour.has_value() ? trackColour->withAlpha (1.0f).withBrightness (0.266f) + : lf.findColour (ResizableWindow::backgroundColourId)); repaint(); } diff --git a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AAX.cpp b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AAX.cpp index ade6f87bd9..122fce805f 100644 --- a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AAX.cpp +++ b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AAX.cpp @@ -1322,7 +1322,7 @@ namespace AAXClasses if (data != nullptr) { AudioProcessor::TrackProperties props; - props.name = String::fromUTF8 (static_cast (data)->Get()); + props.name = std::make_optional (String::fromUTF8 (static_cast (data)->Get())); pluginInstance->updateTrackProperties (props); } diff --git a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AU_1.mm b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AU_1.mm index 809b173cae..09021d7de1 100644 --- a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AU_1.mm +++ b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AU_1.mm @@ -2589,7 +2589,7 @@ private: && juceFilter != nullptr && GetContextName() != nullptr) { AudioProcessor::TrackProperties props; - props.name = String::fromCFString (GetContextName()); + props.name = std::make_optional (String::fromCFString (GetContextName())); juceFilter->updateTrackProperties (props); } diff --git a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm index ae2d7fe41f..a801de44bd 100644 --- a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm +++ b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm @@ -460,7 +460,7 @@ public: if (str != nullptr) { AudioProcessor::TrackProperties props; - props.name = nsStringToJuce (str); + props.name = std::make_optional (nsStringToJuce (str)); getAudioProcessor().updateTrackProperties (props); } diff --git a/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp b/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp index 55fe1004a5..c095690e10 100644 --- a/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp +++ b/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp @@ -1171,14 +1171,14 @@ public: { Vst::String128 channelName; if (list->getString (Vst::ChannelContext::kChannelNameKey, channelName, sizeof (channelName)) == kResultTrue) - trackProperties.name = toString (channelName); + trackProperties.name = std::make_optional (toString (channelName)); } { Steinberg::int64 colour; if (list->getInt (Vst::ChannelContext::kChannelColorKey, colour) == kResultTrue) - trackProperties.colour = Colour (Vst::ChannelContext::GetRed ((uint32) colour), Vst::ChannelContext::GetGreen ((uint32) colour), - Vst::ChannelContext::GetBlue ((uint32) colour), Vst::ChannelContext::GetAlpha ((uint32) colour)); + trackProperties.colour = std::make_optional (Colour (Vst::ChannelContext::GetRed ((uint32) colour), Vst::ChannelContext::GetGreen ((uint32) colour), + Vst::ChannelContext::GetBlue ((uint32) colour), Vst::ChannelContext::GetAlpha ((uint32) colour))); } diff --git a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm index 1008231afc..0238deb33e 100644 --- a/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm +++ b/modules/juce_audio_processors/format_types/juce_AudioUnitPluginFormat.mm @@ -1612,9 +1612,9 @@ public: //============================================================================== void updateTrackProperties (const TrackProperties& properties) override { - if (properties.name.isNotEmpty()) + if (properties.name.has_value()) { - CFObjectHolder contextName { properties.name.toCFString() }; + CFObjectHolder contextName { properties.name->toCFString() }; AudioUnitSetProperty (audioUnit, kAudioUnitProperty_ContextName, kAudioUnitScope_Global, 0, &contextName.object, sizeof (contextName.object)); } diff --git a/modules/juce_audio_processors/format_types/juce_VST3PluginFormat.cpp b/modules/juce_audio_processors/format_types/juce_VST3PluginFormat.cpp index 2aff5e32da..0abb77a962 100644 --- a/modules/juce_audio_processors/format_types/juce_VST3PluginFormat.cpp +++ b/modules/juce_audio_processors/format_types/juce_VST3PluginFormat.cpp @@ -3142,8 +3142,11 @@ public: { if (! std::strcmp (id, Vst::ChannelContext::kChannelNameKey)) { - Steinberg::String str (props.name.toRawUTF8()); - str.copyTo (string, 0, (Steinberg::int32) jmin (size, (Steinberg::uint32) std::numeric_limits::max())); + if (props.name.has_value()) + { + Steinberg::String str (props.name->toRawUTF8()); + str.copyTo (string, 0, (Steinberg::int32) jmin (size, (Steinberg::uint32) std::numeric_limits::max())); + } return kResultTrue; } @@ -3153,9 +3156,12 @@ public: tresult PLUGIN_API getInt (AttrID id, Steinberg::int64& value) override { - if (! std::strcmp (Vst::ChannelContext::kChannelNameLengthKey, id)) value = props.name.length(); - else if (! std::strcmp (Vst::ChannelContext::kChannelColorKey, id)) value = static_cast (props.colour.getARGB()); - else return kResultFalse; + if (! std::strcmp (Vst::ChannelContext::kChannelNameLengthKey, id)) + value = props.name.value_or (String{}).length(); + else if (! std::strcmp (Vst::ChannelContext::kChannelColorKey, id)) + value = static_cast (props.colour.value_or (Colours::transparentBlack).getARGB()); + else + return kResultFalse; return kResultTrue; } diff --git a/modules/juce_audio_processors/processors/juce_AudioProcessor.h b/modules/juce_audio_processors/processors/juce_AudioProcessor.h index bd8ce8556a..63f1ce52f6 100644 --- a/modules/juce_audio_processors/processors/juce_AudioProcessor.h +++ b/modules/juce_audio_processors/processors/juce_AudioProcessor.h @@ -1305,8 +1305,8 @@ public: AudioProcessor is loaded. */ struct TrackProperties { - String name; // The name of the track - this will be empty if the track name is not known - Colour colour; // The colour of the track - this will be transparentBlack if the colour is not known + std::optional name; // The name of the track - this will be empty if the track name is not known + std::optional colour; // The colour of the track - this will be empty if the colour is not known // other properties may be added in the future };