Browse Source

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

Brad King 25 years ago
parent
commit
463e466be3

+ 2 - 0
Source/cmAbstractFilesCommand.h

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

+ 2 - 0
Source/cmAddTargetCommand.h

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

+ 2 - 0
Source/cmAuxSourceDirectoryCommand.h

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

+ 37 - 0
Source/cmCommand.h

@@ -122,6 +122,19 @@ public:
   const char* GetError() 
     {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:
   void SetError(const char* e)
     {
@@ -136,4 +149,28 @@ private:
   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

+ 2 - 0
Source/cmExecutablesCommand.h

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

+ 2 - 0
Source/cmFindIncludeCommand.h

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

+ 2 - 0
Source/cmFindLibraryCommand.h

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

+ 2 - 0
Source/cmFindProgramCommand.h

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

+ 2 - 0
Source/cmIncludeDirectoryCommand.h

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

+ 2 - 0
Source/cmLibraryCommand.h

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

+ 2 - 0
Source/cmLinkDirectoriesCommand.h

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

+ 2 - 0
Source/cmLinkLibrariesCommand.h

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

+ 2 - 0
Source/cmProjectCommand.h

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

+ 2 - 0
Source/cmSourceFilesCommand.h

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

+ 2 - 0
Source/cmSourceFilesRequireCommand.h

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

+ 2 - 0
Source/cmSubdirCommand.h

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

+ 2 - 0
Source/cmTestsCommand.h

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

+ 2 - 0
Source/cmUnixDefinesCommand.h

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

+ 2 - 0
Source/cmUnixLibrariesCommand.h

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

+ 2 - 0
Source/cmWin32DefinesCommand.h

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

+ 2 - 0
Source/cmWin32LibrariesCommand.h

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