Qucs-core  0.0.19
gtest-port.h
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Authors: wan@google.com (Zhanyong Wan)
00031 //
00032 // Low-level types and utilities for porting Google Test to various
00033 // platforms.  They are subject to change without notice.  DO NOT USE
00034 // THEM IN USER CODE.
00035 //
00036 // This file is fundamental to Google Test.  All other Google Test source
00037 // files are expected to #include this.  Therefore, it cannot #include
00038 // any other Google Test header.
00039 
00040 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
00041 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
00042 
00043 // The user can define the following macros in the build script to
00044 // control Google Test's behavior.  If the user doesn't define a macro
00045 // in this list, Google Test will define it.
00046 //
00047 //   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
00048 //                              is/isn't available.
00049 //   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
00050 //                              are enabled.
00051 //   GTEST_HAS_GLOBAL_STRING  - Define it to 1/0 to indicate that ::string
00052 //                              is/isn't available (some systems define
00053 //                              ::string, which is different to std::string).
00054 //   GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
00055 //                              is/isn't available (some systems define
00056 //                              ::wstring, which is different to std::wstring).
00057 //   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
00058 //                              expressions are/aren't available.
00059 //   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
00060 //                              is/isn't available.
00061 //   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
00062 //                              enabled.
00063 //   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
00064 //                              std::wstring does/doesn't work (Google Test can
00065 //                              be used where std::wstring is unavailable).
00066 //   GTEST_HAS_TR1_TUPLE      - Define it to 1/0 to indicate tr1::tuple
00067 //                              is/isn't available.
00068 //   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
00069 //                              compiler supports Microsoft's "Structured
00070 //                              Exception Handling".
00071 //   GTEST_HAS_STREAM_REDIRECTION
00072 //                            - Define it to 1/0 to indicate whether the
00073 //                              platform supports I/O stream redirection using
00074 //                              dup() and dup2().
00075 //   GTEST_USE_OWN_TR1_TUPLE  - Define it to 1/0 to indicate whether Google
00076 //                              Test's own tr1 tuple implementation should be
00077 //                              used.  Unused when the user sets
00078 //                              GTEST_HAS_TR1_TUPLE to 0.
00079 //   GTEST_LANG_CXX11         - Define it to 1/0 to indicate that Google Test
00080 //                              is building in C++11/C++98 mode.
00081 //   GTEST_LINKED_AS_SHARED_LIBRARY
00082 //                            - Define to 1 when compiling tests that use
00083 //                              Google Test as a shared library (known as
00084 //                              DLL on Windows).
00085 //   GTEST_CREATE_SHARED_LIBRARY
00086 //                            - Define to 1 when compiling Google Test itself
00087 //                              as a shared library.
00088 
00089 // This header defines the following utilities:
00090 //
00091 // Macros indicating the current platform (defined to 1 if compiled on
00092 // the given platform; otherwise undefined):
00093 //   GTEST_OS_AIX      - IBM AIX
00094 //   GTEST_OS_CYGWIN   - Cygwin
00095 //   GTEST_OS_HPUX     - HP-UX
00096 //   GTEST_OS_LINUX    - Linux
00097 //     GTEST_OS_LINUX_ANDROID - Google Android
00098 //   GTEST_OS_MAC      - Mac OS X
00099 //     GTEST_OS_IOS    - iOS
00100 //       GTEST_OS_IOS_SIMULATOR - iOS simulator
00101 //   GTEST_OS_NACL     - Google Native Client (NaCl)
00102 //   GTEST_OS_OPENBSD  - OpenBSD
00103 //   GTEST_OS_QNX      - QNX
00104 //   GTEST_OS_SOLARIS  - Sun Solaris
00105 //   GTEST_OS_SYMBIAN  - Symbian
00106 //   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
00107 //     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
00108 //     GTEST_OS_WINDOWS_MINGW    - MinGW
00109 //     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
00110 //   GTEST_OS_ZOS      - z/OS
00111 //
00112 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
00113 // most stable support.  Since core members of the Google Test project
00114 // don't have access to other platforms, support for them may be less
00115 // stable.  If you notice any problems on your platform, please notify
00116 // googletestframework@googlegroups.com (patches for fixing them are
00117 // even more welcome!).
00118 //
00119 // Note that it is possible that none of the GTEST_OS_* macros are defined.
00120 //
00121 // Macros indicating available Google Test features (defined to 1 if
00122 // the corresponding feature is supported; otherwise undefined):
00123 //   GTEST_HAS_COMBINE      - the Combine() function (for value-parameterized
00124 //                            tests)
00125 //   GTEST_HAS_DEATH_TEST   - death tests
00126 //   GTEST_HAS_PARAM_TEST   - value-parameterized tests
00127 //   GTEST_HAS_TYPED_TEST   - typed tests
00128 //   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
00129 //   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
00130 //                            GTEST_HAS_POSIX_RE (see above) which users can
00131 //                            define themselves.
00132 //   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
00133 //                            the above two are mutually exclusive.
00134 //   GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
00135 //
00136 // Macros for basic C++ coding:
00137 //   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
00138 //   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a
00139 //                              variable don't have to be used.
00140 //   GTEST_DISALLOW_ASSIGN_   - disables operator=.
00141 //   GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
00142 //   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.
00143 //
00144 // Synchronization:
00145 //   Mutex, MutexLock, ThreadLocal, GetThreadCount()
00146 //                  - synchronization primitives.
00147 //   GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
00148 //                         synchronization primitives have real implementations
00149 //                         and Google Test is thread-safe; or 0 otherwise.
00150 //
00151 // Template meta programming:
00152 //   is_pointer     - as in TR1; needed on Symbian and IBM XL C/C++ only.
00153 //   IteratorTraits - partial implementation of std::iterator_traits, which
00154 //                    is not available in libCstd when compiled with Sun C++.
00155 //
00156 // Smart pointers:
00157 //   scoped_ptr     - as in TR2.
00158 //
00159 // Regular expressions:
00160 //   RE             - a simple regular expression class using the POSIX
00161 //                    Extended Regular Expression syntax on UNIX-like
00162 //                    platforms, or a reduced regular exception syntax on
00163 //                    other platforms, including Windows.
00164 //
00165 // Logging:
00166 //   GTEST_LOG_()   - logs messages at the specified severity level.
00167 //   LogToStderr()  - directs all log messages to stderr.
00168 //   FlushInfoLog() - flushes informational log messages.
00169 //
00170 // Stdout and stderr capturing:
00171 //   CaptureStdout()     - starts capturing stdout.
00172 //   GetCapturedStdout() - stops capturing stdout and returns the captured
00173 //                         string.
00174 //   CaptureStderr()     - starts capturing stderr.
00175 //   GetCapturedStderr() - stops capturing stderr and returns the captured
00176 //                         string.
00177 //
00178 // Integer types:
00179 //   TypeWithSize   - maps an integer to a int type.
00180 //   Int32, UInt32, Int64, UInt64, TimeInMillis
00181 //                  - integers of known sizes.
00182 //   BiggestInt     - the biggest signed integer type.
00183 //
00184 // Command-line utilities:
00185 //   GTEST_FLAG()       - references a flag.
00186 //   GTEST_DECLARE_*()  - declares a flag.
00187 //   GTEST_DEFINE_*()   - defines a flag.
00188 //   GetInjectableArgvs() - returns the command line as a vector of strings.
00189 //
00190 // Environment variable utilities:
00191 //   GetEnv()             - gets the value of an environment variable.
00192 //   BoolFromGTestEnv()   - parses a bool environment variable.
00193 //   Int32FromGTestEnv()  - parses an Int32 environment variable.
00194 //   StringFromGTestEnv() - parses a string environment variable.
00195 
00196 #include <ctype.h>   // for isspace, etc
00197 #include <stddef.h>  // for ptrdiff_t
00198 #include <stdlib.h>
00199 #include <stdio.h>
00200 #include <string.h>
00201 #ifndef _WIN32_WCE
00202 # include <sys/types.h>
00203 # include <sys/stat.h>
00204 #endif  // !_WIN32_WCE
00205 
00206 #if defined __APPLE__
00207 # include <AvailabilityMacros.h>
00208 # include <TargetConditionals.h>
00209 #endif
00210 
00211 #include <iostream>  // NOLINT
00212 #include <sstream>  // NOLINT
00213 #include <string>  // NOLINT
00214 
00215 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
00216 #define GTEST_FLAG_PREFIX_ "gtest_"
00217 #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
00218 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
00219 #define GTEST_NAME_ "Google Test"
00220 #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
00221 
00222 // Determines the version of gcc that is used to compile this.
00223 #ifdef __GNUC__
00224 // 40302 means version 4.3.2.
00225 # define GTEST_GCC_VER_ \
00226     (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
00227 #endif  // __GNUC__
00228 
00229 // Determines the platform on which Google Test is compiled.
00230 #ifdef __CYGWIN__
00231 # define GTEST_OS_CYGWIN 1
00232 #elif defined __SYMBIAN32__
00233 # define GTEST_OS_SYMBIAN 1
00234 #elif defined _WIN32
00235 # define GTEST_OS_WINDOWS 1
00236 # ifdef _WIN32_WCE
00237 #  define GTEST_OS_WINDOWS_MOBILE 1
00238 # elif defined(__MINGW__) || defined(__MINGW32__)
00239 #  define GTEST_OS_WINDOWS_MINGW 1
00240 # else
00241 #  define GTEST_OS_WINDOWS_DESKTOP 1
00242 # endif  // _WIN32_WCE
00243 #elif defined __APPLE__
00244 # define GTEST_OS_MAC 1
00245 # if TARGET_OS_IPHONE
00246 #  define GTEST_OS_IOS 1
00247 #  if TARGET_IPHONE_SIMULATOR
00248 #   define GTEST_OS_IOS_SIMULATOR 1
00249 #  endif
00250 # endif
00251 #elif defined __linux__
00252 # define GTEST_OS_LINUX 1
00253 # if defined __ANDROID__
00254 #  define GTEST_OS_LINUX_ANDROID 1
00255 # endif
00256 #elif defined __MVS__
00257 # define GTEST_OS_ZOS 1
00258 #elif defined(__sun) && defined(__SVR4)
00259 # define GTEST_OS_SOLARIS 1
00260 #elif defined(_AIX)
00261 # define GTEST_OS_AIX 1
00262 #elif defined(__hpux)
00263 # define GTEST_OS_HPUX 1
00264 #elif defined __native_client__
00265 # define GTEST_OS_NACL 1
00266 #elif defined __OpenBSD__
00267 # define GTEST_OS_OPENBSD 1
00268 #elif defined __QNX__
00269 # define GTEST_OS_QNX 1
00270 #endif  // __CYGWIN__
00271 
00272 #ifndef GTEST_LANG_CXX11
00273 // gcc and clang define __GXX_EXPERIMENTAL_CXX0X__ when
00274 // -std={c,gnu}++{0x,11} is passed.  The C++11 standard specifies a
00275 // value for __cplusplus, and recent versions of clang, gcc, and
00276 // probably other compilers set that too in C++11 mode.
00277 # if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L
00278 // Compiling in at least C++11 mode.
00279 #  define GTEST_LANG_CXX11 1
00280 # else
00281 #  define GTEST_LANG_CXX11 0
00282 # endif
00283 #endif
00284 
00285 // Brings in definitions for functions used in the testing::internal::posix
00286 // namespace (read, write, close, chdir, isatty, stat). We do not currently
00287 // use them on Windows Mobile.
00288 #if !GTEST_OS_WINDOWS
00289 // This assumes that non-Windows OSes provide unistd.h. For OSes where this
00290 // is not the case, we need to include headers that provide the functions
00291 // mentioned above.
00292 # include <unistd.h>
00293 # include <strings.h>
00294 #elif !GTEST_OS_WINDOWS_MOBILE
00295 # include <direct.h>
00296 # include <io.h>
00297 #endif
00298 
00299 #if GTEST_OS_LINUX_ANDROID
00300 // Used to define __ANDROID_API__ matching the target NDK API level.
00301 #  include <android/api-level.h>  // NOLINT
00302 #endif
00303 
00304 // Defines this to true iff Google Test can use POSIX regular expressions.
00305 #ifndef GTEST_HAS_POSIX_RE
00306 # if GTEST_OS_LINUX_ANDROID
00307 // On Android, <regex.h> is only available starting with Gingerbread.
00308 #  define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
00309 # else
00310 #  define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
00311 # endif
00312 #endif
00313 
00314 #if GTEST_HAS_POSIX_RE
00315 
00316 // On some platforms, <regex.h> needs someone to define size_t, and
00317 // won't compile otherwise.  We can #include it here as we already
00318 // included <stdlib.h>, which is guaranteed to define size_t through
00319 // <stddef.h>.
00320 # include <regex.h>  // NOLINT
00321 
00322 # define GTEST_USES_POSIX_RE 1
00323 
00324 #elif GTEST_OS_WINDOWS
00325 
00326 // <regex.h> is not available on Windows.  Use our own simple regex
00327 // implementation instead.
00328 # define GTEST_USES_SIMPLE_RE 1
00329 
00330 #else
00331 
00332 // <regex.h> may not be available on this platform.  Use our own
00333 // simple regex implementation instead.
00334 # define GTEST_USES_SIMPLE_RE 1
00335 
00336 #endif  // GTEST_HAS_POSIX_RE
00337 
00338 #ifndef GTEST_HAS_EXCEPTIONS
00339 // The user didn't tell us whether exceptions are enabled, so we need
00340 // to figure it out.
00341 # if defined(_MSC_VER) || defined(__BORLANDC__)
00342 // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
00343 // macro to enable exceptions, so we'll do the same.
00344 // Assumes that exceptions are enabled by default.
00345 #  ifndef _HAS_EXCEPTIONS
00346 #   define _HAS_EXCEPTIONS 1
00347 #  endif  // _HAS_EXCEPTIONS
00348 #  define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
00349 # elif defined(__GNUC__) && __EXCEPTIONS
00350 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
00351 #  define GTEST_HAS_EXCEPTIONS 1
00352 # elif defined(__SUNPRO_CC)
00353 // Sun Pro CC supports exceptions.  However, there is no compile-time way of
00354 // detecting whether they are enabled or not.  Therefore, we assume that
00355 // they are enabled unless the user tells us otherwise.
00356 #  define GTEST_HAS_EXCEPTIONS 1
00357 # elif defined(__IBMCPP__) && __EXCEPTIONS
00358 // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
00359 #  define GTEST_HAS_EXCEPTIONS 1
00360 # elif defined(__HP_aCC)
00361 // Exception handling is in effect by default in HP aCC compiler. It has to
00362 // be turned of by +noeh compiler option if desired.
00363 #  define GTEST_HAS_EXCEPTIONS 1
00364 # else
00365 // For other compilers, we assume exceptions are disabled to be
00366 // conservative.
00367 #  define GTEST_HAS_EXCEPTIONS 0
00368 # endif  // defined(_MSC_VER) || defined(__BORLANDC__)
00369 #endif  // GTEST_HAS_EXCEPTIONS
00370 
00371 #if !defined(GTEST_HAS_STD_STRING)
00372 // Even though we don't use this macro any longer, we keep it in case
00373 // some clients still depend on it.
00374 # define GTEST_HAS_STD_STRING 1
00375 #elif !GTEST_HAS_STD_STRING
00376 // The user told us that ::std::string isn't available.
00377 # error "Google Test cannot be used where ::std::string isn't available."
00378 #endif  // !defined(GTEST_HAS_STD_STRING)
00379 
00380 #ifndef GTEST_HAS_GLOBAL_STRING
00381 // The user didn't tell us whether ::string is available, so we need
00382 // to figure it out.
00383 
00384 # define GTEST_HAS_GLOBAL_STRING 0
00385 
00386 #endif  // GTEST_HAS_GLOBAL_STRING
00387 
00388 #ifndef GTEST_HAS_STD_WSTRING
00389 // The user didn't tell us whether ::std::wstring is available, so we need
00390 // to figure it out.
00391 // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
00392 //   is available.
00393 
00394 // Cygwin 1.7 and below doesn't support ::std::wstring.
00395 // Solaris' libc++ doesn't support it either.  Android has
00396 // no support for it at least as recent as Froyo (2.2).
00397 # define GTEST_HAS_STD_WSTRING \
00398     (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
00399 
00400 #endif  // GTEST_HAS_STD_WSTRING
00401 
00402 #ifndef GTEST_HAS_GLOBAL_WSTRING
00403 // The user didn't tell us whether ::wstring is available, so we need
00404 // to figure it out.
00405 # define GTEST_HAS_GLOBAL_WSTRING \
00406     (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
00407 #endif  // GTEST_HAS_GLOBAL_WSTRING
00408 
00409 // Determines whether RTTI is available.
00410 #ifndef GTEST_HAS_RTTI
00411 // The user didn't tell us whether RTTI is enabled, so we need to
00412 // figure it out.
00413 
00414 # ifdef _MSC_VER
00415 
00416 #  ifdef _CPPRTTI  // MSVC defines this macro iff RTTI is enabled.
00417 #   define GTEST_HAS_RTTI 1
00418 #  else
00419 #   define GTEST_HAS_RTTI 0
00420 #  endif
00421 
00422 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
00423 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
00424 
00425 #  ifdef __GXX_RTTI
00426 // When building against STLport with the Android NDK and with
00427 // -frtti -fno-exceptions, the build fails at link time with undefined
00428 // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
00429 // so disable RTTI when detected.
00430 #   if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
00431        !defined(__EXCEPTIONS)
00432 #    define GTEST_HAS_RTTI 0
00433 #   else
00434 #    define GTEST_HAS_RTTI 1
00435 #   endif  // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
00436 #  else
00437 #   define GTEST_HAS_RTTI 0
00438 #  endif  // __GXX_RTTI
00439 
00440 // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
00441 // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
00442 // first version with C++ support.
00443 # elif defined(__clang__)
00444 
00445 #  define GTEST_HAS_RTTI __has_feature(cxx_rtti)
00446 
00447 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
00448 // both the typeid and dynamic_cast features are present.
00449 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
00450 
00451 #  ifdef __RTTI_ALL__
00452 #   define GTEST_HAS_RTTI 1
00453 #  else
00454 #   define GTEST_HAS_RTTI 0
00455 #  endif
00456 
00457 # else
00458 
00459 // For all other compilers, we assume RTTI is enabled.
00460 #  define GTEST_HAS_RTTI 1
00461 
00462 # endif  // _MSC_VER
00463 
00464 #endif  // GTEST_HAS_RTTI
00465 
00466 // It's this header's responsibility to #include <typeinfo> when RTTI
00467 // is enabled.
00468 #if GTEST_HAS_RTTI
00469 # include <typeinfo>
00470 #endif
00471 
00472 // Determines whether Google Test can use the pthreads library.
00473 #ifndef GTEST_HAS_PTHREAD
00474 // The user didn't tell us explicitly, so we assume pthreads support is
00475 // available on Linux and Mac.
00476 //
00477 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
00478 // to your compiler flags.
00479 # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \
00480     || GTEST_OS_QNX)
00481 #endif  // GTEST_HAS_PTHREAD
00482 
00483 #if GTEST_HAS_PTHREAD
00484 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
00485 // true.
00486 # include <pthread.h>  // NOLINT
00487 
00488 // For timespec and nanosleep, used below.
00489 # include <time.h>  // NOLINT
00490 #endif
00491 
00492 // Determines whether Google Test can use tr1/tuple.  You can define
00493 // this macro to 0 to prevent Google Test from using tuple (any
00494 // feature depending on tuple with be disabled in this mode).
00495 #ifndef GTEST_HAS_TR1_TUPLE
00496 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
00497 // STLport, provided with the Android NDK, has neither <tr1/tuple> or <tuple>.
00498 #  define GTEST_HAS_TR1_TUPLE 0
00499 # else
00500 // The user didn't tell us not to do it, so we assume it's OK.
00501 #  define GTEST_HAS_TR1_TUPLE 1
00502 # endif
00503 #endif  // GTEST_HAS_TR1_TUPLE
00504 
00505 // Determines whether Google Test's own tr1 tuple implementation
00506 // should be used.
00507 #ifndef GTEST_USE_OWN_TR1_TUPLE
00508 // The user didn't tell us, so we need to figure it out.
00509 
00510 // We use our own TR1 tuple if we aren't sure the user has an
00511 // implementation of it already.  At this time, libstdc++ 4.0.0+ and
00512 // MSVC 2010 are the only mainstream standard libraries that come
00513 // with a TR1 tuple implementation.  NVIDIA's CUDA NVCC compiler
00514 // pretends to be GCC by defining __GNUC__ and friends, but cannot
00515 // compile GCC's tuple implementation.  MSVC 2008 (9.0) provides TR1
00516 // tuple in a 323 MB Feature Pack download, which we cannot assume the
00517 // user has.  QNX's QCC compiler is a modified GCC but it doesn't
00518 // support TR1 tuple.  libc++ only provides std::tuple, in C++11 mode,
00519 // and it can be used with some compilers that define __GNUC__.
00520 # if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
00521       && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600
00522 #  define GTEST_ENV_HAS_TR1_TUPLE_ 1
00523 # endif
00524 
00525 // C++11 specifies that <tuple> provides std::tuple. Use that if gtest is used
00526 // in C++11 mode and libstdc++ isn't very old (binaries targeting OS X 10.6
00527 // can build with clang but need to use gcc4.2's libstdc++).
00528 # if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
00529 #  define GTEST_ENV_HAS_STD_TUPLE_ 1
00530 # endif
00531 
00532 # if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
00533 #  define GTEST_USE_OWN_TR1_TUPLE 0
00534 # else
00535 #  define GTEST_USE_OWN_TR1_TUPLE 1
00536 # endif
00537 
00538 #endif  // GTEST_USE_OWN_TR1_TUPLE
00539 
00540 // To avoid conditional compilation everywhere, we make it
00541 // gtest-port.h's responsibility to #include the header implementing
00542 // tr1/tuple.
00543 #if GTEST_HAS_TR1_TUPLE
00544 
00545 # if GTEST_USE_OWN_TR1_TUPLE
00546 #  include "gtest/internal/gtest-tuple.h"
00547 # elif GTEST_ENV_HAS_STD_TUPLE_
00548 #  include <tuple>
00549 // C++11 puts its tuple into the ::std namespace rather than
00550 // ::std::tr1.  gtest expects tuple to live in ::std::tr1, so put it there.
00551 // This causes undefined behavior, but supported compilers react in
00552 // the way we intend.
00553 namespace std {
00554 namespace tr1 {
00555 using ::std::get;
00556 using ::std::make_tuple;
00557 using ::std::tuple;
00558 using ::std::tuple_element;
00559 using ::std::tuple_size;
00560 }
00561 }
00562 
00563 # elif GTEST_OS_SYMBIAN
00564 
00565 // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
00566 // use STLport's tuple implementation, which unfortunately doesn't
00567 // work as the copy of STLport distributed with Symbian is incomplete.
00568 // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
00569 // use its own tuple implementation.
00570 #  ifdef BOOST_HAS_TR1_TUPLE
00571 #   undef BOOST_HAS_TR1_TUPLE
00572 #  endif  // BOOST_HAS_TR1_TUPLE
00573 
00574 // This prevents <boost/tr1/detail/config.hpp>, which defines
00575 // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
00576 #  define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
00577 #  include <tuple>
00578 
00579 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
00580 // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header.  This does
00581 // not conform to the TR1 spec, which requires the header to be <tuple>.
00582 
00583 #  if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
00584 // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
00585 // which is #included by <tr1/tuple>, to not compile when RTTI is
00586 // disabled.  _TR1_FUNCTIONAL is the header guard for
00587 // <tr1/functional>.  Hence the following #define is a hack to prevent
00588 // <tr1/functional> from being included.
00589 #   define _TR1_FUNCTIONAL 1
00590 #   include <tr1/tuple>
00591 #   undef _TR1_FUNCTIONAL  // Allows the user to #include
00592                         // <tr1/functional> if he chooses to.
00593 #  else
00594 #   include <tr1/tuple>  // NOLINT
00595 #  endif  // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
00596 
00597 # else
00598 // If the compiler is not GCC 4.0+, we assume the user is using a
00599 // spec-conforming TR1 implementation.
00600 #  include <tuple>  // NOLINT
00601 # endif  // GTEST_USE_OWN_TR1_TUPLE
00602 
00603 #endif  // GTEST_HAS_TR1_TUPLE
00604 
00605 // Determines whether clone(2) is supported.
00606 // Usually it will only be available on Linux, excluding
00607 // Linux on the Itanium architecture.
00608 // Also see http://linux.die.net/man/2/clone.
00609 #ifndef GTEST_HAS_CLONE
00610 // The user didn't tell us, so we need to figure it out.
00611 
00612 # if GTEST_OS_LINUX && !defined(__ia64__)
00613 #  if GTEST_OS_LINUX_ANDROID
00614 // On Android, clone() is only available on ARM starting with Gingerbread.
00615 #    if defined(__arm__) && __ANDROID_API__ >= 9
00616 #     define GTEST_HAS_CLONE 1
00617 #    else
00618 #     define GTEST_HAS_CLONE 0
00619 #    endif
00620 #  else
00621 #   define GTEST_HAS_CLONE 1
00622 #  endif
00623 # else
00624 #  define GTEST_HAS_CLONE 0
00625 # endif  // GTEST_OS_LINUX && !defined(__ia64__)
00626 
00627 #endif  // GTEST_HAS_CLONE
00628 
00629 // Determines whether to support stream redirection. This is used to test
00630 // output correctness and to implement death tests.
00631 #ifndef GTEST_HAS_STREAM_REDIRECTION
00632 // By default, we assume that stream redirection is supported on all
00633 // platforms except known mobile ones.
00634 # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN
00635 #  define GTEST_HAS_STREAM_REDIRECTION 0
00636 # else
00637 #  define GTEST_HAS_STREAM_REDIRECTION 1
00638 # endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
00639 #endif  // GTEST_HAS_STREAM_REDIRECTION
00640 
00641 // Determines whether to support death tests.
00642 // Google Test does not support death tests for VC 7.1 and earlier as
00643 // abort() in a VC 7.1 application compiled as GUI in debug config
00644 // pops up a dialog window that cannot be suppressed programmatically.
00645 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
00646      (GTEST_OS_MAC && !GTEST_OS_IOS) || GTEST_OS_IOS_SIMULATOR || \
00647      (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
00648      GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
00649      GTEST_OS_OPENBSD || GTEST_OS_QNX)
00650 # define GTEST_HAS_DEATH_TEST 1
00651 # include <vector>  // NOLINT
00652 #endif
00653 
00654 // We don't support MSVC 7.1 with exceptions disabled now.  Therefore
00655 // all the compilers we care about are adequate for supporting
00656 // value-parameterized tests.
00657 #define GTEST_HAS_PARAM_TEST 1
00658 
00659 // Determines whether to support type-driven tests.
00660 
00661 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
00662 // Sun Pro CC, IBM Visual Age, and HP aCC support.
00663 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
00664     defined(__IBMCPP__) || defined(__HP_aCC)
00665 # define GTEST_HAS_TYPED_TEST 1
00666 # define GTEST_HAS_TYPED_TEST_P 1
00667 #endif
00668 
00669 // Determines whether to support Combine(). This only makes sense when
00670 // value-parameterized tests are enabled.  The implementation doesn't
00671 // work on Sun Studio since it doesn't understand templated conversion
00672 // operators.
00673 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
00674 # define GTEST_HAS_COMBINE 1
00675 #endif
00676 
00677 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
00678 #define GTEST_WIDE_STRING_USES_UTF16_ \
00679     (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
00680 
00681 // Determines whether test results can be streamed to a socket.
00682 #if GTEST_OS_LINUX
00683 # define GTEST_CAN_STREAM_RESULTS_ 1
00684 #endif
00685 
00686 // Defines some utility macros.
00687 
00688 // The GNU compiler emits a warning if nested "if" statements are followed by
00689 // an "else" statement and braces are not used to explicitly disambiguate the
00690 // "else" binding.  This leads to problems with code like:
00691 //
00692 //   if (gate)
00693 //     ASSERT_*(condition) << "Some message";
00694 //
00695 // The "switch (0) case 0:" idiom is used to suppress this.
00696 #ifdef __INTEL_COMPILER
00697 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
00698 #else
00699 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:  // NOLINT
00700 #endif
00701 
00702 // Use this annotation at the end of a struct/class definition to
00703 // prevent the compiler from optimizing away instances that are never
00704 // used.  This is useful when all interesting logic happens inside the
00705 // c'tor and / or d'tor.  Example:
00706 //
00707 //   struct Foo {
00708 //     Foo() { ... }
00709 //   } GTEST_ATTRIBUTE_UNUSED_;
00710 //
00711 // Also use it after a variable or parameter declaration to tell the
00712 // compiler the variable/parameter does not have to be used.
00713 #if defined(__GNUC__) && !defined(COMPILER_ICC)
00714 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
00715 #else
00716 # define GTEST_ATTRIBUTE_UNUSED_
00717 #endif
00718 
00719 // A macro to disallow operator=
00720 // This should be used in the private: declarations for a class.
00721 #define GTEST_DISALLOW_ASSIGN_(type)\
00722   void operator=(type const &)
00723 
00724 // A macro to disallow copy constructor and operator=
00725 // This should be used in the private: declarations for a class.
00726 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
00727   type(type const &);\
00728   GTEST_DISALLOW_ASSIGN_(type)
00729 
00730 // Tell the compiler to warn about unused return values for functions declared
00731 // with this macro.  The macro should be used on function declarations
00732 // following the argument list:
00733 //
00734 //   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
00735 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
00736 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
00737 #else
00738 # define GTEST_MUST_USE_RESULT_
00739 #endif  // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
00740 
00741 // Determine whether the compiler supports Microsoft's Structured Exception
00742 // Handling.  This is supported by several Windows compilers but generally
00743 // does not exist on any other system.
00744 #ifndef GTEST_HAS_SEH
00745 // The user didn't tell us, so we need to figure it out.
00746 
00747 # if defined(_MSC_VER) || defined(__BORLANDC__)
00748 // These two compilers are known to support SEH.
00749 #  define GTEST_HAS_SEH 1
00750 # else
00751 // Assume no SEH.
00752 #  define GTEST_HAS_SEH 0
00753 # endif
00754 
00755 #endif  // GTEST_HAS_SEH
00756 
00757 #ifdef _MSC_VER
00758 
00759 # if GTEST_LINKED_AS_SHARED_LIBRARY
00760 #  define GTEST_API_ __declspec(dllimport)
00761 # elif GTEST_CREATE_SHARED_LIBRARY
00762 #  define GTEST_API_ __declspec(dllexport)
00763 # endif
00764 
00765 #endif  // _MSC_VER
00766 
00767 #ifndef GTEST_API_
00768 # define GTEST_API_
00769 #endif
00770 
00771 #ifdef __GNUC__
00772 // Ask the compiler to never inline a given function.
00773 # define GTEST_NO_INLINE_ __attribute__((noinline))
00774 #else
00775 # define GTEST_NO_INLINE_
00776 #endif
00777 
00778 // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
00779 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
00780 # define GTEST_HAS_CXXABI_H_ 1
00781 #else
00782 # define GTEST_HAS_CXXABI_H_ 0
00783 #endif
00784 
00785 namespace testing {
00786 
00787 class Message;
00788 
00789 namespace internal {
00790 
00791 // A secret type that Google Test users don't know about.  It has no
00792 // definition on purpose.  Therefore it's impossible to create a
00793 // Secret object, which is what we want.
00794 class Secret;
00795 
00796 // The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
00797 // expression is true. For example, you could use it to verify the
00798 // size of a static array:
00799 //
00800 //   GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
00801 //                         content_type_names_incorrect_size);
00802 //
00803 // or to make sure a struct is smaller than a certain size:
00804 //
00805 //   GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
00806 //
00807 // The second argument to the macro is the name of the variable. If
00808 // the expression is false, most compilers will issue a warning/error
00809 // containing the name of the variable.
00810 
00811 template <bool>
00812 struct CompileAssert {
00813 };
00814 
00815 #define GTEST_COMPILE_ASSERT_(expr, msg) \
00816   typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
00817       msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
00818 
00819 // Implementation details of GTEST_COMPILE_ASSERT_:
00820 //
00821 // - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
00822 //   elements (and thus is invalid) when the expression is false.
00823 //
00824 // - The simpler definition
00825 //
00826 //    #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
00827 //
00828 //   does not work, as gcc supports variable-length arrays whose sizes
00829 //   are determined at run-time (this is gcc's extension and not part
00830 //   of the C++ standard).  As a result, gcc fails to reject the
00831 //   following code with the simple definition:
00832 //
00833 //     int foo;
00834 //     GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
00835 //                                      // not a compile-time constant.
00836 //
00837 // - By using the type CompileAssert<(bool(expr))>, we ensures that
00838 //   expr is a compile-time constant.  (Template arguments must be
00839 //   determined at compile-time.)
00840 //
00841 // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
00842 //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
00843 //
00844 //     CompileAssert<bool(expr)>
00845 //
00846 //   instead, these compilers will refuse to compile
00847 //
00848 //     GTEST_COMPILE_ASSERT_(5 > 0, some_message);
00849 //
00850 //   (They seem to think the ">" in "5 > 0" marks the end of the
00851 //   template argument list.)
00852 //
00853 // - The array size is (bool(expr) ? 1 : -1), instead of simply
00854 //
00855 //     ((expr) ? 1 : -1).
00856 //
00857 //   This is to avoid running into a bug in MS VC 7.1, which
00858 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
00859 
00860 // StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
00861 //
00862 // This template is declared, but intentionally undefined.
00863 template <typename T1, typename T2>
00864 struct StaticAssertTypeEqHelper;
00865 
00866 template <typename T>
00867 struct StaticAssertTypeEqHelper<T, T> {};
00868 
00869 #if GTEST_HAS_GLOBAL_STRING
00870 typedef ::string string;
00871 #else
00872 typedef ::std::string string;
00873 #endif  // GTEST_HAS_GLOBAL_STRING
00874 
00875 #if GTEST_HAS_GLOBAL_WSTRING
00876 typedef ::wstring wstring;
00877 #elif GTEST_HAS_STD_WSTRING
00878 typedef ::std::wstring wstring;
00879 #endif  // GTEST_HAS_GLOBAL_WSTRING
00880 
00881 // A helper for suppressing warnings on constant condition.  It just
00882 // returns 'condition'.
00883 GTEST_API_ bool IsTrue(bool condition);
00884 
00885 // Defines scoped_ptr.
00886 
00887 // This implementation of scoped_ptr is PARTIAL - it only contains
00888 // enough stuff to satisfy Google Test's need.
00889 template <typename T>
00890 class scoped_ptr {
00891  public:
00892   typedef T element_type;
00893 
00894   explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
00895   ~scoped_ptr() { reset(); }
00896 
00897   T& operator*() const { return *ptr_; }
00898   T* operator->() const { return ptr_; }
00899   T* get() const { return ptr_; }
00900 
00901   T* release() {
00902     T* const ptr = ptr_;
00903     ptr_ = NULL;
00904     return ptr;
00905   }
00906 
00907   void reset(T* p = NULL) {
00908     if (p != ptr_) {
00909       if (IsTrue(sizeof(T) > 0)) {  // Makes sure T is a complete type.
00910         delete ptr_;
00911       }
00912       ptr_ = p;
00913     }
00914   }
00915 
00916  private:
00917   T* ptr_;
00918 
00919   GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
00920 };
00921 
00922 // Defines RE.
00923 
00924 // A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
00925 // Regular Expression syntax.
00926 class GTEST_API_ RE {
00927  public:
00928   // A copy constructor is required by the Standard to initialize object
00929   // references from r-values.
00930   RE(const RE& other) { Init(other.pattern()); }
00931 
00932   // Constructs an RE from a string.
00933   RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
00934 
00935 #if GTEST_HAS_GLOBAL_STRING
00936 
00937   RE(const ::string& regex) { Init(regex.c_str()); }  // NOLINT
00938 
00939 #endif  // GTEST_HAS_GLOBAL_STRING
00940 
00941   RE(const char* regex) { Init(regex); }  // NOLINT
00942   ~RE();
00943 
00944   // Returns the string representation of the regex.
00945   const char* pattern() const { return pattern_; }
00946 
00947   // FullMatch(str, re) returns true iff regular expression re matches
00948   // the entire str.
00949   // PartialMatch(str, re) returns true iff regular expression re
00950   // matches a substring of str (including str itself).
00951   //
00952   // TODO(wan@google.com): make FullMatch() and PartialMatch() work
00953   // when str contains NUL characters.
00954   static bool FullMatch(const ::std::string& str, const RE& re) {
00955     return FullMatch(str.c_str(), re);
00956   }
00957   static bool PartialMatch(const ::std::string& str, const RE& re) {
00958     return PartialMatch(str.c_str(), re);
00959   }
00960 
00961 #if GTEST_HAS_GLOBAL_STRING
00962 
00963   static bool FullMatch(const ::string& str, const RE& re) {
00964     return FullMatch(str.c_str(), re);
00965   }
00966   static bool PartialMatch(const ::string& str, const RE& re) {
00967     return PartialMatch(str.c_str(), re);
00968   }
00969 
00970 #endif  // GTEST_HAS_GLOBAL_STRING
00971 
00972   static bool FullMatch(const char* str, const RE& re);
00973   static bool PartialMatch(const char* str, const RE& re);
00974 
00975  private:
00976   void Init(const char* regex);
00977 
00978   // We use a const char* instead of an std::string, as Google Test used to be
00979   // used where std::string is not available.  TODO(wan@google.com): change to
00980   // std::string.
00981   const char* pattern_;
00982   bool is_valid_;
00983 
00984 #if GTEST_USES_POSIX_RE
00985 
00986   regex_t full_regex_;     // For FullMatch().
00987   regex_t partial_regex_;  // For PartialMatch().
00988 
00989 #else  // GTEST_USES_SIMPLE_RE
00990 
00991   const char* full_pattern_;  // For FullMatch();
00992 
00993 #endif
00994 
00995   GTEST_DISALLOW_ASSIGN_(RE);
00996 };
00997 
00998 // Formats a source file path and a line number as they would appear
00999 // in an error message from the compiler used to compile this code.
01000 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
01001 
01002 // Formats a file location for compiler-independent XML output.
01003 // Although this function is not platform dependent, we put it next to
01004 // FormatFileLocation in order to contrast the two functions.
01005 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
01006                                                                int line);
01007 
01008 // Defines logging utilities:
01009 //   GTEST_LOG_(severity) - logs messages at the specified severity level. The
01010 //                          message itself is streamed into the macro.
01011 //   LogToStderr()  - directs all log messages to stderr.
01012 //   FlushInfoLog() - flushes informational log messages.
01013 
01014 enum GTestLogSeverity {
01015   GTEST_INFO,
01016   GTEST_WARNING,
01017   GTEST_ERROR,
01018   GTEST_FATAL
01019 };
01020 
01021 // Formats log entry severity, provides a stream object for streaming the
01022 // log message, and terminates the message with a newline when going out of
01023 // scope.
01024 class GTEST_API_ GTestLog {
01025  public:
01026   GTestLog(GTestLogSeverity severity, const char* file, int line);
01027 
01028   // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
01029   ~GTestLog();
01030 
01031   ::std::ostream& GetStream() { return ::std::cerr; }
01032 
01033  private:
01034   const GTestLogSeverity severity_;
01035 
01036   GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
01037 };
01038 
01039 #define GTEST_LOG_(severity) \
01040     ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
01041                                   __FILE__, __LINE__).GetStream()
01042 
01043 inline void LogToStderr() {}
01044 inline void FlushInfoLog() { fflush(NULL); }
01045 
01046 // INTERNAL IMPLEMENTATION - DO NOT USE.
01047 //
01048 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
01049 // is not satisfied.
01050 //  Synopsys:
01051 //    GTEST_CHECK_(boolean_condition);
01052 //     or
01053 //    GTEST_CHECK_(boolean_condition) << "Additional message";
01054 //
01055 //    This checks the condition and if the condition is not satisfied
01056 //    it prints message about the condition violation, including the
01057 //    condition itself, plus additional message streamed into it, if any,
01058 //    and then it aborts the program. It aborts the program irrespective of
01059 //    whether it is built in the debug mode or not.
01060 #define GTEST_CHECK_(condition) \
01061     GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
01062     if (::testing::internal::IsTrue(condition)) \
01063       ; \
01064     else \
01065       GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
01066 
01067 // An all-mode assert to verify that the given POSIX-style function
01068 // call returns 0 (indicating success).  Known limitation: this
01069 // doesn't expand to a balanced 'if' statement, so enclose the macro
01070 // in {} if you need to use it as the only statement in an 'if'
01071 // branch.
01072 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
01073   if (const int gtest_error = (posix_call)) \
01074     GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
01075                       << gtest_error
01076 
01077 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
01078 //
01079 // Use ImplicitCast_ as a safe version of static_cast for upcasting in
01080 // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
01081 // const Foo*).  When you use ImplicitCast_, the compiler checks that
01082 // the cast is safe.  Such explicit ImplicitCast_s are necessary in
01083 // surprisingly many situations where C++ demands an exact type match
01084 // instead of an argument type convertable to a target type.
01085 //
01086 // The syntax for using ImplicitCast_ is the same as for static_cast:
01087 //
01088 //   ImplicitCast_<ToType>(expr)
01089 //
01090 // ImplicitCast_ would have been part of the C++ standard library,
01091 // but the proposal was submitted too late.  It will probably make
01092 // its way into the language in the future.
01093 //
01094 // This relatively ugly name is intentional. It prevents clashes with
01095 // similar functions users may have (e.g., implicit_cast). The internal
01096 // namespace alone is not enough because the function can be found by ADL.
01097 template<typename To>
01098 inline To ImplicitCast_(To x) { return x; }
01099 
01100 // When you upcast (that is, cast a pointer from type Foo to type
01101 // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
01102 // always succeed.  When you downcast (that is, cast a pointer from
01103 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
01104 // how do you know the pointer is really of type SubclassOfFoo?  It
01105 // could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
01106 // when you downcast, you should use this macro.  In debug mode, we
01107 // use dynamic_cast<> to double-check the downcast is legal (we die
01108 // if it's not).  In normal mode, we do the efficient static_cast<>
01109 // instead.  Thus, it's important to test in debug mode to make sure
01110 // the cast is legal!
01111 //    This is the only place in the code we should use dynamic_cast<>.
01112 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
01113 // do RTTI (eg code like this:
01114 //    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
01115 //    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
01116 // You should design the code some other way not to need this.
01117 //
01118 // This relatively ugly name is intentional. It prevents clashes with
01119 // similar functions users may have (e.g., down_cast). The internal
01120 // namespace alone is not enough because the function can be found by ADL.
01121 template<typename To, typename From>  // use like this: DownCast_<T*>(foo);
01122 inline To DownCast_(From* f) {  // so we only accept pointers
01123   // Ensures that To is a sub-type of From *.  This test is here only
01124   // for compile-time type checking, and has no overhead in an
01125   // optimized build at run-time, as it will be optimized away
01126   // completely.
01127   if (false) {
01128     const To to = NULL;
01129     ::testing::internal::ImplicitCast_<From*>(to);
01130   }
01131 
01132 #if GTEST_HAS_RTTI
01133   // RTTI: debug mode only!
01134   GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
01135 #endif
01136   return static_cast<To>(f);
01137 }
01138 
01139 // Downcasts the pointer of type Base to Derived.
01140 // Derived must be a subclass of Base. The parameter MUST
01141 // point to a class of type Derived, not any subclass of it.
01142 // When RTTI is available, the function performs a runtime
01143 // check to enforce this.
01144 template <class Derived, class Base>
01145 Derived* CheckedDowncastToActualType(Base* base) {
01146 #if GTEST_HAS_RTTI
01147   GTEST_CHECK_(typeid(*base) == typeid(Derived));
01148   return dynamic_cast<Derived*>(base);  // NOLINT
01149 #else
01150   return static_cast<Derived*>(base);  // Poor man's downcast.
01151 #endif
01152 }
01153 
01154 #if GTEST_HAS_STREAM_REDIRECTION
01155 
01156 // Defines the stderr capturer:
01157 //   CaptureStdout     - starts capturing stdout.
01158 //   GetCapturedStdout - stops capturing stdout and returns the captured string.
01159 //   CaptureStderr     - starts capturing stderr.
01160 //   GetCapturedStderr - stops capturing stderr and returns the captured string.
01161 //
01162 GTEST_API_ void CaptureStdout();
01163 GTEST_API_ std::string GetCapturedStdout();
01164 GTEST_API_ void CaptureStderr();
01165 GTEST_API_ std::string GetCapturedStderr();
01166 
01167 #endif  // GTEST_HAS_STREAM_REDIRECTION
01168 
01169 
01170 #if GTEST_HAS_DEATH_TEST
01171 
01172 const ::std::vector<testing::internal::string>& GetInjectableArgvs();
01173 void SetInjectableArgvs(const ::std::vector<testing::internal::string>*
01174                              new_argvs);
01175 
01176 // A copy of all command line arguments.  Set by InitGoogleTest().
01177 extern ::std::vector<testing::internal::string> g_argvs;
01178 
01179 #endif  // GTEST_HAS_DEATH_TEST
01180 
01181 // Defines synchronization primitives.
01182 
01183 #if GTEST_HAS_PTHREAD
01184 
01185 // Sleeps for (roughly) n milli-seconds.  This function is only for
01186 // testing Google Test's own constructs.  Don't use it in user tests,
01187 // either directly or indirectly.
01188 inline void SleepMilliseconds(int n) {
01189   const timespec time = {
01190     0,                  // 0 seconds.
01191     n * 1000L * 1000L,  // And n ms.
01192   };
01193   nanosleep(&time, NULL);
01194 }
01195 
01196 // Allows a controller thread to pause execution of newly created
01197 // threads until notified.  Instances of this class must be created
01198 // and destroyed in the controller thread.
01199 //
01200 // This class is only for testing Google Test's own constructs. Do not
01201 // use it in user tests, either directly or indirectly.
01202 class Notification {
01203  public:
01204   Notification() : notified_(false) {
01205     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
01206   }
01207   ~Notification() {
01208     pthread_mutex_destroy(&mutex_);
01209   }
01210 
01211   // Notifies all threads created with this notification to start. Must
01212   // be called from the controller thread.
01213   void Notify() {
01214     pthread_mutex_lock(&mutex_);
01215     notified_ = true;
01216     pthread_mutex_unlock(&mutex_);
01217   }
01218 
01219   // Blocks until the controller thread notifies. Must be called from a test
01220   // thread.
01221   void WaitForNotification() {
01222     for (;;) {
01223       pthread_mutex_lock(&mutex_);
01224       const bool notified = notified_;
01225       pthread_mutex_unlock(&mutex_);
01226       if (notified)
01227         break;
01228       SleepMilliseconds(10);
01229     }
01230   }
01231 
01232  private:
01233   pthread_mutex_t mutex_;
01234   bool notified_;
01235 
01236   GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
01237 };
01238 
01239 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
01240 // Consequently, it cannot select a correct instantiation of ThreadWithParam
01241 // in order to call its Run(). Introducing ThreadWithParamBase as a
01242 // non-templated base class for ThreadWithParam allows us to bypass this
01243 // problem.
01244 class ThreadWithParamBase {
01245  public:
01246   virtual ~ThreadWithParamBase() {}
01247   virtual void Run() = 0;
01248 };
01249 
01250 // pthread_create() accepts a pointer to a function type with the C linkage.
01251 // According to the Standard (7.5/1), function types with different linkages
01252 // are different even if they are otherwise identical.  Some compilers (for
01253 // example, SunStudio) treat them as different types.  Since class methods
01254 // cannot be defined with C-linkage we need to define a free C-function to
01255 // pass into pthread_create().
01256 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
01257   static_cast<ThreadWithParamBase*>(thread)->Run();
01258   return NULL;
01259 }
01260 
01261 // Helper class for testing Google Test's multi-threading constructs.
01262 // To use it, write:
01263 //
01264 //   void ThreadFunc(int param) { /* Do things with param */ }
01265 //   Notification thread_can_start;
01266 //   ...
01267 //   // The thread_can_start parameter is optional; you can supply NULL.
01268 //   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
01269 //   thread_can_start.Notify();
01270 //
01271 // These classes are only for testing Google Test's own constructs. Do
01272 // not use them in user tests, either directly or indirectly.
01273 template <typename T>
01274 class ThreadWithParam : public ThreadWithParamBase {
01275  public:
01276   typedef void (*UserThreadFunc)(T);
01277 
01278   ThreadWithParam(
01279       UserThreadFunc func, T param, Notification* thread_can_start)
01280       : func_(func),
01281         param_(param),
01282         thread_can_start_(thread_can_start),
01283         finished_(false) {
01284     ThreadWithParamBase* const base = this;
01285     // The thread can be created only after all fields except thread_
01286     // have been initialized.
01287     GTEST_CHECK_POSIX_SUCCESS_(
01288         pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
01289   }
01290   ~ThreadWithParam() { Join(); }
01291 
01292   void Join() {
01293     if (!finished_) {
01294       GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
01295       finished_ = true;
01296     }
01297   }
01298 
01299   virtual void Run() {
01300     if (thread_can_start_ != NULL)
01301       thread_can_start_->WaitForNotification();
01302     func_(param_);
01303   }
01304 
01305  private:
01306   const UserThreadFunc func_;  // User-supplied thread function.
01307   const T param_;  // User-supplied parameter to the thread function.
01308   // When non-NULL, used to block execution until the controller thread
01309   // notifies.
01310   Notification* const thread_can_start_;
01311   bool finished_;  // true iff we know that the thread function has finished.
01312   pthread_t thread_;  // The native thread object.
01313 
01314   GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
01315 };
01316 
01317 // MutexBase and Mutex implement mutex on pthreads-based platforms. They
01318 // are used in conjunction with class MutexLock:
01319 //
01320 //   Mutex mutex;
01321 //   ...
01322 //   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the end
01323 //                            // of the current scope.
01324 //
01325 // MutexBase implements behavior for both statically and dynamically
01326 // allocated mutexes.  Do not use MutexBase directly.  Instead, write
01327 // the following to define a static mutex:
01328 //
01329 //   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
01330 //
01331 // You can forward declare a static mutex like this:
01332 //
01333 //   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
01334 //
01335 // To create a dynamic mutex, just define an object of type Mutex.
01336 class MutexBase {
01337  public:
01338   // Acquires this mutex.
01339   void Lock() {
01340     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
01341     owner_ = pthread_self();
01342     has_owner_ = true;
01343   }
01344 
01345   // Releases this mutex.
01346   void Unlock() {
01347     // Since the lock is being released the owner_ field should no longer be
01348     // considered valid. We don't protect writing to has_owner_ here, as it's
01349     // the caller's responsibility to ensure that the current thread holds the
01350     // mutex when this is called.
01351     has_owner_ = false;
01352     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
01353   }
01354 
01355   // Does nothing if the current thread holds the mutex. Otherwise, crashes
01356   // with high probability.
01357   void AssertHeld() const {
01358     GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
01359         << "The current thread is not holding the mutex @" << this;
01360   }
01361 
01362   // A static mutex may be used before main() is entered.  It may even
01363   // be used before the dynamic initialization stage.  Therefore we
01364   // must be able to initialize a static mutex object at link time.
01365   // This means MutexBase has to be a POD and its member variables
01366   // have to be public.
01367  public:
01368   pthread_mutex_t mutex_;  // The underlying pthread mutex.
01369   // has_owner_ indicates whether the owner_ field below contains a valid thread
01370   // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
01371   // accesses to the owner_ field should be protected by a check of this field.
01372   // An alternative might be to memset() owner_ to all zeros, but there's no
01373   // guarantee that a zero'd pthread_t is necessarily invalid or even different
01374   // from pthread_self().
01375   bool has_owner_;
01376   pthread_t owner_;  // The thread holding the mutex.
01377 };
01378 
01379 // Forward-declares a static mutex.
01380 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
01381     extern ::testing::internal::MutexBase mutex
01382 
01383 // Defines and statically (i.e. at link time) initializes a static mutex.
01384 // The initialization list here does not explicitly initialize each field,
01385 // instead relying on default initialization for the unspecified fields. In
01386 // particular, the owner_ field (a pthread_t) is not explicitly initialized.
01387 // This allows initialization to work whether pthread_t is a scalar or struct.
01388 // The flag -Wmissing-field-initializers must not be specified for this to work.
01389 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
01390     ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
01391 
01392 // The Mutex class can only be used for mutexes created at runtime. It
01393 // shares its API with MutexBase otherwise.
01394 class Mutex : public MutexBase {
01395  public:
01396   Mutex() {
01397     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
01398     has_owner_ = false;
01399   }
01400   ~Mutex() {
01401     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
01402   }
01403 
01404  private:
01405   GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
01406 };
01407 
01408 // We cannot name this class MutexLock as the ctor declaration would
01409 // conflict with a macro named MutexLock, which is defined on some
01410 // platforms.  Hence the typedef trick below.
01411 class GTestMutexLock {
01412  public:
01413   explicit GTestMutexLock(MutexBase* mutex)
01414       : mutex_(mutex) { mutex_->Lock(); }
01415 
01416   ~GTestMutexLock() { mutex_->Unlock(); }
01417 
01418  private:
01419   MutexBase* const mutex_;
01420 
01421   GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
01422 };
01423 
01424 typedef GTestMutexLock MutexLock;
01425 
01426 // Helpers for ThreadLocal.
01427 
01428 // pthread_key_create() requires DeleteThreadLocalValue() to have
01429 // C-linkage.  Therefore it cannot be templatized to access
01430 // ThreadLocal<T>.  Hence the need for class
01431 // ThreadLocalValueHolderBase.
01432 class ThreadLocalValueHolderBase {
01433  public:
01434   virtual ~ThreadLocalValueHolderBase() {}
01435 };
01436 
01437 // Called by pthread to delete thread-local data stored by
01438 // pthread_setspecific().
01439 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
01440   delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
01441 }
01442 
01443 // Implements thread-local storage on pthreads-based systems.
01444 //
01445 //   // Thread 1
01446 //   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
01447 //
01448 //   // Thread 2
01449 //   tl.set(150);  // Changes the value for thread 2 only.
01450 //   EXPECT_EQ(150, tl.get());
01451 //
01452 //   // Thread 1
01453 //   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
01454 //   tl.set(200);
01455 //   EXPECT_EQ(200, tl.get());
01456 //
01457 // The template type argument T must have a public copy constructor.
01458 // In addition, the default ThreadLocal constructor requires T to have
01459 // a public default constructor.
01460 //
01461 // An object managed for a thread by a ThreadLocal instance is deleted
01462 // when the thread exits.  Or, if the ThreadLocal instance dies in
01463 // that thread, when the ThreadLocal dies.  It's the user's
01464 // responsibility to ensure that all other threads using a ThreadLocal
01465 // have exited when it dies, or the per-thread objects for those
01466 // threads will not be deleted.
01467 //
01468 // Google Test only uses global ThreadLocal objects.  That means they
01469 // will die after main() has returned.  Therefore, no per-thread
01470 // object managed by Google Test will be leaked as long as all threads
01471 // using Google Test have exited when main() returns.
01472 template <typename T>
01473 class ThreadLocal {
01474  public:
01475   ThreadLocal() : key_(CreateKey()),
01476                   default_() {}
01477   explicit ThreadLocal(const T& value) : key_(CreateKey()),
01478                                          default_(value) {}
01479 
01480   ~ThreadLocal() {
01481     // Destroys the managed object for the current thread, if any.
01482     DeleteThreadLocalValue(pthread_getspecific(key_));
01483 
01484     // Releases resources associated with the key.  This will *not*
01485     // delete managed objects for other threads.
01486     GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
01487   }
01488 
01489   T* pointer() { return GetOrCreateValue(); }
01490   const T* pointer() const { return GetOrCreateValue(); }
01491   const T& get() const { return *pointer(); }
01492   void set(const T& value) { *pointer() = value; }
01493 
01494  private:
01495   // Holds a value of type T.
01496   class ValueHolder : public ThreadLocalValueHolderBase {
01497    public:
01498     explicit ValueHolder(const T& value) : value_(value) {}
01499 
01500     T* pointer() { return &value_; }
01501 
01502    private:
01503     T value_;
01504     GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
01505   };
01506 
01507   static pthread_key_t CreateKey() {
01508     pthread_key_t key;
01509     // When a thread exits, DeleteThreadLocalValue() will be called on
01510     // the object managed for that thread.
01511     GTEST_CHECK_POSIX_SUCCESS_(
01512         pthread_key_create(&key, &DeleteThreadLocalValue));
01513     return key;
01514   }
01515 
01516   T* GetOrCreateValue() const {
01517     ThreadLocalValueHolderBase* const holder =
01518         static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
01519     if (holder != NULL) {
01520       return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
01521     }
01522 
01523     ValueHolder* const new_holder = new ValueHolder(default_);
01524     ThreadLocalValueHolderBase* const holder_base = new_holder;
01525     GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
01526     return new_holder->pointer();
01527   }
01528 
01529   // A key pthreads uses for looking up per-thread values.
01530   const pthread_key_t key_;
01531   const T default_;  // The default value for each thread.
01532 
01533   GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
01534 };
01535 
01536 # define GTEST_IS_THREADSAFE 1
01537 
01538 #else  // GTEST_HAS_PTHREAD
01539 
01540 // A dummy implementation of synchronization primitives (mutex, lock,
01541 // and thread-local variable).  Necessary for compiling Google Test where
01542 // mutex is not supported - using Google Test in multiple threads is not
01543 // supported on such platforms.
01544 
01545 class Mutex {
01546  public:
01547   Mutex() {}
01548   void Lock() {}
01549   void Unlock() {}
01550   void AssertHeld() const {}
01551 };
01552 
01553 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
01554   extern ::testing::internal::Mutex mutex
01555 
01556 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
01557 
01558 class GTestMutexLock {
01559  public:
01560   explicit GTestMutexLock(Mutex*) {}  // NOLINT
01561 };
01562 
01563 typedef GTestMutexLock MutexLock;
01564 
01565 template <typename T>
01566 class ThreadLocal {
01567  public:
01568   ThreadLocal() : value_() {}
01569   explicit ThreadLocal(const T& value) : value_(value) {}
01570   T* pointer() { return &value_; }
01571   const T* pointer() const { return &value_; }
01572   const T& get() const { return value_; }
01573   void set(const T& value) { value_ = value; }
01574  private:
01575   T value_;
01576 };
01577 
01578 // The above synchronization primitives have dummy implementations.
01579 // Therefore Google Test is not thread-safe.
01580 # define GTEST_IS_THREADSAFE 0
01581 
01582 #endif  // GTEST_HAS_PTHREAD
01583 
01584 // Returns the number of threads running in the process, or 0 to indicate that
01585 // we cannot detect it.
01586 GTEST_API_ size_t GetThreadCount();
01587 
01588 // Passing non-POD classes through ellipsis (...) crashes the ARM
01589 // compiler and generates a warning in Sun Studio.  The Nokia Symbian
01590 // and the IBM XL C/C++ compiler try to instantiate a copy constructor
01591 // for objects passed through ellipsis (...), failing for uncopyable
01592 // objects.  We define this to ensure that only POD is passed through
01593 // ellipsis on these systems.
01594 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
01595 // We lose support for NULL detection where the compiler doesn't like
01596 // passing non-POD classes through ellipsis (...).
01597 # define GTEST_ELLIPSIS_NEEDS_POD_ 1
01598 #else
01599 # define GTEST_CAN_COMPARE_NULL 1
01600 #endif
01601 
01602 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
01603 // const T& and const T* in a function template.  These compilers
01604 // _can_ decide between class template specializations for T and T*,
01605 // so a tr1::type_traits-like is_pointer works.
01606 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
01607 # define GTEST_NEEDS_IS_POINTER_ 1
01608 #endif
01609 
01610 template <bool bool_value>
01611 struct bool_constant {
01612   typedef bool_constant<bool_value> type;
01613   static const bool value = bool_value;
01614 };
01615 template <bool bool_value> const bool bool_constant<bool_value>::value;
01616 
01617 typedef bool_constant<false> false_type;
01618 typedef bool_constant<true> true_type;
01619 
01620 template <typename T>
01621 struct is_pointer : public false_type {};
01622 
01623 template <typename T>
01624 struct is_pointer<T*> : public true_type {};
01625 
01626 template <typename Iterator>
01627 struct IteratorTraits {
01628   typedef typename Iterator::value_type value_type;
01629 };
01630 
01631 template <typename T>
01632 struct IteratorTraits<T*> {
01633   typedef T value_type;
01634 };
01635 
01636 template <typename T>
01637 struct IteratorTraits<const T*> {
01638   typedef T value_type;
01639 };
01640 
01641 #if GTEST_OS_WINDOWS
01642 # define GTEST_PATH_SEP_ "\\"
01643 # define GTEST_HAS_ALT_PATH_SEP_ 1
01644 // The biggest signed integer type the compiler supports.
01645 typedef __int64 BiggestInt;
01646 #else
01647 # define GTEST_PATH_SEP_ "/"
01648 # define GTEST_HAS_ALT_PATH_SEP_ 0
01649 typedef long long BiggestInt;  // NOLINT
01650 #endif  // GTEST_OS_WINDOWS
01651 
01652 // Utilities for char.
01653 
01654 // isspace(int ch) and friends accept an unsigned char or EOF.  char
01655 // may be signed, depending on the compiler (or compiler flags).
01656 // Therefore we need to cast a char to unsigned char before calling
01657 // isspace(), etc.
01658 
01659 inline bool IsAlpha(char ch) {
01660   return isalpha(static_cast<unsigned char>(ch)) != 0;
01661 }
01662 inline bool IsAlNum(char ch) {
01663   return isalnum(static_cast<unsigned char>(ch)) != 0;
01664 }
01665 inline bool IsDigit(char ch) {
01666   return isdigit(static_cast<unsigned char>(ch)) != 0;
01667 }
01668 inline bool IsLower(char ch) {
01669   return islower(static_cast<unsigned char>(ch)) != 0;
01670 }
01671 inline bool IsSpace(char ch) {
01672   return isspace(static_cast<unsigned char>(ch)) != 0;
01673 }
01674 inline bool IsUpper(char ch) {
01675   return isupper(static_cast<unsigned char>(ch)) != 0;
01676 }
01677 inline bool IsXDigit(char ch) {
01678   return isxdigit(static_cast<unsigned char>(ch)) != 0;
01679 }
01680 inline bool IsXDigit(wchar_t ch) {
01681   const unsigned char low_byte = static_cast<unsigned char>(ch);
01682   return ch == low_byte && isxdigit(low_byte) != 0;
01683 }
01684 
01685 inline char ToLower(char ch) {
01686   return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
01687 }
01688 inline char ToUpper(char ch) {
01689   return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
01690 }
01691 
01692 // The testing::internal::posix namespace holds wrappers for common
01693 // POSIX functions.  These wrappers hide the differences between
01694 // Windows/MSVC and POSIX systems.  Since some compilers define these
01695 // standard functions as macros, the wrapper cannot have the same name
01696 // as the wrapped function.
01697 
01698 namespace posix {
01699 
01700 // Functions with a different name on Windows.
01701 
01702 #if GTEST_OS_WINDOWS
01703 
01704 typedef struct _stat StatStruct;
01705 
01706 # ifdef __BORLANDC__
01707 inline int IsATTY(int fd) { return isatty(fd); }
01708 inline int StrCaseCmp(const char* s1, const char* s2) {
01709   return stricmp(s1, s2);
01710 }
01711 inline char* StrDup(const char* src) { return strdup(src); }
01712 # else  // !__BORLANDC__
01713 #  if GTEST_OS_WINDOWS_MOBILE
01714 inline int IsATTY(int /* fd */) { return 0; }
01715 #  else
01716 inline int IsATTY(int fd) { return _isatty(fd); }
01717 #  endif  // GTEST_OS_WINDOWS_MOBILE
01718 inline int StrCaseCmp(const char* s1, const char* s2) {
01719   return _stricmp(s1, s2);
01720 }
01721 inline char* StrDup(const char* src) { return _strdup(src); }
01722 # endif  // __BORLANDC__
01723 
01724 # if GTEST_OS_WINDOWS_MOBILE
01725 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
01726 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
01727 // time and thus not defined there.
01728 # else
01729 inline int FileNo(FILE* file) { return _fileno(file); }
01730 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
01731 inline int RmDir(const char* dir) { return _rmdir(dir); }
01732 inline bool IsDir(const StatStruct& st) {
01733   return (_S_IFDIR & st.st_mode) != 0;
01734 }
01735 # endif  // GTEST_OS_WINDOWS_MOBILE
01736 
01737 #else
01738 
01739 typedef struct stat StatStruct;
01740 
01741 inline int FileNo(FILE* file) { return fileno(file); }
01742 inline int IsATTY(int fd) { return isatty(fd); }
01743 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
01744 inline int StrCaseCmp(const char* s1, const char* s2) {
01745   return strcasecmp(s1, s2);
01746 }
01747 inline char* StrDup(const char* src) { return strdup(src); }
01748 inline int RmDir(const char* dir) { return rmdir(dir); }
01749 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
01750 
01751 #endif  // GTEST_OS_WINDOWS
01752 
01753 // Functions deprecated by MSVC 8.0.
01754 
01755 #ifdef _MSC_VER
01756 // Temporarily disable warning 4996 (deprecated function).
01757 # pragma warning(push)
01758 # pragma warning(disable:4996)
01759 #endif
01760 
01761 inline const char* StrNCpy(char* dest, const char* src, size_t n) {
01762   return strncpy(dest, src, n);
01763 }
01764 
01765 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
01766 // StrError() aren't needed on Windows CE at this time and thus not
01767 // defined there.
01768 
01769 #if !GTEST_OS_WINDOWS_MOBILE
01770 inline int ChDir(const char* dir) { return chdir(dir); }
01771 #endif
01772 inline FILE* FOpen(const char* path, const char* mode) {
01773   return fopen(path, mode);
01774 }
01775 #if !GTEST_OS_WINDOWS_MOBILE
01776 inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
01777   return freopen(path, mode, stream);
01778 }
01779 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
01780 #endif
01781 inline int FClose(FILE* fp) { return fclose(fp); }
01782 #if !GTEST_OS_WINDOWS_MOBILE
01783 inline int Read(int fd, void* buf, unsigned int count) {
01784   return static_cast<int>(read(fd, buf, count));
01785 }
01786 inline int Write(int fd, const void* buf, unsigned int count) {
01787   return static_cast<int>(write(fd, buf, count));
01788 }
01789 inline int Close(int fd) { return close(fd); }
01790 inline const char* StrError(int errnum) { return strerror(errnum); }
01791 #endif
01792 inline const char* GetEnv(const char* name) {
01793 #if GTEST_OS_WINDOWS_MOBILE
01794   // We are on Windows CE, which has no environment variables.
01795   return NULL;
01796 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
01797   // Environment variables which we programmatically clear will be set to the
01798   // empty string rather than unset (NULL).  Handle that case.
01799   const char* const env = getenv(name);
01800   return (env != NULL && env[0] != '\0') ? env : NULL;
01801 #else
01802   return getenv(name);
01803 #endif
01804 }
01805 
01806 #ifdef _MSC_VER
01807 # pragma warning(pop)  // Restores the warning state.
01808 #endif
01809 
01810 #if GTEST_OS_WINDOWS_MOBILE
01811 // Windows CE has no C library. The abort() function is used in
01812 // several places in Google Test. This implementation provides a reasonable
01813 // imitation of standard behaviour.
01814 void Abort();
01815 #else
01816 inline void Abort() { abort(); }
01817 #endif  // GTEST_OS_WINDOWS_MOBILE
01818 
01819 }  // namespace posix
01820 
01821 // MSVC "deprecates" snprintf and issues warnings wherever it is used.  In
01822 // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
01823 // MSVC-based platforms.  We map the GTEST_SNPRINTF_ macro to the appropriate
01824 // function in order to achieve that.  We use macro definition here because
01825 // snprintf is a variadic function.
01826 #if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
01827 // MSVC 2005 and above support variadic macros.
01828 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
01829      _snprintf_s(buffer, size, size, format, __VA_ARGS__)
01830 #elif defined(_MSC_VER)
01831 // Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't
01832 // complain about _snprintf.
01833 # define GTEST_SNPRINTF_ _snprintf
01834 #else
01835 # define GTEST_SNPRINTF_ snprintf
01836 #endif
01837 
01838 // The maximum number a BiggestInt can represent.  This definition
01839 // works no matter BiggestInt is represented in one's complement or
01840 // two's complement.
01841 //
01842 // We cannot rely on numeric_limits in STL, as __int64 and long long
01843 // are not part of standard C++ and numeric_limits doesn't need to be
01844 // defined for them.
01845 const BiggestInt kMaxBiggestInt =
01846     ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
01847 
01848 // This template class serves as a compile-time function from size to
01849 // type.  It maps a size in bytes to a primitive type with that
01850 // size. e.g.
01851 //
01852 //   TypeWithSize<4>::UInt
01853 //
01854 // is typedef-ed to be unsigned int (unsigned integer made up of 4
01855 // bytes).
01856 //
01857 // Such functionality should belong to STL, but I cannot find it
01858 // there.
01859 //
01860 // Google Test uses this class in the implementation of floating-point
01861 // comparison.
01862 //
01863 // For now it only handles UInt (unsigned int) as that's all Google Test
01864 // needs.  Other types can be easily added in the future if need
01865 // arises.
01866 template <size_t size>
01867 class TypeWithSize {
01868  public:
01869   // This prevents the user from using TypeWithSize<N> with incorrect
01870   // values of N.
01871   typedef void UInt;
01872 };
01873 
01874 // The specialization for size 4.
01875 template <>
01876 class TypeWithSize<4> {
01877  public:
01878   // unsigned int has size 4 in both gcc and MSVC.
01879   //
01880   // As base/basictypes.h doesn't compile on Windows, we cannot use
01881   // uint32, uint64, and etc here.
01882   typedef int Int;
01883   typedef unsigned int UInt;
01884 };
01885 
01886 // The specialization for size 8.
01887 template <>
01888 class TypeWithSize<8> {
01889  public:
01890 #if GTEST_OS_WINDOWS
01891   typedef __int64 Int;
01892   typedef unsigned __int64 UInt;
01893 #else
01894   typedef long long Int;  // NOLINT
01895   typedef unsigned long long UInt;  // NOLINT
01896 #endif  // GTEST_OS_WINDOWS
01897 };
01898 
01899 // Integer types of known sizes.
01900 typedef TypeWithSize<4>::Int Int32;
01901 typedef TypeWithSize<4>::UInt UInt32;
01902 typedef TypeWithSize<8>::Int Int64;
01903 typedef TypeWithSize<8>::UInt UInt64;
01904 typedef TypeWithSize<8>::Int TimeInMillis;  // Represents time in milliseconds.
01905 
01906 // Utilities for command line flags and environment variables.
01907 
01908 // Macro for referencing flags.
01909 #define GTEST_FLAG(name) FLAGS_gtest_##name
01910 
01911 // Macros for declaring flags.
01912 #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
01913 #define GTEST_DECLARE_int32_(name) \
01914     GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
01915 #define GTEST_DECLARE_string_(name) \
01916     GTEST_API_ extern ::std::string GTEST_FLAG(name)
01917 
01918 // Macros for defining flags.
01919 #define GTEST_DEFINE_bool_(name, default_val, doc) \
01920     GTEST_API_ bool GTEST_FLAG(name) = (default_val)
01921 #define GTEST_DEFINE_int32_(name, default_val, doc) \
01922     GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
01923 #define GTEST_DEFINE_string_(name, default_val, doc) \
01924     GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
01925 
01926 // Thread annotations
01927 #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
01928 #define GTEST_LOCK_EXCLUDED_(locks)
01929 
01930 // Parses 'str' for a 32-bit signed integer.  If successful, writes the result
01931 // to *value and returns true; otherwise leaves *value unchanged and returns
01932 // false.
01933 // TODO(chandlerc): Find a better way to refactor flag and environment parsing
01934 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
01935 // function.
01936 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
01937 
01938 // Parses a bool/Int32/string from the environment variable
01939 // corresponding to the given Google Test flag.
01940 bool BoolFromGTestEnv(const char* flag, bool default_val);
01941 GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
01942 const char* StringFromGTestEnv(const char* flag, const char* default_val);
01943 
01944 }  // namespace internal
01945 }  // namespace testing
01946 
01947 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_