mirror of
https://github.com/cookiengineer/audacity
synced 2026-03-02 09:54:42 +01:00
No more functor objects; all command handler functions take same args
This commit is contained in:
@@ -29,6 +29,8 @@ class wxEvtHandler;
|
||||
// couldn't satisfy the requirement for both base classes at once.
|
||||
using CommandHandlerObject = wxEvtHandler;
|
||||
|
||||
// First of two function pointers registered with each command: an extractor
|
||||
// of the handler object from the AudacityProject
|
||||
using CommandHandlerFinder = CommandHandlerObject &(*)(AudacityProject&);
|
||||
|
||||
class wxEvent;
|
||||
@@ -54,112 +56,9 @@ struct CommandContext {
|
||||
CommandParameter parameter;
|
||||
};
|
||||
|
||||
class wxEvent;
|
||||
typedef wxString PluginID;
|
||||
|
||||
class AUDACITY_DLL_API CommandFunctor /* not final */
|
||||
{
|
||||
public:
|
||||
CommandFunctor(){};
|
||||
virtual ~CommandFunctor(){};
|
||||
virtual void operator()(const CommandContext &context) = 0;
|
||||
};
|
||||
|
||||
using CommandFunctorPointer = std::shared_ptr <CommandFunctor>;
|
||||
|
||||
|
||||
// Define functor subclasses that dispatch to the correct call sequence on
|
||||
// member functions of AudacityProject (or other class!)
|
||||
|
||||
template<typename OBJ>
|
||||
using audCommandFunction = void (OBJ::*)();
|
||||
|
||||
template<typename OBJ>
|
||||
class VoidFunctor final : public CommandFunctor
|
||||
{
|
||||
public:
|
||||
explicit VoidFunctor(OBJ *This, audCommandFunction<OBJ> pfn)
|
||||
: mThis{ This }, mCommandFunction{ pfn } {}
|
||||
void operator () (const CommandContext &context) override
|
||||
{ (mThis->*mCommandFunction) (); }
|
||||
private:
|
||||
OBJ *const mThis;
|
||||
const audCommandFunction<OBJ> mCommandFunction;
|
||||
};
|
||||
|
||||
template<typename OBJ>
|
||||
using audCommandKeyFunction = void (OBJ::*)(const wxEvent *);
|
||||
|
||||
template<typename OBJ>
|
||||
class KeyFunctor final : public CommandFunctor
|
||||
{
|
||||
public:
|
||||
explicit KeyFunctor(OBJ *This, audCommandKeyFunction<OBJ> pfn)
|
||||
: mThis{ This }, mCommandKeyFunction{ pfn } {}
|
||||
void operator () (const CommandContext &context) override
|
||||
{ (mThis->*mCommandKeyFunction) (context.pEvt); }
|
||||
private:
|
||||
OBJ *const mThis;
|
||||
const audCommandKeyFunction<OBJ> mCommandKeyFunction;
|
||||
};
|
||||
|
||||
template<typename OBJ>
|
||||
using audCommandListFunction = void (OBJ::*)(int);
|
||||
|
||||
template<typename OBJ>
|
||||
class ListFunctor final : public CommandFunctor
|
||||
{
|
||||
public:
|
||||
explicit ListFunctor(OBJ *This, audCommandListFunction<OBJ> pfn)
|
||||
: mThis{ This }, mCommandListFunction{ pfn } {}
|
||||
void operator () (const CommandContext &context) override
|
||||
{ (mThis->*mCommandListFunction)(context.index); }
|
||||
private:
|
||||
OBJ *const mThis;
|
||||
const audCommandListFunction<OBJ> mCommandListFunction;
|
||||
};
|
||||
|
||||
template<typename OBJ>
|
||||
using audCommandPluginFunction = void (OBJ::*)(const PluginID &, int);
|
||||
|
||||
template<typename OBJ>
|
||||
class PluginFunctor final : public CommandFunctor
|
||||
{
|
||||
public:
|
||||
explicit PluginFunctor(OBJ *This, audCommandPluginFunction<OBJ> pfn)
|
||||
: mThis{ This }, mCommandPluginFunction{ pfn } {}
|
||||
void operator () (const CommandContext &context) override
|
||||
{ (mThis->*mCommandPluginFunction)
|
||||
(context.parameter,
|
||||
0 // AudacityProject::OnEffectFlags::kNone
|
||||
); }
|
||||
private:
|
||||
OBJ *const mThis;
|
||||
const audCommandPluginFunction<OBJ> mCommandPluginFunction;
|
||||
};
|
||||
|
||||
// Now define an overloaded factory function
|
||||
template<typename OBJ>
|
||||
inline CommandFunctorPointer MakeFunctor(OBJ *This,
|
||||
audCommandFunction<OBJ> pfn)
|
||||
{ return CommandFunctorPointer{ safenew VoidFunctor<OBJ>{ This, pfn } }; }
|
||||
|
||||
template<typename OBJ>
|
||||
inline CommandFunctorPointer MakeFunctor(OBJ *This,
|
||||
audCommandKeyFunction<OBJ> pfn)
|
||||
{ return CommandFunctorPointer{ safenew KeyFunctor<OBJ>{ This, pfn } }; }
|
||||
|
||||
template<typename OBJ>
|
||||
inline CommandFunctorPointer MakeFunctor(OBJ *This,
|
||||
audCommandListFunction<OBJ> pfn)
|
||||
{ return CommandFunctorPointer{ safenew ListFunctor<OBJ>{ This, pfn } }; }
|
||||
|
||||
template<typename OBJ>
|
||||
inline CommandFunctorPointer MakeFunctor(OBJ *This,
|
||||
audCommandPluginFunction<OBJ> pfn)
|
||||
{ return CommandFunctorPointer{ safenew PluginFunctor<OBJ>{ This, pfn } }; }
|
||||
|
||||
// Now define the macro abbreviations that call the factory
|
||||
#define FNT(OBJ, This, X) (MakeFunctor<OBJ>(This, X ))
|
||||
// Second of two function pointers registered with each command: a pointer
|
||||
// to a member function of the handler object
|
||||
using CommandFunctorPointer =
|
||||
void (CommandHandlerObject::*)(const CommandContext &);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1493,7 +1493,8 @@ bool CommandManager::HandleCommandEntry(const CommandListEntry * entry,
|
||||
}
|
||||
|
||||
CommandContext context{ *proj, evt, entry->index, entry->parameter };
|
||||
(*(entry->callback))(context);
|
||||
auto &handler = entry->finder(*proj);
|
||||
(handler.*(entry->callback))(context);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,8 @@ bool OpenProjectCommand::Apply(CommandExecutionContext context)
|
||||
wxString oldFileName = context.GetProject()->GetFileName();
|
||||
if(fileName == wxEmptyString)
|
||||
{
|
||||
context.GetProject()->OnOpen();
|
||||
auto project = context.GetProject();
|
||||
project->OnOpen(*project);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -61,12 +61,14 @@ bool SelectCommand::Apply(CommandExecutionContext context)
|
||||
if (mode.IsSameAs(wxT("None")))
|
||||
{
|
||||
// select none
|
||||
context.GetProject()->OnSelectNone();
|
||||
auto project = context.GetProject();
|
||||
project->OnSelectNone(*project);
|
||||
}
|
||||
else if (mode.IsSameAs(wxT("All")))
|
||||
{
|
||||
// select all
|
||||
context.GetProject()->OnSelectAll();
|
||||
auto project = context.GetProject();
|
||||
project->OnSelectAll(*project);
|
||||
}
|
||||
else if (mode.IsSameAs(wxT("Range")))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user