From a5c3b81f82577e40dcd8c07069f48f32d5869f9f Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 12 Jul 2021 11:58:29 +0100 Subject: [PATCH] Use C++14 lambda capture initialisers for initialising deletion checkers --- .../UserAccount/jucer_LoginFormComponent.h | 11 ++--- .../Source/Application/jucer_Application.cpp | 6 +-- .../Source/Application/jucer_AutoUpdater.cpp | 4 +- .../Source/Application/jucer_MainWindow.cpp | 43 +++++++++-------- .../CodeEditor/jucer_OpenDocumentManager.cpp | 7 ++- .../CodeEditor/jucer_SourceCodeEditor.cpp | 4 +- .../Properties/jucer_PositionPropertyBase.h | 5 +- .../ComponentEditor/jucer_JucerDocument.cpp | 6 +-- .../UI/Sidebar/jucer_ExporterTreeItems.h | 29 ++++++----- .../Project/UI/Sidebar/jucer_FileTreeItems.h | 4 +- .../UI/jucer_ProjectContentComponent.cpp | 6 +-- .../Projucer/Source/Project/jucer_Project.cpp | 10 ++-- .../ProjectSaving/jucer_ProjectSaver.cpp | 4 +- .../Utility/Helpers/jucer_NewFileWizard.cpp | 22 +++++---- .../format_types/juce_VSTPluginFormat.cpp | 6 +-- ..._mac_BluetoothMidiDevicePairingDialogue.mm | 8 ++-- .../components/juce_Component.cpp | 16 +++---- .../components/juce_ModalComponentManager.cpp | 2 +- .../components/juce_ModalComponentManager.h | 22 ++++++--- .../juce_gui_basics/desktop/juce_Desktop.cpp | 6 ++- .../layout/juce_MultiDocumentPanel.cpp | 4 +- .../native/juce_android_ContentSharer.cpp | 11 +++-- .../native/juce_android_FileChooser.cpp | 4 +- .../native/juce_win32_FileChooser.cpp | 9 ++-- .../juce_gui_basics/widgets/juce_ComboBox.cpp | 5 +- .../widgets/juce_TextEditor.cpp | 4 +- .../native/juce_win32_WebBrowserComponent.cpp | 21 ++++---- .../native/juce_android_CameraDevice.h | 48 ++++++++----------- .../juce_video/native/juce_android_Video.h | 4 +- .../juce_video/native/juce_ios_CameraDevice.h | 8 +--- .../juce_video/native/juce_mac_CameraDevice.h | 3 +- modules/juce_video/native/juce_mac_Video.h | 9 ++-- .../native/juce_win32_CameraDevice.h | 3 +- 33 files changed, 160 insertions(+), 194 deletions(-) diff --git a/extras/Projucer/Source/Application/UserAccount/jucer_LoginFormComponent.h b/extras/Projucer/Source/Application/UserAccount/jucer_LoginFormComponent.h index 9d0d001ac1..297680a6c1 100644 --- a/extras/Projucer/Source/Application/UserAccount/jucer_LoginFormComponent.h +++ b/extras/Projucer/Source/Application/UserAccount/jucer_LoginFormComponent.h @@ -213,22 +213,21 @@ private: updateLoginButtonStates (true); - WeakReference weakThis (this); - auto completionCallback = [this, weakThis] (const String& errorMessage) + auto completionCallback = [weakThis = SafePointer { this }] (const String& errorMessage) { if (weakThis == nullptr) return; - updateLoginButtonStates (false); + weakThis->updateLoginButtonStates (false); if (errorMessage.isNotEmpty()) { - showErrorMessage (errorMessage); + weakThis->showErrorMessage (errorMessage); } else { - hideErrorMessage(); - mainWindow.hideLoginFormOverlay(); + weakThis->hideErrorMessage(); + weakThis->mainWindow.hideLoginFormOverlay(); ProjucerApplication::getApp().getCommandManager().commandStatusChanged(); } }; diff --git a/extras/Projucer/Source/Application/jucer_Application.cpp b/extras/Projucer/Source/Application/jucer_Application.cpp index c59c9a1230..fe00ec4407 100644 --- a/extras/Projucer/Source/Application/jucer_Application.cpp +++ b/extras/Projucer/Source/Application/jucer_Application.cpp @@ -1113,8 +1113,7 @@ void ProjucerApplication::createNewProjectFromClipboard() return; } - WeakReference parent { this }; - openFile (tempFile, [parent, cleanup] (bool openedSuccessfully) + openFile (tempFile, [parent = WeakReference { this }, cleanup] (bool openedSuccessfully) { if (parent == nullptr) return; @@ -1172,8 +1171,7 @@ void ProjucerApplication::closeAllMainWindows (std::function callba void ProjucerApplication::closeAllMainWindowsAndQuitIfNeeded() { - WeakReference parent; - closeAllMainWindows ([parent] (bool closedSuccessfully) + closeAllMainWindows ([parent = WeakReference { this }] (bool closedSuccessfully) { #if JUCE_MAC ignoreUnused (parent, closedSuccessfully); diff --git a/extras/Projucer/Source/Application/jucer_AutoUpdater.cpp b/extras/Projucer/Source/Application/jucer_AutoUpdater.cpp index 3a3231c956..b16ed71e6a 100644 --- a/extras/Projucer/Source/Application/jucer_AutoUpdater.cpp +++ b/extras/Projucer/Source/Application/jucer_AutoUpdater.cpp @@ -346,9 +346,7 @@ void LatestVersionCheckerAndUpdater::addNotificationToOpenProjects (const Versio { if (auto* project = window->getProject()) { - Component::SafePointer safeWindow (window); - - auto ignore = [safeWindow] + auto ignore = [safeWindow = Component::SafePointer { window }] { if (safeWindow != nullptr) safeWindow->getProject()->removeProjectMessage (ProjectMessages::Ids::newVersionAvailable); diff --git a/extras/Projucer/Source/Application/jucer_MainWindow.cpp b/extras/Projucer/Source/Application/jucer_MainWindow.cpp index a6a26ac58a..7e066d5d2c 100644 --- a/extras/Projucer/Source/Application/jucer_MainWindow.cpp +++ b/extras/Projucer/Source/Application/jucer_MainWindow.cpp @@ -247,9 +247,10 @@ void MainWindow::closeCurrentProject (OpenDocumentManager::SaveIfNeeded askUserT pcc->hideEditor(); } - SafePointer parent { this }; ProjucerApplication::getApp().openDocumentManager - .closeAllDocumentsUsingProjectAsync (*currentProject, askUserToSave, [parent, askUserToSave, callback] (bool closedSuccessfully) + .closeAllDocumentsUsingProjectAsync (*currentProject, + askUserToSave, + [parent = SafePointer { this }, askUserToSave, callback] (bool closedSuccessfully) { if (parent == nullptr) return; @@ -291,8 +292,8 @@ void MainWindow::closeCurrentProject (OpenDocumentManager::SaveIfNeeded askUserT void MainWindow::moveProject (File newProjectFileToOpen, OpenInIDE openInIDE) { - SafePointer parent { this }; - closeCurrentProject (OpenDocumentManager::SaveIfNeeded::no, [parent, newProjectFileToOpen, openInIDE] (bool) + closeCurrentProject (OpenDocumentManager::SaveIfNeeded::no, + [parent = SafePointer { this }, newProjectFileToOpen, openInIDE] (bool) { if (parent == nullptr) return; @@ -360,9 +361,10 @@ void MainWindow::openFile (const File& file, std::function callback if (result.wasOk()) { - SafePointer parent { this }; - auto sharedDoc = std::make_shared> (std::move (newDoc)); - closeCurrentProject (OpenDocumentManager::SaveIfNeeded::yes, [parent, sharedDoc, callback] (bool saveResult) + closeCurrentProject (OpenDocumentManager::SaveIfNeeded::yes, + [parent = SafePointer { this }, + sharedDoc = std::make_shared> (std::move (newDoc)), + callback] (bool saveResult) { if (parent == nullptr) return; @@ -472,8 +474,7 @@ void MainWindow::openPIP (const File& pipFile, std::function callba return; } - SafePointer parent { this }; - openFile (generator->getJucerFile(), [parent, generator, callback] (bool openedSuccessfully) + openFile (generator->getJucerFile(), [parent = SafePointer { this }, generator, callback] (bool openedSuccessfully) { if (parent == nullptr) return; @@ -768,18 +769,18 @@ void MainWindowList::closeWindow (MainWindow* w) else #endif { - WeakReference parent { this }; - w->closeCurrentProject (OpenDocumentManager::SaveIfNeeded::yes, [parent, w] (bool closedSuccessfully) - { - if (parent == nullptr) - return; - - if (closedSuccessfully) - { - parent->windows.removeObject (w); - parent->saveCurrentlyOpenProjectList(); - } - }); + w->closeCurrentProject (OpenDocumentManager::SaveIfNeeded::yes, + [parent = WeakReference { this }, w] (bool closedSuccessfully) + { + if (parent == nullptr) + return; + + if (closedSuccessfully) + { + parent->windows.removeObject (w); + parent->saveCurrentlyOpenProjectList(); + } + }); } } diff --git a/extras/Projucer/Source/CodeEditor/jucer_OpenDocumentManager.cpp b/extras/Projucer/Source/CodeEditor/jucer_OpenDocumentManager.cpp index 3d74110adb..acab822956 100644 --- a/extras/Projucer/Source/CodeEditor/jucer_OpenDocumentManager.cpp +++ b/extras/Projucer/Source/CodeEditor/jucer_OpenDocumentManager.cpp @@ -176,7 +176,6 @@ void OpenDocumentManager::saveIfNeededAndUserAgrees (OpenDocumentManager::Docume return; } - WeakReference parent { this }; AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon, TRANS("Closing document..."), TRANS("Do you want to save the changes to \"") @@ -185,7 +184,7 @@ void OpenDocumentManager::saveIfNeededAndUserAgrees (OpenDocumentManager::Docume TRANS("Discard changes"), TRANS("Cancel"), nullptr, - ModalCallbackFunction::create ([parent, doc, callback] (int r) + ModalCallbackFunction::create ([parent = WeakReference { this }, doc, callback] (int r) { if (parent == nullptr) return; @@ -241,8 +240,8 @@ void OpenDocumentManager::closeDocumentAsync (Document* doc, SaveIfNeeded saveIf if (saveIfNeeded == SaveIfNeeded::yes) { - WeakReference parent { this }; - saveIfNeededAndUserAgrees (doc, [parent, doc, callback] (FileBasedDocument::SaveResult result) + saveIfNeededAndUserAgrees (doc, + [parent = WeakReference { this }, doc, callback] (FileBasedDocument::SaveResult result) { if (parent == nullptr) return; diff --git a/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp b/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp index 34621588c4..1d0c0952a2 100644 --- a/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp +++ b/extras/Projucer/Source/CodeEditor/jucer_SourceCodeEditor.cpp @@ -667,8 +667,8 @@ void CppCodeEditorComponent::insertComponentClass() asyncAlertWindow->addButton (TRANS ("Insert Code"), 1, KeyPress (KeyPress::returnKey)); asyncAlertWindow->addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey)); - SafePointer parent { this }; - asyncAlertWindow->enterModalState (true, ModalCallbackFunction::create ([parent, classNameField] (int result) + asyncAlertWindow->enterModalState (true, + ModalCallbackFunction::create ([parent = SafePointer { this }, classNameField] (int result) { if (parent == nullptr) return; diff --git a/extras/Projucer/Source/ComponentEditor/Properties/jucer_PositionPropertyBase.h b/extras/Projucer/Source/ComponentEditor/Properties/jucer_PositionPropertyBase.h index 8e21936539..66055d338c 100644 --- a/extras/Projucer/Source/ComponentEditor/Properties/jucer_PositionPropertyBase.h +++ b/extras/Projucer/Source/ComponentEditor/Properties/jucer_PositionPropertyBase.h @@ -262,10 +262,9 @@ public: m.addSubMenu ("Relative to", compLayout->getRelativeTargetMenu (component, (int) dimension)); } - SafePointer ref (this); - m.showMenuAsync (PopupMenu::Options().withTargetComponent (&button), - [ref, compLayout, callback, xAnchor, yAnchor, xMode, yMode, sizeW, sizeH, p, rpr] (int menuResult) mutable + [compLayout, callback, xAnchor, yAnchor, xMode, yMode, sizeW, sizeH, p, rpr, + ref = SafePointer { this }] (int menuResult) mutable { if (menuResult == 0 || ref == nullptr) { diff --git a/extras/Projucer/Source/ComponentEditor/jucer_JucerDocument.cpp b/extras/Projucer/Source/ComponentEditor/jucer_JucerDocument.cpp index f6f98ea534..a216c066e9 100644 --- a/extras/Projucer/Source/ComponentEditor/jucer_JucerDocument.cpp +++ b/extras/Projucer/Source/ComponentEditor/jucer_JucerDocument.cpp @@ -694,8 +694,7 @@ public: void saveAsync (std::function callback) override { - WeakReference parent { this }; - SourceCodeDocument::saveAsync ([parent, callback] (bool saveResult) + SourceCodeDocument::saveAsync ([parent = WeakReference { this }, callback] (bool saveResult) { if (parent == nullptr) return; @@ -720,8 +719,7 @@ public: if (auto* header = odm.openFile (nullptr, getFile().withFileExtension (".h"))) { - WeakReference parent { this }; - header->saveAsync ([parent, callback] (bool saveResult) + header->saveAsync ([parent = WeakReference { this }, callback] (bool saveResult) { if (parent == nullptr) return; diff --git a/extras/Projucer/Source/Project/UI/Sidebar/jucer_ExporterTreeItems.h b/extras/Projucer/Source/Project/UI/Sidebar/jucer_ExporterTreeItems.h index 267befc1b1..b19c6bdc96 100644 --- a/extras/Projucer/Source/Project/UI/Sidebar/jucer_ExporterTreeItems.h +++ b/extras/Projucer/Source/Project/UI/Sidebar/jucer_ExporterTreeItems.h @@ -78,25 +78,25 @@ public: void deleteItem() override { - WeakReference safeThis { this }; + auto resultCallback = [safeThis = WeakReference { this }] (int result) + { + if (safeThis == nullptr || result == 0) + return; + + safeThis->closeSettingsPage(); + + auto parent = safeThis->exporter->settings.getParent(); + parent.removeChild (safeThis->exporter->settings, + safeThis->project.getUndoManagerFor (parent)); + }; + AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, "Delete Exporter", "Are you sure you want to delete this export target?", "", "", nullptr, - ModalCallbackFunction::create ([safeThis] (int result) - { - if (safeThis == nullptr) - return; - - if (result == 0) - return; - - safeThis->closeSettingsPage(); - auto parent = safeThis->exporter->settings.getParent(); - parent.removeChild (safeThis->exporter->settings, safeThis->project.getUndoManagerFor (parent)); - })); + ModalCallbackFunction::create (std::move (resultCallback))); } void addSubItems() override @@ -244,14 +244,13 @@ public: void deleteItem() override { - WeakReference parent { this }; AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, "Delete Configuration", "Are you sure you want to delete this configuration?", "", "", nullptr, - ModalCallbackFunction::create ([parent] (int result) + ModalCallbackFunction::create ([parent = WeakReference { this }] (int result) { if (parent == nullptr) return; diff --git a/extras/Projucer/Source/Project/UI/Sidebar/jucer_FileTreeItems.h b/extras/Projucer/Source/Project/UI/Sidebar/jucer_FileTreeItems.h index 19c7bccc1f..ddea173612 100644 --- a/extras/Projucer/Source/Project/UI/Sidebar/jucer_FileTreeItems.h +++ b/extras/Projucer/Source/Project/UI/Sidebar/jucer_FileTreeItems.h @@ -538,14 +538,14 @@ public: if (correspondingItem.isValid()) { - WeakReference parent { this }; AlertWindow::showOkCancelBox (AlertWindow::NoIcon, "File Rename", "Do you also want to rename the corresponding file \"" + correspondingFile.getFileName() + "\" to match?", {}, {}, nullptr, - ModalCallbackFunction::create ([parent, oldFile, newFile, correspondingFile, correspondingItem] (int result) mutable + ModalCallbackFunction::create ([parent = WeakReference { this }, + oldFile, newFile, correspondingFile, correspondingItem] (int result) mutable { if (parent == nullptr || result == 0) return; diff --git a/extras/Projucer/Source/Project/UI/jucer_ProjectContentComponent.cpp b/extras/Projucer/Source/Project/UI/jucer_ProjectContentComponent.cpp index 38b6a00da2..b5750fae69 100644 --- a/extras/Projucer/Source/Project/UI/jucer_ProjectContentComponent.cpp +++ b/extras/Projucer/Source/Project/UI/jucer_ProjectContentComponent.cpp @@ -330,8 +330,7 @@ void ProjectContentComponent::saveDocumentAsync() { if (currentDocument != nullptr) { - SafePointer parent { this }; - currentDocument->saveAsync ([parent] (bool savedSuccessfully) + currentDocument->saveAsync ([parent = SafePointer { this }] (bool savedSuccessfully) { if (parent == nullptr) return; @@ -352,8 +351,7 @@ void ProjectContentComponent::saveAsAsync() { if (currentDocument != nullptr) { - SafePointer parent { this }; - currentDocument->saveAsAsync ([parent] (bool savedSuccessfully) + currentDocument->saveAsAsync ([parent = SafePointer { this }] (bool savedSuccessfully) { if (parent == nullptr) return; diff --git a/extras/Projucer/Source/Project/jucer_Project.cpp b/extras/Projucer/Source/Project/jucer_Project.cpp index e46eed5765..47b1e9ac67 100644 --- a/extras/Projucer/Source/Project/jucer_Project.cpp +++ b/extras/Projucer/Source/Project/jucer_Project.cpp @@ -725,10 +725,9 @@ void Project::saveProject (Async async, registerRecentFile (getFile()); } - WeakReference ref (this); - saver = std::make_unique (*this); - saver->save (async, exporterToSave, [ref, onCompletion] (Result result) + + saver->save (async, exporterToSave, [ref = WeakReference { this }, onCompletion] (Result result) { if (ref == nullptr) return; @@ -1040,9 +1039,8 @@ void Project::saveAndMoveTemporaryProject (bool openInIDE) // reload project from new location if (auto* window = ProjucerApplication::getApp().mainWindowList.getMainWindowForFile (getFile())) { - Component::SafePointer safeWindow (window); - - MessageManager::callAsync ([safeWindow, newDirectory, oldJucerFileName, openInIDE]() mutable + MessageManager::callAsync ([newDirectory, oldJucerFileName, openInIDE, + safeWindow = Component::SafePointer { window }]() mutable { if (safeWindow != nullptr) safeWindow->moveProject (newDirectory.getChildFile (oldJucerFileName), diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp b/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp index a779a1bdae..d18b649406 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectSaver.cpp @@ -54,8 +54,8 @@ void ProjectSaver::saveProjectAsync (ProjectExporter* exporterToSave, std::funct { jassert (saveThread == nullptr); - WeakReference ref (this); - saveThread = std::make_unique (*this, exporterToSave, [ref, onCompletion] (Result result) + saveThread = std::make_unique (*this, exporterToSave, + [ref = WeakReference { this }, onCompletion] (Result result) { if (ref == nullptr) return; diff --git a/extras/Projucer/Source/Utility/Helpers/jucer_NewFileWizard.cpp b/extras/Projucer/Source/Utility/Helpers/jucer_NewFileWizard.cpp index 5a9920a5ee..96f4f6a3a7 100644 --- a/extras/Projucer/Source/Utility/Helpers/jucer_NewFileWizard.cpp +++ b/extras/Projucer/Source/Utility/Helpers/jucer_NewFileWizard.cpp @@ -177,8 +177,7 @@ private: asyncAlertWindow->addButton (TRANS ("Create Files"), 1, KeyPress (KeyPress::returnKey)); asyncAlertWindow->addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey)); - WeakReference safeThis { this }; - asyncAlertWindow->enterModalState (true, ModalCallbackFunction::create ([safeThis, parent] (int result) + auto resultCallback = [safeThis = WeakReference { this }, parent] (int result) { if (safeThis == nullptr) return; @@ -195,21 +194,24 @@ private: if (className == build_tools::makeValidIdentifier (className, false, true, false)) { - safeThis->askUserToChooseNewFile (className + ".h", "*.h;*.cpp", parent, [safeThis, parent, className] (File newFile) - { - if (safeThis == nullptr) - return; + safeThis->askUserToChooseNewFile (className + ".h", "*.h;*.cpp", + parent, + [safeThis, parent, className] (File newFile) + { + if (safeThis == nullptr) + return; - if (newFile != File()) - safeThis->createFiles (parent, className, newFile); - }); + if (newFile != File()) + safeThis->createFiles (parent, className, newFile); + }); return; } safeThis->createNewFileInternal (parent); + }; - }), false); + asyncAlertWindow->enterModalState (true, ModalCallbackFunction::create (std::move (resultCallback)), false); } std::unique_ptr asyncAlertWindow; diff --git a/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp b/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp index 9b0c7e9b15..ebe13d330e 100644 --- a/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp +++ b/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp @@ -2938,12 +2938,10 @@ public: setScaleFactorAndDispatchMessage (peer->getPlatformScaleFactor()); #if JUCE_LINUX || JUCE_BSD - SafePointer safeThis (this); - - MessageManager::callAsync ([this, safeThis] + MessageManager::callAsync ([safeThis = SafePointer { this }] { if (safeThis != nullptr) - componentMovedOrResized (true, true); + safeThis->componentMovedOrResized (true, true); }); #else componentMovedOrResized (true, true); diff --git a/modules/juce_audio_utils/native/juce_mac_BluetoothMidiDevicePairingDialogue.mm b/modules/juce_audio_utils/native/juce_mac_BluetoothMidiDevicePairingDialogue.mm index 39f442ac82..f558db0821 100644 --- a/modules/juce_audio_utils/native/juce_mac_BluetoothMidiDevicePairingDialogue.mm +++ b/modules/juce_audio_utils/native/juce_mac_BluetoothMidiDevicePairingDialogue.mm @@ -130,12 +130,10 @@ public: static BluetoothMidiPairingWindowClass cls; window.reset (cls.createInstance()); - WeakReference safeThis (this); - - auto deletionCB = [=] + auto deletionCB = [safeThis = WeakReference { this }] { - if (auto* t = safeThis.get()) - delete t; + if (safeThis != nullptr) + delete safeThis.get(); }; callbacks.reset (new BluetoothMidiPairingWindowClass::Callbacks { std::move (exitCB), diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 927fc78b78..bb2ca32a3e 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1753,12 +1753,10 @@ void Component::exitModalState (int returnValue) } else { - WeakReference target (this); - - MessageManager::callAsync ([=] + MessageManager::callAsync ([target = WeakReference { this }, returnValue] { - if (auto* c = target.get()) - c->exitModalState (returnValue); + if (target != nullptr) + target->exitModalState (returnValue); }); } } @@ -2306,12 +2304,10 @@ void Component::internalModalInputAttempt() //============================================================================== void Component::postCommandMessage (int commandID) { - WeakReference target (this); - - MessageManager::callAsync ([=] + MessageManager::callAsync ([target = WeakReference { this }, commandID] { - if (auto* c = target.get()) - c->handleCommandMessage (commandID); + if (target != nullptr) + target->handleCommandMessage (commandID); }); } diff --git a/modules/juce_gui_basics/components/juce_ModalComponentManager.cpp b/modules/juce_gui_basics/components/juce_ModalComponentManager.cpp index d4d7266fa4..44b54642ae 100644 --- a/modules/juce_gui_basics/components/juce_ModalComponentManager.cpp +++ b/modules/juce_gui_basics/components/juce_ModalComponentManager.cpp @@ -207,7 +207,7 @@ void ModalComponentManager::handleAsyncUpdate() Component::SafePointer compToDelete (item->autoDelete ? item->component : nullptr); for (int j = item->callbacks.size(); --j >= 0;) - item->callbacks.getUnchecked(j)->modalStateFinished (item->returnValue); + item->callbacks.getUnchecked (j)->modalStateFinished (item->returnValue); compToDelete.deleteAndZero(); } diff --git a/modules/juce_gui_basics/components/juce_ModalComponentManager.h b/modules/juce_gui_basics/components/juce_ModalComponentManager.h index deee072327..ca1dde2469 100644 --- a/modules/juce_gui_basics/components/juce_ModalComponentManager.h +++ b/modules/juce_gui_basics/components/juce_ModalComponentManager.h @@ -198,7 +198,10 @@ public: static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType), ParamType parameterValue) { - return create ([=] (int r) { functionToCall (r, parameterValue); }); + return create ([functionToCall, parameterValue] (int r) + { + functionToCall (r, parameterValue); + }); } //============================================================================== @@ -228,7 +231,10 @@ public: ParamType1 parameterValue1, ParamType2 parameterValue2) { - return create ([=] (int r) { functionToCall (r, parameterValue1, parameterValue2); }); + return create ([functionToCall, parameterValue1, parameterValue2] (int r) + { + functionToCall (r, parameterValue1, parameterValue2); + }); } //============================================================================== @@ -258,8 +264,10 @@ public: static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*), ComponentType* component) { - WeakReference comp (component); - return create ([=] (int r) { functionToCall (r, static_cast (comp.get())); }); + return create ([functionToCall, comp = WeakReference { component }] (int r) + { + functionToCall (r, static_cast (comp.get())); + }); } //============================================================================== @@ -290,8 +298,10 @@ public: ComponentType* component, ParamType param) { - WeakReference comp (component); - return create ([=] (int r) { functionToCall (r, static_cast (comp.get()), param); }); + return create ([functionToCall, param, comp = WeakReference { component }] (int r) + { + functionToCall (r, static_cast (comp.get()), param); + }); } private: diff --git a/modules/juce_gui_basics/desktop/juce_Desktop.cpp b/modules/juce_gui_basics/desktop/juce_Desktop.cpp index 047bf1b15a..8dcde1b3a0 100644 --- a/modules/juce_gui_basics/desktop/juce_Desktop.cpp +++ b/modules/juce_gui_basics/desktop/juce_Desktop.cpp @@ -192,8 +192,10 @@ void Desktop::handleAsyncUpdate() { // The component may be deleted during this operation, but we'll use a SafePointer rather than a // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer). - WeakReference currentFocus (Component::getCurrentlyFocusedComponent()); - focusListeners.call ([&] (FocusChangeListener& l) { l.globalFocusChanged (currentFocus.get()); }); + focusListeners.call ([currentFocus = WeakReference { Component::getCurrentlyFocusedComponent() }] (FocusChangeListener& l) + { + l.globalFocusChanged (currentFocus.get()); + }); } //============================================================================== diff --git a/modules/juce_gui_basics/layout/juce_MultiDocumentPanel.cpp b/modules/juce_gui_basics/layout/juce_MultiDocumentPanel.cpp index 871134cd1c..bd19fd3cd4 100644 --- a/modules/juce_gui_basics/layout/juce_MultiDocumentPanel.cpp +++ b/modules/juce_gui_basics/layout/juce_MultiDocumentPanel.cpp @@ -392,8 +392,8 @@ void MultiDocumentPanel::closeDocumentAsync (Component* component, { if (checkItsOkToCloseFirst) { - SafePointer parent { this }; - tryToCloseDocumentAsync (component, [parent, component, callback] (bool closedSuccessfully) + tryToCloseDocumentAsync (component, + [parent = SafePointer { this }, component, callback] (bool closedSuccessfully) { if (parent == nullptr) return; diff --git a/modules/juce_gui_basics/native/juce_android_ContentSharer.cpp b/modules/juce_gui_basics/native/juce_android_ContentSharer.cpp index 813a2bd0e5..57c86d0e83 100644 --- a/modules/juce_gui_basics/native/juce_android_ContentSharer.cpp +++ b/modules/juce_gui_basics/native/juce_android_ContentSharer.cpp @@ -490,10 +490,10 @@ public: auto chooserIntent = LocalRef (env->CallStaticObjectMethod (AndroidIntent, AndroidIntent.createChooser, intent.get(), javaString ("Choose share target").get())); - WeakReference weakRef (this); - startAndroidActivityForResult (chooserIntent, 1003, - [weakRef] (int /*requestCode*/, int resultCode, LocalRef /*intentData*/) mutable + [weakRef = WeakReference { this }] (int /*requestCode*/, + int resultCode, + LocalRef /*intentData*/) mutable { if (weakRef != nullptr) weakRef->sharingFinished (resultCode); @@ -680,10 +680,11 @@ private: AndroidIntent.createChooser, intent.get(), javaString ("Choose share target").get())); - WeakReference weakRef (this); startAndroidActivityForResult (chooserIntent, 1003, - [weakRef] (int /*requestCode*/, int resultCode, LocalRef /*intentData*/) mutable + [weakRef = WeakReference { this }] (int /*requestCode*/, + int resultCode, + LocalRef /*intentData*/) mutable { if (weakRef != nullptr) weakRef->sharingFinished (resultCode); diff --git a/modules/juce_gui_basics/native/juce_android_FileChooser.cpp b/modules/juce_gui_basics/native/juce_android_FileChooser.cpp index fea21a2e2c..b354224e23 100644 --- a/modules/juce_gui_basics/native/juce_android_FileChooser.cpp +++ b/modules/juce_gui_basics/native/juce_android_FileChooser.cpp @@ -151,10 +151,8 @@ public: if (currentFileChooser != nullptr) { - WeakReference myself (this); - startAndroidActivityForResult (LocalRef (env->NewLocalRef (intent.get())), /*READ_REQUEST_CODE*/ 42, - [myself] (int requestCode, int resultCode, LocalRef intentData) mutable + [myself = WeakReference { this }] (int requestCode, int resultCode, LocalRef intentData) mutable { if (myself != nullptr) myself->onActivityResult (requestCode, resultCode, intentData); diff --git a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp index 8409a23dae..2970b78d6f 100644 --- a/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp +++ b/modules/juce_gui_basics/native/juce_win32_FileChooser.cpp @@ -680,12 +680,11 @@ private: } else { - Component::SafePointer safeComp (comp); - - File selectedFile (path); - MessageManager::callAsync ([safeComp, selectedFile]() mutable + MessageManager::callAsync ([safeComp = Component::SafePointer { comp }, + selectedFile = File { path }]() mutable { - safeComp->selectedFileChanged (selectedFile); + if (safeComp != nullptr) + safeComp->selectedFileChanged (selectedFile); }); } } diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp index 391b41b46f..b9ae5d3320 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp @@ -482,14 +482,11 @@ void ComboBox::showPopupIfNotActive() { menuActive = true; - SafePointer safePointer (this); - // as this method was triggered by a mouse event, the same mouse event may have // exited the modal state of other popups currently on the screen. By calling // showPopup asynchronously, we are giving the other popups a chance to properly // close themselves - MessageManager::callAsync ([safePointer]() mutable { if (safePointer != nullptr) safePointer->showPopup(); }); - + MessageManager::callAsync ([safePointer = SafePointer { this }]() mutable { if (safePointer != nullptr) safePointer->showPopup(); }); repaint(); } } diff --git a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp index ab0e699e5f..ef6b8fec1b 100644 --- a/modules/juce_gui_basics/widgets/juce_TextEditor.cpp +++ b/modules/juce_gui_basics/widgets/juce_TextEditor.cpp @@ -1875,10 +1875,8 @@ void TextEditor::mouseDown (const MouseEvent& e) menuActive = true; - SafePointer safeThis (this); - m.showMenuAsync (PopupMenu::Options(), - [safeThis] (int menuResult) + [safeThis = SafePointer { this }] (int menuResult) { if (auto* editor = safeThis.getComponent()) { diff --git a/modules/juce_gui_extra/native/juce_win32_WebBrowserComponent.cpp b/modules/juce_gui_extra/native/juce_win32_WebBrowserComponent.cpp index e2b028f37b..a192d4b593 100644 --- a/modules/juce_gui_extra/native/juce_win32_WebBrowserComponent.cpp +++ b/modules/juce_gui_extra/native/juce_win32_WebBrowserComponent.cpp @@ -631,15 +631,14 @@ private: auto options = Microsoft::WRL::Make(); - WeakReference weakThis (this); auto hr = createWebViewEnvironmentWithOptions (nullptr, userDataFolder != File() ? userDataFolder.getFullPathName().toWideCharPointer() : nullptr, options.Get(), Callback( - [this, weakThis] (HRESULT, ICoreWebView2Environment* env) -> HRESULT + [weakThis = WeakReference { this }] (HRESULT, ICoreWebView2Environment* env) -> HRESULT { if (weakThis != nullptr) - webViewEnvironment = env; + weakThis->webViewEnvironment = env; return S_OK; }).Get()); @@ -657,22 +656,22 @@ private: webViewEnvironment->CreateCoreWebView2Controller ((HWND) peer->getNativeHandle(), Callback ( - [this, weakThis] (HRESULT, ICoreWebView2Controller* controller) -> HRESULT + [weakThis = WeakReference { this }] (HRESULT, ICoreWebView2Controller* controller) -> HRESULT { if (weakThis != nullptr) { - isCreating = false; + weakThis->isCreating = false; if (controller != nullptr) { - webViewController = controller; - controller->get_CoreWebView2 (webView.resetAndGetPointerAddress()); + weakThis->webViewController = controller; + controller->get_CoreWebView2 (weakThis->webView.resetAndGetPointerAddress()); - addEventHandlers(); - componentMovedOrResized (true, true); + weakThis->addEventHandlers(); + weakThis->componentMovedOrResized (true, true); - if (webView != nullptr && urlRequest.url.isNotEmpty()) - webView->Navigate (urlRequest.url.toWideCharPointer()); + if (weakThis->webView != nullptr && weakThis->urlRequest.url.isNotEmpty()) + weakThis->webView->Navigate (weakThis->urlRequest.url.toWideCharPointer()); } } diff --git a/modules/juce_video/native/juce_android_CameraDevice.h b/modules/juce_video/native/juce_android_CameraDevice.h index f46f248fdc..a131e4467c 100644 --- a/modules/juce_video/native/juce_android_CameraDevice.h +++ b/modules/juce_video/native/juce_android_CameraDevice.h @@ -530,8 +530,8 @@ struct CameraDevice::Pimpl if (cameraOpenCallback == nullptr || scopedCameraDevice != nullptr) return; - WeakReference safeThis (this); - RuntimePermissions::request (RuntimePermissions::camera, [safeThis] (bool granted) mutable + RuntimePermissions::request (RuntimePermissions::camera, + [safeThis = WeakReference { this }] (bool granted) mutable { if (safeThis != nullptr) safeThis->continueOpenRequest (granted); @@ -1189,13 +1189,15 @@ private: env->CallVoidMethod (jImage, AndroidImage.close); - WeakReference safeThis (this); - owner.callListeners (image); // Android may take multiple pictures before it handles a request to stop. if (hasNotifiedListeners.compareAndSetBool (1, 0)) - MessageManager::callAsync ([safeThis, image]() mutable { if (safeThis != nullptr) safeThis->owner.notifyPictureTaken (image); }); + MessageManager::callAsync ([safeThis = WeakReference { this }, image]() mutable + { + if (safeThis != nullptr) + safeThis->owner.notifyPictureTaken (image); + }); } struct ImageBuffer @@ -2183,14 +2185,10 @@ private: JUCE_CAMERA_LOG ("cameraCaptureSessionConfigureFailed()"); ignoreUnused (session); - WeakReference weakRef (this); - - MessageManager::callAsync ([this, weakRef]() + MessageManager::callAsync ([weakRef = WeakReference { this }] { - if (weakRef == nullptr) - return; - - configuredCallback.captureSessionConfigured (nullptr); + if (weakRef != nullptr) + weakRef->configuredCallback.captureSessionConfigured (nullptr); }); } @@ -2218,17 +2216,18 @@ private: captureSession = GlobalRef (session); } - WeakReference weakRef (this); - - MessageManager::callAsync ([this, weakRef]() + MessageManager::callAsync ([weakRef = WeakReference { this }] { if (weakRef == nullptr) return; - stillPictureTaker.reset (new StillPictureTaker (captureSession, captureRequestBuilder, - previewCaptureRequest, handler, autoFocusMode)); + weakRef->stillPictureTaker.reset (new StillPictureTaker (weakRef->captureSession, + weakRef->captureRequestBuilder, + weakRef->previewCaptureRequest, + weakRef->handler, + weakRef->autoFocusMode)); - configuredCallback.captureSessionConfigured (this); + weakRef->configuredCallback.captureSessionConfigured (weakRef.get()); }); } @@ -2542,18 +2541,11 @@ private: cameraLensFacing (cameraLensFacingToUse), streamConfigurationMap (streamConfigurationMapToUse) { - WeakReference> weakRef (this); - - if (weakRef == nullptr) - return; - // async so that the object is fully constructed before the callback gets invoked - MessageManager::callAsync ([this, weakRef]() + MessageManager::callAsync ([weakRef = WeakReference> { this }] { - if (weakRef == nullptr) - return; - - previewDisplay.addListener (this); + if (weakRef != nullptr) + weakRef->previewDisplay.addListener (weakRef.get()); }); } diff --git a/modules/juce_video/native/juce_android_Video.h b/modules/juce_video/native/juce_android_Video.h index 42c0bc00d0..66d25cf649 100644 --- a/modules/juce_video/native/juce_android_Video.h +++ b/modules/juce_video/native/juce_android_Video.h @@ -1716,9 +1716,7 @@ private: //============================================================================== void systemVolumeChanged() { - WeakReference weakThis (this); - - MessageManager::callAsync ([weakThis]() mutable + MessageManager::callAsync ([weakThis = WeakReference { this }]() mutable { if (weakThis == nullptr) return; diff --git a/modules/juce_video/native/juce_ios_CameraDevice.h b/modules/juce_video/native/juce_ios_CameraDevice.h index cbefc19c4a..fc0de80942 100644 --- a/modules/juce_video/native/juce_ios_CameraDevice.h +++ b/modules/juce_video/native/juce_ios_CameraDevice.h @@ -407,9 +407,7 @@ private: if (error.isNotEmpty()) { - WeakReference weakRef (this); - - MessageManager::callAsync ([weakRef, error]() mutable + MessageManager::callAsync ([weakRef = WeakReference { this }, error]() mutable { if (weakRef != nullptr) weakRef->owner.cameraOpenCallback ({}, error); @@ -423,9 +421,7 @@ private: if (error.isNotEmpty()) { - WeakReference weakRef (this); - - MessageManager::callAsync ([weakRef, error]() mutable + MessageManager::callAsync ([weakRef = WeakReference { this }, error]() mutable { if (weakRef != nullptr) weakRef->owner.cameraOpenCallback ({}, error); diff --git a/modules/juce_video/native/juce_mac_CameraDevice.h b/modules/juce_video/native/juce_mac_CameraDevice.h index da3ca40ebe..cec2929da1 100644 --- a/modules/juce_video/native/juce_mac_CameraDevice.h +++ b/modules/juce_video/native/juce_mac_CameraDevice.h @@ -496,8 +496,7 @@ private: { handleImageCapture (image); - WeakReference weakRef (this); - MessageManager::callAsync ([weakRef, image]() mutable + MessageManager::callAsync ([weakRef = WeakReference { this }, image]() mutable { if (weakRef != nullptr && weakRef->pictureTakenCallback != nullptr) weakRef->pictureTakenCallback (image); diff --git a/modules/juce_video/native/juce_mac_Video.h b/modules/juce_video/native/juce_mac_Video.h index 9e82bd5bae..7d0f0ca402 100644 --- a/modules/juce_video/native/juce_mac_Video.h +++ b/modules/juce_video/native/juce_mac_Video.h @@ -435,9 +435,8 @@ private: //============================================================================== void notifyOwnerPreparationFinished (const URL& url, Result r, AVPlayer* preparedPlayer) { - WeakReference safeThis (this); - - MessageManager::callAsync ([safeThis, url, r, preparedPlayer]() mutable + MessageManager::callAsync ([url, preparedPlayer, r, + safeThis = WeakReference { this }]() mutable { if (safeThis != nullptr) safeThis->owner.playerPreparationFinished (url, r, preparedPlayer); @@ -530,9 +529,7 @@ private: void playbackReachedEndTime() { - WeakReference safeThis (this); - - MessageManager::callAsync ([safeThis]() mutable + MessageManager::callAsync ([safeThis = WeakReference { this }]() mutable { if (safeThis != nullptr) safeThis->owner.playbackReachedEndTime(); diff --git a/modules/juce_video/native/juce_win32_CameraDevice.h b/modules/juce_video/native/juce_win32_CameraDevice.h index 93727cf287..4e24313012 100644 --- a/modules/juce_video/native/juce_win32_CameraDevice.h +++ b/modules/juce_video/native/juce_win32_CameraDevice.h @@ -258,8 +258,7 @@ struct CameraDevice::Pimpl : public ChangeBroadcaster return; } - WeakReference weakRef (this); - MessageManager::callAsync ([weakRef, image]() mutable + MessageManager::callAsync ([weakRef = WeakReference { this }, image]() mutable { if (weakRef == nullptr) return;