mirror of
https://github.com/cookiengineer/audacity
synced 2026-03-04 21:50:51 +01:00
Simplify the FN macros in src/menus/*.cpp...
... Eliminate repetitious passing of the finder function into each of the calls to Command and CommandGroup.
This commit is contained in:
@@ -490,13 +490,57 @@ namespace MenuTable {
|
||||
~SeparatorItem() override;
|
||||
};
|
||||
|
||||
// usage:
|
||||
// auto scope = FinderScope( findCommandHandler );
|
||||
// return Items( ... );
|
||||
//
|
||||
// or:
|
||||
// return FinderScope( findCommandHandler )
|
||||
// .Eval( Items( ... ) );
|
||||
//
|
||||
// where findCommandHandler names a function.
|
||||
// This is used before a sequence of many calls to Command() and
|
||||
// CommandGroup(), so that the finder argument need not be specified
|
||||
// in each call.
|
||||
class FinderScope : ValueRestorer< CommandHandlerFinder >
|
||||
{
|
||||
static CommandHandlerFinder sFinder;
|
||||
|
||||
public:
|
||||
static CommandHandlerFinder DefaultFinder() { return sFinder; }
|
||||
|
||||
explicit
|
||||
FinderScope( CommandHandlerFinder finder )
|
||||
: ValueRestorer( sFinder, finder )
|
||||
{}
|
||||
|
||||
// See usage comment above about this pass-through function
|
||||
template< typename Value > Value&& Eval( Value &&value ) const
|
||||
{ return std::forward<Value>(value); }
|
||||
};
|
||||
|
||||
struct CommandItem final : BaseItem {
|
||||
CommandItem(const CommandID &name_,
|
||||
const TranslatableString &label_in_,
|
||||
CommandHandlerFinder finder_,
|
||||
CommandFunctorPointer callback_,
|
||||
CommandFlag flags_,
|
||||
const CommandManager::Options &options_);
|
||||
const CommandManager::Options &options_,
|
||||
CommandHandlerFinder finder_);
|
||||
|
||||
// Takes a pointer to member function directly, and delegates to the
|
||||
// previous constructor; useful within the lifetime of a FinderScope
|
||||
template< typename Handler >
|
||||
CommandItem(const CommandID &name_,
|
||||
const TranslatableString &label_in_,
|
||||
void (Handler::*pmf)(const CommandContext&),
|
||||
CommandFlag flags_,
|
||||
const CommandManager::Options &options_,
|
||||
CommandHandlerFinder finder = FinderScope::DefaultFinder())
|
||||
: CommandItem(name_, label_in_,
|
||||
static_cast<CommandFunctorPointer>(pmf),
|
||||
flags_, options_, finder)
|
||||
{}
|
||||
|
||||
~CommandItem() override;
|
||||
|
||||
const CommandID name;
|
||||
@@ -510,10 +554,25 @@ namespace MenuTable {
|
||||
struct CommandGroupItem final : BaseItem {
|
||||
CommandGroupItem(const wxString &name_,
|
||||
std::initializer_list< ComponentInterfaceSymbol > items_,
|
||||
CommandHandlerFinder finder_,
|
||||
CommandFunctorPointer callback_,
|
||||
CommandFlag flags_,
|
||||
bool isEffect_);
|
||||
bool isEffect_,
|
||||
CommandHandlerFinder finder_);
|
||||
|
||||
// Takes a pointer to member function directly, and delegates to the
|
||||
// previous constructor; useful within the lifetime of a FinderScope
|
||||
template< typename Handler >
|
||||
CommandGroupItem(const wxString &name_,
|
||||
std::initializer_list< ComponentInterfaceSymbol > items_,
|
||||
void (Handler::*pmf)(const CommandContext&),
|
||||
CommandFlag flags_,
|
||||
bool isEffect_,
|
||||
CommandHandlerFinder finder = FinderScope::DefaultFinder())
|
||||
: CommandGroupItem(name_, items_,
|
||||
static_cast<CommandFunctorPointer>(pmf),
|
||||
flags_, isEffect_, finder)
|
||||
{}
|
||||
|
||||
~CommandGroupItem() override;
|
||||
|
||||
const wxString name;
|
||||
@@ -588,25 +647,29 @@ namespace MenuTable {
|
||||
inline std::unique_ptr<SeparatorItem> Separator()
|
||||
{ return std::make_unique<SeparatorItem>(); }
|
||||
|
||||
template< typename Handler >
|
||||
inline std::unique_ptr<CommandItem> Command(
|
||||
const CommandID &name,
|
||||
const TranslatableString &label_in,
|
||||
CommandHandlerFinder finder, CommandFunctorPointer callback,
|
||||
CommandFlag flags, const CommandManager::Options &options = {})
|
||||
void (Handler::*pmf)(const CommandContext&),
|
||||
CommandFlag flags, const CommandManager::Options &options = {},
|
||||
CommandHandlerFinder finder = FinderScope::DefaultFinder())
|
||||
{
|
||||
return std::make_unique<CommandItem>(
|
||||
name, label_in, finder, callback, flags, options
|
||||
name, label_in, pmf, flags, options, finder
|
||||
);
|
||||
}
|
||||
|
||||
template< typename Handler >
|
||||
inline std::unique_ptr<CommandGroupItem> CommandGroup(
|
||||
const wxString &name,
|
||||
std::initializer_list< ComponentInterfaceSymbol > items,
|
||||
CommandHandlerFinder finder, CommandFunctorPointer callback,
|
||||
CommandFlag flags, bool isEffect = false)
|
||||
void (Handler::*pmf)(const CommandContext&),
|
||||
CommandFlag flags, bool isEffect = false,
|
||||
CommandHandlerFinder finder = FinderScope::DefaultFinder())
|
||||
{
|
||||
return std::make_unique<CommandGroupItem>(
|
||||
name, items, finder, callback, flags, isEffect
|
||||
name, items, pmf, flags, isEffect, finder
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user