ENH: Added safe downcast support (without RTTI) to cmCommand and its subclasses.

This commit is contained in:
Brad King 2001-02-26 12:07:53 -05:00
parent d31ce24413
commit 463e466be3
21 changed files with 77 additions and 0 deletions

View File

@ -54,6 +54,8 @@ public:
return return
"ABSTRACT_FILES(file1 file2 ..)"; "ABSTRACT_FILES(file1 file2 ..)";
} }
cmTypeMacro(cmAbstractFilesCommand, cmCommand);
}; };

View File

@ -65,6 +65,8 @@ public:
return return
"ADD_TARGET(Name \"command to run\")"; "ADD_TARGET(Name \"command to run\")";
} }
cmTypeMacro(cmAddTargetCommand, cmCommand);
}; };
#endif #endif

View File

@ -67,6 +67,8 @@ public:
return return
"AUX_SOURCE_DIRECTORY(dir)"; "AUX_SOURCE_DIRECTORY(dir)";
} }
cmTypeMacro(cmAuxSourceDirectoryCommand, cmCommand);
}; };

View File

@ -122,6 +122,19 @@ public:
const char* GetError() const char* GetError()
{return m_Error.c_str();} {return m_Error.c_str();}
/**
* Returns true if this class is the given class, or a subclass of it.
*/
static bool IsTypeOf(const char *type)
{ return !strcmp("cmCommand", type); }
/**
* Returns true if this object is an instance of the given class or
* a subclass of it.
*/
virtual bool IsA(const char *type)
{ return cmCommand::IsTypeOf(type); }
protected: protected:
void SetError(const char* e) void SetError(const char* e)
{ {
@ -136,4 +149,28 @@ private:
std::string m_Error; std::string m_Error;
}; };
// All subclasses of cmCommand should invoke this macro.
#define cmTypeMacro(thisClass,superclass) \
static bool IsTypeOf(const char *type) \
{ \
if ( !strcmp(#thisClass,type) ) \
{ \
return true; \
} \
return superclass::IsTypeOf(type); \
} \
virtual bool IsA(const char *type) \
{ \
return thisClass::IsTypeOf(type); \
} \
static thisClass* SafeDownCast(cmCommand *c) \
{ \
if ( c && c->IsA(#thisClass) ) \
{ \
return (thisClass *)c; \
} \
return 0;\
}
#endif #endif

View File

@ -63,6 +63,8 @@ public:
return return
"EXECUTABLES(file1 file2 ...)"; "EXECUTABLES(file1 file2 ...)";
} }
cmTypeMacro(cmExecutablesCommand, cmCommand);
}; };

View File

@ -70,6 +70,8 @@ public:
return return
"FIND_INCLUDE(DEFINE try1 try2 ...)"; "FIND_INCLUDE(DEFINE try1 try2 ...)";
} }
cmTypeMacro(cmFindIncludeCommand, cmCommand);
}; };

View File

@ -71,6 +71,8 @@ public:
return return
"FIND_LIBRARY(DEFINE libraryName path1 path2 path3...)"; "FIND_LIBRARY(DEFINE libraryName path1 path2 path3...)";
} }
cmTypeMacro(cmFindLibraryCommand, cmCommand);
}; };

View File

@ -71,6 +71,8 @@ public:
return return
"FIND_PROGRAM(NAME executable1 executable2 ...)"; "FIND_PROGRAM(NAME executable1 executable2 ...)";
} }
cmTypeMacro(cmFindProgramCommand, cmCommand);
}; };

View File

@ -69,6 +69,8 @@ public:
return return
"INCLUDE_DIRECTORIES(dir1 dir2 ...)"; "INCLUDE_DIRECTORIES(dir1 dir2 ...)";
} }
cmTypeMacro(cmIncludeDirectoryCommand, cmCommand);
}; };

View File

@ -64,6 +64,8 @@ public:
return return
"LIBRARY(libraryname)"; "LIBRARY(libraryname)";
} }
cmTypeMacro(cmLibraryCommand, cmCommand);
}; };

View File

@ -74,6 +74,8 @@ public:
"The directories can use built in definitions like \n" "The directories can use built in definitions like \n"
"CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR."; "CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR.";
} }
cmTypeMacro(cmLinkDirectoriesCommand, cmCommand);
}; };

View File

@ -76,6 +76,8 @@ public:
"down to all other commands. The library name should be\n" "down to all other commands. The library name should be\n"
"the same as the name used in the LIBRARY(library) command."; "the same as the name used in the LIBRARY(library) command.";
} }
cmTypeMacro(cmLinkLibrariesCommand, cmCommand);
}; };

View File

@ -74,6 +74,8 @@ public:
return return
"PROJECT(projectname) Sets the name of the Microsoft workspace .dsw file. Does nothing on UNIX currently\n"; "PROJECT(projectname) Sets the name of the Microsoft workspace .dsw file. Does nothing on UNIX currently\n";
} }
cmTypeMacro(cmProjectCommand, cmCommand);
}; };

View File

@ -68,6 +68,8 @@ public:
return return
"SOURCE_FILES(file1 file2 ...)"; "SOURCE_FILES(file1 file2 ...)";
} }
cmTypeMacro(cmSourceFilesCommand, cmCommand);
}; };

View File

@ -67,6 +67,8 @@ public:
return return
"SOURCE_FILES_REQUIRE(var1 var2 ... SOURCES_BEGIN file1 file2 ...)"; "SOURCE_FILES_REQUIRE(var1 var2 ... SOURCES_BEGIN file1 file2 ...)";
} }
cmTypeMacro(cmSourceFilesRequireCommand, cmCommand);
}; };

View File

@ -67,6 +67,8 @@ public:
"This will cause any CMakeLists.txt files in the sub directories\n" "This will cause any CMakeLists.txt files in the sub directories\n"
"to be processed by CMake."; "to be processed by CMake.";
} }
cmTypeMacro(cmSubdirCommand, cmCommand);
}; };

View File

@ -67,6 +67,8 @@ public:
return return
"TESTS(file1 file2 ...)"; "TESTS(file1 file2 ...)";
} }
cmTypeMacro(cmTestsCommand, cmCommand);
}; };

View File

@ -75,6 +75,8 @@ public:
"UNIX_DEFINES(-DFOO -DBAR)\n" "UNIX_DEFINES(-DFOO -DBAR)\n"
"Add -D flags to the command line for Unix only."; "Add -D flags to the command line for Unix only.";
} }
cmTypeMacro(cmUnixDefinesCommand, cmCommand);
}; };

View File

@ -74,6 +74,8 @@ public:
return return
"UNIX_LIBRARIES(library -lm ...)"; "UNIX_LIBRARIES(library -lm ...)";
} }
cmTypeMacro(cmUnixLibrariesCommand, cmCommand);
}; };

View File

@ -75,6 +75,8 @@ public:
"WIN32_DEFINES(-DFOO -DBAR ...)\n" "WIN32_DEFINES(-DFOO -DBAR ...)\n"
"Add -D define flags to command line for Win32 environments."; "Add -D define flags to command line for Win32 environments.";
} }
cmTypeMacro(cmWin32DefinesCommand, cmCommand);
}; };

View File

@ -74,6 +74,8 @@ public:
return return
"WIN32_LIBRARIES(library -lm ...)"; "WIN32_LIBRARIES(library -lm ...)";
} }
cmTypeMacro(cmWin32LibrariesCommand, cmCommand);
}; };