Browse Source

Used lambdas to simplify some ModalCallbackFunction methods

tags/2021-05-28
jules 8 years ago
parent
commit
29ca9a59cf
1 changed files with 17 additions and 93 deletions
  1. +17
    -93
      modules/juce_gui_basics/components/juce_ModalComponentManager.h

+ 17
- 93
modules/juce_gui_basics/components/juce_ModalComponentManager.h View File

@@ -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<void(int)>);
//==============================================================================
/** 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<ParamType> (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<void(int)>);
//==============================================================================
/** 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<ParamType1, ParamType2> (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<ComponentType> (functionToCall, component);
WeakReference<Component> comp (component);
return create ([=] (int r) { functionToCall (r, static_cast<ComponentType*> (comp.get())); });
}
//==============================================================================
@@ -285,90 +286,13 @@ public:
ComponentType* component,
ParamType param)
{
return new ComponentCaller2<ComponentType, ParamType> (functionToCall, component, param);
WeakReference<Component> comp (component);
return create ([=] (int r) { functionToCall (r, static_cast<ComponentType*> (comp.get()), param); });
}
private:
//==============================================================================
template <typename ParamType>
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 <typename ParamType1, typename ParamType2>
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 <typename ComponentType>
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<ComponentType*> (comp.get()));
}
private:
const FunctionType function;
WeakReference<Component> comp;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller1)
};
template <typename ComponentType, typename ParamType1>
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<ComponentType*> (comp.get()), param1);
}
private:
const FunctionType function;
WeakReference<Component> comp;
ParamType1 param1;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentCaller2)
};
ModalCallbackFunction();
~ModalCallbackFunction();
JUCE_DECLARE_NON_COPYABLE (ModalCallbackFunction)
ModalCallbackFunction() = delete;
~ModalCallbackFunction() = delete;
};
} // namespace juce

Loading…
Cancel
Save