cmCommand.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. {
  103. if(m_Error.length() == 0)
  104. {
  105. std::string m_Error = this->GetName();
  106. m_Error += " uknown error.";
  107. }
  108. return m_Error.c_str();
  109. }
  110. /**
  111. * Returns true if this class is the given class, or a subclass of it.
  112. */
  113. static bool IsTypeOf(const char *type)
  114. { return !strcmp("cmCommand", type); }
  115. /**
  116. * Returns true if this object is an instance of the given class or
  117. * a subclass of it.
  118. */
  119. virtual bool IsA(const char *type)
  120. { return cmCommand::IsTypeOf(type); }
  121. protected:
  122. void SetError(const char* e)
  123. {
  124. m_Error = this->GetName();
  125. m_Error += " ";
  126. m_Error += e;
  127. }
  128. cmMakefile* m_Makefile;
  129. private:
  130. bool m_Enabled;
  131. std::string m_Error;
  132. };
  133. // All subclasses of cmCommand should invoke this macro.
  134. #define cmTypeMacro(thisClass,superclass) \
  135. static bool IsTypeOf(const char *type) \
  136. { \
  137. if ( !strcmp(#thisClass,type) ) \
  138. { \
  139. return true; \
  140. } \
  141. return superclass::IsTypeOf(type); \
  142. } \
  143. virtual bool IsA(const char *type) \
  144. { \
  145. return thisClass::IsTypeOf(type); \
  146. } \
  147. static thisClass* SafeDownCast(cmCommand *c) \
  148. { \
  149. if ( c && c->IsA(#thisClass) ) \
  150. { \
  151. return (thisClass *)c; \
  152. } \
  153. return 0;\
  154. }
  155. #endif