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