Refactor TargetTypeNames.

Make it a static method instead of an array. It is safer for the
type checking and if we add a new target type we will be warned to add
a case to the switch.
This commit is contained in:
Nicolas Despres 2011-03-20 17:57:42 +01:00 committed by Peter Collingbourne
parent 89bdc3e213
commit 3db2973bd2
5 changed files with 35 additions and 45 deletions

View File

@ -404,7 +404,7 @@ cmComputeTargetDepends
// Describe the depender.
e << " \"" << depender->GetName() << "\" of type "
<< cmTarget::TargetTypeNames[depender->GetType()] << "\n";
<< cmTarget::GetTargetTypeName(depender->GetType()) << "\n";
// List its dependencies that are inside the component.
EdgeList const& nl = this->InitialGraph[i];

View File

@ -996,7 +996,7 @@ cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
}
if(variable == "TARGET_TYPE")
{
return cmTarget::TargetTypeNames[replaceValues.CMTarget->GetType()];
return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
}
}
if(replaceValues.Output)

View File

@ -1379,7 +1379,7 @@ void cmMakefile::AddLinkLibraryForTarget(const char *target,
{
cmOStringStream e;
e << "Target \"" << lib << "\" of type "
<< cmTarget::TargetTypeNames[static_cast<int>(tgt->GetType())]
<< cmTarget::GetTargetTypeName(tgt->GetType())
<< " may not be linked into another target. "
<< "One may link only to STATIC or SHARED libraries, or "
<< "to executables with the ENABLE_EXPORTS property set.";

View File

@ -25,12 +25,35 @@
#include <queue>
#include <stdlib.h> // required for atof
#include <assert.h>
const char* cmTarget::TargetTypeNames[] = {
"EXECUTABLE", "STATIC_LIBRARY",
"SHARED_LIBRARY", "MODULE_LIBRARY", "UTILITY", "GLOBAL_TARGET",
"INSTALL_FILES", "INSTALL_PROGRAMS", "INSTALL_DIRECTORY",
"UNKNOWN_LIBRARY"
};
const char* cmTarget::GetTargetTypeName(TargetType targetType)
{
switch( targetType )
{
case cmTarget::STATIC_LIBRARY:
return "STATIC_LIBRARY";
case cmTarget::MODULE_LIBRARY:
return "MODULE_LIBRARY";
case cmTarget::SHARED_LIBRARY:
return "SHARED_LIBRARY";
case cmTarget::EXECUTABLE:
return "EXECUTABLE";
case cmTarget::UTILITY:
return "UTILITY";
case cmTarget::GLOBAL_TARGET:
return "GLOBAL_TARGET";
case cmTarget::INSTALL_FILES:
return "INSTALL_FILES";
case cmTarget::INSTALL_PROGRAMS:
return "INSTALL_PROGRAMS";
case cmTarget::INSTALL_DIRECTORY:
return "INSTALL_DIRECTORY";
case cmTarget::UNKNOWN_LIBRARY:
return "UNKNOWN_LIBRARY";
}
assert(0 && "Unexpected target type");
return 0;
}
//----------------------------------------------------------------------------
struct cmTarget::OutputInfo
@ -2346,7 +2369,7 @@ cmTarget::OutputInfo const* cmTarget::GetOutputInfo(const char* config)
std::string msg = "cmTarget::GetOutputInfo called for ";
msg += this->GetName();
msg += " which has type ";
msg += cmTarget::TargetTypeNames[this->GetType()];
msg += cmTarget::GetTargetTypeName(this->GetType());
this->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR, msg);
abort();
return 0;
@ -2645,40 +2668,7 @@ const char *cmTarget::GetProperty(const char* prop,
// the type property returns what type the target is
if (!strcmp(prop,"TYPE"))
{
switch( this->GetType() )
{
case cmTarget::STATIC_LIBRARY:
return "STATIC_LIBRARY";
// break; /* unreachable */
case cmTarget::MODULE_LIBRARY:
return "MODULE_LIBRARY";
// break; /* unreachable */
case cmTarget::SHARED_LIBRARY:
return "SHARED_LIBRARY";
// break; /* unreachable */
case cmTarget::EXECUTABLE:
return "EXECUTABLE";
// break; /* unreachable */
case cmTarget::UTILITY:
return "UTILITY";
// break; /* unreachable */
case cmTarget::GLOBAL_TARGET:
return "GLOBAL_TARGET";
// break; /* unreachable */
case cmTarget::INSTALL_FILES:
return "INSTALL_FILES";
// break; /* unreachable */
case cmTarget::INSTALL_PROGRAMS:
return "INSTALL_PROGRAMS";
// break; /* unreachable */
case cmTarget::INSTALL_DIRECTORY:
return "INSTALL_DIRECTORY";
// break; /* unreachable */
case cmTarget::UNKNOWN_LIBRARY:
return "UNKNOWN_LIBRARY";
// break; /* unreachable */
}
return 0;
return cmTarget::GetTargetTypeName(this->GetType());
}
bool chain = false;
const char *retVal =

View File

@ -62,7 +62,7 @@ public:
SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY,
UNKNOWN_LIBRARY};
static const char* TargetTypeNames[];
static const char* GetTargetTypeName(TargetType targetType);
enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
/**