From 016e1949ae0f89ee87c4c1ae7d4f6c25d5e36649 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 18 May 2019 13:20:19 -0400 Subject: [PATCH] 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. --- src/commands/BatchEvalCommand.cpp | 5 +++++ src/commands/CommandBuilder.cpp | 2 +- src/commands/CommandDirectory.cpp | 25 ++++++++++++++----------- src/commands/CommandDirectory.h | 14 ++++++++++---- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/commands/BatchEvalCommand.cpp b/src/commands/BatchEvalCommand.cpp index 8b9b86a8f..c486a07cf 100644 --- a/src/commands/BatchEvalCommand.cpp +++ b/src/commands/BatchEvalCommand.cpp @@ -17,6 +17,11 @@ #include "BatchEvalCommand.h" #include "CommandContext.h" +#include "CommandDirectory.h" + +static CommandDirectory::RegisterType sRegisterType{ + std::make_unique() +}; ComponentInterfaceSymbol BatchEvalCommandType::BuildName() { diff --git a/src/commands/CommandBuilder.cpp b/src/commands/CommandBuilder.cpp index 5b2da4cb1..44f9d9d2a 100644 --- a/src/commands/CommandBuilder.cpp +++ b/src/commands/CommandBuilder.cpp @@ -25,7 +25,7 @@ system by constructing BatchCommandEval objects. #include "CommandBuilder.h" #include "CommandDirectory.h" -#include "BatchEvalCommand.h" +#include "Command.h" #include "ScriptCommandRelay.h" #include "CommandContext.h" #include "CommandTargets.h" diff --git a/src/commands/CommandDirectory.cpp b/src/commands/CommandDirectory.cpp index 1fb58f6de..ba75042a4 100644 --- a/src/commands/CommandDirectory.cpp +++ b/src/commands/CommandDirectory.cpp @@ -17,10 +17,6 @@ functions to look up a command by name. #include "../Audacity.h" #include "CommandDirectory.h" -#include "HelpCommand.h" -#include "MessageCommand.h" -#include "BatchEvalCommand.h" - std::unique_ptr CommandDirectory::mInstance; CommandDirectory::CommandDirectory() @@ -28,7 +24,8 @@ CommandDirectory::CommandDirectory() // Create the command map. // First we have commands which return information //AddCommand(std::make_unique()); - AddCommand(std::make_unique()); + +// AddCommand(std::make_unique()); // Legacy adapter commands that previously was needed to @@ -68,24 +65,30 @@ CommandDirectory::~CommandDirectory() OldStyleCommandType *CommandDirectory::LookUp(const wxString &cmdName) const { - CommandMap::const_iterator iter = mCmdMap.find(cmdName); - if (iter == mCmdMap.end()) + auto iter = sCmdMap().find(cmdName); + if (iter == sCmdMap().end()) { - return NULL; + return nullptr; } return iter->second.get(); } -void CommandDirectory::AddCommand(std::unique_ptr &&type) +CommandMap &CommandDirectory::sCmdMap() +{ + static CommandMap theMap; + return theMap; +} + +void CommandDirectory::AddCommand(std::unique_ptr type) { wxASSERT(type != NULL); // Internal string is shown but only in assertion message 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(" already exists.")); - mCmdMap[cmdName] = std::move(type); + sCmdMap()[cmdName] = std::move(type); } CommandDirectory *CommandDirectory::Get() diff --git a/src/commands/CommandDirectory.h b/src/commands/CommandDirectory.h index 2122f4bed..3635871ee 100644 --- a/src/commands/CommandDirectory.h +++ b/src/commands/CommandDirectory.h @@ -31,8 +31,17 @@ class CommandDirectory { private: static std::unique_ptr mInstance; - CommandMap mCmdMap; + static CommandMap &sCmdMap(); + + static void AddCommand(std::unique_ptr type); public: + /// Register a type of command with the directory with a statically + /// constructed instance of this class. + struct RegisterType{ + RegisterType( std::unique_ptr type ) + { AddCommand( std::move( type ) ); } + }; + ~CommandDirectory(); /// If a command with the given name has been registered in the directory, @@ -40,9 +49,6 @@ public: /// Otherwise return NULL. OldStyleCommandType *LookUp(const wxString &cmdName) const; - /// Register a type of command with the directory. - void AddCommand(std::unique_ptr &&type); - /// Get a pointer to the singleton instance static CommandDirectory *Get();