cmStandardIncludes.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. /**
  11. * Include header files as a function of the build process, compiler,
  12. * and operating system.
  13. */
  14. #ifndef cmStandardIncludes_h
  15. #define cmStandardIncludes_h
  16. // include configure generated header to define CMAKE_NO_ANSI_STREAM_HEADERS,
  17. // CMAKE_NO_STD_NAMESPACE, and other macros.
  18. #include <cmConfigure.h>
  19. #include <cmsys/Configure.hxx>
  20. #ifdef _MSC_VER
  21. #pragma warning ( disable : 4786 )
  22. #pragma warning ( disable : 4503 )
  23. #pragma warning ( disable : 4512 ) /* operator=() could not be generated */
  24. #define CMAKE_NO_ANSI_FOR_SCOPE
  25. #endif
  26. #ifdef __ICL
  27. #pragma warning ( disable : 985 )
  28. #pragma warning ( disable : 1572 ) /* floating-point equality test */
  29. #endif
  30. // Provide fixed-size integer types.
  31. #include <cmIML/INT.h>
  32. // Include stream compatibility layer from KWSys.
  33. // This is needed to work with large file support
  34. // on some platforms whose stream operators do not
  35. // support the large integer types.
  36. #if defined(CMAKE_BUILD_WITH_CMAKE)
  37. # include <cmsys/IOStream.hxx>
  38. #endif
  39. // Avoid warnings in system headers.
  40. #if defined(_MSC_VER)
  41. # pragma warning (push,1)
  42. #endif
  43. #ifndef CMAKE_NO_ANSI_STREAM_HEADERS
  44. # include <fstream>
  45. # include <iostream>
  46. # include <iomanip>
  47. #else
  48. # include <fstream.h>
  49. # include <iostream.h>
  50. # include <iomanip.h>
  51. #endif
  52. #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
  53. # include <sstream>
  54. #elif !defined(CMAKE_NO_ANSI_STREAM_HEADERS)
  55. # include <strstream>
  56. #else
  57. # include <strstream.h>
  58. #endif
  59. // we must have stl with the standard include style
  60. #include <vector>
  61. #include <string>
  62. #include <iterator>
  63. #include <algorithm>
  64. #include <functional>
  65. #include <map>
  66. #include <list>
  67. #include <set>
  68. #include <deque>
  69. #if defined(_MSC_VER)
  70. # pragma warning(pop)
  71. #endif
  72. // include the "c" string header
  73. #include <string.h>
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. // if std:: is not supported, then just #define it away
  77. #ifdef CMAKE_NO_STD_NAMESPACE
  78. #define std
  79. #endif
  80. // if the compiler does not support ansi for scoping of vars use a
  81. // #define hack
  82. #ifdef CMAKE_NO_ANSI_FOR_SCOPE
  83. #define for if(false) {} else for
  84. #endif
  85. #if defined( _MSC_VER )
  86. typedef unsigned short mode_t;
  87. #endif
  88. // use this class to shrink the size of symbols in .o files
  89. // std::string is really basic_string<....lots of stuff....>
  90. // when combined with a map or set, the symbols can be > 2000 chars!
  91. #include <cmsys/String.hxx>
  92. //typedef cmsys::String std::string;
  93. // Define cmOStringStream and cmIStringStream wrappers to hide
  94. // differences between std::stringstream and the old strstream.
  95. #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
  96. class cmOStringStream: public std::ostringstream
  97. {
  98. public:
  99. cmOStringStream();
  100. ~cmOStringStream();
  101. private:
  102. cmOStringStream(const cmOStringStream&);
  103. void operator=(const cmOStringStream&);
  104. };
  105. class cmIStringStream: public std::istringstream
  106. {
  107. public:
  108. typedef std::istringstream Superclass;
  109. cmIStringStream() {}
  110. cmIStringStream(const std::string& s): Superclass(s) {}
  111. private:
  112. cmIStringStream(const cmIStringStream&);
  113. void operator=(const cmIStringStream&);
  114. };
  115. #else
  116. class cmOStrStreamCleanup
  117. {
  118. public:
  119. cmOStrStreamCleanup(std::ostrstream& ostr): OStrStream(ostr) {}
  120. ~cmOStrStreamCleanup() { this->OStrStream.rdbuf()->freeze(0); }
  121. static void IgnoreUnusedVariable(const cmOStrStreamCleanup&) {}
  122. protected:
  123. std::ostrstream& OStrStream;
  124. };
  125. class cmOStringStream: public std::ostrstream
  126. {
  127. public:
  128. typedef std::ostrstream Superclass;
  129. cmOStringStream() {}
  130. std::string str()
  131. {
  132. cmOStrStreamCleanup cleanup(*this);
  133. cmOStrStreamCleanup::IgnoreUnusedVariable(cleanup);
  134. int pcount = this->pcount();
  135. const char* ptr = this->Superclass::str();
  136. return std::string(ptr?ptr:"", pcount);
  137. }
  138. private:
  139. cmOStringStream(const cmOStringStream&);
  140. void operator=(const cmOStringStream&);
  141. };
  142. class cmIStringStream: private std::string, public std::istrstream
  143. {
  144. public:
  145. typedef std::string StdString;
  146. typedef std::istrstream IStrStream;
  147. cmIStringStream(): StdString(), IStrStream(StdString::c_str()) {}
  148. cmIStringStream(const std::string& s):
  149. StdString(s), IStrStream(StdString::c_str()) {}
  150. std::string str() const { return *this; }
  151. void str(const std::string& s)
  152. {
  153. // Very dangerous. If this throws, the object is hosed. When the
  154. // destructor is later called, the program is hosed too.
  155. this->~cmIStringStream();
  156. new (this) cmIStringStream(s);
  157. }
  158. private:
  159. cmIStringStream(const cmIStringStream&);
  160. void operator=(const cmIStringStream&);
  161. };
  162. #endif
  163. /* Poison this operator to avoid common mistakes. */
  164. extern void operator << (std::ostream&, const cmOStringStream&);
  165. /** Standard documentation entry for cmDocumentation's formatting. */
  166. struct cmDocumentationEntry
  167. {
  168. std::string Name;
  169. std::string Brief;
  170. cmDocumentationEntry(){}
  171. cmDocumentationEntry(const char *doc[2])
  172. { if (doc[0]) this->Name = doc[0];
  173. if (doc[1]) this->Brief = doc[1];}
  174. cmDocumentationEntry(const char *n, const char *b)
  175. { if (n) this->Name = n; if (b) this->Brief = b; }
  176. };
  177. /** Data structure to represent a single command line. */
  178. class cmCustomCommandLine: public std::vector<std::string>
  179. {
  180. public:
  181. typedef std::vector<std::string> Superclass;
  182. typedef Superclass::iterator iterator;
  183. typedef Superclass::const_iterator const_iterator;
  184. };
  185. /** Data structure to represent a list of command lines. */
  186. class cmCustomCommandLines: public std::vector<cmCustomCommandLine>
  187. {
  188. public:
  189. typedef std::vector<cmCustomCommandLine> Superclass;
  190. typedef Superclass::iterator iterator;
  191. typedef Superclass::const_iterator const_iterator;
  192. };
  193. // All subclasses of cmCommand or cmCTestGenericHandler should
  194. // invoke this macro.
  195. #define cmTypeMacro(thisClass,superclass) \
  196. virtual const char* GetNameOfClass() { return #thisClass; } \
  197. typedef superclass Superclass; \
  198. static bool IsTypeOf(const char *type) \
  199. { \
  200. if ( !strcmp(#thisClass,type) ) \
  201. { \
  202. return true; \
  203. } \
  204. return Superclass::IsTypeOf(type); \
  205. } \
  206. virtual bool IsA(const char *type) \
  207. { \
  208. return thisClass::IsTypeOf(type); \
  209. } \
  210. static thisClass* SafeDownCast(cmObject *c) \
  211. { \
  212. if ( c && c->IsA(#thisClass) ) \
  213. { \
  214. return static_cast<thisClass *>(c); \
  215. } \
  216. return 0;\
  217. } \
  218. class cmTypeMacro_UseTrailingSemicolon
  219. inline bool cmHasLiteralPrefixImpl(const std::string &str1,
  220. const char *str2,
  221. size_t N)
  222. {
  223. return strncmp(str1.c_str(), str2, N) == 0;
  224. }
  225. inline bool cmHasLiteralPrefixImpl(const char* str1,
  226. const char *str2,
  227. size_t N)
  228. {
  229. return strncmp(str1, str2, N) == 0;
  230. }
  231. inline bool cmHasLiteralSuffixImpl(const std::string &str1,
  232. const char *str2,
  233. size_t N)
  234. {
  235. size_t len = str1.size();
  236. return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
  237. }
  238. inline bool cmHasLiteralSuffixImpl(const char* str1,
  239. const char* str2,
  240. size_t N)
  241. {
  242. size_t len = strlen(str1);
  243. return len >= N && strcmp(str1 + len - N, str2) == 0;
  244. }
  245. template<typename T, size_t N>
  246. const T* cmArrayBegin(const T (&a)[N]) { return a; }
  247. template<typename T, size_t N>
  248. const T* cmArrayEnd(const T (&a)[N]) { return a + N; }
  249. template<typename T, size_t N>
  250. size_t cmArraySize(const T (&)[N]) { return N; }
  251. template<typename T, size_t N>
  252. bool cmHasLiteralPrefix(T str1, const char (&str2)[N])
  253. {
  254. return cmHasLiteralPrefixImpl(str1, str2, N - 1);
  255. }
  256. template<typename T, size_t N>
  257. bool cmHasLiteralSuffix(T str1, const char (&str2)[N])
  258. {
  259. return cmHasLiteralSuffixImpl(str1, str2, N - 1);
  260. }
  261. struct cmStrCmp {
  262. cmStrCmp(const char *test) : m_test(test) {}
  263. cmStrCmp(const std::string &test) : m_test(test) {}
  264. bool operator()(const std::string& input) const
  265. {
  266. return m_test == input;
  267. }
  268. bool operator()(const char * input) const
  269. {
  270. return strcmp(input, m_test.c_str()) == 0;
  271. }
  272. private:
  273. const std::string m_test;
  274. };
  275. #endif