From 4ba25a823e62271a74dfd1845f535400e2da125b Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Wed, 10 May 2017 22:08:05 +0200 Subject: cmState: separate builtin and scripted commands --- Source/cmState.cxx | 117 +++++++++++++++++++++++------------------------------ Source/cmState.h | 5 +-- 2 files changed, 52 insertions(+), 70 deletions(-) diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 6de4c9f..11ad6c0 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -36,7 +36,8 @@ cmState::cmState() cmState::~cmState() { delete this->CacheManager; - cmDeleteAll(this->Commands); + cmDeleteAll(this->BuiltinCommands); + cmDeleteAll(this->ScriptedCommands); } const char* cmState::GetTargetTypeName(cmStateEnums::TargetType targetType) @@ -377,47 +378,12 @@ void cmState::SetIsGeneratorMultiConfig(bool b) this->IsGeneratorMultiConfig = 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::AddBuiltinCommand(std::string const& name, cmCommand* command) { assert(name == cmSystemTools::LowerCase(name)); assert(name == cmSystemTools::LowerCase(command->GetName())); - assert(this->Commands.find(name) == this->Commands.end()); - this->Commands.insert(std::make_pair(name, command)); + assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end()); + this->BuiltinCommands.insert(std::make_pair(name, command)); } void cmState::AddDisallowedCommand(std::string const& name, cmCommand* command, @@ -435,53 +401,70 @@ void cmState::AddUnexpectedCommand(std::string const& name, const char* error) void cmState::AddScriptedCommand(std::string const& name, cmCommand* command) { - this->RenameCommand(name, "_" + name); - this->AddCommand(command); + std::string sName = cmSystemTools::LowerCase(name); + + // if the command already exists, give a new name to the old command. + if (cmCommand* oldCmd = this->GetCommand(sName)) { + std::string const newName = "_" + sName; + std::map::iterator pos = + this->ScriptedCommands.find(newName); + if (pos != this->ScriptedCommands.end()) { + delete pos->second; + this->ScriptedCommands.erase(pos); + } + this->ScriptedCommands.insert(std::make_pair(newName, oldCmd->Clone())); + } + + // if the command already exists, free the old one + std::map::iterator pos = + this->ScriptedCommands.find(sName); + if (pos != this->ScriptedCommands.end()) { + delete pos->second; + this->ScriptedCommands.erase(pos); + } + this->ScriptedCommands.insert(std::make_pair(sName, command)); } cmCommand* cmState::GetCommand(std::string const& name) const { - cmCommand* command = CM_NULLPTR; std::string sName = cmSystemTools::LowerCase(name); - std::map::const_iterator pos = - this->Commands.find(sName); - if (pos != this->Commands.end()) { - command = (*pos).second; + std::map::const_iterator pos; + pos = this->ScriptedCommands.find(sName); + if (pos != this->ScriptedCommands.end()) { + return pos->second; } - return command; + pos = this->BuiltinCommands.find(sName); + if (pos != this->BuiltinCommands.end()) { + return pos->second; + } + return CM_NULLPTR; } 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.reserve(this->BuiltinCommands.size() + + this->ScriptedCommands.size()); + for (std::map::const_iterator cmds = + this->BuiltinCommands.begin(); + cmds != this->BuiltinCommands.end(); ++cmds) { + commandNames.push_back(cmds->first); + } + for (std::map::const_iterator cmds = + this->ScriptedCommands.begin(); + cmds != this->ScriptedCommands.end(); ++cmds) { commandNames.push_back(cmds->first); } + std::sort(commandNames.begin(), commandNames.end()); + commandNames.erase(std::unique(commandNames.begin(), commandNames.end()), + commandNames.end()); return commandNames; } void cmState::RemoveUserDefinedCommands() { - std::vector renamedCommands; - for (std::map::iterator j = this->Commands.begin(); - j != this->Commands.end();) { - if (j->second->IsUserDefined()) { - delete j->second; - this->Commands.erase(j++); - } else if (j->first != j->second->GetName()) { - renamedCommands.push_back(j->second); - this->Commands.erase(j++); - } else { - ++j; - } - } - for (std::vector::const_iterator it = renamedCommands.begin(); - it != renamedCommands.end(); ++it) { - this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it; - } + cmDeleteAll(this->ScriptedCommands); + this->ScriptedCommands.clear(); } void cmState::SetGlobalProperty(const std::string& prop, const char* value) diff --git a/Source/cmState.h b/Source/cmState.h index 895561e..1a5738f 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -121,13 +121,11 @@ public: void SetIsGeneratorMultiConfig(bool b); cmCommand* GetCommand(std::string const& name) const; - void AddCommand(cmCommand* command); void AddBuiltinCommand(std::string const& name, cmCommand* command); void AddDisallowedCommand(std::string const& name, cmCommand* command, cmPolicies::PolicyID policy, const char* message); void AddUnexpectedCommand(std::string const& name, const char* error); void AddScriptedCommand(std::string const& name, cmCommand* command); - void RenameCommand(std::string const& oldName, std::string const& newName); void RemoveUserDefinedCommands(); std::vector GetCommandNames() const; @@ -166,7 +164,8 @@ private: std::map PropertyDefinitions; std::vector EnabledLanguages; - std::map Commands; + std::map BuiltinCommands; + std::map ScriptedCommands; cmPropertyMap GlobalProperties; cmCacheManager* CacheManager; -- cgit v0.12