cmCommand.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #ifndef cmCommand_h
  33. #define cmCommand_h
  34. #include "cmStandardIncludes.h"
  35. #include "cmMakefile.h"
  36. /** \class cmCommand
  37. * \brief Superclass for all commands in CMake.
  38. *
  39. * cmCommand is the base class for all commands in CMake. A command
  40. * manifests as an entry in CMakeLists.txt and produces one or
  41. * more makefile rules. Commands are associated with a particular
  42. * makefile. This base class cmCommand defines the API for commands
  43. * to support such features as enable/disable, inheritance,
  44. * documentation, and construction.
  45. */
  46. class cmCommand
  47. {
  48. public:
  49. /**
  50. * Construct the command. By default it is enabled with no makefile.
  51. */
  52. cmCommand()
  53. {m_Makefile = 0; m_Enabled = true;}
  54. /**
  55. * Need virtual destructor to destroy real command type.
  56. */
  57. virtual ~cmCommand() {}
  58. /**
  59. * Specify the makefile.
  60. */
  61. void SetMakefile(cmMakefile*m)
  62. {m_Makefile = m; }
  63. /**
  64. * This is called when the command is first encountered in
  65. * the CMakeLists.txt file.
  66. */
  67. virtual bool Invoke(std::vector<std::string>& args) = 0;
  68. /**
  69. * This is called at the end after all the information
  70. * specified by the command is accumulated. Most commands do
  71. * not implement this method. At this point, reading and
  72. * writing to the cache can be done.
  73. */
  74. virtual void FinalPass() {};
  75. /**
  76. * This is a virtual constructor for the command.
  77. */
  78. virtual cmCommand* Clone() = 0;
  79. /**
  80. * This determines if the command gets propagated down
  81. * to makefiles located in subdirectories.
  82. */
  83. virtual bool IsInherited()
  84. {
  85. return false;
  86. }
  87. /**
  88. * The name of the command as specified in CMakeList.txt.
  89. */
  90. virtual const char* GetName() = 0;
  91. /**
  92. * Succinct documentation.
  93. */
  94. virtual const char* GetTerseDocumentation() = 0;
  95. /**
  96. * More documentation.
  97. */
  98. virtual const char* GetFullDocumentation() = 0;
  99. /**
  100. * Enable the command.
  101. */
  102. void EnabledOn()
  103. {m_Enabled = true;}
  104. /**
  105. * Disable the command.
  106. */
  107. void EnabledOff()
  108. {m_Enabled = false;}
  109. /**
  110. * Query whether the command is enabled.
  111. */
  112. bool GetEnabled()
  113. {return m_Enabled;}
  114. /**
  115. * Disable or enable the command.
  116. */
  117. void SetEnabled(bool enabled)
  118. {m_Enabled = enabled;}
  119. /**
  120. * Return the last error string.
  121. */
  122. const char* GetError()
  123. {
  124. if(m_Error.length() == 0)
  125. {
  126. std::string m_Error = this->GetName();
  127. m_Error += " uknown error.";
  128. }
  129. return m_Error.c_str();
  130. }
  131. /**
  132. * Returns true if this class is the given class, or a subclass of it.
  133. */
  134. static bool IsTypeOf(const char *type)
  135. { return !strcmp("cmCommand", type); }
  136. /**
  137. * Returns true if this object is an instance of the given class or
  138. * a subclass of it.
  139. */
  140. virtual bool IsA(const char *type)
  141. { return cmCommand::IsTypeOf(type); }
  142. protected:
  143. void SetError(const char* e)
  144. {
  145. m_Error = this->GetName();
  146. m_Error += " ";
  147. m_Error += e;
  148. }
  149. cmMakefile* m_Makefile;
  150. private:
  151. bool m_Enabled;
  152. std::string m_Error;
  153. };
  154. // All subclasses of cmCommand should invoke this macro.
  155. #define cmTypeMacro(thisClass,superclass) \
  156. static bool IsTypeOf(const char *type) \
  157. { \
  158. if ( !strcmp(#thisClass,type) ) \
  159. { \
  160. return true; \
  161. } \
  162. return superclass::IsTypeOf(type); \
  163. } \
  164. virtual bool IsA(const char *type) \
  165. { \
  166. return thisClass::IsTypeOf(type); \
  167. } \
  168. static thisClass* SafeDownCast(cmCommand *c) \
  169. { \
  170. if ( c && c->IsA(#thisClass) ) \
  171. { \
  172. return (thisClass *)c; \
  173. } \
  174. return 0;\
  175. }
  176. #endif