cmCommand.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #ifndef cmCommand_h
  12. #define cmCommand_h
  13. #include "cmStandardIncludes.h"
  14. #include "cmMakefile.h"
  15. /** \class cmCommand
  16. * \brief Superclass for all commands in CMake.
  17. *
  18. * cmCommand is the base class for all commands in CMake. A command
  19. * manifests as an entry in CMakeLists.txt and produces one or
  20. * more makefile rules. Commands are associated with a particular
  21. * makefile. This base class cmCommand defines the API for commands
  22. * to support such features as enable/disable, inheritance,
  23. * documentation, and construction.
  24. */
  25. class cmCommand
  26. {
  27. public:
  28. /**
  29. * Construct the command. By default it is enabled with no makefile.
  30. */
  31. cmCommand()
  32. {m_Makefile = 0; m_Enabled = true;}
  33. /**
  34. * Need virtual destructor to destroy real command type.
  35. */
  36. virtual ~cmCommand() {}
  37. /**
  38. * Specify the makefile.
  39. */
  40. void SetMakefile(cmMakefile*m)
  41. {m_Makefile = m; }
  42. /**
  43. * This is called when the command is first encountered in
  44. * the CMakeLists.txt file.
  45. */
  46. virtual bool Invoke(std::vector<std::string>& args) = 0;
  47. /**
  48. * This is called at the end after all the information
  49. * specified by the command is accumulated. Most commands do
  50. * not implement this method. At this point, reading and
  51. * writing to the cache can be done.
  52. */
  53. virtual void FinalPass() {};
  54. /**
  55. * This is a virtual constructor for the command.
  56. */
  57. virtual cmCommand* Clone() = 0;
  58. /**
  59. * This determines if the command gets propagated down
  60. * to makefiles located in subdirectories.
  61. */
  62. virtual bool IsInherited()
  63. {
  64. return false;
  65. }
  66. /**
  67. * The name of the command as specified in CMakeList.txt.
  68. */
  69. virtual const char* GetName() = 0;
  70. /**
  71. * Succinct documentation.
  72. */
  73. virtual const char* GetTerseDocumentation() = 0;
  74. /**
  75. * More documentation.
  76. */
  77. virtual const char* GetFullDocumentation() = 0;
  78. /**
  79. * Enable the command.
  80. */
  81. void EnabledOn()
  82. {m_Enabled = true;}
  83. /**
  84. * Disable the command.
  85. */
  86. void EnabledOff()
  87. {m_Enabled = false;}
  88. /**
  89. * Query whether the command is enabled.
  90. */
  91. bool GetEnabled()
  92. {return m_Enabled;}
  93. /**
  94. * Disable or enable the command.
  95. */
  96. void SetEnabled(bool enabled)
  97. {m_Enabled = enabled;}
  98. /**
  99. * Return the last error string.
  100. */
  101. const char* GetError()
  102. {return m_Error.c_str();}
  103. /**
  104. * Returns true if this class is the given class, or a subclass of it.
  105. */
  106. static bool IsTypeOf(const char *type)
  107. { return !strcmp("cmCommand", type); }
  108. /**
  109. * Returns true if this object is an instance of the given class or
  110. * a subclass of it.
  111. */
  112. virtual bool IsA(const char *type)
  113. { return cmCommand::IsTypeOf(type); }
  114. protected:
  115. void SetError(const char* e)
  116. {
  117. m_Error = this->GetName();
  118. m_Error += " ";
  119. m_Error += e;
  120. }
  121. cmMakefile* m_Makefile;
  122. private:
  123. bool m_Enabled;
  124. std::string m_Error;
  125. };
  126. // All subclasses of cmCommand should invoke this macro.
  127. #define cmTypeMacro(thisClass,superclass) \
  128. static bool IsTypeOf(const char *type) \
  129. { \
  130. if ( !strcmp(#thisClass,type) ) \
  131. { \
  132. return true; \
  133. } \
  134. return superclass::IsTypeOf(type); \
  135. } \
  136. virtual bool IsA(const char *type) \
  137. { \
  138. return thisClass::IsTypeOf(type); \
  139. } \
  140. static thisClass* SafeDownCast(cmCommand *c) \
  141. { \
  142. if ( c && c->IsA(#thisClass) ) \
  143. { \
  144. return (thisClass *)c; \
  145. } \
  146. return 0;\
  147. }
  148. #endif