diff --git a/modules/juce_gui_basics/components/juce_ModalComponentManager.h b/modules/juce_gui_basics/components/juce_ModalComponentManager.h index 0d9df70dcf..412e0c9312 100644 --- a/modules/juce_gui_basics/components/juce_ModalComponentManager.h +++ b/modules/juce_gui_basics/components/juce_ModalComponentManager.h @@ -159,6 +159,15 @@ private: class ModalCallbackFunction { public: + /** This is a utility function to create a ModalComponentManager::Callback that will + call a lambda function. + The lambda that you supply must take an integer parameter, which is the result code that + was returned when the modal component was dismissed. + + @see ModalComponentManager::Callback + */ + static ModalComponentManager::Callback* create (std::function); + //============================================================================== /** This is a utility function to create a ModalComponentManager::Callback that will call a static function with a parameter. @@ -185,18 +194,9 @@ public: static ModalComponentManager::Callback* create (void (*functionToCall) (int, ParamType), ParamType parameterValue) { - return new FunctionCaller1 (functionToCall, parameterValue); + return create ([=] (int r) { functionToCall (r, parameterValue); }); } - /** This is a utility function to create a ModalComponentManager::Callback that will - call a lambda function. - The lambda that you supply must take an integer parameter, which is the result code that - was returned when the modal component was dismissed. - - @see ModalComponentManager::Callback - */ - static ModalComponentManager::Callback* create (std::function); - //============================================================================== /** This is a utility function to create a ModalComponentManager::Callback that will call a static function with two custom parameters. @@ -224,7 +224,7 @@ public: ParamType1 parameterValue1, ParamType2 parameterValue2) { - return new FunctionCaller2 (functionToCall, parameterValue1, parameterValue2); + return create ([=] (int r) { functionToCall (r, parameterValue1, parameterValue2); }); } //============================================================================== @@ -254,7 +254,8 @@ public: static ModalComponentManager::Callback* forComponent (void (*functionToCall) (int, ComponentType*), ComponentType* component) { - return new ComponentCaller1 (functionToCall, component); + WeakReference comp (component); + return create ([=] (int r) { functionToCall (r, static_cast (comp.get())); }); } //============================================================================== @@ -285,90 +286,13 @@ public: ComponentType* component, ParamType param) { - return new ComponentCaller2 (functionToCall, component, param); + WeakReference comp (component); + return create ([=] (int r) { functionToCall (r, static_cast (comp.get()), param); }); } private: - //============================================================================== - template - struct FunctionCaller1 : public ModalComponentManager::Callback - { - typedef void (*FunctionType) (int, ParamType); - - FunctionCaller1 (FunctionType& f, ParamType& p1) - : function (f), param (p1) {} - - void modalStateFinished (int returnValue) override { function (returnValue, param); } - - private: - const FunctionType function; - ParamType param; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller1) - }; - - template - struct FunctionCaller2 : public ModalComponentManager::Callback - { - typedef void (*FunctionType) (int, ParamType1, ParamType2); - - FunctionCaller2 (FunctionType& f, ParamType1& p1, ParamType2& p2) - : function (f), param1 (p1), param2 (p2) {} - - void modalStateFinished (int returnValue) override { function (returnValue, param1, param2); } - - private: - const FunctionType function; - ParamType1 param1; - ParamType2 param2; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FunctionCaller2) - }; - - template - struct ComponentCaller1 : public ModalComponentManager::Callback - { - typedef void (*FunctionType) (int, ComponentType*); - - ComponentCaller1 (FunctionType& f, ComponentType* c) - : function (f), comp (c) {} - - void modalStateFinished (int returnValue) override - { - function (returnValue, static_cast (comp.get())); - } - - private: - const FunctionType function; - WeakReference comp; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller1) - }; - - template - struct ComponentCaller2 : public ModalComponentManager::Callback - { - typedef void (*FunctionType) (int, ComponentType*, ParamType1); - - ComponentCaller2 (FunctionType& f, ComponentType* c, ParamType1 p1) - : function (f), comp (c), param1 (p1) {} - - void modalStateFinished (int returnValue) override - { - function (returnValue, static_cast (comp.get()), param1); - } - - private: - const FunctionType function; - WeakReference comp; - ParamType1 param1; - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller2) - }; - - ModalCallbackFunction(); - ~ModalCallbackFunction(); - JUCE_DECLARE_NON_COPYABLE (ModalCallbackFunction) + ModalCallbackFunction() = delete; + ~ModalCallbackFunction() = delete; }; } // namespace juce