Browse Source

cmState: separate builtin and scripted commands

Daniel Pfeifer 8 years ago
parent
commit
4ba25a823e
2 changed files with 52 additions and 70 deletions
  1. 50 67
      Source/cmState.cxx
  2. 2 3
      Source/cmState.h

+ 50 - 67
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<std::string, cmCommand*>::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<std::string, cmCommand*>::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<std::string, cmCommand*>::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<std::string, cmCommand*>::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<std::string, cmCommand*>::const_iterator pos =
-    this->Commands.find(sName);
-  if (pos != this->Commands.end()) {
-    command = (*pos).second;
+  std::map<std::string, cmCommand*>::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<std::string> cmState::GetCommandNames() const
 {
   std::vector<std::string> commandNames;
-  commandNames.reserve(this->Commands.size());
-  std::map<std::string, cmCommand*>::const_iterator cmds =
-    this->Commands.begin();
-  for (; cmds != this->Commands.end(); ++cmds) {
+  commandNames.reserve(this->BuiltinCommands.size() +
+                       this->ScriptedCommands.size());
+  for (std::map<std::string, cmCommand*>::const_iterator cmds =
+         this->BuiltinCommands.begin();
+       cmds != this->BuiltinCommands.end(); ++cmds) {
+    commandNames.push_back(cmds->first);
+  }
+  for (std::map<std::string, cmCommand*>::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<cmCommand*> renamedCommands;
-  for (std::map<std::string, cmCommand*>::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<cmCommand*>::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)

+ 2 - 3
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<std::string> GetCommandNames() const;
 
@@ -166,7 +164,8 @@ private:
 
   std::map<cmProperty::ScopeType, cmPropertyDefinitionMap> PropertyDefinitions;
   std::vector<std::string> EnabledLanguages;
-  std::map<std::string, cmCommand*> Commands;
+  std::map<std::string, cmCommand*> BuiltinCommands;
+  std::map<std::string, cmCommand*> ScriptedCommands;
   cmPropertyMap GlobalProperties;
   cmCacheManager* CacheManager;