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