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