1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-24 16:20:05 +02:00

LoadCommands.cpp doesn't depend on individual command classes...

... Remove many #includes and macros.  Use instead a hash generated from the
registry.
This commit is contained in:
Paul Licameli 2019-01-17 13:43:59 -05:00
parent 2522459637
commit b5c59b29a7
2 changed files with 23 additions and 132 deletions

View File

@ -15,25 +15,13 @@ modelled on BuiltinEffectsModule
#include "../Audacity.h"
#include "LoadCommands.h"
#include "AudacityCommand.h"
#include "../Prefs.h"
#include "Demo.h"
#include "../commands/MessageCommand.h"
#include "../commands/ScreenshotCommand.h"
#include "../commands/CompareAudioCommand.h"
#include "../commands/GetTrackInfoCommand.h"
#include "../commands/SelectCommand.h"
#include "../commands/PreferenceCommands.h"
#include "../commands/GetInfoCommand.h"
#include "../commands/HelpCommand.h"
#include "../commands/ImportExportCommands.h"
#include "../commands/OpenSaveCommands.h"
#include "../commands/SetLabelCommand.h"
#include "../commands/SetEnvelopeCommand.h"
#include "../commands/SetClipCommand.h"
#include "../commands/SetProjectCommand.h"
#include "../commands/DragCommand.h"
namespace {
bool sInitialized = false;
}
struct BuiltinCommandsModule::Entry {
wxString name;
@ -50,97 +38,10 @@ struct BuiltinCommandsModule::Entry {
void BuiltinCommandsModule::DoRegistration(
const ComponentInterfaceSymbol &name, const Factory &factory )
{
wxASSERT( !sInitialized );
Entry::Registry().emplace_back( Entry{ name.Internal(), factory } );
}
//
// Define the list of COMMANDs that will be autoregistered and how to instantiate each
//
#define COMMAND_LIST \
COMMAND( DEMO, DemoCommand, () ) \
COMMAND( MESSAGE, MessageCommand, () ) \
COMMAND( SCREENSHOT, ScreenshotCommand, () ) \
COMMAND( DRAG, DragCommand, () ) \
COMMAND( COMPARE_AUDIO, CompareAudioCommand, () ) \
COMMAND( SET_TRACK, SetTrackCommand, () ) \
COMMAND( SET_TRACK_STATUS, SetTrackStatusCommand, () ) \
COMMAND( SET_TRACK_AUDIO, SetTrackAudioCommand, () ) \
COMMAND( SET_TRACK_VISUALS, SetTrackVisualsCommand, () ) \
COMMAND( SET_ENVELOPE, SetEnvelopeCommand, () ) \
COMMAND( SET_CLIP, SetClipCommand, () ) \
COMMAND( SET_LABEL, SetLabelCommand, () ) \
COMMAND( SET_PROJECT, SetProjectCommand, () ) \
COMMAND( SELECT, SelectCommand, () ) \
COMMAND( SELECT_TIME, SelectTimeCommand, () ) \
COMMAND( SELECT_FREQUENCIES, SelectFrequenciesCommand, () )\
COMMAND( SELECT_TRACKS, SelectTracksCommand, () ) \
COMMAND( GET_PREFERENCE, GetPreferenceCommand, () ) \
COMMAND( SET_PREFERENCE, SetPreferenceCommand, () ) \
COMMAND( GET_INFO, GetInfoCommand, () ) \
COMMAND( HELP, HelpCommand, () ) \
COMMAND( IMPORT, ImportCommand, () ) \
COMMAND( EXPORT, ExportCommand, () ) \
COMMAND( OPEN_PROJECT, OpenProjectCommand, () ) \
COMMAND( SAVE_PROJECT, SaveProjectCommand, () ) \
// GET_TRACK_INFO subsumed by GET_INFO
//COMMAND( GET_TRACK_INFO, GetTrackInfoCommand, () )
// SELECT_TIME and SELECT_TRACKS subsumed by SELECT
//COMMAND( SELECT_TIME, SelectTimeCommand, () )
//COMMAND( SELECT_TRACKS, SelectTracksCommand, () )
//
// Define the list of COMMANDs that do not get autoregistered
//
#define EXCLUDE_LIST \
//
// Define the COMMAND() macro to generate enum names
//
#define COMMAND(n, i, args) ENUM_ ## n,
//
// Create the enum for the list of COMMANDs (will be used in a switch statement)
//
enum
{
COMMAND_LIST
EXCLUDE_LIST
};
//
// Redefine COMMAND() to add the COMMAND's name to an array
//
#undef COMMAND
#define COMMAND(n, i, args) results.push_back( (i :: Symbol).Internal() );
//
// Create the COMMAND name array
//
static const std::vector<wxString> kCOMMANDNames()
{
std::vector<wxString> results;
COMMAND_LIST;
return results;
};
/*
//
// Create the COMMAND name array of excluded COMMANDs
//
static const wxChar *kExcludedNames[] =
{
EXCLUDE_LIST
};
*/
//
// Redefine COMMAND() to generate a case statement for the lookup switch
//
#undef COMMAND
#define COMMAND(n, i, args) case ENUM_ ## n: return std::make_unique<i> args;
// ============================================================================
// Module registration entry point
//
@ -219,20 +120,11 @@ TranslatableString BuiltinCommandsModule::GetDescription()
bool BuiltinCommandsModule::Initialize()
{
const auto &names = kCOMMANDNames();
for (const auto &name : names)
{
//wxLogDebug("Adding %s", name );
mNames.push_back(wxString(BUILTIN_GENERIC_COMMAND_PREFIX) + name);
for ( const auto &entry : Entry::Registry() ) {
auto path = wxString(BUILTIN_GENERIC_COMMAND_PREFIX) + entry.name;
mCommands[ path ] = &entry;
}
/*
for (size_t i = 0; i < WXSIZEOF(kExcludedNames); i++)
{
mNames.push_back(wxString(BUILTIN_COMMAND_PREFIX) + kExcludedNames[i]);
}
*/
sInitialized = true;
return true;
}
@ -257,11 +149,9 @@ const FileExtensions &BuiltinCommandsModule::GetFileExtensions()
bool BuiltinCommandsModule::AutoRegisterPlugins(PluginManagerInterface & pm)
{
TranslatableString ignoredErrMsg;
const auto &names = kCOMMANDNames();
for (const auto &name : names)
for (const auto &pair : mCommands)
{
wxString path(wxString(BUILTIN_GENERIC_COMMAND_PREFIX) + name);
const auto &path = pair.first;
if (!pm.IsPluginRegistered(path))
{
// No checking of error ?
@ -278,7 +168,10 @@ bool BuiltinCommandsModule::AutoRegisterPlugins(PluginManagerInterface & pm)
PluginPaths BuiltinCommandsModule::FindPluginPaths(PluginManagerInterface & WXUNUSED(pm))
{
return mNames;
PluginPaths names;
for ( const auto &pair : mCommands )
names.push_back( pair.first );
return names;
}
unsigned BuiltinCommandsModule::DiscoverPluginsAtPath(
@ -303,7 +196,7 @@ bool BuiltinCommandsModule::IsPluginValid(const PluginPath & path, bool bFast)
{
// bFast is unused as checking in the list is fast.
static_cast<void>(bFast); // avoid unused variable warning
return make_iterator_range( mNames ).contains( path );
return mCommands.find( path ) != mCommands.end();
}
ComponentInterface *BuiltinCommandsModule::CreateInstance(const PluginPath & path)
@ -328,14 +221,10 @@ void BuiltinCommandsModule::DeleteInstance(ComponentInterface *instance)
std::unique_ptr<AudacityCommand> BuiltinCommandsModule::Instantiate(const PluginPath & path)
{
wxASSERT(path.StartsWith(BUILTIN_GENERIC_COMMAND_PREFIX));
auto index = make_iterator_range( mNames ).index( path );
wxASSERT( index != wxNOT_FOUND );
switch ( index )
{
COMMAND_LIST;
EXCLUDE_LIST;
}
auto iter = mCommands.find( path );
if ( iter != mCommands.end() )
return iter->second->factory();
wxASSERT( false );
return nullptr;
}

View File

@ -13,6 +13,7 @@
#include <functional>
#include <memory>
#include <unordered_map>
#include "../MemoryX.h"
class AudacityCommand;
@ -82,5 +83,6 @@ private:
ModuleManagerInterface *mModMan;
wxString mPath;
PluginPaths mNames;
using CommandHash = std::unordered_map< wxString, const Entry* > ;
CommandHash mCommands;
};