12228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Copyright 2005, Google Inc.
22228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// All rights reserved.
32228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
42228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Redistribution and use in source and binary forms, with or without
54f0d97057c5c640b25518358886f8c47da9fc052Jean-Michel Trivi// modification, are permitted provided that the following conditions are
62228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// met:
72228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
82228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     * Redistributions of source code must retain the above copyright
92228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// notice, this list of conditions and the following disclaimer.
102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     * Redistributions in binary form must reproduce the above
112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// copyright notice, this list of conditions and the following disclaimer
122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// in the documentation and/or other materials provided with the
132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// distribution.
142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     * Neither the name of Google Inc. nor the names of its
152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// contributors may be used to endorse or promote products derived from
162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// this software without specific prior written permission.
172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Authors: wan@google.com (Zhanyong Wan)
312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Low-level types and utilities for porting Google Test to various
332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// platforms.  They are subject to change without notice.  DO NOT USE
342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// THEM IN USER CODE.
352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This file is fundamental to Google Test.  All other Google Test source
372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// files are expected to #include this.  Therefore, it cannot #include
382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// any other Google Test header.
392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user can define the following macros in the build script to
442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// control Google Test's behavior.  If the user doesn't define a macro
452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// in this list, Google Test will define it.
462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is/isn't available.
492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              are enabled.
512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_GLOBAL_STRING  - Define it to 1/0 to indicate that ::string
522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is/isn't available (some systems define
532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              ::string, which is different to std::string).
542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is/isn't available (some systems define
562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              ::wstring, which is different to std::wstring).
572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              expressions are/aren't available.
592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is/isn't available.
612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              enabled.
632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              std::wstring does/doesn't work (Google Test can
652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              be used where std::wstring is unavailable).
662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_TR1_TUPLE      - Define it to 1/0 to indicate tr1::tuple
672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is/isn't available.
682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              compiler supports Microsoft's "Structured
702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              Exception Handling".
712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_STREAM_REDIRECTION
722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            - Define it to 1/0 to indicate whether the
732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              platform supports I/O stream redirection using
742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              dup() and dup2().
752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_USE_OWN_TR1_TUPLE  - Define it to 1/0 to indicate whether Google
762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              Test's own tr1 tuple implementation should be
772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              used.  Unused when the user sets
782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              GTEST_HAS_TR1_TUPLE to 0.
792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_LANG_CXX11         - Define it to 1/0 to indicate that Google Test
802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              is building in C++11/C++98 mode.
812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_LINKED_AS_SHARED_LIBRARY
822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            - Define to 1 when compiling tests that use
832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              Google Test as a shared library (known as
842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              DLL on Windows).
852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_CREATE_SHARED_LIBRARY
862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            - Define to 1 when compiling Google Test itself
872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              as a shared library.
882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This header defines the following utilities:
902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Macros indicating the current platform (defined to 1 if compiled on
922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// the given platform; otherwise undefined):
932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_AIX      - IBM AIX
942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_CYGWIN   - Cygwin
952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_HPUX     - HP-UX
962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_LINUX    - Linux
972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     GTEST_OS_LINUX_ANDROID - Google Android
982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_MAC      - Mac OS X
992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     GTEST_OS_IOS    - iOS
1002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//       GTEST_OS_IOS_SIMULATOR - iOS simulator
1012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_NACL     - Google Native Client (NaCl)
1022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_OPENBSD  - OpenBSD
1032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_QNX      - QNX
1042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_SOLARIS  - Sun Solaris
1052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_SYMBIAN  - Symbian
1062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
1072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
1082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     GTEST_OS_WINDOWS_MINGW    - MinGW
1092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
1102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_OS_ZOS      - z/OS
1112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
1132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// most stable support.  Since core members of the Google Test project
1142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// don't have access to other platforms, support for them may be less
1152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// stable.  If you notice any problems on your platform, please notify
1162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// googletestframework@googlegroups.com (patches for fixing them are
1172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// even more welcome!).
1182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Note that it is possible that none of the GTEST_OS_* macros are defined.
1202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Macros indicating available Google Test features (defined to 1 if
1222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// the corresponding feature is supported; otherwise undefined):
1232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_COMBINE      - the Combine() function (for value-parameterized
1242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            tests)
1252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_DEATH_TEST   - death tests
1262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_PARAM_TEST   - value-parameterized tests
1272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_TYPED_TEST   - typed tests
1282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
1292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
1302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            GTEST_HAS_POSIX_RE (see above) which users can
1312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            define themselves.
1322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
1332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                            the above two are mutually exclusive.
1342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
1352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Macros for basic C++ coding:
1372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
1382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a
1392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                              variable don't have to be used.
1402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_DISALLOW_ASSIGN_   - disables operator=.
1412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
1422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.
1432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Synchronization:
1452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   Mutex, MutexLock, ThreadLocal, GetThreadCount()
1462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                  - synchronization primitives.
1472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
1482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                         synchronization primitives have real implementations
1492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                         and Google Test is thread-safe; or 0 otherwise.
1502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Template meta programming:
1522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   is_pointer     - as in TR1; needed on Symbian and IBM XL C/C++ only.
1532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   IteratorTraits - partial implementation of std::iterator_traits, which
1542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                    is not available in libCstd when compiled with Sun C++.
1552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Smart pointers:
1572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   scoped_ptr     - as in TR2.
1582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Regular expressions:
1602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   RE             - a simple regular expression class using the POSIX
1612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                    Extended Regular Expression syntax on UNIX-like
1622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                    platforms, or a reduced regular exception syntax on
1632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                    other platforms, including Windows.
1642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Logging:
1662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_LOG_()   - logs messages at the specified severity level.
1672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   LogToStderr()  - directs all log messages to stderr.
1682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   FlushInfoLog() - flushes informational log messages.
1692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Stdout and stderr capturing:
1712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   CaptureStdout()     - starts capturing stdout.
1722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GetCapturedStdout() - stops capturing stdout and returns the captured
1732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                         string.
1742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   CaptureStderr()     - starts capturing stderr.
1752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GetCapturedStderr() - stops capturing stderr and returns the captured
1762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                         string.
1772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Integer types:
1792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   TypeWithSize   - maps an integer to a int type.
1802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   Int32, UInt32, Int64, UInt64, TimeInMillis
1812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//                  - integers of known sizes.
1822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   BiggestInt     - the biggest signed integer type.
1832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Command-line utilities:
1852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_FLAG()       - references a flag.
1862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_DECLARE_*()  - declares a flag.
1872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GTEST_DEFINE_*()   - defines a flag.
1882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GetInjectableArgvs() - returns the command line as a vector of strings.
1892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
1902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Environment variable utilities:
1912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   GetEnv()             - gets the value of an environment variable.
1922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   BoolFromGTestEnv()   - parses a bool environment variable.
1932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   Int32FromGTestEnv()  - parses an Int32 environment variable.
1942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   StringFromGTestEnv() - parses a string environment variable.
1952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
1962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <ctype.h>   // for isspace, etc
1972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <stddef.h>  // for ptrdiff_t
1982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <stdlib.h>
1992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <stdio.h>
2002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <string.h>
2012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef _WIN32_WCE
2022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <sys/types.h>
2032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <sys/stat.h>
2042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // !_WIN32_WCE
2052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if defined __APPLE__
2072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <AvailabilityMacros.h>
2082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <TargetConditionals.h>
2092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
2102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <iostream>  // NOLINT
2122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <sstream>  // NOLINT
2132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#include <string>  // NOLINT
2142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
2162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_FLAG_PREFIX_ "gtest_"
2172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_FLAG_PREFIX_DASH_ "gtest-"
2182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
2192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_NAME_ "Google Test"
2202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
2212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines the version of gcc that is used to compile this.
2232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifdef __GNUC__
2242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// 40302 means version 4.3.2.
2252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_GCC_VER_ \
2262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
2272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // __GNUC__
2282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines the platform on which Google Test is compiled.
2302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifdef __CYGWIN__
2312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_CYGWIN 1
2322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __SYMBIAN32__
2332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_SYMBIAN 1
2342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined _WIN32
2352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_WINDOWS 1
2362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# ifdef _WIN32_WCE
2372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_OS_WINDOWS_MOBILE 1
2382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__MINGW__) || defined(__MINGW32__)
2392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_OS_WINDOWS_MINGW 1
2402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
2412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_OS_WINDOWS_DESKTOP 1
2422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // _WIN32_WCE
2432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __APPLE__
2442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_MAC 1
2452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if TARGET_OS_IPHONE
2462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_OS_IOS 1
2472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  if TARGET_IPHONE_SIMULATOR
2482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_OS_IOS_SIMULATOR 1
2492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif
2502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
2512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __linux__
2522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_LINUX 1
2532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if defined __ANDROID__
2542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_OS_LINUX_ANDROID 1
2552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
2562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __MVS__
2572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_ZOS 1
2582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined(__sun) && defined(__SVR4)
2592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_SOLARIS 1
2602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined(_AIX)
2612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_AIX 1
2622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined(__hpux)
2632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_HPUX 1
2642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __native_client__
2652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_NACL 1
2662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __OpenBSD__
2672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_OPENBSD 1
2682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif defined __QNX__
2692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_OS_QNX 1
2702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // __CYGWIN__
2712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_LANG_CXX11
2732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// gcc and clang define __GXX_EXPERIMENTAL_CXX0X__ when
2742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// -std={c,gnu}++{0x,11} is passed.  The C++11 standard specifies a
2752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// value for __cplusplus, and recent versions of clang, gcc, and
2762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// probably other compilers set that too in C++11 mode.
2772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L
2782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Compiling in at least C++11 mode.
2792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_LANG_CXX11 1
2802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
2812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_LANG_CXX11 0
2822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
2832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
2842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
2852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Brings in definitions for functions used in the testing::internal::posix
2862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// namespace (read, write, close, chdir, isatty, stat). We do not currently
2872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// use them on Windows Mobile.
2882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_OS_WINDOWS
2892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if !GTEST_OS_WINDOWS_MOBILE
2902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <direct.h>
2912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <io.h>
2922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
2932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// In order to avoid having to include <windows.h>, use forward declaration
2942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// assuming CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
2952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This assumption is verified by
2962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
2972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectstruct _RTL_CRITICAL_SECTION;
2982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#else
2992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This assumes that non-Windows OSes provide unistd.h. For OSes where this
3002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// is not the case, we need to include headers that provide the functions
3012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// mentioned above.
3022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <unistd.h>
3032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <strings.h>
3042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_OS_WINDOWS
3052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_OS_LINUX_ANDROID
3072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Used to define __ANDROID_API__ matching the target NDK API level.
3082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <android/api-level.h>  // NOLINT
3092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
3102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Defines this to true iff Google Test can use POSIX regular expressions.
3122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_POSIX_RE
3132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_OS_LINUX_ANDROID
3142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// On Android, <regex.h> is only available starting with Gingerbread.
3152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
3162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
3172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
3182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
3192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
3202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_HAS_POSIX_RE
3222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// On some platforms, <regex.h> needs someone to define size_t, and
3242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// won't compile otherwise.  We can #include it here as we already
3252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// included <stdlib.h>, which is guaranteed to define size_t through
3262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// <stddef.h>.
3272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <regex.h>  // NOLINT
3282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_USES_POSIX_RE 1
3302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif GTEST_OS_WINDOWS
3322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// <regex.h> is not available on Windows.  Use our own simple regex
3342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// implementation instead.
3352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_USES_SIMPLE_RE 1
3362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#else
3382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// <regex.h> may not be available on this platform.  Use our own
3402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// simple regex implementation instead.
3412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_USES_SIMPLE_RE 1
3422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_POSIX_RE
3442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_EXCEPTIONS
3462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us whether exceptions are enabled, so we need
3472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// to figure it out.
3482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if defined(_MSC_VER) || defined(__BORLANDC__)
3492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
3502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// macro to enable exceptions, so we'll do the same.
3512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Assumes that exceptions are enabled by default.
3522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  ifndef _HAS_EXCEPTIONS
3532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define _HAS_EXCEPTIONS 1
3542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif  // _HAS_EXCEPTIONS
3552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
3562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__GNUC__) && __EXCEPTIONS
3572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
3582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS 1
3592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__SUNPRO_CC)
3602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Sun Pro CC supports exceptions.  However, there is no compile-time way of
3612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// detecting whether they are enabled or not.  Therefore, we assume that
3622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// they are enabled unless the user tells us otherwise.
3632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS 1
3642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__IBMCPP__) && __EXCEPTIONS
3652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
3662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS 1
3672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__HP_aCC)
3682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Exception handling is in effect by default in HP aCC compiler. It has to
3692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// be turned of by +noeh compiler option if desired.
3702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS 1
3712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
3722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// For other compilers, we assume exceptions are disabled to be
3732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// conservative.
3742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_EXCEPTIONS 0
3752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // defined(_MSC_VER) || defined(__BORLANDC__)
3762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_EXCEPTIONS
3772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if !defined(GTEST_HAS_STD_STRING)
3792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Even though we don't use this macro any longer, we keep it in case
3802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// some clients still depend on it.
3812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_HAS_STD_STRING 1
3822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#elif !GTEST_HAS_STD_STRING
3832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user told us that ::std::string isn't available.
3842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# error "Google Test cannot be used where ::std::string isn't available."
3852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // !defined(GTEST_HAS_STD_STRING)
3862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_GLOBAL_STRING
3882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us whether ::string is available, so we need
3892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// to figure it out.
3902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_HAS_GLOBAL_STRING 0
3922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_GLOBAL_STRING
3942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
3952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_STD_WSTRING
3962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us whether ::std::wstring is available, so we need
3972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// to figure it out.
3982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
3992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//   is available.
4002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Cygwin 1.7 and below doesn't support ::std::wstring.
4022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Solaris' libc++ doesn't support it either.  Android has
4032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// no support for it at least as recent as Froyo (2.2).
4042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_HAS_STD_WSTRING \
4052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
4062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_STD_WSTRING
4082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_GLOBAL_WSTRING
4102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us whether ::wstring is available, so we need
4112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// to figure it out.
4122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_HAS_GLOBAL_WSTRING \
4132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
4142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_GLOBAL_WSTRING
4152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether RTTI is available.
4172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_RTTI
4182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us whether RTTI is enabled, so we need to
4192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// figure it out.
4202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# ifdef _MSC_VER
4222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  ifdef _CPPRTTI  // MSVC defines this macro iff RTTI is enabled.
4242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_RTTI 1
4252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  else
4262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_RTTI 0
4272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif
4282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
4302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
4312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  ifdef __GXX_RTTI
4332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// When building against STLport with the Android NDK and with
4342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// -frtti -fno-exceptions, the build fails at link time with undefined
4352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
4362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// so disable RTTI when detected.
4372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
4382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project       !defined(__EXCEPTIONS)
4392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#    define GTEST_HAS_RTTI 0
4402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   else
4412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#    define GTEST_HAS_RTTI 1
4422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   endif  // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
4432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  else
4442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_RTTI 0
4452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif  // __GXX_RTTI
4462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
4482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
4492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// first version with C++ support.
4502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__clang__)
4512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_RTTI __has_feature(cxx_rtti)
4532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
4552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// both the typeid and dynamic_cast features are present.
4562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
4572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  ifdef __RTTI_ALL__
4592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_RTTI 1
4602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  else
4612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_RTTI 0
4622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif
4632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
4652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// For all other compilers, we assume RTTI is enabled.
4672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_RTTI 1
4682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // _MSC_VER
4702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_RTTI
4722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// It's this header's responsibility to #include <typeinfo> when RTTI
4742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// is enabled.
4752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_HAS_RTTI
4762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <typeinfo>
4772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
4782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether Google Test can use the pthreads library.
4802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_PTHREAD
4812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us explicitly, so we assume pthreads support is
4822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// available on Linux and Mac.
4832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project//
4842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
4852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// to your compiler flags.
4862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \
4872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project    || GTEST_OS_QNX)
4882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_PTHREAD
4892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_HAS_PTHREAD
4912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
4922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// true.
4932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <pthread.h>  // NOLINT
4942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// For timespec and nanosleep, used below.
4962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# include <time.h>  // NOLINT
4972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif
4982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
4992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether Google Test can use tr1/tuple.  You can define
5002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// this macro to 0 to prevent Google Test from using tuple (any
5012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// feature depending on tuple with be disabled in this mode).
5022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_TR1_TUPLE
5032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
5042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// STLport, provided with the Android NDK, has neither <tr1/tuple> or <tuple>.
5052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_TR1_TUPLE 0
5062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
5072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us not to do it, so we assume it's OK.
5082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_TR1_TUPLE 1
5092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
5102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_TR1_TUPLE
5112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether Google Test's own tr1 tuple implementation
5132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// should be used.
5142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_USE_OWN_TR1_TUPLE
5152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us, so we need to figure it out.
5162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// We use our own TR1 tuple if we aren't sure the user has an
5182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// implementation of it already.  At this time, libstdc++ 4.0.0+ and
5192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// MSVC 2010 are the only mainstream standard libraries that come
5202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// with a TR1 tuple implementation.  NVIDIA's CUDA NVCC compiler
5212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// pretends to be GCC by defining __GNUC__ and friends, but cannot
5222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// compile GCC's tuple implementation.  MSVC 2008 (9.0) provides TR1
5232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// tuple in a 323 MB Feature Pack download, which we cannot assume the
5242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// user has.  QNX's QCC compiler is a modified GCC but it doesn't
5252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// support TR1 tuple.  libc++ only provides std::tuple, in C++11 mode,
5262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// and it can be used with some compilers that define __GNUC__.
5272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
5282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project      && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600
5292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_ENV_HAS_TR1_TUPLE_ 1
5302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
5312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// C++11 specifies that <tuple> provides std::tuple. Use that if gtest is used
5332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// in C++11 mode and libstdc++ isn't very old (binaries targeting OS X 10.6
5342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// can build with clang but need to use gcc4.2's libstdc++).
5352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
5362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_ENV_HAS_STD_TUPLE_ 1
5372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
5382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
5402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_USE_OWN_TR1_TUPLE 0
5412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
5422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_USE_OWN_TR1_TUPLE 1
5432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif
5442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_USE_OWN_TR1_TUPLE
5462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// To avoid conditional compilation everywhere, we make it
5482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// gtest-port.h's responsibility to #include the header implementing
5492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// tr1/tuple.
5502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if GTEST_HAS_TR1_TUPLE
5512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_USE_OWN_TR1_TUPLE
5532228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include "gtest/internal/gtest-tuple.h"
5542228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif GTEST_ENV_HAS_STD_TUPLE_
5552228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <tuple>
5562228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// C++11 puts its tuple into the ::std namespace rather than
5572228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// ::std::tr1.  gtest expects tuple to live in ::std::tr1, so put it there.
5582228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This causes undefined behavior, but supported compilers react in
5592228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// the way we intend.
5602228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectnamespace std {
5612228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectnamespace tr1 {
5622228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectusing ::std::get;
5632228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectusing ::std::make_tuple;
5642228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectusing ::std::tuple;
5652228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectusing ::std::tuple_element;
5662228e360595641dd906bf1773307f43d304f5b2The Android Open Source Projectusing ::std::tuple_size;
5672228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
5682228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project}
5692228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5702228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif GTEST_OS_SYMBIAN
5712228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5722228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
5732228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// use STLport's tuple implementation, which unfortunately doesn't
5742228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// work as the copy of STLport distributed with Symbian is incomplete.
5752228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
5762228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// use its own tuple implementation.
5772228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  ifdef BOOST_HAS_TR1_TUPLE
5782228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   undef BOOST_HAS_TR1_TUPLE
5792228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif  // BOOST_HAS_TR1_TUPLE
5802228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5812228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// This prevents <boost/tr1/detail/config.hpp>, which defines
5822228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
5832228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
5842228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <tuple>
5852228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5862228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
5872228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header.  This does
5882228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// not conform to the TR1 spec, which requires the header to be <tuple>.
5892228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
5902228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
5912228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
5922228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// which is #included by <tr1/tuple>, to not compile when RTTI is
5932228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// disabled.  _TR1_FUNCTIONAL is the header guard for
5942228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// <tr1/functional>.  Hence the following #define is a hack to prevent
5952228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// <tr1/functional> from being included.
5962228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define _TR1_FUNCTIONAL 1
5972228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   include <tr1/tuple>
5982228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   undef _TR1_FUNCTIONAL  // Allows the user to #include
5992228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project                        // <tr1/functional> if he chooses to.
6002228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  else
6012228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   include <tr1/tuple>  // NOLINT
6022228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif  // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
6032228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6042228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
6052228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// If the compiler is not GCC 4.0+, we assume the user is using a
6062228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// spec-conforming TR1 implementation.
6072228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  include <tuple>  // NOLINT
6082228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // GTEST_USE_OWN_TR1_TUPLE
6092228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6102228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_TR1_TUPLE
6112228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6122228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether clone(2) is supported.
6132228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Usually it will only be available on Linux, excluding
6142228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Linux on the Itanium architecture.
6152228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Also see http://linux.die.net/man/2/clone.
6162228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_CLONE
6172228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// The user didn't tell us, so we need to figure it out.
6182228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6192228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_OS_LINUX && !defined(__ia64__)
6202228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  if GTEST_OS_LINUX_ANDROID
6212228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// On Android, clone() is only available on ARM starting with Gingerbread.
6222228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#    if defined(__arm__) && __ANDROID_API__ >= 9
6232228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#     define GTEST_HAS_CLONE 1
6242228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#    else
6252228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#     define GTEST_HAS_CLONE 0
6262228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#    endif
6272228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  else
6282228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#   define GTEST_HAS_CLONE 1
6292228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  endif
6302228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
6312228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_CLONE 0
6322228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // GTEST_OS_LINUX && !defined(__ia64__)
6332228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6342228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_CLONE
6352228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6362228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether to support stream redirection. This is used to test
6372228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// output correctness and to implement death tests.
6382228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#ifndef GTEST_HAS_STREAM_REDIRECTION
6392228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// By default, we assume that stream redirection is supported on all
6402228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// platforms except known mobile ones.
6412228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN
6422228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_STREAM_REDIRECTION 0
6432228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# else
6442228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#  define GTEST_HAS_STREAM_REDIRECTION 1
6452228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project# endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
6462228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#endif  // GTEST_HAS_STREAM_REDIRECTION
6472228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project
6482228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Determines whether to support death tests.
6492228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// Google Test does not support death tests for VC 7.1 and earlier as
6502228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// abort() in a VC 7.1 application compiled as GUI in debug config
6512228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project// pops up a dialog window that cannot be suppressed programmatically.
6522228e360595641dd906bf1773307f43d304f5b2The Android Open Source Project#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
653     (GTEST_OS_MAC && !GTEST_OS_IOS) || GTEST_OS_IOS_SIMULATOR || \
654     (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
655     GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
656     GTEST_OS_OPENBSD || GTEST_OS_QNX)
657# define GTEST_HAS_DEATH_TEST 1
658# include <vector>  // NOLINT
659#endif
660
661// We don't support MSVC 7.1 with exceptions disabled now.  Therefore
662// all the compilers we care about are adequate for supporting
663// value-parameterized tests.
664#define GTEST_HAS_PARAM_TEST 1
665
666// Determines whether to support type-driven tests.
667
668// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
669// Sun Pro CC, IBM Visual Age, and HP aCC support.
670#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
671    defined(__IBMCPP__) || defined(__HP_aCC)
672# define GTEST_HAS_TYPED_TEST 1
673# define GTEST_HAS_TYPED_TEST_P 1
674#endif
675
676// Determines whether to support Combine(). This only makes sense when
677// value-parameterized tests are enabled.  The implementation doesn't
678// work on Sun Studio since it doesn't understand templated conversion
679// operators.
680#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
681# define GTEST_HAS_COMBINE 1
682#endif
683
684// Determines whether the system compiler uses UTF-16 for encoding wide strings.
685#define GTEST_WIDE_STRING_USES_UTF16_ \
686    (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
687
688// Determines whether test results can be streamed to a socket.
689#if GTEST_OS_LINUX
690# define GTEST_CAN_STREAM_RESULTS_ 1
691#endif
692
693// Defines some utility macros.
694
695// The GNU compiler emits a warning if nested "if" statements are followed by
696// an "else" statement and braces are not used to explicitly disambiguate the
697// "else" binding.  This leads to problems with code like:
698//
699//   if (gate)
700//     ASSERT_*(condition) << "Some message";
701//
702// The "switch (0) case 0:" idiom is used to suppress this.
703#ifdef __INTEL_COMPILER
704# define GTEST_AMBIGUOUS_ELSE_BLOCKER_
705#else
706# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:  // NOLINT
707#endif
708
709// Use this annotation at the end of a struct/class definition to
710// prevent the compiler from optimizing away instances that are never
711// used.  This is useful when all interesting logic happens inside the
712// c'tor and / or d'tor.  Example:
713//
714//   struct Foo {
715//     Foo() { ... }
716//   } GTEST_ATTRIBUTE_UNUSED_;
717//
718// Also use it after a variable or parameter declaration to tell the
719// compiler the variable/parameter does not have to be used.
720#if defined(__GNUC__) && !defined(COMPILER_ICC)
721# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
722#else
723# define GTEST_ATTRIBUTE_UNUSED_
724#endif
725
726// A macro to disallow operator=
727// This should be used in the private: declarations for a class.
728#define GTEST_DISALLOW_ASSIGN_(type)\
729  void operator=(type const &)
730
731// A macro to disallow copy constructor and operator=
732// This should be used in the private: declarations for a class.
733#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
734  type(type const &);\
735  GTEST_DISALLOW_ASSIGN_(type)
736
737// Tell the compiler to warn about unused return values for functions declared
738// with this macro.  The macro should be used on function declarations
739// following the argument list:
740//
741//   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
742#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
743# define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
744#else
745# define GTEST_MUST_USE_RESULT_
746#endif  // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
747
748// Determine whether the compiler supports Microsoft's Structured Exception
749// Handling.  This is supported by several Windows compilers but generally
750// does not exist on any other system.
751#ifndef GTEST_HAS_SEH
752// The user didn't tell us, so we need to figure it out.
753
754# if defined(_MSC_VER) || defined(__BORLANDC__)
755// These two compilers are known to support SEH.
756#  define GTEST_HAS_SEH 1
757# else
758// Assume no SEH.
759#  define GTEST_HAS_SEH 0
760# endif
761
762#define GTEST_IS_THREADSAFE \
763    (GTEST_OS_WINDOWS || GTEST_HAS_PTHREAD)
764
765#endif  // GTEST_HAS_SEH
766
767#ifdef _MSC_VER
768
769# if GTEST_LINKED_AS_SHARED_LIBRARY
770#  define GTEST_API_ __declspec(dllimport)
771# elif GTEST_CREATE_SHARED_LIBRARY
772#  define GTEST_API_ __declspec(dllexport)
773# endif
774
775#endif  // _MSC_VER
776
777#if defined(__GNUC__) && __GNUC__ >= 4 && GTEST_CREATE_SHARED_LIBRARY
778# define GTEST_API_ __attribute__((visibility("default")))
779#endif
780
781#ifndef GTEST_API_
782# define GTEST_API_
783#endif
784
785#ifdef __GNUC__
786// Ask the compiler to never inline a given function.
787# define GTEST_NO_INLINE_ __attribute__((noinline))
788#else
789# define GTEST_NO_INLINE_
790#endif
791
792// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
793#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
794# define GTEST_HAS_CXXABI_H_ 1
795#else
796# define GTEST_HAS_CXXABI_H_ 0
797#endif
798
799namespace testing {
800
801class Message;
802
803namespace internal {
804
805// A secret type that Google Test users don't know about.  It has no
806// definition on purpose.  Therefore it's impossible to create a
807// Secret object, which is what we want.
808class Secret;
809
810// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
811// expression is true. For example, you could use it to verify the
812// size of a static array:
813//
814//   GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
815//                         content_type_names_incorrect_size);
816//
817// or to make sure a struct is smaller than a certain size:
818//
819//   GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
820//
821// The second argument to the macro is the name of the variable. If
822// the expression is false, most compilers will issue a warning/error
823// containing the name of the variable.
824
825template <bool>
826struct CompileAssert {
827};
828
829#define GTEST_COMPILE_ASSERT_(expr, msg) \
830  typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
831      msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
832
833// Implementation details of GTEST_COMPILE_ASSERT_:
834//
835// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
836//   elements (and thus is invalid) when the expression is false.
837//
838// - The simpler definition
839//
840//    #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
841//
842//   does not work, as gcc supports variable-length arrays whose sizes
843//   are determined at run-time (this is gcc's extension and not part
844//   of the C++ standard).  As a result, gcc fails to reject the
845//   following code with the simple definition:
846//
847//     int foo;
848//     GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
849//                                      // not a compile-time constant.
850//
851// - By using the type CompileAssert<(bool(expr))>, we ensures that
852//   expr is a compile-time constant.  (Template arguments must be
853//   determined at compile-time.)
854//
855// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
856//   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
857//
858//     CompileAssert<bool(expr)>
859//
860//   instead, these compilers will refuse to compile
861//
862//     GTEST_COMPILE_ASSERT_(5 > 0, some_message);
863//
864//   (They seem to think the ">" in "5 > 0" marks the end of the
865//   template argument list.)
866//
867// - The array size is (bool(expr) ? 1 : -1), instead of simply
868//
869//     ((expr) ? 1 : -1).
870//
871//   This is to avoid running into a bug in MS VC 7.1, which
872//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
873
874// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
875//
876// This template is declared, but intentionally undefined.
877template <typename T1, typename T2>
878struct StaticAssertTypeEqHelper;
879
880template <typename T>
881struct StaticAssertTypeEqHelper<T, T> {};
882
883#if GTEST_HAS_GLOBAL_STRING
884typedef ::string string;
885#else
886typedef ::std::string string;
887#endif  // GTEST_HAS_GLOBAL_STRING
888
889#if GTEST_HAS_GLOBAL_WSTRING
890typedef ::wstring wstring;
891#elif GTEST_HAS_STD_WSTRING
892typedef ::std::wstring wstring;
893#endif  // GTEST_HAS_GLOBAL_WSTRING
894
895// A helper for suppressing warnings on constant condition.  It just
896// returns 'condition'.
897GTEST_API_ bool IsTrue(bool condition);
898
899// Defines scoped_ptr.
900
901// This implementation of scoped_ptr is PARTIAL - it only contains
902// enough stuff to satisfy Google Test's need.
903template <typename T>
904class scoped_ptr {
905 public:
906  typedef T element_type;
907
908  explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
909  ~scoped_ptr() { reset(); }
910
911  T& operator*() const { return *ptr_; }
912  T* operator->() const { return ptr_; }
913  T* get() const { return ptr_; }
914
915  T* release() {
916    T* const ptr = ptr_;
917    ptr_ = NULL;
918    return ptr;
919  }
920
921  void reset(T* p = NULL) {
922    if (p != ptr_) {
923      if (IsTrue(sizeof(T) > 0)) {  // Makes sure T is a complete type.
924        delete ptr_;
925      }
926      ptr_ = p;
927    }
928  }
929
930 private:
931  T* ptr_;
932
933  GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
934};
935
936// Defines RE.
937
938// A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
939// Regular Expression syntax.
940class GTEST_API_ RE {
941 public:
942  // A copy constructor is required by the Standard to initialize object
943  // references from r-values.
944  RE(const RE& other) { Init(other.pattern()); }
945
946  // Constructs an RE from a string.
947  RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
948
949#if GTEST_HAS_GLOBAL_STRING
950
951  RE(const ::string& regex) { Init(regex.c_str()); }  // NOLINT
952
953#endif  // GTEST_HAS_GLOBAL_STRING
954
955  RE(const char* regex) { Init(regex); }  // NOLINT
956  ~RE();
957
958  // Returns the string representation of the regex.
959  const char* pattern() const { return pattern_; }
960
961  // FullMatch(str, re) returns true iff regular expression re matches
962  // the entire str.
963  // PartialMatch(str, re) returns true iff regular expression re
964  // matches a substring of str (including str itself).
965  //
966  // TODO(wan@google.com): make FullMatch() and PartialMatch() work
967  // when str contains NUL characters.
968  static bool FullMatch(const ::std::string& str, const RE& re) {
969    return FullMatch(str.c_str(), re);
970  }
971  static bool PartialMatch(const ::std::string& str, const RE& re) {
972    return PartialMatch(str.c_str(), re);
973  }
974
975#if GTEST_HAS_GLOBAL_STRING
976
977  static bool FullMatch(const ::string& str, const RE& re) {
978    return FullMatch(str.c_str(), re);
979  }
980  static bool PartialMatch(const ::string& str, const RE& re) {
981    return PartialMatch(str.c_str(), re);
982  }
983
984#endif  // GTEST_HAS_GLOBAL_STRING
985
986  static bool FullMatch(const char* str, const RE& re);
987  static bool PartialMatch(const char* str, const RE& re);
988
989 private:
990  void Init(const char* regex);
991
992  // We use a const char* instead of an std::string, as Google Test used to be
993  // used where std::string is not available.  TODO(wan@google.com): change to
994  // std::string.
995  const char* pattern_;
996  bool is_valid_;
997
998#if GTEST_USES_POSIX_RE
999
1000  regex_t full_regex_;     // For FullMatch().
1001  regex_t partial_regex_;  // For PartialMatch().
1002
1003#else  // GTEST_USES_SIMPLE_RE
1004
1005  const char* full_pattern_;  // For FullMatch();
1006
1007#endif
1008
1009  GTEST_DISALLOW_ASSIGN_(RE);
1010};
1011
1012// Formats a source file path and a line number as they would appear
1013// in an error message from the compiler used to compile this code.
1014GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
1015
1016// Formats a file location for compiler-independent XML output.
1017// Although this function is not platform dependent, we put it next to
1018// FormatFileLocation in order to contrast the two functions.
1019GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
1020                                                               int line);
1021
1022// Defines logging utilities:
1023//   GTEST_LOG_(severity) - logs messages at the specified severity level. The
1024//                          message itself is streamed into the macro.
1025//   LogToStderr()  - directs all log messages to stderr.
1026//   FlushInfoLog() - flushes informational log messages.
1027
1028enum GTestLogSeverity {
1029  GTEST_INFO,
1030  GTEST_WARNING,
1031  GTEST_ERROR,
1032  GTEST_FATAL
1033};
1034
1035// Formats log entry severity, provides a stream object for streaming the
1036// log message, and terminates the message with a newline when going out of
1037// scope.
1038class GTEST_API_ GTestLog {
1039 public:
1040  GTestLog(GTestLogSeverity severity, const char* file, int line);
1041
1042  // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1043  ~GTestLog();
1044
1045  ::std::ostream& GetStream() { return ::std::cerr; }
1046
1047 private:
1048  const GTestLogSeverity severity_;
1049
1050  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
1051};
1052
1053#define GTEST_LOG_(severity) \
1054    ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1055                                  __FILE__, __LINE__).GetStream()
1056
1057inline void LogToStderr() {}
1058inline void FlushInfoLog() { fflush(NULL); }
1059
1060// INTERNAL IMPLEMENTATION - DO NOT USE.
1061//
1062// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1063// is not satisfied.
1064//  Synopsys:
1065//    GTEST_CHECK_(boolean_condition);
1066//     or
1067//    GTEST_CHECK_(boolean_condition) << "Additional message";
1068//
1069//    This checks the condition and if the condition is not satisfied
1070//    it prints message about the condition violation, including the
1071//    condition itself, plus additional message streamed into it, if any,
1072//    and then it aborts the program. It aborts the program irrespective of
1073//    whether it is built in the debug mode or not.
1074#define GTEST_CHECK_(condition) \
1075    GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1076    if (::testing::internal::IsTrue(condition)) \
1077      ; \
1078    else \
1079      GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1080
1081// An all-mode assert to verify that the given POSIX-style function
1082// call returns 0 (indicating success).  Known limitation: this
1083// doesn't expand to a balanced 'if' statement, so enclose the macro
1084// in {} if you need to use it as the only statement in an 'if'
1085// branch.
1086#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1087  if (const int gtest_error = (posix_call)) \
1088    GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1089                      << gtest_error
1090
1091// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1092//
1093// Use ImplicitCast_ as a safe version of static_cast for upcasting in
1094// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1095// const Foo*).  When you use ImplicitCast_, the compiler checks that
1096// the cast is safe.  Such explicit ImplicitCast_s are necessary in
1097// surprisingly many situations where C++ demands an exact type match
1098// instead of an argument type convertable to a target type.
1099//
1100// The syntax for using ImplicitCast_ is the same as for static_cast:
1101//
1102//   ImplicitCast_<ToType>(expr)
1103//
1104// ImplicitCast_ would have been part of the C++ standard library,
1105// but the proposal was submitted too late.  It will probably make
1106// its way into the language in the future.
1107//
1108// This relatively ugly name is intentional. It prevents clashes with
1109// similar functions users may have (e.g., implicit_cast). The internal
1110// namespace alone is not enough because the function can be found by ADL.
1111template<typename To>
1112inline To ImplicitCast_(To x) { return x; }
1113
1114// When you upcast (that is, cast a pointer from type Foo to type
1115// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
1116// always succeed.  When you downcast (that is, cast a pointer from
1117// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
1118// how do you know the pointer is really of type SubclassOfFoo?  It
1119// could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
1120// when you downcast, you should use this macro.  In debug mode, we
1121// use dynamic_cast<> to double-check the downcast is legal (we die
1122// if it's not).  In normal mode, we do the efficient static_cast<>
1123// instead.  Thus, it's important to test in debug mode to make sure
1124// the cast is legal!
1125//    This is the only place in the code we should use dynamic_cast<>.
1126// In particular, you SHOULDN'T be using dynamic_cast<> in order to
1127// do RTTI (eg code like this:
1128//    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
1129//    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
1130// You should design the code some other way not to need this.
1131//
1132// This relatively ugly name is intentional. It prevents clashes with
1133// similar functions users may have (e.g., down_cast). The internal
1134// namespace alone is not enough because the function can be found by ADL.
1135template<typename To, typename From>  // use like this: DownCast_<T*>(foo);
1136inline To DownCast_(From* f) {  // so we only accept pointers
1137  // Ensures that To is a sub-type of From *.  This test is here only
1138  // for compile-time type checking, and has no overhead in an
1139  // optimized build at run-time, as it will be optimized away
1140  // completely.
1141  if (false) {
1142    const To to = NULL;
1143    ::testing::internal::ImplicitCast_<From*>(to);
1144  }
1145
1146#if GTEST_HAS_RTTI
1147  // RTTI: debug mode only!
1148  GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
1149#endif
1150  return static_cast<To>(f);
1151}
1152
1153// Downcasts the pointer of type Base to Derived.
1154// Derived must be a subclass of Base. The parameter MUST
1155// point to a class of type Derived, not any subclass of it.
1156// When RTTI is available, the function performs a runtime
1157// check to enforce this.
1158template <class Derived, class Base>
1159Derived* CheckedDowncastToActualType(Base* base) {
1160#if GTEST_HAS_RTTI
1161  GTEST_CHECK_(typeid(*base) == typeid(Derived));
1162  return dynamic_cast<Derived*>(base);  // NOLINT
1163#else
1164  return static_cast<Derived*>(base);  // Poor man's downcast.
1165#endif
1166}
1167
1168#if GTEST_HAS_STREAM_REDIRECTION
1169
1170// Defines the stderr capturer:
1171//   CaptureStdout     - starts capturing stdout.
1172//   GetCapturedStdout - stops capturing stdout and returns the captured string.
1173//   CaptureStderr     - starts capturing stderr.
1174//   GetCapturedStderr - stops capturing stderr and returns the captured string.
1175//
1176GTEST_API_ void CaptureStdout();
1177GTEST_API_ std::string GetCapturedStdout();
1178GTEST_API_ void CaptureStderr();
1179GTEST_API_ std::string GetCapturedStderr();
1180
1181#endif  // GTEST_HAS_STREAM_REDIRECTION
1182
1183
1184#if GTEST_HAS_DEATH_TEST
1185
1186const ::std::vector<testing::internal::string>& GetInjectableArgvs();
1187void SetInjectableArgvs(const ::std::vector<testing::internal::string>*
1188                             new_argvs);
1189
1190// A copy of all command line arguments.  Set by InitGoogleTest().
1191extern ::std::vector<testing::internal::string> g_argvs;
1192
1193#endif  // GTEST_HAS_DEATH_TEST
1194
1195// Defines synchronization primitives.
1196#if GTEST_IS_THREADSAFE
1197# if GTEST_HAS_PTHREAD
1198// Sleeps for (roughly) n milliseconds.  This function is only for testing
1199// Google Test's own constructs.  Don't use it in user tests, either
1200// directly or indirectly.
1201inline void SleepMilliseconds(int n) {
1202  const timespec time = {
1203    0,                  // 0 seconds.
1204    n * 1000L * 1000L,  // And n ms.
1205  };
1206  nanosleep(&time, NULL);
1207}
1208# endif  // GTEST_HAS_PTHREAD
1209
1210# if 0  // OS detection
1211# elif GTEST_HAS_PTHREAD
1212// Allows a controller thread to pause execution of newly created
1213// threads until notified.  Instances of this class must be created
1214// and destroyed in the controller thread.
1215//
1216// This class is only for testing Google Test's own constructs. Do not
1217// use it in user tests, either directly or indirectly.
1218class Notification {
1219 public:
1220  Notification() : notified_(false) {
1221    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1222  }
1223  ~Notification() {
1224    pthread_mutex_destroy(&mutex_);
1225  }
1226
1227  // Notifies all threads created with this notification to start. Must
1228  // be called from the controller thread.
1229  void Notify() {
1230    pthread_mutex_lock(&mutex_);
1231    notified_ = true;
1232    pthread_mutex_unlock(&mutex_);
1233  }
1234
1235  // Blocks until the controller thread notifies. Must be called from a test
1236  // thread.
1237  void WaitForNotification() {
1238    for (;;) {
1239      pthread_mutex_lock(&mutex_);
1240      const bool notified = notified_;
1241      pthread_mutex_unlock(&mutex_);
1242      if (notified)
1243        break;
1244      SleepMilliseconds(10);
1245    }
1246  }
1247
1248 private:
1249  pthread_mutex_t mutex_;
1250  bool notified_;
1251
1252  GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1253};
1254
1255# elif GTEST_OS_WINDOWS
1256
1257GTEST_API_ void SleepMilliseconds(int n);
1258
1259// Provides leak-safe Windows kernel handle ownership.
1260// Used in death tests and in threading support.
1261class GTEST_API_ AutoHandle {
1262 public:
1263  // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1264  // avoid including <windows.h> in this header file. Including <windows.h> is
1265  // undesirable because it defines a lot of symbols and macros that tend to
1266  // conflict with client code. This assumption is verified by
1267  // WindowsTypesTest.HANDLEIsVoidStar.
1268  typedef void* Handle;
1269  AutoHandle();
1270  explicit AutoHandle(Handle handle);
1271
1272  ~AutoHandle();
1273
1274  Handle Get() const;
1275  void Reset();
1276  void Reset(Handle handle);
1277
1278 private:
1279  // Returns true iff the handle is a valid handle object that can be closed.
1280  bool IsCloseable() const;
1281
1282  Handle handle_;
1283
1284  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
1285};
1286
1287// Allows a controller thread to pause execution of newly created
1288// threads until notified.  Instances of this class must be created
1289// and destroyed in the controller thread.
1290//
1291// This class is only for testing Google Test's own constructs. Do not
1292// use it in user tests, either directly or indirectly.
1293class GTEST_API_ Notification {
1294 public:
1295  Notification();
1296  void Notify();
1297  void WaitForNotification();
1298
1299 private:
1300  AutoHandle event_;
1301
1302  GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1303};
1304# endif  // OS detection
1305
1306// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1307// defined, but we don't want to use MinGW's pthreads implementation, which
1308// has conformance problems with some versions of the POSIX standard.
1309# if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1310
1311// As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1312// Consequently, it cannot select a correct instantiation of ThreadWithParam
1313// in order to call its Run(). Introducing ThreadWithParamBase as a
1314// non-templated base class for ThreadWithParam allows us to bypass this
1315// problem.
1316class ThreadWithParamBase {
1317 public:
1318  virtual ~ThreadWithParamBase() {}
1319  virtual void Run() = 0;
1320};
1321
1322// pthread_create() accepts a pointer to a function type with the C linkage.
1323// According to the Standard (7.5/1), function types with different linkages
1324// are different even if they are otherwise identical.  Some compilers (for
1325// example, SunStudio) treat them as different types.  Since class methods
1326// cannot be defined with C-linkage we need to define a free C-function to
1327// pass into pthread_create().
1328extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1329  static_cast<ThreadWithParamBase*>(thread)->Run();
1330  return NULL;
1331}
1332
1333// Helper class for testing Google Test's multi-threading constructs.
1334// To use it, write:
1335//
1336//   void ThreadFunc(int param) { /* Do things with param */ }
1337//   Notification thread_can_start;
1338//   ...
1339//   // The thread_can_start parameter is optional; you can supply NULL.
1340//   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1341//   thread_can_start.Notify();
1342//
1343// These classes are only for testing Google Test's own constructs. Do
1344// not use them in user tests, either directly or indirectly.
1345template <typename T>
1346class ThreadWithParam : public ThreadWithParamBase {
1347 public:
1348  typedef void UserThreadFunc(T);
1349
1350  ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1351      : func_(func),
1352        param_(param),
1353        thread_can_start_(thread_can_start),
1354        finished_(false) {
1355    ThreadWithParamBase* const base = this;
1356    // The thread can be created only after all fields except thread_
1357    // have been initialized.
1358    GTEST_CHECK_POSIX_SUCCESS_(
1359        pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
1360  }
1361  ~ThreadWithParam() { Join(); }
1362
1363  void Join() {
1364    if (!finished_) {
1365      GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
1366      finished_ = true;
1367    }
1368  }
1369
1370  virtual void Run() {
1371    if (thread_can_start_ != NULL)
1372      thread_can_start_->WaitForNotification();
1373    func_(param_);
1374  }
1375
1376 private:
1377  UserThreadFunc* const func_;  // User-supplied thread function.
1378  const T param_;  // User-supplied parameter to the thread function.
1379  // When non-NULL, used to block execution until the controller thread
1380  // notifies.
1381  Notification* const thread_can_start_;
1382  bool finished_;  // true iff we know that the thread function has finished.
1383  pthread_t thread_;  // The native thread object.
1384
1385  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1386};
1387# endif  // GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1388
1389# if 0  // OS detection
1390# elif GTEST_OS_WINDOWS
1391
1392// Mutex implements mutex on Windows platforms.  It is used in conjunction
1393// with class MutexLock:
1394//
1395//   Mutex mutex;
1396//   ...
1397//   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the
1398//                            // end of the current scope.
1399//
1400// A static Mutex *must* be defined or declared using one of the following
1401// macros:
1402//   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1403//   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1404//
1405// (A non-static Mutex is defined/declared in the usual way).
1406class GTEST_API_ Mutex {
1407 public:
1408  enum MutexType { kStatic = 0, kDynamic = 1 };
1409  // We rely on kStaticMutex being 0 as it is to what the linker initializes
1410  // type_ in static mutexes.  critical_section_ will be initialized lazily
1411  // in ThreadSafeLazyInit().
1412  enum StaticConstructorSelector { kStaticMutex = 0 };
1413
1414  // This constructor intentionally does nothing.  It relies on type_ being
1415  // statically initialized to 0 (effectively setting it to kStatic) and on
1416  // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1417  explicit Mutex(StaticConstructorSelector /*dummy*/) {}
1418
1419  Mutex();
1420  ~Mutex();
1421
1422  void Lock();
1423
1424  void Unlock();
1425
1426  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1427  // with high probability.
1428  void AssertHeld();
1429
1430 private:
1431  // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1432  void ThreadSafeLazyInit();
1433
1434  // Per http://blogs.msdn.com/b/oldnewthing/archive/2004/02/23/78395.aspx,
1435  // we assume that 0 is an invalid value for thread IDs.
1436  unsigned int owner_thread_id_;
1437
1438  // For static mutexes, we rely on these members being initialized to zeros
1439  // by the linker.
1440  MutexType type_;
1441  long critical_section_init_phase_;  // NOLINT
1442  _RTL_CRITICAL_SECTION* critical_section_;
1443
1444  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1445};
1446
1447# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1448    extern ::testing::internal::Mutex mutex
1449
1450# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1451    ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1452
1453// We cannot name this class MutexLock because the ctor declaration would
1454// conflict with a macro named MutexLock, which is defined on some
1455// platforms. That macro is used as a defensive measure to prevent against
1456// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1457// "MutexLock l(&mu)".  Hence the typedef trick below.
1458class GTestMutexLock {
1459 public:
1460  explicit GTestMutexLock(Mutex* mutex)
1461      : mutex_(mutex) { mutex_->Lock(); }
1462
1463  ~GTestMutexLock() { mutex_->Unlock(); }
1464
1465 private:
1466  Mutex* const mutex_;
1467
1468  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1469};
1470
1471typedef GTestMutexLock MutexLock;
1472
1473// Base class for ValueHolder<T>.  Allows a caller to hold and delete a value
1474// without knowing its type.
1475class ThreadLocalValueHolderBase {
1476 public:
1477  virtual ~ThreadLocalValueHolderBase() {}
1478};
1479
1480// Provides a way for a thread to send notifications to a ThreadLocal
1481// regardless of its parameter type.
1482class ThreadLocalBase {
1483 public:
1484  // Creates a new ValueHolder<T> object holding a default value passed to
1485  // this ThreadLocal<T>'s constructor and returns it.  It is the caller's
1486  // responsibility not to call this when the ThreadLocal<T> instance already
1487  // has a value on the current thread.
1488  virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1489
1490 protected:
1491  ThreadLocalBase() {}
1492  virtual ~ThreadLocalBase() {}
1493
1494 private:
1495  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase);
1496};
1497
1498// Maps a thread to a set of ThreadLocals that have values instantiated on that
1499// thread and notifies them when the thread exits.  A ThreadLocal instance is
1500// expected to persist until all threads it has values on have terminated.
1501class GTEST_API_ ThreadLocalRegistry {
1502 public:
1503  // Registers thread_local_instance as having value on the current thread.
1504  // Returns a value that can be used to identify the thread from other threads.
1505  static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1506      const ThreadLocalBase* thread_local_instance);
1507
1508  // Invoked when a ThreadLocal instance is destroyed.
1509  static void OnThreadLocalDestroyed(
1510      const ThreadLocalBase* thread_local_instance);
1511};
1512
1513class GTEST_API_ ThreadWithParamBase {
1514 public:
1515  void Join();
1516
1517 protected:
1518  class Runnable {
1519   public:
1520    virtual ~Runnable() {}
1521    virtual void Run() = 0;
1522  };
1523
1524  ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
1525  virtual ~ThreadWithParamBase();
1526
1527 private:
1528  AutoHandle thread_;
1529};
1530
1531// Helper class for testing Google Test's multi-threading constructs.
1532template <typename T>
1533class ThreadWithParam : public ThreadWithParamBase {
1534 public:
1535  typedef void UserThreadFunc(T);
1536
1537  ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1538      : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
1539  }
1540  virtual ~ThreadWithParam() {}
1541
1542 private:
1543  class RunnableImpl : public Runnable {
1544   public:
1545    RunnableImpl(UserThreadFunc* func, T param)
1546        : func_(func),
1547          param_(param) {
1548    }
1549    virtual ~RunnableImpl() {}
1550    virtual void Run() {
1551      func_(param_);
1552    }
1553
1554   private:
1555    UserThreadFunc* const func_;
1556    const T param_;
1557
1558    GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl);
1559  };
1560
1561  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1562};
1563
1564// Implements thread-local storage on Windows systems.
1565//
1566//   // Thread 1
1567//   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
1568//
1569//   // Thread 2
1570//   tl.set(150);  // Changes the value for thread 2 only.
1571//   EXPECT_EQ(150, tl.get());
1572//
1573//   // Thread 1
1574//   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
1575//   tl.set(200);
1576//   EXPECT_EQ(200, tl.get());
1577//
1578// The template type argument T must have a public copy constructor.
1579// In addition, the default ThreadLocal constructor requires T to have
1580// a public default constructor.
1581//
1582// The users of a TheadLocal instance have to make sure that all but one
1583// threads (including the main one) using that instance have exited before
1584// destroying it. Otherwise, the per-thread objects managed for them by the
1585// ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1586//
1587// Google Test only uses global ThreadLocal objects.  That means they
1588// will die after main() has returned.  Therefore, no per-thread
1589// object managed by Google Test will be leaked as long as all threads
1590// using Google Test have exited when main() returns.
1591template <typename T>
1592class ThreadLocal : public ThreadLocalBase {
1593 public:
1594  ThreadLocal() : default_() {}
1595  explicit ThreadLocal(const T& value) : default_(value) {}
1596
1597  ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1598
1599  T* pointer() { return GetOrCreateValue(); }
1600  const T* pointer() const { return GetOrCreateValue(); }
1601  const T& get() const { return *pointer(); }
1602  void set(const T& value) { *pointer() = value; }
1603
1604 private:
1605  // Holds a value of T.  Can be deleted via its base class without the caller
1606  // knowing the type of T.
1607  class ValueHolder : public ThreadLocalValueHolderBase {
1608   public:
1609    explicit ValueHolder(const T& value) : value_(value) {}
1610
1611    T* pointer() { return &value_; }
1612
1613   private:
1614    T value_;
1615    GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1616  };
1617
1618
1619  T* GetOrCreateValue() const {
1620    return static_cast<ValueHolder*>(
1621        ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();
1622  }
1623
1624  virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const {
1625    return new ValueHolder(default_);
1626  }
1627
1628  const T default_;  // The default value for each thread.
1629
1630  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1631};
1632
1633# elif GTEST_HAS_PTHREAD
1634
1635// MutexBase and Mutex implement mutex on pthreads-based platforms.
1636class MutexBase {
1637 public:
1638  // Acquires this mutex.
1639  void Lock() {
1640    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1641    owner_ = pthread_self();
1642    has_owner_ = true;
1643  }
1644
1645  // Releases this mutex.
1646  void Unlock() {
1647    // Since the lock is being released the owner_ field should no longer be
1648    // considered valid. We don't protect writing to has_owner_ here, as it's
1649    // the caller's responsibility to ensure that the current thread holds the
1650    // mutex when this is called.
1651    has_owner_ = false;
1652    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1653  }
1654
1655  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1656  // with high probability.
1657  void AssertHeld() const {
1658    GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1659        << "The current thread is not holding the mutex @" << this;
1660  }
1661
1662  // A static mutex may be used before main() is entered.  It may even
1663  // be used before the dynamic initialization stage.  Therefore we
1664  // must be able to initialize a static mutex object at link time.
1665  // This means MutexBase has to be a POD and its member variables
1666  // have to be public.
1667 public:
1668  pthread_mutex_t mutex_;  // The underlying pthread mutex.
1669  // has_owner_ indicates whether the owner_ field below contains a valid thread
1670  // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1671  // accesses to the owner_ field should be protected by a check of this field.
1672  // An alternative might be to memset() owner_ to all zeros, but there's no
1673  // guarantee that a zero'd pthread_t is necessarily invalid or even different
1674  // from pthread_self().
1675  bool has_owner_;
1676  pthread_t owner_;  // The thread holding the mutex.
1677};
1678
1679// Forward-declares a static mutex.
1680#  define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1681     extern ::testing::internal::MutexBase mutex
1682
1683// Defines and statically (i.e. at link time) initializes a static mutex.
1684// The initialization list here does not explicitly initialize each field,
1685// instead relying on default initialization for the unspecified fields. In
1686// particular, the owner_ field (a pthread_t) is not explicitly initialized.
1687// This allows initialization to work whether pthread_t is a scalar or struct.
1688// The flag -Wmissing-field-initializers must not be specified for this to work.
1689#  define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1690     ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
1691
1692// The Mutex class can only be used for mutexes created at runtime. It
1693// shares its API with MutexBase otherwise.
1694class Mutex : public MutexBase {
1695 public:
1696  Mutex() {
1697    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1698    has_owner_ = false;
1699  }
1700  ~Mutex() {
1701    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1702  }
1703
1704 private:
1705  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1706};
1707
1708// We cannot name this class MutexLock because the ctor declaration would
1709// conflict with a macro named MutexLock, which is defined on some
1710// platforms. That macro is used as a defensive measure to prevent against
1711// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1712// "MutexLock l(&mu)".  Hence the typedef trick below.
1713class GTestMutexLock {
1714 public:
1715  explicit GTestMutexLock(MutexBase* mutex)
1716      : mutex_(mutex) { mutex_->Lock(); }
1717
1718  ~GTestMutexLock() { mutex_->Unlock(); }
1719
1720 private:
1721  MutexBase* const mutex_;
1722
1723  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1724};
1725
1726typedef GTestMutexLock MutexLock;
1727
1728// Helpers for ThreadLocal.
1729
1730// pthread_key_create() requires DeleteThreadLocalValue() to have
1731// C-linkage.  Therefore it cannot be templatized to access
1732// ThreadLocal<T>.  Hence the need for class
1733// ThreadLocalValueHolderBase.
1734class ThreadLocalValueHolderBase {
1735 public:
1736  virtual ~ThreadLocalValueHolderBase() {}
1737};
1738
1739// Called by pthread to delete thread-local data stored by
1740// pthread_setspecific().
1741extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1742  delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1743}
1744
1745// Implements thread-local storage on pthreads-based systems.
1746template <typename T>
1747class ThreadLocal {
1748 public:
1749  ThreadLocal() : key_(CreateKey()),
1750                  default_() {}
1751  explicit ThreadLocal(const T& value) : key_(CreateKey()),
1752                                         default_(value) {}
1753
1754  ~ThreadLocal() {
1755    // Destroys the managed object for the current thread, if any.
1756    DeleteThreadLocalValue(pthread_getspecific(key_));
1757
1758    // Releases resources associated with the key.  This will *not*
1759    // delete managed objects for other threads.
1760    GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1761  }
1762
1763  T* pointer() { return GetOrCreateValue(); }
1764  const T* pointer() const { return GetOrCreateValue(); }
1765  const T& get() const { return *pointer(); }
1766  void set(const T& value) { *pointer() = value; }
1767
1768 private:
1769  // Holds a value of type T.
1770  class ValueHolder : public ThreadLocalValueHolderBase {
1771   public:
1772    explicit ValueHolder(const T& value) : value_(value) {}
1773
1774    T* pointer() { return &value_; }
1775
1776   private:
1777    T value_;
1778    GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1779  };
1780
1781  static pthread_key_t CreateKey() {
1782    pthread_key_t key;
1783    // When a thread exits, DeleteThreadLocalValue() will be called on
1784    // the object managed for that thread.
1785    GTEST_CHECK_POSIX_SUCCESS_(
1786        pthread_key_create(&key, &DeleteThreadLocalValue));
1787    return key;
1788  }
1789
1790  T* GetOrCreateValue() const {
1791    ThreadLocalValueHolderBase* const holder =
1792        static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1793    if (holder != NULL) {
1794      return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1795    }
1796
1797    ValueHolder* const new_holder = new ValueHolder(default_);
1798    ThreadLocalValueHolderBase* const holder_base = new_holder;
1799    GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1800    return new_holder->pointer();
1801  }
1802
1803  // A key pthreads uses for looking up per-thread values.
1804  const pthread_key_t key_;
1805  const T default_;  // The default value for each thread.
1806
1807  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1808};
1809
1810# endif  // OS detection
1811
1812#else  // GTEST_IS_THREADSAFE
1813
1814// A dummy implementation of synchronization primitives (mutex, lock,
1815// and thread-local variable).  Necessary for compiling Google Test where
1816// mutex is not supported - using Google Test in multiple threads is not
1817// supported on such platforms.
1818
1819class Mutex {
1820 public:
1821  Mutex() {}
1822  void Lock() {}
1823  void Unlock() {}
1824  void AssertHeld() const {}
1825};
1826
1827# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1828  extern ::testing::internal::Mutex mutex
1829
1830# define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1831
1832// We cannot name this class MutexLock because the ctor declaration would
1833// conflict with a macro named MutexLock, which is defined on some
1834// platforms. That macro is used as a defensive measure to prevent against
1835// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1836// "MutexLock l(&mu)".  Hence the typedef trick below.
1837class GTestMutexLock {
1838 public:
1839  explicit GTestMutexLock(Mutex*) {}  // NOLINT
1840};
1841
1842typedef GTestMutexLock MutexLock;
1843
1844template <typename T>
1845class ThreadLocal {
1846 public:
1847  ThreadLocal() : value_() {}
1848  explicit ThreadLocal(const T& value) : value_(value) {}
1849  T* pointer() { return &value_; }
1850  const T* pointer() const { return &value_; }
1851  const T& get() const { return value_; }
1852  void set(const T& value) { value_ = value; }
1853 private:
1854  T value_;
1855};
1856
1857#endif  // GTEST_IS_THREADSAFE
1858
1859// Returns the number of threads running in the process, or 0 to indicate that
1860// we cannot detect it.
1861GTEST_API_ size_t GetThreadCount();
1862
1863// Passing non-POD classes through ellipsis (...) crashes the ARM
1864// compiler and generates a warning in Sun Studio.  The Nokia Symbian
1865// and the IBM XL C/C++ compiler try to instantiate a copy constructor
1866// for objects passed through ellipsis (...), failing for uncopyable
1867// objects.  We define this to ensure that only POD is passed through
1868// ellipsis on these systems.
1869#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
1870// We lose support for NULL detection where the compiler doesn't like
1871// passing non-POD classes through ellipsis (...).
1872# define GTEST_ELLIPSIS_NEEDS_POD_ 1
1873#else
1874# define GTEST_CAN_COMPARE_NULL 1
1875#endif
1876
1877// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
1878// const T& and const T* in a function template.  These compilers
1879// _can_ decide between class template specializations for T and T*,
1880// so a tr1::type_traits-like is_pointer works.
1881#if defined(__SYMBIAN32__) || defined(__IBMCPP__)
1882# define GTEST_NEEDS_IS_POINTER_ 1
1883#endif
1884
1885template <bool bool_value>
1886struct bool_constant {
1887  typedef bool_constant<bool_value> type;
1888  static const bool value = bool_value;
1889};
1890template <bool bool_value> const bool bool_constant<bool_value>::value;
1891
1892typedef bool_constant<false> false_type;
1893typedef bool_constant<true> true_type;
1894
1895template <typename T>
1896struct is_pointer : public false_type {};
1897
1898template <typename T>
1899struct is_pointer<T*> : public true_type {};
1900
1901template <typename Iterator>
1902struct IteratorTraits {
1903  typedef typename Iterator::value_type value_type;
1904};
1905
1906template <typename T>
1907struct IteratorTraits<T*> {
1908  typedef T value_type;
1909};
1910
1911template <typename T>
1912struct IteratorTraits<const T*> {
1913  typedef T value_type;
1914};
1915
1916#if GTEST_OS_WINDOWS
1917# define GTEST_PATH_SEP_ "\\"
1918# define GTEST_HAS_ALT_PATH_SEP_ 1
1919// The biggest signed integer type the compiler supports.
1920typedef __int64 BiggestInt;
1921#else
1922# define GTEST_PATH_SEP_ "/"
1923# define GTEST_HAS_ALT_PATH_SEP_ 0
1924typedef long long BiggestInt;  // NOLINT
1925#endif  // GTEST_OS_WINDOWS
1926
1927// Utilities for char.
1928
1929// isspace(int ch) and friends accept an unsigned char or EOF.  char
1930// may be signed, depending on the compiler (or compiler flags).
1931// Therefore we need to cast a char to unsigned char before calling
1932// isspace(), etc.
1933
1934inline bool IsAlpha(char ch) {
1935  return isalpha(static_cast<unsigned char>(ch)) != 0;
1936}
1937inline bool IsAlNum(char ch) {
1938  return isalnum(static_cast<unsigned char>(ch)) != 0;
1939}
1940inline bool IsDigit(char ch) {
1941  return isdigit(static_cast<unsigned char>(ch)) != 0;
1942}
1943inline bool IsLower(char ch) {
1944  return islower(static_cast<unsigned char>(ch)) != 0;
1945}
1946inline bool IsSpace(char ch) {
1947  return isspace(static_cast<unsigned char>(ch)) != 0;
1948}
1949inline bool IsUpper(char ch) {
1950  return isupper(static_cast<unsigned char>(ch)) != 0;
1951}
1952inline bool IsXDigit(char ch) {
1953  return isxdigit(static_cast<unsigned char>(ch)) != 0;
1954}
1955inline bool IsXDigit(wchar_t ch) {
1956  const unsigned char low_byte = static_cast<unsigned char>(ch);
1957  return ch == low_byte && isxdigit(low_byte) != 0;
1958}
1959
1960inline char ToLower(char ch) {
1961  return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1962}
1963inline char ToUpper(char ch) {
1964  return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1965}
1966
1967// The testing::internal::posix namespace holds wrappers for common
1968// POSIX functions.  These wrappers hide the differences between
1969// Windows/MSVC and POSIX systems.  Since some compilers define these
1970// standard functions as macros, the wrapper cannot have the same name
1971// as the wrapped function.
1972
1973namespace posix {
1974
1975// Functions with a different name on Windows.
1976
1977#if GTEST_OS_WINDOWS
1978
1979typedef struct _stat StatStruct;
1980
1981# ifdef __BORLANDC__
1982inline int IsATTY(int fd) { return isatty(fd); }
1983inline int StrCaseCmp(const char* s1, const char* s2) {
1984  return stricmp(s1, s2);
1985}
1986inline char* StrDup(const char* src) { return strdup(src); }
1987# else  // !__BORLANDC__
1988#  if GTEST_OS_WINDOWS_MOBILE
1989inline int IsATTY(int /* fd */) { return 0; }
1990#  else
1991inline int IsATTY(int fd) { return _isatty(fd); }
1992#  endif  // GTEST_OS_WINDOWS_MOBILE
1993inline int StrCaseCmp(const char* s1, const char* s2) {
1994  return _stricmp(s1, s2);
1995}
1996inline char* StrDup(const char* src) { return _strdup(src); }
1997# endif  // __BORLANDC__
1998
1999# if GTEST_OS_WINDOWS_MOBILE
2000inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
2001// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
2002// time and thus not defined there.
2003# else
2004inline int FileNo(FILE* file) { return _fileno(file); }
2005inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2006inline int RmDir(const char* dir) { return _rmdir(dir); }
2007inline bool IsDir(const StatStruct& st) {
2008  return (_S_IFDIR & st.st_mode) != 0;
2009}
2010# endif  // GTEST_OS_WINDOWS_MOBILE
2011
2012#else
2013
2014typedef struct stat StatStruct;
2015
2016inline int FileNo(FILE* file) { return fileno(file); }
2017inline int IsATTY(int fd) { return isatty(fd); }
2018inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
2019inline int StrCaseCmp(const char* s1, const char* s2) {
2020  return strcasecmp(s1, s2);
2021}
2022inline char* StrDup(const char* src) { return strdup(src); }
2023inline int RmDir(const char* dir) { return rmdir(dir); }
2024inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2025
2026#endif  // GTEST_OS_WINDOWS
2027
2028// Functions deprecated by MSVC 8.0.
2029
2030#ifdef _MSC_VER
2031// Temporarily disable warning 4996 (deprecated function).
2032# pragma warning(push)
2033# pragma warning(disable:4996)
2034#endif
2035
2036inline const char* StrNCpy(char* dest, const char* src, size_t n) {
2037  return strncpy(dest, src, n);
2038}
2039
2040// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2041// StrError() aren't needed on Windows CE at this time and thus not
2042// defined there.
2043
2044#if !GTEST_OS_WINDOWS_MOBILE
2045inline int ChDir(const char* dir) { return chdir(dir); }
2046#endif
2047inline FILE* FOpen(const char* path, const char* mode) {
2048  return fopen(path, mode);
2049}
2050#if !GTEST_OS_WINDOWS_MOBILE
2051inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
2052  return freopen(path, mode, stream);
2053}
2054inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2055#endif
2056inline int FClose(FILE* fp) { return fclose(fp); }
2057#if !GTEST_OS_WINDOWS_MOBILE
2058inline int Read(int fd, void* buf, unsigned int count) {
2059  return static_cast<int>(read(fd, buf, count));
2060}
2061inline int Write(int fd, const void* buf, unsigned int count) {
2062  return static_cast<int>(write(fd, buf, count));
2063}
2064inline int Close(int fd) { return close(fd); }
2065inline const char* StrError(int errnum) { return strerror(errnum); }
2066#endif
2067inline const char* GetEnv(const char* name) {
2068#if GTEST_OS_WINDOWS_MOBILE
2069  // We are on Windows CE, which has no environment variables.
2070  return NULL;
2071#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2072  // Environment variables which we programmatically clear will be set to the
2073  // empty string rather than unset (NULL).  Handle that case.
2074  const char* const env = getenv(name);
2075  return (env != NULL && env[0] != '\0') ? env : NULL;
2076#else
2077  return getenv(name);
2078#endif
2079}
2080
2081#ifdef _MSC_VER
2082# pragma warning(pop)  // Restores the warning state.
2083#endif
2084
2085#if GTEST_OS_WINDOWS_MOBILE
2086// Windows CE has no C library. The abort() function is used in
2087// several places in Google Test. This implementation provides a reasonable
2088// imitation of standard behaviour.
2089void Abort();
2090#else
2091inline void Abort() { abort(); }
2092#endif  // GTEST_OS_WINDOWS_MOBILE
2093
2094}  // namespace posix
2095
2096// MSVC "deprecates" snprintf and issues warnings wherever it is used.  In
2097// order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2098// MSVC-based platforms.  We map the GTEST_SNPRINTF_ macro to the appropriate
2099// function in order to achieve that.  We use macro definition here because
2100// snprintf is a variadic function.
2101#if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
2102// MSVC 2005 and above support variadic macros.
2103# define GTEST_SNPRINTF_(buffer, size, format, ...) \
2104     _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2105#elif defined(_MSC_VER)
2106// Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't
2107// complain about _snprintf.
2108# define GTEST_SNPRINTF_ _snprintf
2109#else
2110# define GTEST_SNPRINTF_ snprintf
2111#endif
2112
2113// The maximum number a BiggestInt can represent.  This definition
2114// works no matter BiggestInt is represented in one's complement or
2115// two's complement.
2116//
2117// We cannot rely on numeric_limits in STL, as __int64 and long long
2118// are not part of standard C++ and numeric_limits doesn't need to be
2119// defined for them.
2120const BiggestInt kMaxBiggestInt =
2121    ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
2122
2123// This template class serves as a compile-time function from size to
2124// type.  It maps a size in bytes to a primitive type with that
2125// size. e.g.
2126//
2127//   TypeWithSize<4>::UInt
2128//
2129// is typedef-ed to be unsigned int (unsigned integer made up of 4
2130// bytes).
2131//
2132// Such functionality should belong to STL, but I cannot find it
2133// there.
2134//
2135// Google Test uses this class in the implementation of floating-point
2136// comparison.
2137//
2138// For now it only handles UInt (unsigned int) as that's all Google Test
2139// needs.  Other types can be easily added in the future if need
2140// arises.
2141template <size_t size>
2142class TypeWithSize {
2143 public:
2144  // This prevents the user from using TypeWithSize<N> with incorrect
2145  // values of N.
2146  typedef void UInt;
2147};
2148
2149// The specialization for size 4.
2150template <>
2151class TypeWithSize<4> {
2152 public:
2153  // unsigned int has size 4 in both gcc and MSVC.
2154  //
2155  // As base/basictypes.h doesn't compile on Windows, we cannot use
2156  // uint32, uint64, and etc here.
2157  typedef int Int;
2158  typedef unsigned int UInt;
2159};
2160
2161// The specialization for size 8.
2162template <>
2163class TypeWithSize<8> {
2164 public:
2165#if GTEST_OS_WINDOWS
2166  typedef __int64 Int;
2167  typedef unsigned __int64 UInt;
2168#else
2169  typedef long long Int;  // NOLINT
2170  typedef unsigned long long UInt;  // NOLINT
2171#endif  // GTEST_OS_WINDOWS
2172};
2173
2174// Integer types of known sizes.
2175typedef TypeWithSize<4>::Int Int32;
2176typedef TypeWithSize<4>::UInt UInt32;
2177typedef TypeWithSize<8>::Int Int64;
2178typedef TypeWithSize<8>::UInt UInt64;
2179typedef TypeWithSize<8>::Int TimeInMillis;  // Represents time in milliseconds.
2180
2181// Utilities for command line flags and environment variables.
2182
2183// Macro for referencing flags.
2184#define GTEST_FLAG(name) FLAGS_gtest_##name
2185
2186// Macros for declaring flags.
2187#define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2188#define GTEST_DECLARE_int32_(name) \
2189    GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
2190#define GTEST_DECLARE_string_(name) \
2191    GTEST_API_ extern ::std::string GTEST_FLAG(name)
2192
2193// Macros for defining flags.
2194#define GTEST_DEFINE_bool_(name, default_val, doc) \
2195    GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2196#define GTEST_DEFINE_int32_(name, default_val, doc) \
2197    GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
2198#define GTEST_DEFINE_string_(name, default_val, doc) \
2199    GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2200
2201// Thread annotations
2202#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2203#define GTEST_LOCK_EXCLUDED_(locks)
2204
2205// Parses 'str' for a 32-bit signed integer.  If successful, writes the result
2206// to *value and returns true; otherwise leaves *value unchanged and returns
2207// false.
2208// TODO(chandlerc): Find a better way to refactor flag and environment parsing
2209// out of both gtest-port.cc and gtest.cc to avoid exporting this utility
2210// function.
2211bool ParseInt32(const Message& src_text, const char* str, Int32* value);
2212
2213// Parses a bool/Int32/string from the environment variable
2214// corresponding to the given Google Test flag.
2215bool BoolFromGTestEnv(const char* flag, bool default_val);
2216GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
2217const char* StringFromGTestEnv(const char* flag, const char* default_val);
2218
2219}  // namespace internal
2220}  // namespace testing
2221
2222#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
2223