From 96f8c5f9a3bd60f553af054b43e06ce4864269e0 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sat, 11 Apr 2015 12:30:26 +0200 Subject: [PATCH] cmState: Move cmCommand-related methods from cmake class. --- Source/cmState.cxx | 110 +++++++++++++++++++++++++++++++++++++++++++++ Source/cmState.h | 10 +++++ Source/cmake.cxx | 88 ++++-------------------------------- Source/cmake.h | 2 - 4 files changed, 128 insertions(+), 82 deletions(-) diff --git a/Source/cmState.cxx b/Source/cmState.cxx index be6a76646..17b6cf20b 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -13,6 +13,10 @@ #include "cmake.h" #include "cmCacheManager.h" +#include "cmCommand.h" +#include "cmAlgorithms.h" + +#include cmState::cmState(cmake* cm) : CMakeInstance(cm), @@ -20,6 +24,11 @@ cmState::cmState(cmake* cm) { } +cmState::~cmState() +{ + cmDeleteAll(this->Commands); +} + const char* cmCacheEntryTypes[] = { "BOOL", "PATH", @@ -274,3 +283,104 @@ void cmState::SetIsInTryCompile(bool b) { this->IsInTryCompile = b; } + +void cmState::RenameCommand(std::string const& oldName, + std::string const& newName) +{ + // if the command already exists, free the old one + std::string sOldName = cmSystemTools::LowerCase(oldName); + std::string sNewName = cmSystemTools::LowerCase(newName); + std::map::iterator pos = + this->Commands.find(sOldName); + if ( pos == this->Commands.end() ) + { + return; + } + cmCommand* cmd = pos->second; + + pos = this->Commands.find(sNewName); + if (pos != this->Commands.end()) + { + delete pos->second; + this->Commands.erase(pos); + } + this->Commands.insert(std::make_pair(sNewName, cmd)); + pos = this->Commands.find(sOldName); + this->Commands.erase(pos); +} + +void cmState::AddCommand(cmCommand* command) +{ + std::string name = cmSystemTools::LowerCase(command->GetName()); + // if the command already exists, free the old one + std::map::iterator pos = this->Commands.find(name); + if (pos != this->Commands.end()) + { + delete pos->second; + this->Commands.erase(pos); + } + this->Commands.insert(std::make_pair(name, command)); +} + +void cmState::RemoveUnscriptableCommands() +{ + std::vector unscriptableCommands; + for (std::map::iterator + pos = this->Commands.begin(); + pos != this->Commands.end(); ) + { + if (!pos->second->IsScriptable()) + { + delete pos->second; + this->Commands.erase(pos++); + } + else + { + ++pos; + } + } +} + +cmCommand* cmState::GetCommand(std::string const& name) const +{ + cmCommand* command = 0; + std::string sName = cmSystemTools::LowerCase(name); + std::map::const_iterator pos = + this->Commands.find(sName); + if (pos != this->Commands.end()) + { + command = (*pos).second; + } + return command; +} + +std::vector cmState::GetCommandNames() const +{ + std::vector commandNames; + commandNames.reserve(this->Commands.size()); + std::map::const_iterator cmds + = this->Commands.begin(); + for ( ; cmds != this->Commands.end(); ++ cmds ) + { + commandNames.push_back(cmds->first); + } + return commandNames; +} + +void cmState::RemoveUserDefinedCommands() +{ + for(std::map::iterator j = this->Commands.begin(); + j != this->Commands.end(); ) + { + if (j->second->IsA("cmMacroHelperCommand") || + j->second->IsA("cmFunctionHelperCommand")) + { + delete j->second; + this->Commands.erase(j++); + } + else + { + ++j; + } + } +} diff --git a/Source/cmState.h b/Source/cmState.h index 6df618253..a7a17eef3 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -16,11 +16,13 @@ #include "cmPropertyDefinitionMap.h" class cmake; +class cmCommand; class cmState { public: cmState(cmake* cm); + ~cmState(); enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC, UNINITIALIZED }; @@ -79,9 +81,17 @@ public: bool GetIsInTryCompile() const; void SetIsInTryCompile(bool b); + cmCommand* GetCommand(std::string const& name) const; + void AddCommand(cmCommand* command); + void RemoveUnscriptableCommands(); + void RenameCommand(std::string const& oldName, std::string const& newName); + void RemoveUserDefinedCommands(); + std::vector GetCommandNames() const; + private: std::map PropertyDefinitions; std::vector EnabledLanguages; + std::map Commands; cmake* CMakeInstance; bool IsInTryCompile; }; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 6518207e3..89d5feac8 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -179,7 +179,6 @@ cmake::~cmake() delete this->GlobalGenerator; this->GlobalGenerator = 0; } - cmDeleteAll(this->Commands); cmDeleteAll(this->Generators); #ifdef CMAKE_BUILD_WITH_CMAKE delete this->VariableWatch; @@ -197,94 +196,33 @@ void cmake::InitializeProperties() void cmake::CleanupCommandsAndMacros() { this->InitializeProperties(); - for(RegisteredCommandsMap::iterator j = this->Commands.begin(); - j != this->Commands.end(); ) - { - if (j->second->IsA("cmMacroHelperCommand") || - j->second->IsA("cmFunctionHelperCommand")) - { - delete j->second; - this->Commands.erase(j++); - } - else - { - ++j; - } - } + this->State->RemoveUserDefinedCommands(); } bool cmake::CommandExists(const std::string& name) const { - return this->GetCommand(name) ? true : false; + return this->State->GetCommand(name) ? true : false; } cmCommand *cmake::GetCommand(const std::string& name) const { - cmCommand* command = 0; - std::string sName = cmSystemTools::LowerCase(name); - RegisteredCommandsMap::const_iterator pos = this->Commands.find(sName); - if (pos != this->Commands.end()) - { - command = (*pos).second; - } - return command; + return this->State->GetCommand(name); } void cmake::RenameCommand(const std::string& oldName, const std::string& newName) { - // if the command already exists, free the old one - std::string sOldName = cmSystemTools::LowerCase(oldName); - RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName); - if ( pos == this->Commands.end() ) - { - return; - } - std::string sNewName = cmSystemTools::LowerCase(newName); - cmCommand* cmd = pos->second; - - pos = this->Commands.find(sNewName); - if (pos != this->Commands.end()) - { - delete pos->second; - this->Commands.erase(pos); - } - this->Commands.insert(std::make_pair(sNewName, cmd)); - pos = this->Commands.find(sOldName); - this->Commands.erase(pos); + this->State->RenameCommand(oldName, newName); } void cmake::AddCommand(cmCommand* command) { - std::string name = cmSystemTools::LowerCase(command->GetName()); - // if the command already exists, free the old one - RegisteredCommandsMap::iterator pos = this->Commands.find(name); - if (pos != this->Commands.end()) - { - delete pos->second; - this->Commands.erase(pos); - } - this->Commands.insert(std::make_pair(name, command)); + this->State->AddCommand(command); } - void cmake::RemoveUnscriptableCommands() { - std::vector unscriptableCommands; - for (cmake::RegisteredCommandsMap::iterator - pos = this->Commands.begin(); - pos != this->Commands.end(); ) - { - if (!pos->second->IsScriptable()) - { - delete pos->second; - this->Commands.erase(pos++); - } - else - { - ++pos; - } - } + this->State->RemoveUnscriptableCommands(); } // Parse the args @@ -2301,18 +2239,8 @@ const char *cmake::GetProperty(const std::string& prop, } else if ( prop == "COMMANDS" ) { - cmake::RegisteredCommandsMap::iterator cmds - = this->Commands.begin(); - for (unsigned int cc=0 ; cmds != this->Commands.end(); ++ cmds ) - { - if ( cc > 0 ) - { - output += ";"; - } - output += cmds->first.c_str(); - cc++; - } - this->SetProperty("COMMANDS",output.c_str()); + std::vector commands = this->State->GetCommandNames(); + this->SetProperty("COMMANDS", cmJoin(commands, ";").c_str()); } else if ( prop == "IN_TRY_COMPILE" ) { diff --git a/Source/cmake.h b/Source/cmake.h index 455b54eca..e7a8acb81 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -93,7 +93,6 @@ class cmake */ FIND_PACKAGE_MODE }; - typedef std::map RegisteredCommandsMap; typedef std::map InstalledFilesMap; /// Default constructor @@ -362,7 +361,6 @@ protected: typedef std::map RegisteredExtraGeneratorsMap; typedef std::vector RegisteredGeneratorsVector; - RegisteredCommandsMap Commands; RegisteredGeneratorsVector Generators; RegisteredExtraGeneratorsMap ExtraGenerators; void AddDefaultCommands();