1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-19 14:17:41 +02:00

CommandDirectory.cpp has fewer dependencies...

... This involved populating it with a new registration system.

BatchEvalCommand, HelpCommand, MessageCommand escape from cycles and move
higher in the dependency graph, and CommandDirectory moves lower.
This commit is contained in:
Paul Licameli 2019-05-18 13:20:19 -04:00
parent 13713aab67
commit 016e1949ae
4 changed files with 30 additions and 16 deletions

View File

@ -17,6 +17,11 @@
#include "BatchEvalCommand.h" #include "BatchEvalCommand.h"
#include "CommandContext.h" #include "CommandContext.h"
#include "CommandDirectory.h"
static CommandDirectory::RegisterType sRegisterType{
std::make_unique<BatchEvalCommandType>()
};
ComponentInterfaceSymbol BatchEvalCommandType::BuildName() ComponentInterfaceSymbol BatchEvalCommandType::BuildName()
{ {

View File

@ -25,7 +25,7 @@ system by constructing BatchCommandEval objects.
#include "CommandBuilder.h" #include "CommandBuilder.h"
#include "CommandDirectory.h" #include "CommandDirectory.h"
#include "BatchEvalCommand.h" #include "Command.h"
#include "ScriptCommandRelay.h" #include "ScriptCommandRelay.h"
#include "CommandContext.h" #include "CommandContext.h"
#include "CommandTargets.h" #include "CommandTargets.h"

View File

@ -17,10 +17,6 @@ functions to look up a command by name.
#include "../Audacity.h" #include "../Audacity.h"
#include "CommandDirectory.h" #include "CommandDirectory.h"
#include "HelpCommand.h"
#include "MessageCommand.h"
#include "BatchEvalCommand.h"
std::unique_ptr<CommandDirectory> CommandDirectory::mInstance; std::unique_ptr<CommandDirectory> CommandDirectory::mInstance;
CommandDirectory::CommandDirectory() CommandDirectory::CommandDirectory()
@ -28,7 +24,8 @@ CommandDirectory::CommandDirectory()
// Create the command map. // Create the command map.
// First we have commands which return information // First we have commands which return information
//AddCommand(std::make_unique<MessageCommandType>()); //AddCommand(std::make_unique<MessageCommandType>());
AddCommand(std::make_unique<BatchEvalCommandType>());
// AddCommand(std::make_unique<BatchEvalCommandType>());
// Legacy adapter commands that previously was needed to // Legacy adapter commands that previously was needed to
@ -68,24 +65,30 @@ CommandDirectory::~CommandDirectory()
OldStyleCommandType *CommandDirectory::LookUp(const wxString &cmdName) const OldStyleCommandType *CommandDirectory::LookUp(const wxString &cmdName) const
{ {
CommandMap::const_iterator iter = mCmdMap.find(cmdName); auto iter = sCmdMap().find(cmdName);
if (iter == mCmdMap.end()) if (iter == sCmdMap().end())
{ {
return NULL; return nullptr;
} }
return iter->second.get(); return iter->second.get();
} }
void CommandDirectory::AddCommand(std::unique_ptr<OldStyleCommandType> &&type) CommandMap &CommandDirectory::sCmdMap()
{
static CommandMap theMap;
return theMap;
}
void CommandDirectory::AddCommand(std::unique_ptr<OldStyleCommandType> type)
{ {
wxASSERT(type != NULL); wxASSERT(type != NULL);
// Internal string is shown but only in assertion message // Internal string is shown but only in assertion message
auto cmdName = type->GetSymbol().Internal(); auto cmdName = type->GetSymbol().Internal();
wxASSERT_MSG(mCmdMap.find(cmdName) == mCmdMap.end() wxASSERT_MSG(sCmdMap().find(cmdName) == sCmdMap().end()
, wxT("A command named ") + cmdName , wxT("A command named ") + cmdName
+ wxT(" already exists.")); + wxT(" already exists."));
mCmdMap[cmdName] = std::move(type); sCmdMap()[cmdName] = std::move(type);
} }
CommandDirectory *CommandDirectory::Get() CommandDirectory *CommandDirectory::Get()

View File

@ -31,8 +31,17 @@ class CommandDirectory
{ {
private: private:
static std::unique_ptr<CommandDirectory> mInstance; static std::unique_ptr<CommandDirectory> mInstance;
CommandMap mCmdMap; static CommandMap &sCmdMap();
static void AddCommand(std::unique_ptr<OldStyleCommandType> type);
public: public:
/// Register a type of command with the directory with a statically
/// constructed instance of this class.
struct RegisterType{
RegisterType( std::unique_ptr<OldStyleCommandType> type )
{ AddCommand( std::move( type ) ); }
};
~CommandDirectory(); ~CommandDirectory();
/// If a command with the given name has been registered in the directory, /// If a command with the given name has been registered in the directory,
@ -40,9 +49,6 @@ public:
/// Otherwise return NULL. /// Otherwise return NULL.
OldStyleCommandType *LookUp(const wxString &cmdName) const; OldStyleCommandType *LookUp(const wxString &cmdName) const;
/// Register a type of command with the directory.
void AddCommand(std::unique_ptr<OldStyleCommandType> &&type);
/// Get a pointer to the singleton instance /// Get a pointer to the singleton instance
static CommandDirectory *Get(); static CommandDirectory *Get();