cmState: Move cmCommand-related methods from cmake class.

This commit is contained in:
Stephen Kelly 2015-04-11 12:30:26 +02:00
parent 97e53ebb3c
commit 96f8c5f9a3
4 changed files with 128 additions and 82 deletions

View File

@ -13,6 +13,10 @@
#include "cmake.h" #include "cmake.h"
#include "cmCacheManager.h" #include "cmCacheManager.h"
#include "cmCommand.h"
#include "cmAlgorithms.h"
#include <assert.h>
cmState::cmState(cmake* cm) cmState::cmState(cmake* cm)
: CMakeInstance(cm), : CMakeInstance(cm),
@ -20,6 +24,11 @@ cmState::cmState(cmake* cm)
{ {
} }
cmState::~cmState()
{
cmDeleteAll(this->Commands);
}
const char* cmCacheEntryTypes[] = const char* cmCacheEntryTypes[] =
{ "BOOL", { "BOOL",
"PATH", "PATH",
@ -274,3 +283,104 @@ void cmState::SetIsInTryCompile(bool b)
{ {
this->IsInTryCompile = 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<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::RemoveUnscriptableCommands()
{
std::vector<std::string> unscriptableCommands;
for (std::map<std::string, cmCommand*>::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<std::string, cmCommand*>::const_iterator pos =
this->Commands.find(sName);
if (pos != this->Commands.end())
{
command = (*pos).second;
}
return command;
}
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.push_back(cmds->first);
}
return commandNames;
}
void cmState::RemoveUserDefinedCommands()
{
for(std::map<std::string, cmCommand*>::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;
}
}
}

View File

@ -16,11 +16,13 @@
#include "cmPropertyDefinitionMap.h" #include "cmPropertyDefinitionMap.h"
class cmake; class cmake;
class cmCommand;
class cmState class cmState
{ {
public: public:
cmState(cmake* cm); cmState(cmake* cm);
~cmState();
enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC, enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
UNINITIALIZED }; UNINITIALIZED };
@ -79,9 +81,17 @@ public:
bool GetIsInTryCompile() const; bool GetIsInTryCompile() const;
void SetIsInTryCompile(bool b); 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<std::string> GetCommandNames() const;
private: private:
std::map<cmProperty::ScopeType, cmPropertyDefinitionMap> PropertyDefinitions; std::map<cmProperty::ScopeType, cmPropertyDefinitionMap> PropertyDefinitions;
std::vector<std::string> EnabledLanguages; std::vector<std::string> EnabledLanguages;
std::map<std::string, cmCommand*> Commands;
cmake* CMakeInstance; cmake* CMakeInstance;
bool IsInTryCompile; bool IsInTryCompile;
}; };

View File

@ -179,7 +179,6 @@ cmake::~cmake()
delete this->GlobalGenerator; delete this->GlobalGenerator;
this->GlobalGenerator = 0; this->GlobalGenerator = 0;
} }
cmDeleteAll(this->Commands);
cmDeleteAll(this->Generators); cmDeleteAll(this->Generators);
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
delete this->VariableWatch; delete this->VariableWatch;
@ -197,94 +196,33 @@ void cmake::InitializeProperties()
void cmake::CleanupCommandsAndMacros() void cmake::CleanupCommandsAndMacros()
{ {
this->InitializeProperties(); this->InitializeProperties();
for(RegisteredCommandsMap::iterator j = this->Commands.begin(); this->State->RemoveUserDefinedCommands();
j != this->Commands.end(); )
{
if (j->second->IsA("cmMacroHelperCommand") ||
j->second->IsA("cmFunctionHelperCommand"))
{
delete j->second;
this->Commands.erase(j++);
}
else
{
++j;
}
}
} }
bool cmake::CommandExists(const std::string& name) const 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 *cmake::GetCommand(const std::string& name) const
{ {
cmCommand* command = 0; return this->State->GetCommand(name);
std::string sName = cmSystemTools::LowerCase(name);
RegisteredCommandsMap::const_iterator pos = this->Commands.find(sName);
if (pos != this->Commands.end())
{
command = (*pos).second;
}
return command;
} }
void cmake::RenameCommand(const std::string& oldName, void cmake::RenameCommand(const std::string& oldName,
const std::string& newName) const std::string& newName)
{ {
// if the command already exists, free the old one this->State->RenameCommand(oldName, newName);
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);
} }
void cmake::AddCommand(cmCommand* command) void cmake::AddCommand(cmCommand* command)
{ {
std::string name = cmSystemTools::LowerCase(command->GetName()); this->State->AddCommand(command);
// 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));
} }
void cmake::RemoveUnscriptableCommands() void cmake::RemoveUnscriptableCommands()
{ {
std::vector<std::string> unscriptableCommands; this->State->RemoveUnscriptableCommands();
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;
}
}
} }
// Parse the args // Parse the args
@ -2301,18 +2239,8 @@ const char *cmake::GetProperty(const std::string& prop,
} }
else if ( prop == "COMMANDS" ) else if ( prop == "COMMANDS" )
{ {
cmake::RegisteredCommandsMap::iterator cmds std::vector<std::string> commands = this->State->GetCommandNames();
= this->Commands.begin(); this->SetProperty("COMMANDS", cmJoin(commands, ";").c_str());
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());
} }
else if ( prop == "IN_TRY_COMPILE" ) else if ( prop == "IN_TRY_COMPILE" )
{ {

View File

@ -93,7 +93,6 @@ class cmake
*/ */
FIND_PACKAGE_MODE FIND_PACKAGE_MODE
}; };
typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
typedef std::map<std::string, cmInstalledFile> InstalledFilesMap; typedef std::map<std::string, cmInstalledFile> InstalledFilesMap;
/// Default constructor /// Default constructor
@ -362,7 +361,6 @@ protected:
typedef std::map<std::string, typedef std::map<std::string,
CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap; CreateExtraGeneratorFunctionType> RegisteredExtraGeneratorsMap;
typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector; typedef std::vector<cmGlobalGeneratorFactory*> RegisteredGeneratorsVector;
RegisteredCommandsMap Commands;
RegisteredGeneratorsVector Generators; RegisteredGeneratorsVector Generators;
RegisteredExtraGeneratorsMap ExtraGenerators; RegisteredExtraGeneratorsMap ExtraGenerators;
void AddDefaultCommands(); void AddDefaultCommands();