cmStandardIncludes.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. # undef GetCurrentDirectory // Borland <iosfwd> includes windows.h
  39. #endif
  40. // Avoid warnings in system headers.
  41. #if defined(_MSC_VER)
  42. # pragma warning (push,1)
  43. #endif
  44. #ifndef CMAKE_NO_ANSI_STREAM_HEADERS
  45. # include <fstream>
  46. # include <iostream>
  47. # include <iomanip>
  48. #else
  49. # include <fstream.h>
  50. # include <iostream.h>
  51. # include <iomanip.h>
  52. #endif
  53. #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
  54. # include <sstream>
  55. #elif !defined(CMAKE_NO_ANSI_STREAM_HEADERS)
  56. # include <strstream>
  57. #else
  58. # include <strstream.h>
  59. #endif
  60. // we must have stl with the standard include style
  61. #include <vector>
  62. #include <string>
  63. #include <iterator>
  64. #include <algorithm>
  65. #include <functional>
  66. #include <map>
  67. #include <list>
  68. #include <set>
  69. #include <deque>
  70. #if defined(_MSC_VER)
  71. # pragma warning(pop)
  72. #endif
  73. // include the "c" string header
  74. #include <string.h>
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #if !defined(_WIN32) && defined(__COMO__)
  78. // Hack for como strict mode to avoid defining _SVID_SOURCE or _BSD_SOURCE.
  79. extern "C"
  80. {
  81. extern FILE *popen (__const char *__command, __const char *__modes) __THROW;
  82. extern int pclose (FILE *__stream) __THROW;
  83. extern char *realpath (__const char *__restrict __name,
  84. char *__restrict __resolved) __THROW;
  85. extern char *strdup (__const char *__s) __THROW;
  86. extern int putenv (char *__string) __THROW;
  87. }
  88. #endif
  89. // if std:: is not supported, then just #define it away
  90. #ifdef CMAKE_NO_STD_NAMESPACE
  91. #define std
  92. #endif
  93. // if the compiler does not support ansi for scoping of vars use a
  94. // #define hack
  95. #ifdef CMAKE_NO_ANSI_FOR_SCOPE
  96. #define for if(false) {} else for
  97. #endif
  98. #ifdef __DECCXX_VER
  99. # if __DECCXX_VER <= 60390008
  100. # define CM_HAS_STD_BUT_NOT_FOR_IOSTREAM
  101. # endif
  102. #endif
  103. #if defined( _MSC_VER )
  104. typedef unsigned short mode_t;
  105. #endif
  106. #ifdef CM_HAS_STD_BUT_NOT_FOR_IOSTREAM
  107. // some compilers have std:: but not for the stream library,
  108. // so we have to bring it into the std namespace by hand.
  109. namespace std {
  110. using ::ostream;
  111. using ::istream;
  112. using ::ios;
  113. using ::cout;
  114. using ::cerr;
  115. using ::cin;
  116. using ::ifstream;
  117. using ::ofstream;
  118. #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
  119. using ::ostringstream;
  120. using ::istringstream;
  121. #else
  122. using ::ostrstream;
  123. using ::istrstream;
  124. #endif
  125. using ::endl;
  126. using ::ends;
  127. using ::flush;
  128. using ::dec;
  129. using ::hex;
  130. using ::setw;
  131. using ::setiosflags;
  132. using ::setfill;
  133. using ::setprecision;
  134. }
  135. // The string class is missing these operators so add them
  136. #if !defined(cmsys_STL_STRING_NEQ_CHAR_DEFINED)
  137. # define cmsys_STL_STRING_NO_NEQ_CHAR
  138. inline bool operator!=(std::string const& a, const char* b)
  139. { return !(a==std::string(b)); }
  140. #endif
  141. inline bool operator==(std::string const& a, const char* b)
  142. { return (a==std::string(b)); }
  143. # endif // end CM_HAS_STD_BUT_NOT_FOR_IOSTREAM
  144. // use this class to shrink the size of symbols in .o files
  145. // std::string is really basic_string<....lots of stuff....>
  146. // when combined with a map or set, the symbols can be > 2000 chars!
  147. #include <cmsys/String.hxx>
  148. //typedef cmsys::String std::string;
  149. // Define cmOStringStream and cmIStringStream wrappers to hide
  150. // differences between std::stringstream and the old strstream.
  151. #if !defined(CMAKE_NO_ANSI_STRING_STREAM)
  152. class cmOStringStream: public std::ostringstream
  153. {
  154. public:
  155. cmOStringStream();
  156. ~cmOStringStream();
  157. private:
  158. cmOStringStream(const cmOStringStream&);
  159. void operator=(const cmOStringStream&);
  160. };
  161. class cmIStringStream: public std::istringstream
  162. {
  163. public:
  164. typedef std::istringstream Superclass;
  165. cmIStringStream() {}
  166. cmIStringStream(const std::string& s): Superclass(s) {}
  167. private:
  168. cmIStringStream(const cmIStringStream&);
  169. void operator=(const cmIStringStream&);
  170. };
  171. #else
  172. class cmOStrStreamCleanup
  173. {
  174. public:
  175. cmOStrStreamCleanup(std::ostrstream& ostr): OStrStream(ostr) {}
  176. ~cmOStrStreamCleanup() { this->OStrStream.rdbuf()->freeze(0); }
  177. static void IgnoreUnusedVariable(const cmOStrStreamCleanup&) {}
  178. protected:
  179. std::ostrstream& OStrStream;
  180. };
  181. class cmOStringStream: public std::ostrstream
  182. {
  183. public:
  184. typedef std::ostrstream Superclass;
  185. cmOStringStream() {}
  186. std::string str()
  187. {
  188. cmOStrStreamCleanup cleanup(*this);
  189. cmOStrStreamCleanup::IgnoreUnusedVariable(cleanup);
  190. int pcount = this->pcount();
  191. const char* ptr = this->Superclass::str();
  192. return std::string(ptr?ptr:"", pcount);
  193. }
  194. private:
  195. cmOStringStream(const cmOStringStream&);
  196. void operator=(const cmOStringStream&);
  197. };
  198. class cmIStringStream: private std::string, public std::istrstream
  199. {
  200. public:
  201. typedef std::string StdString;
  202. typedef std::istrstream IStrStream;
  203. cmIStringStream(): StdString(), IStrStream(StdString::c_str()) {}
  204. cmIStringStream(const std::string& s):
  205. StdString(s), IStrStream(StdString::c_str()) {}
  206. std::string str() const { return *this; }
  207. void str(const std::string& s)
  208. {
  209. // Very dangerous. If this throws, the object is hosed. When the
  210. // destructor is later called, the program is hosed too.
  211. this->~cmIStringStream();
  212. new (this) cmIStringStream(s);
  213. }
  214. private:
  215. cmIStringStream(const cmIStringStream&);
  216. void operator=(const cmIStringStream&);
  217. };
  218. #endif
  219. /* Poison this operator to avoid common mistakes. */
  220. extern void operator << (std::ostream&, const cmOStringStream&);
  221. /** Standard documentation entry for cmDocumentation's formatting. */
  222. struct cmDocumentationEntry
  223. {
  224. std::string Name;
  225. std::string Brief;
  226. cmDocumentationEntry(){}
  227. cmDocumentationEntry(const char *doc[2])
  228. { if (doc[0]) this->Name = doc[0];
  229. if (doc[1]) this->Brief = doc[1];}
  230. cmDocumentationEntry(const char *n, const char *b)
  231. { if (n) this->Name = n; if (b) this->Brief = b; }
  232. };
  233. /** Data structure to represent a single command line. */
  234. class cmCustomCommandLine: public std::vector<std::string>
  235. {
  236. public:
  237. typedef std::vector<std::string> Superclass;
  238. typedef Superclass::iterator iterator;
  239. typedef Superclass::const_iterator const_iterator;
  240. };
  241. /** Data structure to represent a list of command lines. */
  242. class cmCustomCommandLines: public std::vector<cmCustomCommandLine>
  243. {
  244. public:
  245. typedef std::vector<cmCustomCommandLine> Superclass;
  246. typedef Superclass::iterator iterator;
  247. typedef Superclass::const_iterator const_iterator;
  248. };
  249. // All subclasses of cmCommand or cmCTestGenericHandler should
  250. // invoke this macro.
  251. #define cmTypeMacro(thisClass,superclass) \
  252. virtual const char* GetNameOfClass() { return #thisClass; } \
  253. typedef superclass Superclass; \
  254. static bool IsTypeOf(const char *type) \
  255. { \
  256. if ( !strcmp(#thisClass,type) ) \
  257. { \
  258. return true; \
  259. } \
  260. return Superclass::IsTypeOf(type); \
  261. } \
  262. virtual bool IsA(const char *type) \
  263. { \
  264. return thisClass::IsTypeOf(type); \
  265. } \
  266. static thisClass* SafeDownCast(cmObject *c) \
  267. { \
  268. if ( c && c->IsA(#thisClass) ) \
  269. { \
  270. return static_cast<thisClass *>(c); \
  271. } \
  272. return 0;\
  273. } \
  274. class cmTypeMacro_UseTrailingSemicolon
  275. inline bool cmHasLiteralPrefixImpl(const std::string &str1,
  276. const char *str2,
  277. size_t N)
  278. {
  279. return strncmp(str1.c_str(), str2, N) == 0;
  280. }
  281. inline bool cmHasLiteralPrefixImpl(const char* str1,
  282. const char *str2,
  283. size_t N)
  284. {
  285. return strncmp(str1, str2, N) == 0;
  286. }
  287. inline bool cmHasLiteralSuffixImpl(const std::string &str1,
  288. const char *str2,
  289. size_t N)
  290. {
  291. size_t len = str1.size();
  292. return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
  293. }
  294. inline bool cmHasLiteralSuffixImpl(const char* str1,
  295. const char* str2,
  296. size_t N)
  297. {
  298. size_t len = strlen(str1);
  299. return len >= N && strcmp(str1 + len - N, str2) == 0;
  300. }
  301. template<typename T, size_t N>
  302. const T* cmArrayBegin(const T (&a)[N]) { return a; }
  303. template<typename T, size_t N>
  304. const T* cmArrayEnd(const T (&a)[N]) { return a + N; }
  305. template<typename T, size_t N>
  306. size_t cmArraySize(const T (&)[N]) { return N; }
  307. template<typename T, size_t N>
  308. bool cmHasLiteralPrefix(T str1, const char (&str2)[N])
  309. {
  310. return cmHasLiteralPrefixImpl(str1, str2, N - 1);
  311. }
  312. template<typename T, size_t N>
  313. bool cmHasLiteralSuffix(T str1, const char (&str2)[N])
  314. {
  315. return cmHasLiteralSuffixImpl(str1, str2, N - 1);
  316. }
  317. struct cmStrCmp {
  318. cmStrCmp(const char *test) : m_test(test) {}
  319. cmStrCmp(const std::string &test) : m_test(test) {}
  320. bool operator()(const std::string& input) const
  321. {
  322. return m_test == input;
  323. }
  324. bool operator()(const char * input) const
  325. {
  326. return strcmp(input, m_test.c_str()) == 0;
  327. }
  328. private:
  329. const std::string m_test;
  330. };
  331. #endif