cmCommand.h 4.1 KB

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