PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/testlibs/gtest/include/gtest/internal/gtest-port.h

https://github.com/deltaforge/nebu-app-mongo
C Header | 1775 lines | 909 code | 211 blank | 655 comment | 70 complexity | bc05970d66bf647989b4c65b44d97792 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Authors: wan@google.com (Zhanyong Wan)
  31. //
  32. // Low-level types and utilities for porting Google Test to various
  33. // platforms. They are subject to change without notice. DO NOT USE
  34. // THEM IN USER CODE.
  35. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
  36. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
  37. // The user can define the following macros in the build script to
  38. // control Google Test's behavior. If the user doesn't define a macro
  39. // in this list, Google Test will define it.
  40. //
  41. // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
  42. // is/isn't available.
  43. // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
  44. // are enabled.
  45. // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
  46. // is/isn't available (some systems define
  47. // ::string, which is different to std::string).
  48. // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
  49. // is/isn't available (some systems define
  50. // ::wstring, which is different to std::wstring).
  51. // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular
  52. // expressions are/aren't available.
  53. // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
  54. // is/isn't available.
  55. // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
  56. // enabled.
  57. // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
  58. // std::wstring does/doesn't work (Google Test can
  59. // be used where std::wstring is unavailable).
  60. // GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
  61. // is/isn't available.
  62. // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
  63. // compiler supports Microsoft's "Structured
  64. // Exception Handling".
  65. // GTEST_HAS_STREAM_REDIRECTION
  66. // - Define it to 1/0 to indicate whether the
  67. // platform supports I/O stream redirection using
  68. // dup() and dup2().
  69. // GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
  70. // Test's own tr1 tuple implementation should be
  71. // used. Unused when the user sets
  72. // GTEST_HAS_TR1_TUPLE to 0.
  73. // GTEST_LINKED_AS_SHARED_LIBRARY
  74. // - Define to 1 when compiling tests that use
  75. // Google Test as a shared library (known as
  76. // DLL on Windows).
  77. // GTEST_CREATE_SHARED_LIBRARY
  78. // - Define to 1 when compiling Google Test itself
  79. // as a shared library.
  80. // This header defines the following utilities:
  81. //
  82. // Macros indicating the current platform (defined to 1 if compiled on
  83. // the given platform; otherwise undefined):
  84. // GTEST_OS_AIX - IBM AIX
  85. // GTEST_OS_CYGWIN - Cygwin
  86. // GTEST_OS_HPUX - HP-UX
  87. // GTEST_OS_LINUX - Linux
  88. // GTEST_OS_LINUX_ANDROID - Google Android
  89. // GTEST_OS_MAC - Mac OS X
  90. // GTEST_OS_NACL - Google Native Client (NaCl)
  91. // GTEST_OS_SOLARIS - Sun Solaris
  92. // GTEST_OS_SYMBIAN - Symbian
  93. // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
  94. // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
  95. // GTEST_OS_WINDOWS_MINGW - MinGW
  96. // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
  97. // GTEST_OS_ZOS - z/OS
  98. //
  99. // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
  100. // most stable support. Since core members of the Google Test project
  101. // don't have access to other platforms, support for them may be less
  102. // stable. If you notice any problems on your platform, please notify
  103. // googletestframework@googlegroups.com (patches for fixing them are
  104. // even more welcome!).
  105. //
  106. // Note that it is possible that none of the GTEST_OS_* macros are defined.
  107. //
  108. // Macros indicating available Google Test features (defined to 1 if
  109. // the corresponding feature is supported; otherwise undefined):
  110. // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
  111. // tests)
  112. // GTEST_HAS_DEATH_TEST - death tests
  113. // GTEST_HAS_PARAM_TEST - value-parameterized tests
  114. // GTEST_HAS_TYPED_TEST - typed tests
  115. // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
  116. // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
  117. // GTEST_HAS_POSIX_RE (see above) which users can
  118. // define themselves.
  119. // GTEST_USES_SIMPLE_RE - our own simple regex is used;
  120. // the above two are mutually exclusive.
  121. // GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
  122. //
  123. // Macros for basic C++ coding:
  124. // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
  125. // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
  126. // variable don't have to be used.
  127. // GTEST_DISALLOW_ASSIGN_ - disables operator=.
  128. // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
  129. // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
  130. //
  131. // Synchronization:
  132. // Mutex, MutexLock, ThreadLocal, GetThreadCount()
  133. // - synchronization primitives.
  134. // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
  135. // synchronization primitives have real implementations
  136. // and Google Test is thread-safe; or 0 otherwise.
  137. //
  138. // Template meta programming:
  139. // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
  140. // IteratorTraits - partial implementation of std::iterator_traits, which
  141. // is not available in libCstd when compiled with Sun C++.
  142. //
  143. // Smart pointers:
  144. // scoped_ptr - as in TR2.
  145. //
  146. // Regular expressions:
  147. // RE - a simple regular expression class using the POSIX
  148. // Extended Regular Expression syntax on UNIX-like
  149. // platforms, or a reduced regular exception syntax on
  150. // other platforms, including Windows.
  151. //
  152. // Logging:
  153. // GTEST_LOG_() - logs messages at the specified severity level.
  154. // LogToStderr() - directs all log messages to stderr.
  155. // FlushInfoLog() - flushes informational log messages.
  156. //
  157. // Stdout and stderr capturing:
  158. // CaptureStdout() - starts capturing stdout.
  159. // GetCapturedStdout() - stops capturing stdout and returns the captured
  160. // string.
  161. // CaptureStderr() - starts capturing stderr.
  162. // GetCapturedStderr() - stops capturing stderr and returns the captured
  163. // string.
  164. //
  165. // Integer types:
  166. // TypeWithSize - maps an integer to a int type.
  167. // Int32, UInt32, Int64, UInt64, TimeInMillis
  168. // - integers of known sizes.
  169. // BiggestInt - the biggest signed integer type.
  170. //
  171. // Command-line utilities:
  172. // GTEST_FLAG() - references a flag.
  173. // GTEST_DECLARE_*() - declares a flag.
  174. // GTEST_DEFINE_*() - defines a flag.
  175. // GetArgvs() - returns the command line as a vector of strings.
  176. //
  177. // Environment variable utilities:
  178. // GetEnv() - gets the value of an environment variable.
  179. // BoolFromGTestEnv() - parses a bool environment variable.
  180. // Int32FromGTestEnv() - parses an Int32 environment variable.
  181. // StringFromGTestEnv() - parses a string environment variable.
  182. #include <ctype.h> // for isspace, etc
  183. #include <stddef.h> // for ptrdiff_t
  184. #include <stdlib.h>
  185. #include <stdio.h>
  186. #include <string.h>
  187. #ifndef _WIN32_WCE
  188. # include <sys/types.h>
  189. # include <sys/stat.h>
  190. #endif // !_WIN32_WCE
  191. #include <iostream> // NOLINT
  192. #include <sstream> // NOLINT
  193. #include <string> // NOLINT
  194. #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
  195. #define GTEST_FLAG_PREFIX_ "gtest_"
  196. #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
  197. #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
  198. #define GTEST_NAME_ "Google Test"
  199. #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
  200. // Determines the version of gcc that is used to compile this.
  201. #ifdef __GNUC__
  202. // 40302 means version 4.3.2.
  203. # define GTEST_GCC_VER_ \
  204. (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
  205. #endif // __GNUC__
  206. // Determines the platform on which Google Test is compiled.
  207. #ifdef __CYGWIN__
  208. # define GTEST_OS_CYGWIN 1
  209. #elif defined __SYMBIAN32__
  210. # define GTEST_OS_SYMBIAN 1
  211. #elif defined _WIN32
  212. # define GTEST_OS_WINDOWS 1
  213. # ifdef _WIN32_WCE
  214. # define GTEST_OS_WINDOWS_MOBILE 1
  215. # elif defined(__MINGW__) || defined(__MINGW32__)
  216. # define GTEST_OS_WINDOWS_MINGW 1
  217. # else
  218. # define GTEST_OS_WINDOWS_DESKTOP 1
  219. # endif // _WIN32_WCE
  220. #elif defined __APPLE__
  221. # define GTEST_OS_MAC 1
  222. #elif defined __linux__
  223. # define GTEST_OS_LINUX 1
  224. # ifdef ANDROID
  225. # define GTEST_OS_LINUX_ANDROID 1
  226. # endif // ANDROID
  227. #elif defined __MVS__
  228. # define GTEST_OS_ZOS 1
  229. #elif defined(__sun) && defined(__SVR4)
  230. # define GTEST_OS_SOLARIS 1
  231. #elif defined(_AIX)
  232. # define GTEST_OS_AIX 1
  233. #elif defined(__hpux)
  234. # define GTEST_OS_HPUX 1
  235. #elif defined __native_client__
  236. # define GTEST_OS_NACL 1
  237. #endif // __CYGWIN__
  238. // Brings in definitions for functions used in the testing::internal::posix
  239. // namespace (read, write, close, chdir, isatty, stat). We do not currently
  240. // use them on Windows Mobile.
  241. #if !GTEST_OS_WINDOWS
  242. // This assumes that non-Windows OSes provide unistd.h. For OSes where this
  243. // is not the case, we need to include headers that provide the functions
  244. // mentioned above.
  245. # include <unistd.h>
  246. # if !GTEST_OS_NACL
  247. // TODO(vladl@google.com): Remove this condition when Native Client SDK adds
  248. // strings.h (tracked in
  249. // http://code.google.com/p/nativeclient/issues/detail?id=1175).
  250. # include <strings.h> // Native Client doesn't provide strings.h.
  251. # endif
  252. #elif !GTEST_OS_WINDOWS_MOBILE
  253. # include <direct.h>
  254. # include <io.h>
  255. #endif
  256. // Defines this to true iff Google Test can use POSIX regular expressions.
  257. #ifndef GTEST_HAS_POSIX_RE
  258. # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
  259. #endif
  260. #if GTEST_HAS_POSIX_RE
  261. // On some platforms, <regex.h> needs someone to define size_t, and
  262. // won't compile otherwise. We can #include it here as we already
  263. // included <stdlib.h>, which is guaranteed to define size_t through
  264. // <stddef.h>.
  265. # include <regex.h> // NOLINT
  266. # define GTEST_USES_POSIX_RE 1
  267. #elif GTEST_OS_WINDOWS
  268. // <regex.h> is not available on Windows. Use our own simple regex
  269. // implementation instead.
  270. # define GTEST_USES_SIMPLE_RE 1
  271. #else
  272. // <regex.h> may not be available on this platform. Use our own
  273. // simple regex implementation instead.
  274. # define GTEST_USES_SIMPLE_RE 1
  275. #endif // GTEST_HAS_POSIX_RE
  276. #ifndef GTEST_HAS_EXCEPTIONS
  277. // The user didn't tell us whether exceptions are enabled, so we need
  278. // to figure it out.
  279. # if defined(_MSC_VER) || defined(__BORLANDC__)
  280. // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
  281. // macro to enable exceptions, so we'll do the same.
  282. // Assumes that exceptions are enabled by default.
  283. # ifndef _HAS_EXCEPTIONS
  284. # define _HAS_EXCEPTIONS 1
  285. # endif // _HAS_EXCEPTIONS
  286. # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
  287. # elif defined(__GNUC__) && __EXCEPTIONS
  288. // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
  289. # define GTEST_HAS_EXCEPTIONS 1
  290. # elif defined(__SUNPRO_CC)
  291. // Sun Pro CC supports exceptions. However, there is no compile-time way of
  292. // detecting whether they are enabled or not. Therefore, we assume that
  293. // they are enabled unless the user tells us otherwise.
  294. # define GTEST_HAS_EXCEPTIONS 1
  295. # elif defined(__IBMCPP__) && __EXCEPTIONS
  296. // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
  297. # define GTEST_HAS_EXCEPTIONS 1
  298. # elif defined(__HP_aCC)
  299. // Exception handling is in effect by default in HP aCC compiler. It has to
  300. // be turned of by +noeh compiler option if desired.
  301. # define GTEST_HAS_EXCEPTIONS 1
  302. # else
  303. // For other compilers, we assume exceptions are disabled to be
  304. // conservative.
  305. # define GTEST_HAS_EXCEPTIONS 0
  306. # endif // defined(_MSC_VER) || defined(__BORLANDC__)
  307. #endif // GTEST_HAS_EXCEPTIONS
  308. #if !defined(GTEST_HAS_STD_STRING)
  309. // Even though we don't use this macro any longer, we keep it in case
  310. // some clients still depend on it.
  311. # define GTEST_HAS_STD_STRING 1
  312. #elif !GTEST_HAS_STD_STRING
  313. // The user told us that ::std::string isn't available.
  314. # error "Google Test cannot be used where ::std::string isn't available."
  315. #endif // !defined(GTEST_HAS_STD_STRING)
  316. #ifndef GTEST_HAS_GLOBAL_STRING
  317. // The user didn't tell us whether ::string is available, so we need
  318. // to figure it out.
  319. # define GTEST_HAS_GLOBAL_STRING 0
  320. #endif // GTEST_HAS_GLOBAL_STRING
  321. #ifndef GTEST_HAS_STD_WSTRING
  322. // The user didn't tell us whether ::std::wstring is available, so we need
  323. // to figure it out.
  324. // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
  325. // is available.
  326. // Cygwin 1.7 and below doesn't support ::std::wstring.
  327. // Solaris' libc++ doesn't support it either. Android has
  328. // no support for it at least as recent as Froyo (2.2).
  329. # define GTEST_HAS_STD_WSTRING \
  330. (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
  331. #endif // GTEST_HAS_STD_WSTRING
  332. #ifndef GTEST_HAS_GLOBAL_WSTRING
  333. // The user didn't tell us whether ::wstring is available, so we need
  334. // to figure it out.
  335. # define GTEST_HAS_GLOBAL_WSTRING \
  336. (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
  337. #endif // GTEST_HAS_GLOBAL_WSTRING
  338. // Determines whether RTTI is available.
  339. #ifndef GTEST_HAS_RTTI
  340. // The user didn't tell us whether RTTI is enabled, so we need to
  341. // figure it out.
  342. # ifdef _MSC_VER
  343. # ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
  344. # define GTEST_HAS_RTTI 1
  345. # else
  346. # define GTEST_HAS_RTTI 0
  347. # endif
  348. // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
  349. # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
  350. # ifdef __GXX_RTTI
  351. # define GTEST_HAS_RTTI 1
  352. # else
  353. # define GTEST_HAS_RTTI 0
  354. # endif // __GXX_RTTI
  355. // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
  356. // both the typeid and dynamic_cast features are present.
  357. # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
  358. # ifdef __RTTI_ALL__
  359. # define GTEST_HAS_RTTI 1
  360. # else
  361. # define GTEST_HAS_RTTI 0
  362. # endif
  363. # else
  364. // For all other compilers, we assume RTTI is enabled.
  365. # define GTEST_HAS_RTTI 1
  366. # endif // _MSC_VER
  367. #endif // GTEST_HAS_RTTI
  368. // It's this header's responsibility to #include <typeinfo> when RTTI
  369. // is enabled.
  370. #if GTEST_HAS_RTTI
  371. # include <typeinfo>
  372. #endif
  373. // Determines whether Google Test can use the pthreads library.
  374. #ifndef GTEST_HAS_PTHREAD
  375. // The user didn't tell us explicitly, so we assume pthreads support is
  376. // available on Linux and Mac.
  377. //
  378. // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
  379. // to your compiler flags.
  380. # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX)
  381. #endif // GTEST_HAS_PTHREAD
  382. #if GTEST_HAS_PTHREAD
  383. // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
  384. // true.
  385. # include <pthread.h> // NOLINT
  386. // For timespec and nanosleep, used below.
  387. # include <time.h> // NOLINT
  388. #endif
  389. // Determines whether Google Test can use tr1/tuple. You can define
  390. // this macro to 0 to prevent Google Test from using tuple (any
  391. // feature depending on tuple with be disabled in this mode).
  392. #ifndef GTEST_HAS_TR1_TUPLE
  393. // The user didn't tell us not to do it, so we assume it's OK.
  394. # define GTEST_HAS_TR1_TUPLE 1
  395. #endif // GTEST_HAS_TR1_TUPLE
  396. // Determines whether Google Test's own tr1 tuple implementation
  397. // should be used.
  398. #ifndef GTEST_USE_OWN_TR1_TUPLE
  399. // The user didn't tell us, so we need to figure it out.
  400. // We use our own TR1 tuple if we aren't sure the user has an
  401. // implementation of it already. At this time, GCC 4.0.0+ and MSVC
  402. // 2010 are the only mainstream compilers that come with a TR1 tuple
  403. // implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by
  404. // defining __GNUC__ and friends, but cannot compile GCC's tuple
  405. // implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB
  406. // Feature Pack download, which we cannot assume the user has.
  407. # if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \
  408. || _MSC_VER >= 1600
  409. # define GTEST_USE_OWN_TR1_TUPLE 0
  410. # else
  411. # define GTEST_USE_OWN_TR1_TUPLE 1
  412. # endif
  413. #endif // GTEST_USE_OWN_TR1_TUPLE
  414. // To avoid conditional compilation everywhere, we make it
  415. // gtest-port.h's responsibility to #include the header implementing
  416. // tr1/tuple.
  417. #if GTEST_HAS_TR1_TUPLE
  418. # if GTEST_USE_OWN_TR1_TUPLE
  419. # include "gtest/internal/gtest-tuple.h"
  420. # elif GTEST_OS_SYMBIAN
  421. // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
  422. // use STLport's tuple implementation, which unfortunately doesn't
  423. // work as the copy of STLport distributed with Symbian is incomplete.
  424. // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
  425. // use its own tuple implementation.
  426. # ifdef BOOST_HAS_TR1_TUPLE
  427. # undef BOOST_HAS_TR1_TUPLE
  428. # endif // BOOST_HAS_TR1_TUPLE
  429. // This prevents <boost/tr1/detail/config.hpp>, which defines
  430. // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
  431. # define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
  432. # include <tuple>
  433. # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
  434. // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
  435. // not conform to the TR1 spec, which requires the header to be <tuple>.
  436. # if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
  437. // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
  438. // which is #included by <tr1/tuple>, to not compile when RTTI is
  439. // disabled. _TR1_FUNCTIONAL is the header guard for
  440. // <tr1/functional>. Hence the following #define is a hack to prevent
  441. // <tr1/functional> from being included.
  442. # define _TR1_FUNCTIONAL 1
  443. # include <tr1/tuple>
  444. # undef _TR1_FUNCTIONAL // Allows the user to #include
  445. // <tr1/functional> if he chooses to.
  446. # else
  447. # include <tr1/tuple> // NOLINT
  448. # endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
  449. # else
  450. // If the compiler is not GCC 4.0+, we assume the user is using a
  451. // spec-conforming TR1 implementation.
  452. # include <tuple> // NOLINT
  453. # endif // GTEST_USE_OWN_TR1_TUPLE
  454. #endif // GTEST_HAS_TR1_TUPLE
  455. // Determines whether clone(2) is supported.
  456. // Usually it will only be available on Linux, excluding
  457. // Linux on the Itanium architecture.
  458. // Also see http://linux.die.net/man/2/clone.
  459. #ifndef GTEST_HAS_CLONE
  460. // The user didn't tell us, so we need to figure it out.
  461. # if GTEST_OS_LINUX && !defined(__ia64__)
  462. # define GTEST_HAS_CLONE 1
  463. # else
  464. # define GTEST_HAS_CLONE 0
  465. # endif // GTEST_OS_LINUX && !defined(__ia64__)
  466. #endif // GTEST_HAS_CLONE
  467. // Determines whether to support stream redirection. This is used to test
  468. // output correctness and to implement death tests.
  469. #ifndef GTEST_HAS_STREAM_REDIRECTION
  470. // By default, we assume that stream redirection is supported on all
  471. // platforms except known mobile ones.
  472. # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN
  473. # define GTEST_HAS_STREAM_REDIRECTION 0
  474. # else
  475. # define GTEST_HAS_STREAM_REDIRECTION 1
  476. # endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
  477. #endif // GTEST_HAS_STREAM_REDIRECTION
  478. // Determines whether to support death tests.
  479. // Google Test does not support death tests for VC 7.1 and earlier as
  480. // abort() in a VC 7.1 application compiled as GUI in debug config
  481. // pops up a dialog window that cannot be suppressed programmatically.
  482. #if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
  483. (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
  484. GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX)
  485. # define GTEST_HAS_DEATH_TEST 1
  486. # include <vector> // NOLINT
  487. #endif
  488. // We don't support MSVC 7.1 with exceptions disabled now. Therefore
  489. // all the compilers we care about are adequate for supporting
  490. // value-parameterized tests.
  491. #define GTEST_HAS_PARAM_TEST 1
  492. // Determines whether to support type-driven tests.
  493. // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
  494. // Sun Pro CC, IBM Visual Age, and HP aCC support.
  495. #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
  496. defined(__IBMCPP__) || defined(__HP_aCC)
  497. # define GTEST_HAS_TYPED_TEST 1
  498. # define GTEST_HAS_TYPED_TEST_P 1
  499. #endif
  500. // Determines whether to support Combine(). This only makes sense when
  501. // value-parameterized tests are enabled. The implementation doesn't
  502. // work on Sun Studio since it doesn't understand templated conversion
  503. // operators.
  504. #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
  505. # define GTEST_HAS_COMBINE 1
  506. #endif
  507. // Determines whether the system compiler uses UTF-16 for encoding wide strings.
  508. #define GTEST_WIDE_STRING_USES_UTF16_ \
  509. (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
  510. // Determines whether test results can be streamed to a socket.
  511. #if GTEST_OS_LINUX
  512. # define GTEST_CAN_STREAM_RESULTS_ 1
  513. #endif
  514. // Defines some utility macros.
  515. // The GNU compiler emits a warning if nested "if" statements are followed by
  516. // an "else" statement and braces are not used to explicitly disambiguate the
  517. // "else" binding. This leads to problems with code like:
  518. //
  519. // if (gate)
  520. // ASSERT_*(condition) << "Some message";
  521. //
  522. // The "switch (0) case 0:" idiom is used to suppress this.
  523. #ifdef __INTEL_COMPILER
  524. # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
  525. #else
  526. # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
  527. #endif
  528. // Use this annotation at the end of a struct/class definition to
  529. // prevent the compiler from optimizing away instances that are never
  530. // used. This is useful when all interesting logic happens inside the
  531. // c'tor and / or d'tor. Example:
  532. //
  533. // struct Foo {
  534. // Foo() { ... }
  535. // } GTEST_ATTRIBUTE_UNUSED_;
  536. //
  537. // Also use it after a variable or parameter declaration to tell the
  538. // compiler the variable/parameter does not have to be used.
  539. #if defined(__GNUC__) && !defined(COMPILER_ICC)
  540. # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
  541. #else
  542. # define GTEST_ATTRIBUTE_UNUSED_
  543. #endif
  544. // A macro to disallow operator=
  545. // This should be used in the private: declarations for a class.
  546. #define GTEST_DISALLOW_ASSIGN_(type)\
  547. void operator=(type const &)
  548. // A macro to disallow copy constructor and operator=
  549. // This should be used in the private: declarations for a class.
  550. #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
  551. type(type const &);\
  552. GTEST_DISALLOW_ASSIGN_(type)
  553. // Tell the compiler to warn about unused return values for functions declared
  554. // with this macro. The macro should be used on function declarations
  555. // following the argument list:
  556. //
  557. // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
  558. #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
  559. # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
  560. #else
  561. # define GTEST_MUST_USE_RESULT_
  562. #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
  563. // Determine whether the compiler supports Microsoft's Structured Exception
  564. // Handling. This is supported by several Windows compilers but generally
  565. // does not exist on any other system.
  566. #ifndef GTEST_HAS_SEH
  567. // The user didn't tell us, so we need to figure it out.
  568. # if defined(_MSC_VER) || defined(__BORLANDC__)
  569. // These two compilers are known to support SEH.
  570. # define GTEST_HAS_SEH 1
  571. # else
  572. // Assume no SEH.
  573. # define GTEST_HAS_SEH 0
  574. # endif
  575. #endif // GTEST_HAS_SEH
  576. #ifdef _MSC_VER
  577. # if GTEST_LINKED_AS_SHARED_LIBRARY
  578. # define GTEST_API_ __declspec(dllimport)
  579. # elif GTEST_CREATE_SHARED_LIBRARY
  580. # define GTEST_API_ __declspec(dllexport)
  581. # endif
  582. #endif // _MSC_VER
  583. #ifndef GTEST_API_
  584. # define GTEST_API_
  585. #endif
  586. #ifdef __GNUC__
  587. // Ask the compiler to never inline a given function.
  588. # define GTEST_NO_INLINE_ __attribute__((noinline))
  589. #else
  590. # define GTEST_NO_INLINE_
  591. #endif
  592. namespace testing {
  593. class Message;
  594. namespace internal {
  595. class String;
  596. // The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
  597. // expression is true. For example, you could use it to verify the
  598. // size of a static array:
  599. //
  600. // GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
  601. // content_type_names_incorrect_size);
  602. //
  603. // or to make sure a struct is smaller than a certain size:
  604. //
  605. // GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
  606. //
  607. // The second argument to the macro is the name of the variable. If
  608. // the expression is false, most compilers will issue a warning/error
  609. // containing the name of the variable.
  610. template <bool>
  611. struct CompileAssert {
  612. };
  613. #define GTEST_COMPILE_ASSERT_(expr, msg) \
  614. typedef ::testing::internal::CompileAssert<(bool(expr))> \
  615. msg[bool(expr) ? 1 : -1]
  616. // Implementation details of GTEST_COMPILE_ASSERT_:
  617. //
  618. // - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
  619. // elements (and thus is invalid) when the expression is false.
  620. //
  621. // - The simpler definition
  622. //
  623. // #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
  624. //
  625. // does not work, as gcc supports variable-length arrays whose sizes
  626. // are determined at run-time (this is gcc's extension and not part
  627. // of the C++ standard). As a result, gcc fails to reject the
  628. // following code with the simple definition:
  629. //
  630. // int foo;
  631. // GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
  632. // // not a compile-time constant.
  633. //
  634. // - By using the type CompileAssert<(bool(expr))>, we ensures that
  635. // expr is a compile-time constant. (Template arguments must be
  636. // determined at compile-time.)
  637. //
  638. // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
  639. // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
  640. //
  641. // CompileAssert<bool(expr)>
  642. //
  643. // instead, these compilers will refuse to compile
  644. //
  645. // GTEST_COMPILE_ASSERT_(5 > 0, some_message);
  646. //
  647. // (They seem to think the ">" in "5 > 0" marks the end of the
  648. // template argument list.)
  649. //
  650. // - The array size is (bool(expr) ? 1 : -1), instead of simply
  651. //
  652. // ((expr) ? 1 : -1).
  653. //
  654. // This is to avoid running into a bug in MS VC 7.1, which
  655. // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
  656. // StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
  657. //
  658. // This template is declared, but intentionally undefined.
  659. template <typename T1, typename T2>
  660. struct StaticAssertTypeEqHelper;
  661. template <typename T>
  662. struct StaticAssertTypeEqHelper<T, T> {};
  663. #if GTEST_HAS_GLOBAL_STRING
  664. typedef ::string string;
  665. #else
  666. typedef ::std::string string;
  667. #endif // GTEST_HAS_GLOBAL_STRING
  668. #if GTEST_HAS_GLOBAL_WSTRING
  669. typedef ::wstring wstring;
  670. #elif GTEST_HAS_STD_WSTRING
  671. typedef ::std::wstring wstring;
  672. #endif // GTEST_HAS_GLOBAL_WSTRING
  673. // A helper for suppressing warnings on constant condition. It just
  674. // returns 'condition'.
  675. GTEST_API_ bool IsTrue(bool condition);
  676. // Defines scoped_ptr.
  677. // This implementation of scoped_ptr is PARTIAL - it only contains
  678. // enough stuff to satisfy Google Test's need.
  679. template <typename T>
  680. class scoped_ptr {
  681. public:
  682. typedef T element_type;
  683. explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
  684. ~scoped_ptr() { reset(); }
  685. T& operator*() const { return *ptr_; }
  686. T* operator->() const { return ptr_; }
  687. T* get() const { return ptr_; }
  688. T* release() {
  689. T* const ptr = ptr_;
  690. ptr_ = NULL;
  691. return ptr;
  692. }
  693. void reset(T* p = NULL) {
  694. if (p != ptr_) {
  695. if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
  696. delete ptr_;
  697. }
  698. ptr_ = p;
  699. }
  700. }
  701. private:
  702. T* ptr_;
  703. GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
  704. };
  705. // Defines RE.
  706. // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
  707. // Regular Expression syntax.
  708. class GTEST_API_ RE {
  709. public:
  710. // A copy constructor is required by the Standard to initialize object
  711. // references from r-values.
  712. RE(const RE& other) { Init(other.pattern()); }
  713. // Constructs an RE from a string.
  714. RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
  715. #if GTEST_HAS_GLOBAL_STRING
  716. RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
  717. #endif // GTEST_HAS_GLOBAL_STRING
  718. RE(const char* regex) { Init(regex); } // NOLINT
  719. ~RE();
  720. // Returns the string representation of the regex.
  721. const char* pattern() const { return pattern_; }
  722. // FullMatch(str, re) returns true iff regular expression re matches
  723. // the entire str.
  724. // PartialMatch(str, re) returns true iff regular expression re
  725. // matches a substring of str (including str itself).
  726. //
  727. // TODO(wan@google.com): make FullMatch() and PartialMatch() work
  728. // when str contains NUL characters.
  729. static bool FullMatch(const ::std::string& str, const RE& re) {
  730. return FullMatch(str.c_str(), re);
  731. }
  732. static bool PartialMatch(const ::std::string& str, const RE& re) {
  733. return PartialMatch(str.c_str(), re);
  734. }
  735. #if GTEST_HAS_GLOBAL_STRING
  736. static bool FullMatch(const ::string& str, const RE& re) {
  737. return FullMatch(str.c_str(), re);
  738. }
  739. static bool PartialMatch(const ::string& str, const RE& re) {
  740. return PartialMatch(str.c_str(), re);
  741. }
  742. #endif // GTEST_HAS_GLOBAL_STRING
  743. static bool FullMatch(const char* str, const RE& re);
  744. static bool PartialMatch(const char* str, const RE& re);
  745. private:
  746. void Init(const char* regex);
  747. // We use a const char* instead of a string, as Google Test may be used
  748. // where string is not available. We also do not use Google Test's own
  749. // String type here, in order to simplify dependencies between the
  750. // files.
  751. const char* pattern_;
  752. bool is_valid_;
  753. #if GTEST_USES_POSIX_RE
  754. regex_t full_regex_; // For FullMatch().
  755. regex_t partial_regex_; // For PartialMatch().
  756. #else // GTEST_USES_SIMPLE_RE
  757. const char* full_pattern_; // For FullMatch();
  758. #endif
  759. GTEST_DISALLOW_ASSIGN_(RE);
  760. };
  761. // Formats a source file path and a line number as they would appear
  762. // in an error message from the compiler used to compile this code.
  763. GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
  764. // Formats a file location for compiler-independent XML output.
  765. // Although this function is not platform dependent, we put it next to
  766. // FormatFileLocation in order to contrast the two functions.
  767. GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
  768. int line);
  769. // Defines logging utilities:
  770. // GTEST_LOG_(severity) - logs messages at the specified severity level. The
  771. // message itself is streamed into the macro.
  772. // LogToStderr() - directs all log messages to stderr.
  773. // FlushInfoLog() - flushes informational log messages.
  774. enum GTestLogSeverity {
  775. GTEST_INFO,
  776. GTEST_WARNING,
  777. GTEST_ERROR,
  778. GTEST_FATAL
  779. };
  780. // Formats log entry severity, provides a stream object for streaming the
  781. // log message, and terminates the message with a newline when going out of
  782. // scope.
  783. class GTEST_API_ GTestLog {
  784. public:
  785. GTestLog(GTestLogSeverity severity, const char* file, int line);
  786. // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
  787. ~GTestLog();
  788. ::std::ostream& GetStream() { return ::std::cerr; }
  789. private:
  790. const GTestLogSeverity severity_;
  791. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
  792. };
  793. #define GTEST_LOG_(severity) \
  794. ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
  795. __FILE__, __LINE__).GetStream()
  796. inline void LogToStderr() {}
  797. inline void FlushInfoLog() { fflush(NULL); }
  798. // INTERNAL IMPLEMENTATION - DO NOT USE.
  799. //
  800. // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
  801. // is not satisfied.
  802. // Synopsys:
  803. // GTEST_CHECK_(boolean_condition);
  804. // or
  805. // GTEST_CHECK_(boolean_condition) << "Additional message";
  806. //
  807. // This checks the condition and if the condition is not satisfied
  808. // it prints message about the condition violation, including the
  809. // condition itself, plus additional message streamed into it, if any,
  810. // and then it aborts the program. It aborts the program irrespective of
  811. // whether it is built in the debug mode or not.
  812. #define GTEST_CHECK_(condition) \
  813. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  814. if (::testing::internal::IsTrue(condition)) \
  815. ; \
  816. else \
  817. GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
  818. // An all-mode assert to verify that the given POSIX-style function
  819. // call returns 0 (indicating success). Known limitation: this
  820. // doesn't expand to a balanced 'if' statement, so enclose the macro
  821. // in {} if you need to use it as the only statement in an 'if'
  822. // branch.
  823. #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
  824. if (const int gtest_error = (posix_call)) \
  825. GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
  826. << gtest_error
  827. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  828. //
  829. // Use ImplicitCast_ as a safe version of static_cast for upcasting in
  830. // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
  831. // const Foo*). When you use ImplicitCast_, the compiler checks that
  832. // the cast is safe. Such explicit ImplicitCast_s are necessary in
  833. // surprisingly many situations where C++ demands an exact type match
  834. // instead of an argument type convertable to a target type.
  835. //
  836. // The syntax for using ImplicitCast_ is the same as for static_cast:
  837. //
  838. // ImplicitCast_<ToType>(expr)
  839. //
  840. // ImplicitCast_ would have been part of the C++ standard library,
  841. // but the proposal was submitted too late. It will probably make
  842. // its way into the language in the future.
  843. //
  844. // This relatively ugly name is intentional. It prevents clashes with
  845. // similar functions users may have (e.g., implicit_cast). The internal
  846. // namespace alone is not enough because the function can be found by ADL.
  847. template<typename To>
  848. inline To ImplicitCast_(To x) { return x; }
  849. // When you upcast (that is, cast a pointer from type Foo to type
  850. // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
  851. // always succeed. When you downcast (that is, cast a pointer from
  852. // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
  853. // how do you know the pointer is really of type SubclassOfFoo? It
  854. // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
  855. // when you downcast, you should use this macro. In debug mode, we
  856. // use dynamic_cast<> to double-check the downcast is legal (we die
  857. // if it's not). In normal mode, we do the efficient static_cast<>
  858. // instead. Thus, it's important to test in debug mode to make sure
  859. // the cast is legal!
  860. // This is the only place in the code we should use dynamic_cast<>.
  861. // In particular, you SHOULDN'T be using dynamic_cast<> in order to
  862. // do RTTI (eg code like this:
  863. // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
  864. // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
  865. // You should design the code some other way not to need this.
  866. //
  867. // This relatively ugly name is intentional. It prevents clashes with
  868. // similar functions users may have (e.g., down_cast). The internal
  869. // namespace alone is not enough because the function can be found by ADL.
  870. template<typename To, typename From> // use like this: DownCast_<T*>(foo);
  871. inline To DownCast_(From* f) { // so we only accept pointers
  872. // Ensures that To is a sub-type of From *. This test is here only
  873. // for compile-time type checking, and has no overhead in an
  874. // optimized build at run-time, as it will be optimized away
  875. // completely.
  876. if (false) {
  877. const To to = NULL;
  878. ::testing::internal::ImplicitCast_<From*>(to);
  879. }
  880. #if GTEST_HAS_RTTI
  881. // RTTI: debug mode only!
  882. GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
  883. #endif
  884. return static_cast<To>(f);
  885. }
  886. // Downcasts the pointer of type Base to Derived.
  887. // Derived must be a subclass of Base. The parameter MUST
  888. // point to a class of type Derived, not any subclass of it.
  889. // When RTTI is available, the function performs a runtime
  890. // check to enforce this.
  891. template <class Derived, class Base>
  892. Derived* CheckedDowncastToActualType(Base* base) {
  893. #if GTEST_HAS_RTTI
  894. GTEST_CHECK_(typeid(*base) == typeid(Derived));
  895. return dynamic_cast<Derived*>(base); // NOLINT
  896. #else
  897. return static_cast<Derived*>(base); // Poor man's downcast.
  898. #endif
  899. }
  900. #if GTEST_HAS_STREAM_REDIRECTION
  901. // Defines the stderr capturer:
  902. // CaptureStdout - starts capturing stdout.
  903. // GetCapturedStdout - stops capturing stdout and returns the captured string.
  904. // CaptureStderr - starts capturing stderr.
  905. // GetCapturedStderr - stops capturing stderr and returns the captured string.
  906. //
  907. GTEST_API_ void CaptureStdout();
  908. GTEST_API_ String GetCapturedStdout();
  909. GTEST_API_ void CaptureStderr();
  910. GTEST_API_ String GetCapturedStderr();
  911. #endif // GTEST_HAS_STREAM_REDIRECTION
  912. #if GTEST_HAS_DEATH_TEST
  913. // A copy of all command line arguments. Set by InitGoogleTest().
  914. extern ::std::vector<String> g_argvs;
  915. // GTEST_HAS_DEATH_TEST implies we have ::std::string.
  916. const ::std::vector<String>& GetArgvs();
  917. #endif // GTEST_HAS_DEATH_TEST
  918. // Defines synchronization primitives.
  919. #if GTEST_HAS_PTHREAD
  920. // Sleeps for (roughly) n milli-seconds. This function is only for
  921. // testing Google Test's own constructs. Don't use it in user tests,
  922. // either directly or indirectly.
  923. inline void SleepMilliseconds(int n) {
  924. const timespec time = {
  925. 0, // 0 seconds.
  926. n * 1000L * 1000L, // And n ms.
  927. };
  928. nanosleep(&time, NULL);
  929. }
  930. // Allows a controller thread to pause execution of newly created
  931. // threads until notified. Instances of this class must be created
  932. // and destroyed in the controller thread.
  933. //
  934. // This class is only for testing Google Test's own constructs. Do not
  935. // use it in user tests, either directly or indirectly.
  936. class Notification {
  937. public:
  938. Notification() : notified_(false) {}
  939. // Notifies all threads created with this notification to start. Must
  940. // be called from the controller thread.
  941. void Notify() { notified_ = true; }
  942. // Blocks until the controller thread notifies. Must be called from a test
  943. // thread.
  944. void WaitForNotification() {
  945. while(!notified_) {
  946. SleepMilliseconds(10);
  947. }
  948. }
  949. private:
  950. volatile bool notified_;
  951. GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
  952. };
  953. // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
  954. // Consequently, it cannot select a correct instantiation of ThreadWithParam
  955. // in order to call its Run(). Introducing ThreadWithParamBase as a
  956. // non-templated base class for ThreadWithParam allows us to bypass this
  957. // problem.
  958. class ThreadWithParamBase {
  959. public:
  960. virtual ~ThreadWithParamBase() {}
  961. virtual void Run() = 0;
  962. };
  963. // pthread_create() accepts a pointer to a function type with the C linkage.
  964. // According to the Standard (7.5/1), function types with different linkages
  965. // are different even if they are otherwise identical. Some compilers (for
  966. // example, SunStudio) treat them as different types. Since class methods
  967. // cannot be defined with C-linkage we need to define a free C-function to
  968. // pass into pthread_create().
  969. extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
  970. static_cast<ThreadWithParamBase*>(thread)->Run();
  971. return NULL;
  972. }
  973. // Helper class for testing Google Test's multi-threading constructs.
  974. // To use it, write:
  975. //
  976. // void ThreadFunc(int param) { /* Do things with param */ }
  977. // Notification thread_can_start;
  978. // ...
  979. // // The thread_can_start parameter is optional; you can supply NULL.
  980. // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
  981. // thread_can_start.Notify();
  982. //
  983. // These classes are only for testing Google Test's own constructs. Do
  984. // not use them in user tests, either directly or indirectly.
  985. template <typename T>
  986. class ThreadWithParam : public ThreadWithParamBase {
  987. public:
  988. typedef void (*UserThreadFunc)(T);
  989. ThreadWithParam(
  990. UserThreadFunc func, T param, Notification* thread_can_start)
  991. : func_(func),
  992. param_(param),
  993. thread_can_start_(thread_can_start),
  994. finished_(false) {
  995. ThreadWithParamBase* const base = this;
  996. // The thread can be created only after all fields except thread_
  997. // have been initialized.
  998. GTEST_CHECK_POSIX_SUCCESS_(
  999. pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
  1000. }
  1001. ~ThreadWithParam() { Join(); }
  1002. void Join() {
  1003. if (!finished_) {
  1004. GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
  1005. finished_ = true;
  1006. }
  1007. }
  1008. virtual void Run() {
  1009. if (thread_can_start_ != NULL)
  1010. thread_can_start_->WaitForNotification();
  1011. func_(param_);
  1012. }
  1013. private:
  1014. const UserThreadFunc func_; // User-supplied thread function.
  1015. const T param_; // User-supplied parameter to the thread function.
  1016. // When non-NULL, used to block execution until the controller thread
  1017. // notifies.
  1018. Notification* const thread_can_start_;
  1019. bool finished_; // true iff we know that the thread function has finished.
  1020. pthread_t thread_; // The native thread object.
  1021. GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
  1022. };
  1023. // MutexBase and Mutex implement mutex on pthreads-based platforms. They
  1024. // are used in conjunction with class MutexLock:
  1025. //
  1026. // Mutex mutex;
  1027. // ...
  1028. // MutexLock lock(&mutex); // Acquires the mutex and releases it at the end
  1029. // // of the current scope.
  1030. //
  1031. // MutexBase implements behavior for both statically and dynamically
  1032. // allocated mutexes. Do not use MutexBase directly. Instead, write
  1033. // the following to define a static mutex:
  1034. //
  1035. // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
  1036. //
  1037. // You can forward declare a static mutex like this:
  1038. //
  1039. // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
  1040. //
  1041. // To create a dynamic mutex, just define an object of type Mutex.
  1042. class MutexBase {
  1043. public:
  1044. // Acquires this mutex.
  1045. void Lock() {
  1046. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
  1047. owner_ = pthread_self();
  1048. }
  1049. // Releases this mutex.
  1050. void Unlock() {
  1051. // We don't protect writing to owner_ here, as it's the caller's
  1052. // responsibility to ensure that the current thread holds the
  1053. // mutex when this is called.
  1054. owner_ = 0;
  1055. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
  1056. }
  1057. // Does nothing if the current thread holds the mutex. Otherwise, crashes
  1058. // with high probability.
  1059. void AssertHeld() const {
  1060. GTEST_CHECK_(owner_ == pthread_self())
  1061. << "The current thread is not holding the mutex @" << this;
  1062. }
  1063. // A static mutex may be used before main() is entered. It may even
  1064. // be used before the dynamic initialization stage. Therefore we
  1065. // must be able to initialize a static mutex object at link time.
  1066. // This means MutexBase has to be a POD and its member variables
  1067. // have to be public.
  1068. public:
  1069. pthread_mutex_t mutex_; // The underlying pthread mutex.
  1070. pthread_t owner_; // The thread holding the mutex; 0 means no one holds it.
  1071. };
  1072. // Forward-declares a static mutex.
  1073. # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
  1074. extern ::testing::internal::MutexBase mutex
  1075. // Defines and statically (i.e. at link time) initializes a static mutex.
  1076. # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
  1077. ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
  1078. // The Mutex class can only be used for mutexes created at runtime. It
  1079. // shares its API with MutexBase otherwise.
  1080. class Mutex : public MutexBase {
  1081. public:
  1082. Mutex() {
  1083. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
  1084. owner_ = 0;
  1085. }
  1086. ~Mutex() {
  1087. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
  1088. }
  1089. private:
  1090. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
  1091. };
  1092. // We cannot name this class MutexLock as the ctor declaration would
  1093. // conflict with a macro named MutexLock, which is defined on some
  1094. // platforms. Hence the typedef trick below.
  1095. class GTestMutexLock {
  1096. public:
  1097. explicit GTestMutexLock(MutexBase* mutex)
  1098. : mutex_(mutex) { mutex_->Lock(); }
  1099. ~GTestMutexLock() { mutex_->Unlock(); }
  1100. private:
  1101. MutexBase* const mutex_;
  1102. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
  1103. };
  1104. typedef GTestMutexLock MutexLock;
  1105. // Helpers for ThreadLocal.
  1106. // pthread_key_create() requires DeleteThreadLocalValue() to have
  1107. // C-linkage. Therefore it cannot be templatized to access
  1108. // ThreadLocal<T>. Hence the need for class
  1109. // ThreadLocalValueHolderBase.
  1110. class ThreadLocalValueHolderBase {
  1111. public:
  1112. virtual ~ThreadLocalValueHolderBase() {}
  1113. };
  1114. // Called by pthread to delete thread-local data stored by
  1115. // pthread_setspecific().
  1116. extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
  1117. delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
  1118. }
  1119. // Implements thread-local storage on pthreads-based systems.
  1120. //
  1121. // // Thread 1
  1122. // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
  1123. //
  1124. // // Thread 2
  1125. // tl.set(150); // Changes the value for thread 2 only.
  1126. // EXPECT_EQ(150, tl.get());
  1127. //
  1128. // // Thread 1
  1129. // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
  1130. // tl.set(200);
  1131. // EXPECT_EQ(200, tl.get());
  1132. //
  1133. // The template type argument T must have a public copy constructor.
  1134. // In addition, the default ThreadLocal constructor requires T to have
  1135. // a public default constructor.
  1136. //
  1137. // An object managed for a thread by a ThreadLocal instance is deleted
  1138. // when the thread exits. Or, if the ThreadLocal instance dies in
  1139. // that thread, when the ThreadLocal dies. It's the user's
  1140. // responsibility to ensure that all other threads using a ThreadLocal
  1141. // have exited when it dies, or the per-thread objects for those
  1142. // threads will not be deleted.
  1143. //
  1144. // Google Test only uses global ThreadLocal objects. That means they
  1145. // will die after main() has returned. Therefore, no per-thread
  1146. // object managed by Google Test will be leaked as long as all threads
  1147. // using Google Test have exited when main() returns.
  1148. template <typename T>
  1149. class ThreadLocal {
  1150. public:
  1151. ThreadLocal() : key_(CreateKey()),
  1152. default_() {}
  1153. explicit ThreadLocal(const T& value) : key_(CreateKey()),
  1154. default_(value) {}
  1155. ~ThreadLocal() {
  1156. // Destroys the managed object for the current thread, if any.
  1157. DeleteThreadLocalValue(pthread_getspecific(key_));
  1158. // Releases resources associated with the key. This will *not*
  1159. // delete managed objects for other threads.
  1160. GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
  1161. }
  1162. T* pointer() { return GetOrCreateValue(); }
  1163. const T* pointer() const { return GetOrCreateValue(); }
  1164. const T& get() const { return *pointer(); }
  1165. void set(const T& value) { *pointer() = value; }
  1166. private:
  1167. // Holds a value of type T.
  1168. class ValueHolder : public ThreadLocalValueHolderBase {
  1169. public:
  1170. explicit ValueHolder(const T& value) : value_(value) {}
  1171. T* pointer() { return &value_; }
  1172. private:
  1173. T value_;
  1174. GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
  1175. };
  1176. static pthread_key_t CreateKey() {
  1177. pthread_key_t key;
  1178. // When a thread exits, DeleteThreadLocalValue() will be called on
  1179. // the object managed for that thread.
  1180. GTEST_CHECK_POSIX_SUCCESS_(
  1181. pthread_key_create(&key, &DeleteThreadLocalValue));
  1182. return key;
  1183. }
  1184. T* GetOrCreateValue() const {
  1185. ThreadLocalValueHolderBase* const holder =
  1186. static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
  1187. if (holder != NULL) {
  1188. return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
  1189. }
  1190. ValueHolder* const new_holder = new ValueHolder(default_);
  1191. ThreadLocalValueHolderBase* const holder_base = new_holder;
  1192. GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
  1193. return new_holder->pointer();
  1194. }
  1195. // A key pthreads uses for looking up per-thread values.
  1196. const pthread_key_t key_;
  1197. const T default_; // The default value for each thread.
  1198. GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
  1199. };
  1200. # define GTEST_IS_THREADSAFE 1
  1201. #else // GTEST_HAS_PTHREAD
  1202. // A dummy implementation of synchronization primitives (mutex, lock,
  1203. // and thread-local variable). Necessary for compiling Google Test where
  1204. // mutex is not supported - using Google Test in multiple threads is not
  1205. // supported on such platforms.
  1206. class Mutex {
  1207. public:
  1208. Mutex() {}
  1209. void AssertHeld() const {}
  1210. };
  1211. # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
  1212. extern ::testing::internal::Mutex mutex
  1213. # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
  1214. class GTestMutexLock {
  1215. public:
  1216. explicit GTestMutexLock(Mutex*) {} // NOLINT
  1217. };
  1218. typedef GTestMutexLock MutexLock;
  1219. template <typename T>
  1220. class ThreadLocal {
  1221. public:
  1222. ThreadLocal() : value_() {}
  1223. explicit ThreadLocal(const T& value) : value_(value) {}
  1224. T* pointer() { return &value_; }
  1225. const T* pointer() const { return &value_; }
  1226. const T& get() const { return value_; }
  1227. void set(const T& value) { value_ = value; }
  1228. private:
  1229. T value_;
  1230. };
  1231. // The above synchronization primitives have dummy implementations.
  1232. // Therefore Google Test is not thread-safe.
  1233. # define GTEST_IS_THREADSAFE 0
  1234. #endif // GTEST_HAS_PTHREAD
  1235. // Returns the number of threads running in the process, or 0 to indicate that
  1236. // we cannot detect it.
  1237. GTEST_API_ size_t GetThreadCount();
  1238. // Passing non-POD classes through ellipsis (...) crashes the ARM
  1239. // compiler and generates a warning in Sun Studio. The Nokia Symbian
  1240. // and the IBM XL C/C++ compiler try to instantiate a copy constructor
  1241. // for objects passed through ellipsis (...), failing for uncopyable
  1242. // objects. We define this to ensure that only POD is passed through
  1243. // ellipsis on these systems.
  1244. #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
  1245. // We lose support for NULL detection where the compiler doesn't like
  1246. // passing non-POD classes through ellipsis (...).
  1247. # define GTEST_ELLIPSIS_NEEDS_POD_ 1
  1248. #else
  1249. # define GTEST_CAN_COMPARE_NULL 1
  1250. #endif
  1251. // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
  1252. // const T& and const T* in a function template. These compilers
  1253. // _can_ decide between class template specializations for T and T*,
  1254. // so a tr1::type_traits-like is_pointer works.
  1255. #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
  1256. # define GTEST_NEEDS_IS_POINTER_ 1
  1257. #endif
  1258. template <bool bool_value>
  1259. struct bool_constant {
  1260. typedef bool_constant<bool_value> type;
  1261. static const bool value = bool_value;
  1262. };
  1263. template <bool bool_value> const bool bool_constant<bool_value>::value;
  1264. typedef bool_constant<false> false_type;
  1265. typedef bool_constant<true> true_type;
  1266. template <typename T>
  1267. struct is_pointer : public false_type {};
  1268. template <typename T>
  1269. struct is_pointer<T*> : public true_type {};
  1270. template <typename Iterator>
  1271. struct IteratorTraits {
  1272. typedef typename Iterator::value_type value_type;
  1273. };
  1274. template <typename T>
  1275. struct IteratorTraits<T*> {
  1276. typedef T value_type;
  1277. };
  1278. template <typename T>
  1279. struct IteratorTraits<const T*> {
  1280. typedef T value_type;
  1281. };
  1282. #if GTEST_OS_WINDOWS
  1283. # define GTEST_PATH_SEP_ "\\"
  1284. # define GTEST_HAS_ALT_PATH_SEP_ 1
  1285. // The biggest signed integer type the compiler supports.
  1286. typedef __int64 BiggestInt;
  1287. #else
  1288. # define GTEST_PATH_SEP_ "/"
  1289. # define GTEST_HAS_ALT_PATH_SEP_ 0
  1290. typedef long long BiggestInt; // NOLINT
  1291. #endif // GTEST_OS_WINDOWS
  1292. // Utilities for char.
  1293. // isspace(int ch) and friends accept an unsigned char or EOF. char
  1294. // may be signed, depending on the compiler (or compiler flags).
  1295. // Therefore we need to cast a char to unsigned char before calling
  1296. // isspace(), etc.
  1297. inline bool IsAlpha(char ch) {
  1298. return isalpha(static_cast<unsigned char>(ch)) != 0;
  1299. }
  1300. inline bool IsAlNum(char ch) {
  1301. return isalnum(static_cast<unsigned char>(ch)) != 0;
  1302. }
  1303. inline bool IsDigit(char ch) {
  1304. return isdigit(static_cast<unsigned char>(ch)) != 0;
  1305. }
  1306. inline bool IsLower(char ch) {
  1307. return islower(static_cast<unsigned char>(ch)) != 0;
  1308. }
  1309. inline bool IsSpace(char ch) {
  1310. return isspace(static_cast<unsigned char>(ch)) != 0;
  1311. }
  1312. inline bool IsUpper(char ch) {
  1313. return isupper(static_cast<unsigned char>(ch)) != 0;
  1314. }
  1315. inline bool IsXDigit(char ch) {
  1316. return isxdigit(static_cast<unsigned char>(ch)) != 0;
  1317. }
  1318. inline char ToLower(char ch) {
  1319. return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
  1320. }
  1321. inline char ToUpper(char ch) {
  1322. return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
  1323. }
  1324. // The testing::internal::posix namespace holds wrappers for common
  1325. // POSIX functions. These wrappers hide the differences between
  1326. // Windows/MSVC and POSIX systems. Since some compilers define these
  1327. // standard functions as macros, the wrapper cannot have the same name
  1328. // as the wrapped function.
  1329. namespace posix {
  1330. // Functions with a different name on Windows.
  1331. #if GTEST_OS_WINDOWS
  1332. typedef struct _stat StatStruct;
  1333. # ifdef __BORLANDC__
  1334. inline int IsATTY(int fd) { return isatty(fd); }
  1335. inline int StrCaseCmp(const char* s1, const char* s2) {
  1336. return stricmp(s1, s2);
  1337. }
  1338. inline char* StrDup(const char* src) { return strdup(src); }
  1339. # else // !__BORLANDC__
  1340. # if GTEST_OS_WINDOWS_MOBILE
  1341. inline int IsATTY(int /* fd */) { return 0; }
  1342. # else
  1343. inline int IsATTY(int fd) { return _isatty(fd); }
  1344. # endif // GTEST_OS_WINDOWS_MOBILE
  1345. inline int StrCaseCmp(const char* s1, const char* s2) {
  1346. return _stricmp(s1, s2);
  1347. }
  1348. inline char* StrDup(const char* src) { return _strdup(src); }
  1349. # endif // __BORLANDC__
  1350. # if GTEST_OS_WINDOWS_MOBILE
  1351. inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
  1352. // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
  1353. // time and thus not defined there.
  1354. # else
  1355. inline int FileNo(FILE* file) { return _fileno(file); }
  1356. inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
  1357. inline int RmDir(const char* dir) { return _rmdir(dir); }
  1358. inline bool IsDir(const StatStruct& st) {
  1359. return (_S_IFDIR & st.st_mode) != 0;
  1360. }
  1361. # endif // GTEST_OS_WINDOWS_MOBILE
  1362. #else
  1363. typedef struct stat StatStruct;
  1364. inline int FileNo(FILE* file) { return fileno(file); }
  1365. inline int IsATTY(int fd) { return isatty(fd); }
  1366. inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
  1367. inline int StrCaseCmp(const char* s1, const char* s2) {
  1368. return strcasecmp(s1, s2);
  1369. }
  1370. inline char* StrDup(const char* src) { return strdup(src); }
  1371. inline int RmDir(const char* dir) { return rmdir(dir); }
  1372. inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
  1373. #endif // GTEST_OS_WINDOWS
  1374. // Functions deprecated by MSVC 8.0.
  1375. #ifdef _MSC_VER
  1376. // Temporarily disable warning 4996 (deprecated function).
  1377. # pragma warning(push)
  1378. # pragma warning(disable:4996)
  1379. #endif
  1380. inline const char* StrNCpy(char* dest, const char* src, size_t n) {
  1381. return strncpy(dest, src, n);
  1382. }
  1383. // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
  1384. // StrError() aren't needed on Windows CE at this time and thus not
  1385. // defined there.
  1386. #if !GTEST_OS_WINDOWS_MOBILE
  1387. inline int ChDir(const char* dir) { return chdir(dir); }
  1388. #endif
  1389. inline FILE* FOpen(const char* path, const char* mode) {
  1390. return fopen(path, mode);
  1391. }
  1392. #if !GTEST_OS_WINDOWS_MOBILE
  1393. inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
  1394. return freopen(path, mode, stream);
  1395. }
  1396. inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
  1397. #endif
  1398. inline int FClose(FILE* fp) { return fclose(fp); }
  1399. #if !GTEST_OS_WINDOWS_MOBILE
  1400. inline int Read(int fd, void* buf, unsigned int count) {
  1401. return static_cast<int>(read(fd, buf, count));
  1402. }
  1403. inline int Write(int fd, const void* buf, unsigned int count) {
  1404. return static_cast<int>(write(fd, buf, count));
  1405. }
  1406. inline int Close(int fd) { return close(fd); }
  1407. inline const char* StrError(int errnum) { return strerror(errnum); }
  1408. #endif
  1409. inline const char* GetEnv(const char* name) {
  1410. #if GTEST_OS_WINDOWS_MOBILE
  1411. // We are on Windows CE, which has no environment variables.
  1412. return NULL;
  1413. #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
  1414. // Environment variables which we programmatically clear will be set to the
  1415. // empty string rather than unset (NULL). Handle that case.
  1416. const char* const env = getenv(name);
  1417. return (env != NULL && env[0] != '\0') ? env : NULL;
  1418. #else
  1419. return getenv(name);
  1420. #endif
  1421. }
  1422. #ifdef _MSC_VER
  1423. # pragma warning(pop) // Restores the warning state.
  1424. #endif
  1425. #if GTEST_OS_WINDOWS_MOBILE
  1426. // Windows CE has no C library. The abort() function is used in
  1427. // several places in Google Test. This implementation provides a reasonable
  1428. // imitation of standard behaviour.
  1429. void Abort();
  1430. #else
  1431. inline void Abort() { abort(); }
  1432. #endif // GTEST_OS_WINDOWS_MOBILE
  1433. } // namespace posix
  1434. // The maximum number a BiggestInt can represent. This definition
  1435. // works no matter BiggestInt is represented in one's complement or
  1436. // two's complement.
  1437. //
  1438. // We cannot rely on numeric_limits in STL, as __int64 and long long
  1439. // are not part of standard C++ and numeric_limits doesn't need to be
  1440. // defined for them.
  1441. const BiggestInt kMaxBiggestInt =
  1442. ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
  1443. // This template class serves as a compile-time function from size to
  1444. // type. It maps a size in bytes to a primitive type with that
  1445. // size. e.g.
  1446. //
  1447. // TypeWithSize<4>::UInt
  1448. //
  1449. // is typedef-ed to be unsigned int (unsigned integer made up of 4
  1450. // bytes).
  1451. //
  1452. // Such functionality should belong to STL, but I cannot find it
  1453. // there.
  1454. //
  1455. // Google Test uses this class in the implementation of floating-point
  1456. // comparison.
  1457. //
  1458. // For now it only handles UInt (unsigned int) as that's all Google Test
  1459. // needs. Other types can be easily added in the future if need
  1460. // arises.
  1461. template <size_t size>
  1462. class TypeWithSize {
  1463. public:
  1464. // This prevents the user from using TypeWithSize<N> with incorrect
  1465. // values of N.
  1466. typedef void UInt;
  1467. };
  1468. // The specialization for size 4.
  1469. template <>
  1470. class TypeWithSize<4> {
  1471. public:
  1472. // unsigned int has size 4 in both gcc and MSVC.
  1473. //
  1474. // As base/basictypes.h doesn't compile on Windows, we cannot use
  1475. // uint32, uint64, and etc here.
  1476. typedef int Int;
  1477. typedef unsigned int UInt;
  1478. };
  1479. // The specialization for size 8.
  1480. template <>
  1481. class TypeWithSize<8> {
  1482. public:
  1483. #if GTEST_OS_WINDOWS
  1484. typedef __int64 Int;
  1485. typedef unsigned __int64 UInt;
  1486. #else
  1487. typedef long long Int; // NOLINT
  1488. typedef unsigned long long UInt; // NOLINT
  1489. #endif // GTEST_OS_WINDOWS
  1490. };
  1491. // Integer types of known sizes.
  1492. typedef TypeWithSize<4>::Int Int32;
  1493. typedef TypeWithSize<4>::UInt UInt32;
  1494. typedef TypeWithSize<8>::Int Int64;
  1495. typedef TypeWithSize<8>::UInt UInt64;
  1496. typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
  1497. // Utilities for command line flags and environment variables.
  1498. // Macro for referencing flags.
  1499. #define GTEST_FLAG(name) FLAGS_gtest_##name
  1500. // Macros for declaring flags.
  1501. #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
  1502. #define GTEST_DECLARE_int32_(name) \
  1503. GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
  1504. #define GTEST_DECLARE_string_(name) \
  1505. GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name)
  1506. // Macros for defining flags.
  1507. #define GTEST_DEFINE_bool_(name, default_val, doc) \
  1508. GTEST_API_ bool GTEST_FLAG(name) = (default_val)
  1509. #define GTEST_DEFINE_int32_(name, default_val, doc) \
  1510. GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
  1511. #define GTEST_DEFINE_string_(name, default_val, doc) \
  1512. GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val)
  1513. // Parses 'str' for a 32-bit signed integer. If successful, writes the result
  1514. // to *value and returns true; otherwise leaves *value unchanged and returns
  1515. // false.
  1516. // TODO(chandlerc): Find a better way to refactor flag and environment parsing
  1517. // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
  1518. // function.
  1519. bool ParseInt32(const Message& src_text, const char* str, Int32* value);
  1520. // Parses a bool/Int32/string from the environment variable
  1521. // corresponding to the given Google Test flag.
  1522. bool BoolFromGTestEnv(const char* flag, bool default_val);
  1523. GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
  1524. const char* StringFromGTestEnv(const char* flag, const char* default_val);
  1525. } // namespace internal
  1526. } // namespace testing
  1527. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_