Avoid making the controller take a reference on the editor

This commit is contained in:
Jean Pierre Cimalando 2020-12-11 07:52:19 +01:00
parent 66dec90846
commit 219babcb7a
2 changed files with 17 additions and 16 deletions

View file

@ -139,14 +139,14 @@ IPlugView* PLUGIN_API SfizzVstController::createView(FIDString _name)
if (name != Vst::ViewType::kEditor) if (name != Vst::ViewType::kEditor)
return nullptr; return nullptr;
if (_editor) { if (IPtr<SfizzVstEditor> editor = _editor.lock()) {
withStateLock([this]() { withStateLock([this, editor]() {
_uiState = _editor->getCurrentUiState(); _uiState = editor->getCurrentUiState();
}); });
} }
SfizzVstEditor* editor = new SfizzVstEditor(this); IPtr<SfizzVstEditor> editor = Steinberg::owned(new SfizzVstEditor(this));
_editor = Steinberg::owned(editor); _editor = editor->getWeakPtr();
withStateLock([this, editor]() { withStateLock([this, editor]() {
editor->updateState(_state); editor->updateState(_state);
@ -209,14 +209,14 @@ tresult PLUGIN_API SfizzVstController::setParamNormalized(Vst::ParamID tag, Vst:
if (slotF32 && *slotF32 != value) { if (slotF32 && *slotF32 != value) {
withStateLock([this, slotF32, value]() { withStateLock([this, slotF32, value]() {
*slotF32 = value; *slotF32 = value;
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateState(_state); editor->updateState(_state);
}); });
} }
else if (slotI32 && *slotI32 != (int32)value) { else if (slotI32 && *slotI32 != (int32)value) {
withStateLock([this, slotI32, value]() { withStateLock([this, slotI32, value]() {
*slotI32 = (int32)value; *slotI32 = (int32)value;
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateState(_state); editor->updateState(_state);
}); });
} }
@ -234,7 +234,7 @@ tresult PLUGIN_API SfizzVstController::setState(IBStream* stream)
withStateLock([this, &s]() { withStateLock([this, &s]() {
_uiState = s; _uiState = s;
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateUiState(_uiState); editor->updateUiState(_uiState);
}); });
@ -246,8 +246,8 @@ tresult PLUGIN_API SfizzVstController::getState(IBStream* stream)
tresult result; tresult result;
withStateLock([this, stream, &result]() { withStateLock([this, stream, &result]() {
if (_editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
_uiState = _editor->getCurrentUiState(); _uiState = editor->getCurrentUiState();
result = _uiState.store(stream); result = _uiState.store(stream);
}); });
@ -272,7 +272,7 @@ tresult PLUGIN_API SfizzVstController::setComponentState(IBStream* stream)
withStateLock([this, &s]() { withStateLock([this, &s]() {
_state = s; _state = s;
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateState(_state); editor->updateState(_state);
}); });
@ -300,7 +300,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message)
withStateLock([this, data, size]() { withStateLock([this, data, size]() {
_state.sfzFile.assign(static_cast<const char *>(data), size); _state.sfzFile.assign(static_cast<const char *>(data), size);
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateState(_state); editor->updateState(_state);
}); });
} }
@ -314,7 +314,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message)
withStateLock([this, data, size]() { withStateLock([this, data, size]() {
_state.scalaFile.assign(static_cast<const char *>(data), size); _state.scalaFile.assign(static_cast<const char *>(data), size);
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updateState(_state); editor->updateState(_state);
}); });
} }
@ -328,7 +328,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message)
withStateLock([this, data]() { withStateLock([this, data]() {
_playState = *static_cast<const SfizzPlayState*>(data); _playState = *static_cast<const SfizzPlayState*>(data);
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->updatePlayState(_playState); editor->updatePlayState(_playState);
}); });
} }
@ -340,7 +340,7 @@ tresult SfizzVstController::notify(Vst::IMessage* message)
if (result != kResultTrue) if (result != kResultTrue)
return result; return result;
if (SfizzVstEditor* editor = _editor) if (IPtr<SfizzVstEditor> editor = _editor.lock())
editor->receiveMessage(data, size); editor->receiveMessage(data, size);
} }

View file

@ -9,6 +9,7 @@
#include "public.sdk/source/vst/vsteditcontroller.h" #include "public.sdk/source/vst/vsteditcontroller.h"
#include "public.sdk/source/vst/vstparameters.h" #include "public.sdk/source/vst/vstparameters.h"
#include "vstgui/plugin-bindings/vst3editor.h" #include "vstgui/plugin-bindings/vst3editor.h"
#include "WeakPtr.h"
#include <sfizz_message.h> #include <sfizz_message.h>
#include <mutex> #include <mutex>
class SfizzVstState; class SfizzVstState;
@ -66,5 +67,5 @@ private:
SfizzVstState _state {}; SfizzVstState _state {};
SfizzUiState _uiState {}; // updated on UI open/close/state-request SfizzUiState _uiState {}; // updated on UI open/close/state-request
SfizzPlayState _playState {}; SfizzPlayState _playState {};
Steinberg::IPtr<SfizzVstEditor> _editor; WeakPtr<SfizzVstEditor> _editor;
}; };