From 476f09f2c95409ce5a8224083b1bd64efa064ae9 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 7 Aug 2025 16:30:18 +0100 Subject: [PATCH] AudioProcessorParameter: Remove friendship with unrelated types --- .../juce_LegacyAudioParameter.cpp | 54 +++++++++---------- .../processors/juce_AudioProcessor.cpp | 6 +-- .../juce_AudioProcessorParameter.cpp | 6 +++ .../processors/juce_AudioProcessorParameter.h | 12 +++-- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/modules/juce_audio_processors/format_types/juce_LegacyAudioParameter.cpp b/modules/juce_audio_processors/format_types/juce_LegacyAudioParameter.cpp index 977206dc07..33a8751968 100644 --- a/modules/juce_audio_processors/format_types/juce_LegacyAudioParameter.cpp +++ b/modules/juce_audio_processors/format_types/juce_LegacyAudioParameter.cpp @@ -44,25 +44,25 @@ public: { processor = &audioProcessorToUse; - parameterIndex = audioParameterIndex; - jassert (parameterIndex < processor->getNumParameters()); + setParameterIndex (audioParameterIndex); + jassert (getParameterIndex() < processor->getNumParameters()); } //============================================================================== - float getValue() const override { return processor->getParameter (parameterIndex); } - void setValue (float newValue) override { processor->setParameter (parameterIndex, newValue); } - float getDefaultValue() const override { return processor->getParameterDefaultValue (parameterIndex); } - String getName (int maxLen) const override { return processor->getParameterName (parameterIndex, maxLen); } - String getLabel() const override { return processor->getParameterLabel (parameterIndex); } - int getNumSteps() const override { return processor->getParameterNumSteps (parameterIndex); } - bool isDiscrete() const override { return processor->isParameterDiscrete (parameterIndex); } + float getValue() const override { return processor->getParameter (getParameterIndex()); } + void setValue (float newValue) override { processor->setParameter (getParameterIndex(), newValue); } + float getDefaultValue() const override { return processor->getParameterDefaultValue (getParameterIndex()); } + String getName (int maxLen) const override { return processor->getParameterName (getParameterIndex(), maxLen); } + String getLabel() const override { return processor->getParameterLabel (getParameterIndex()); } + int getNumSteps() const override { return processor->getParameterNumSteps (getParameterIndex()); } + bool isDiscrete() const override { return processor->isParameterDiscrete (getParameterIndex()); } bool isBoolean() const override { return false; } - bool isOrientationInverted() const override { return processor->isParameterOrientationInverted (parameterIndex); } - bool isAutomatable() const override { return processor->isParameterAutomatable (parameterIndex); } - bool isMetaParameter() const override { return processor->isMetaParameter (parameterIndex); } - Category getCategory() const override { return processor->getParameterCategory (parameterIndex); } - String getCurrentValueAsText() const override { return processor->getParameterText (parameterIndex); } - String getParameterID() const override { return processor->getParameterID (parameterIndex); } + bool isOrientationInverted() const override { return processor->isParameterOrientationInverted (getParameterIndex()); } + bool isAutomatable() const override { return processor->isParameterAutomatable (getParameterIndex()); } + bool isMetaParameter() const override { return processor->isMetaParameter (getParameterIndex()); } + Category getCategory() const override { return processor->getParameterCategory (getParameterIndex()); } + String getCurrentValueAsText() const override { return processor->getParameterText (getParameterIndex()); } + String getParameterID() const override { return processor->getParameterID (getParameterIndex()); } //============================================================================== float getValueForText (const String&) const override @@ -85,22 +85,18 @@ public: return (dynamic_cast (param) != nullptr); } - static int getParamIndex (AudioProcessor& processor, AudioProcessorParameter* param) noexcept + static int getParamIndex (AudioProcessor& proc, AudioProcessorParameter* param) noexcept { if (auto* legacy = dynamic_cast (param)) - { - return legacy->parameterIndex; - } - else - { - auto n = processor.getNumParameters(); - jassert (n == processor.getParameters().size()); + return legacy->getParameterIndex(); - for (int i = 0; i < n; ++i) - { - if (processor.getParameters()[i] == param) - return i; - } + auto n = proc.getNumParameters(); + jassert (n == proc.getParameters().size()); + + for (int i = 0; i < n; ++i) + { + if (proc.getParameters()[i] == param) + return i; } return -1; @@ -109,7 +105,7 @@ public: static String getParamID (const AudioProcessorParameter* param, bool forceLegacyParamIDs) noexcept { if (auto* legacy = dynamic_cast (param)) - return forceLegacyParamIDs ? String (legacy->parameterIndex) : legacy->getParameterID(); + return forceLegacyParamIDs ? String (legacy->getParameterIndex()) : legacy->getParameterID(); if (auto* paramWithID = dynamic_cast (param)) { diff --git a/modules/juce_audio_processors/processors/juce_AudioProcessor.cpp b/modules/juce_audio_processors/processors/juce_AudioProcessor.cpp index d0d61136fe..0d14c6ab23 100644 --- a/modules/juce_audio_processors/processors/juce_AudioProcessor.cpp +++ b/modules/juce_audio_processors/processors/juce_AudioProcessor.cpp @@ -522,7 +522,7 @@ void AudioProcessor::addParameter (AudioProcessorParameter* param) parameterTree.addChild (std::unique_ptr (param)); param->processor = this; - param->parameterIndex = flatParameterList.size(); + param->setParameterIndex (flatParameterList.size()); flatParameterList.add (param); validateParameter (param); @@ -540,7 +540,7 @@ void AudioProcessor::addParameterGroup (std::unique_ptrprocessor = this; - p->parameterIndex = i; + p->setParameterIndex (i); validateParameter (p); } @@ -567,7 +567,7 @@ void AudioProcessor::setParameterTree (AudioProcessorParameterGroup&& newTree) { auto p = flatParameterList.getUnchecked (i); p->processor = this; - p->parameterIndex = i; + p->setParameterIndex (i); validateParameter (p); } diff --git a/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.cpp b/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.cpp index 259e9d014e..a5ed0090d3 100644 --- a/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.cpp +++ b/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.cpp @@ -44,6 +44,12 @@ AudioProcessorParameter::~AudioProcessorParameter() #endif } +void AudioProcessorParameter::setParameterIndex (int index) noexcept +{ + jassert (parameterIndex < 0 && 0 <= index); + parameterIndex = index; +} + void AudioProcessorParameter::setValueNotifyingHost (float newValue) { setValue (newValue); diff --git a/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.h b/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.h index 7978246366..426b9f2284 100644 --- a/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.h +++ b/modules/juce_audio_processors/processors/juce_AudioProcessorParameter.h @@ -262,6 +262,12 @@ public: /** Returns the index of this parameter in its parent processor's parameter list. */ int getParameterIndex() const noexcept { return parameterIndex; } + /** @internal + This should only be called by the owner of the parameter after it has been added to + a processor. Do not call this function; changing parameter indices *will* break things! + */ + void setParameterIndex (int index) noexcept; + //============================================================================== /** Returns the current value of the parameter as a String. @@ -349,11 +355,11 @@ public: /** @internal */ void sendValueChangedMessageToListeners (float newValue); + /** @internal */ + AudioProcessor* processor = nullptr; + private: //============================================================================== - friend class AudioProcessor; - friend class LegacyAudioParameter; - AudioProcessor* processor = nullptr; int parameterIndex = -1; int version = 0; CriticalSection listenerLock;