gtest-port.h revision e96d247b20116646f343b6e2ec37af154f655977
176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Copyright 2005, Google Inc.
276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// All rights reserved.
376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Redistribution and use in source and binary forms, with or without
576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// modification, are permitted provided that the following conditions are
676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// met:
776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     * Redistributions of source code must retain the above copyright
976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// notice, this list of conditions and the following disclaimer.
1076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     * Redistributions in binary form must reproduce the above
1176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// copyright notice, this list of conditions and the following disclaimer
1276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// in the documentation and/or other materials provided with the
1376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// distribution.
1476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     * Neither the name of Google Inc. nor the names of its
1576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// contributors may be used to endorse or promote products derived from
1676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// this software without specific prior written permission.
1776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
1876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
3076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Authors: wan@google.com (Zhanyong Wan)
3176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
3276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Low-level types and utilities for porting Google Test to various
3376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// platforms.  They are subject to change without notice.  DO NOT USE
3476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// THEM IN USER CODE.
3576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
3676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
3776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
3876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
3976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user can define the following macros in the build script to
4076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// control Google Test's behavior.  If the user doesn't define a macro
4176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// in this list, Google Test will define it.
4276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
4376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
4476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              is/isn't available.
4576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
4676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              are enabled.
4776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_GLOBAL_STRING  - Define it to 1/0 to indicate that ::string
4876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              is/isn't available (some systems define
4976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              ::string, which is different to std::string).
5076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
5176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              is/isn't available (some systems define
5276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              ::wstring, which is different to std::wstring).
5376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
5476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              expressions are/aren't available.
5576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
5676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              is/isn't available.
5776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
5876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              enabled.
5976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
6076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              std::wstring does/doesn't work (Google Test can
6176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              be used where std::wstring is unavailable).
6276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_TR1_TUPLE      - Define it to 1/0 to indicate tr1::tuple
6376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              is/isn't available.
6476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
6576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              compiler supports Microsoft's "Structured
6676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              Exception Handling".
6776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_USE_OWN_TR1_TUPLE  - Define it to 1/0 to indicate whether Google
6876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              Test's own tr1 tuple implementation should be
6976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              used.  Unused when the user sets
7076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              GTEST_HAS_TR1_TUPLE to 0.
7176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_LINKED_AS_SHARED_LIBRARY
7276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            - Define to 1 when compiling tests that use
7376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              Google Test as a shared library (known as
7476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              DLL on Windows).
7576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_CREATE_SHARED_LIBRARY
7676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            - Define to 1 when compiling Google Test itself
7776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              as a shared library.
7876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
7976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// This header defines the following utilities:
8076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
8176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Macros indicating the current platform (defined to 1 if compiled on
8276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// the given platform; otherwise undefined):
8376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_AIX      - IBM AIX
8476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_CYGWIN   - Cygwin
8576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_LINUX    - Linux
8676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_MAC      - Mac OS X
8776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_SOLARIS  - Sun Solaris
8876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_SYMBIAN  - Symbian
8976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
9076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
9176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     GTEST_OS_WINDOWS_MINGW    - MinGW
9276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
9376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_OS_ZOS      - z/OS
9476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
9576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
9676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// most stable support.  Since core members of the Google Test project
9776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// don't have access to other platforms, support for them may be less
9876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// stable.  If you notice any problems on your platform, please notify
9976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// googletestframework@googlegroups.com (patches for fixing them are
10076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// even more welcome!).
10176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
10276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Note that it is possible that none of the GTEST_OS_* macros are defined.
10376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
10476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Macros indicating available Google Test features (defined to 1 if
10576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// the corresponding feature is supported; otherwise undefined):
10676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_COMBINE      - the Combine() function (for value-parameterized
10776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            tests)
10876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_DEATH_TEST   - death tests
10976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_PARAM_TEST   - value-parameterized tests
11076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_TYPED_TEST   - typed tests
11176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
11276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
11376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            GTEST_HAS_POSIX_RE (see above) which users can
11476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            define themselves.
11576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
11676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                            the above two are mutually exclusive.
11776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
11876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
11976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Macros for basic C++ coding:
12076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
12176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a
12276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                              variable don't have to be used.
12376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_DISALLOW_ASSIGN_   - disables operator=.
12476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
12576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.
12676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
12776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Synchronization:
12876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   Mutex, MutexLock, ThreadLocal, GetThreadCount()
12976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                  - synchronization primitives.
13076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
13176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                         synchronization primitives have real implementations
13276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                         and Google Test is thread-safe; or 0 otherwise.
13376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
13476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Template meta programming:
13576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   is_pointer     - as in TR1; needed on Symbian and IBM XL C/C++ only.
13676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
13776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Smart pointers:
13876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   scoped_ptr     - as in TR2.
13976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
14076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Regular expressions:
14176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   RE             - a simple regular expression class using the POSIX
14276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                    Extended Regular Expression syntax.  Not available on
14376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                    Windows.
14476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
14576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Logging:
14676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_LOG_()   - logs messages at the specified severity level.
14776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   LogToStderr()  - directs all log messages to stderr.
14876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   FlushInfoLog() - flushes informational log messages.
14976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
15076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Stdout and stderr capturing:
15176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   CaptureStdout()     - starts capturing stdout.
15276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GetCapturedStdout() - stops capturing stdout and returns the captured
15376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                         string.
15476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   CaptureStderr()     - starts capturing stderr.
15576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GetCapturedStderr() - stops capturing stderr and returns the captured
15676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                         string.
15776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
15876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Integer types:
15976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   TypeWithSize   - maps an integer to a int type.
16076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   Int32, UInt32, Int64, UInt64, TimeInMillis
16176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//                  - integers of known sizes.
16276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   BiggestInt     - the biggest signed integer type.
16376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
16476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Command-line utilities:
16576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_FLAG()       - references a flag.
16676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_DECLARE_*()  - declares a flag.
16776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GTEST_DEFINE_*()   - defines a flag.
16876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GetArgvs()         - returns the command line as a vector of strings.
16976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//
17076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Environment variable utilities:
17176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   GetEnv()             - gets the value of an environment variable.
17276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   BoolFromGTestEnv()   - parses a bool environment variable.
17376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   Int32FromGTestEnv()  - parses an Int32 environment variable.
17476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   StringFromGTestEnv() - parses a string environment variable.
17576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
17676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stddef.h>  // For ptrdiff_t
17776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdlib.h>
17876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdio.h>
17976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <string.h>
18076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef _WIN32_WCE
18176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <sys/types.h>
18276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <sys/stat.h>
18376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // !_WIN32_WCE
18476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
18576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <iostream>  // NOLINT
18676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <sstream>  // NOLINT
18776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <string>  // NOLINT
18876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
18976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
19076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_FLAG_PREFIX_ "gtest_"
19176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_FLAG_PREFIX_DASH_ "gtest-"
19276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
19376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_NAME_ "Google Test"
19476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
19576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
19676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Determines the version of gcc that is used to compile this.
19776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef __GNUC__
19876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// 40302 means version 4.3.2.
19976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_GCC_VER_ \
20076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
20176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // __GNUC__
20276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
20376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Determines the platform on which Google Test is compiled.
20476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef __CYGWIN__
20576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_CYGWIN 1
20676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined __SYMBIAN32__
20776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_SYMBIAN 1
20876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined _WIN32
20976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_WINDOWS 1
21076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef _WIN32_WCE
21176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_WINDOWS_MOBILE 1
21276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__MINGW__) || defined(__MINGW32__)
21376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_WINDOWS_MINGW 1
21476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
21576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_WINDOWS_DESKTOP 1
21676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // _WIN32_WCE
21776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined __APPLE__
21876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_MAC 1
21976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined __linux__
22076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_LINUX 1
22176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined __MVS__
22276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_ZOS 1
22376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__sun) && defined(__SVR4)
22476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_SOLARIS 1
22576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(_AIX)
22676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_OS_AIX 1
22776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // __CYGWIN__
22876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
22976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Brings in definitions for functions used in the testing::internal::posix
23076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// namespace (read, write, close, chdir, isatty, stat). We do not currently
23176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// use them on Windows Mobile.
23276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#if !GTEST_OS_WINDOWS
23376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// This assumes that non-Windows OSes provide unistd.h. For OSes where this
23476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// is not the case, we need to include headers that provide the functions
23576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// mentioned above.
23676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <unistd.h>
23776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <strings.h>
23876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif !GTEST_OS_WINDOWS_MOBILE
23976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <direct.h>
24076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <io.h>
24176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif
24276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
24376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Defines this to true iff Google Test can use POSIX regular expressions.
24476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_POSIX_RE
24576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
24676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif
24776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
24876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#if GTEST_HAS_POSIX_RE
24976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// On some platforms, <regex.h> needs someone to define size_t, and
25176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// won't compile otherwise.  We can #include it here as we already
25276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// included <stdlib.h>, which is guaranteed to define size_t through
25376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// <stddef.h>.
25476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <regex.h>  // NOLINT
25576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_USES_POSIX_RE 1
25776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif GTEST_OS_WINDOWS
25976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
26076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// <regex.h> is not available on Windows.  Use our own simple regex
26176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// implementation instead.
26276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_USES_SIMPLE_RE 1
26376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
26476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
26576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
26676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// <regex.h> may not be available on this platform.  Use our own
26776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// simple regex implementation instead.
26876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_USES_SIMPLE_RE 1
26976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
27076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // GTEST_HAS_POSIX_RE
27176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
27276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_EXCEPTIONS
27376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user didn't tell us whether exceptions are enabled, so we need
27476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// to figure it out.
27576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#if defined(_MSC_VER) || defined(__BORLANDC__)
27676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
27776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// macro to enable exceptions, so we'll do the same.
27876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Assumes that exceptions are enabled by default.
27976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef _HAS_EXCEPTIONS
28076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define _HAS_EXCEPTIONS 1
28176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // _HAS_EXCEPTIONS
28276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
28376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__GNUC__) && __EXCEPTIONS
28476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
28576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_EXCEPTIONS 1
28676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__SUNPRO_CC)
28776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Sun Pro CC supports exceptions.  However, there is no compile-time way of
28876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// detecting whether they are enabled or not.  Therefore, we assume that
28976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// they are enabled unless the user tells us otherwise.
29076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_EXCEPTIONS 1
29176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__IBMCPP__) && __EXCEPTIONS
29276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
29376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_EXCEPTIONS 1
29476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
29576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// For other compilers, we assume exceptions are disabled to be
29676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// conservative.
29776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_EXCEPTIONS 0
29876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // defined(_MSC_VER) || defined(__BORLANDC__)
29976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // GTEST_HAS_EXCEPTIONS
30076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
30176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#if !defined(GTEST_HAS_STD_STRING)
30276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Even though we don't use this macro any longer, we keep it in case
30376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// some clients still depend on it.
30476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_STD_STRING 1
30576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif !GTEST_HAS_STD_STRING
30676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user told us that ::std::string isn't available.
30776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#error "Google Test cannot be used where ::std::string isn't available."
30876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // !defined(GTEST_HAS_STD_STRING)
30976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_GLOBAL_STRING
31176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user didn't tell us whether ::string is available, so we need
31276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// to figure it out.
31376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_GLOBAL_STRING 0
31576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // GTEST_HAS_GLOBAL_STRING
31776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_STD_WSTRING
31976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user didn't tell us whether ::std::wstring is available, so we need
32076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// to figure it out.
32176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
32276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman//   is available.
32376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
32476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Cygwin 1.7 and below doesn't support ::std::wstring.
32576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Solaris' libc++ doesn't support it either.
32676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_STD_WSTRING (!(GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
32776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
32876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // GTEST_HAS_STD_WSTRING
32976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
33076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_GLOBAL_WSTRING
33176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user didn't tell us whether ::wstring is available, so we need
33276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// to figure it out.
33376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_GLOBAL_WSTRING \
33476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
33576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // GTEST_HAS_GLOBAL_WSTRING
33676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
33776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Determines whether RTTI is available.
33876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifndef GTEST_HAS_RTTI
33976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// The user didn't tell us whether RTTI is enabled, so we need to
34076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// figure it out.
34176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
34276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef _MSC_VER
34376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
34476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef _CPPRTTI  // MSVC defines this macro iff RTTI is enabled.
34576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_RTTI 1
34676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
34776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_RTTI 0
34876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif
34976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
35076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
35176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
35276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
35376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#ifdef __GXX_RTTI
35476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_RTTI 1
35576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
35676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#define GTEST_HAS_RTTI 0
35776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif  // __GXX_RTTI
35876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
35976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
360// both the typeid and dynamic_cast features are present.
361#elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
362
363#ifdef __RTTI_ALL__
364#define GTEST_HAS_RTTI 1
365#else
366#define GTEST_HAS_RTTI 0
367#endif
368
369#else
370
371// For all other compilers, we assume RTTI is enabled.
372#define GTEST_HAS_RTTI 1
373
374#endif  // _MSC_VER
375
376#endif  // GTEST_HAS_RTTI
377
378// It's this header's responsibility to #include <typeinfo> when RTTI
379// is enabled.
380#if GTEST_HAS_RTTI
381#include <typeinfo>
382#endif
383
384// Determines whether Google Test can use the pthreads library.
385#ifndef GTEST_HAS_PTHREAD
386// The user didn't tell us explicitly, so we assume pthreads support is
387// available on Linux and Mac.
388//
389// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
390// to your compiler flags.
391#define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC)
392#endif  // GTEST_HAS_PTHREAD
393
394#if GTEST_HAS_PTHREAD
395// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
396// true.
397#include <pthread.h>  // NOLINT
398
399// For timespec and nanosleep, used below.
400#include <time.h>  // NOLINT
401#endif
402
403// Determines whether Google Test can use tr1/tuple.  You can define
404// this macro to 0 to prevent Google Test from using tuple (any
405// feature depending on tuple with be disabled in this mode).
406#ifndef GTEST_HAS_TR1_TUPLE
407// The user didn't tell us not to do it, so we assume it's OK.
408#define GTEST_HAS_TR1_TUPLE 1
409#endif  // GTEST_HAS_TR1_TUPLE
410
411// Determines whether Google Test's own tr1 tuple implementation
412// should be used.
413#ifndef GTEST_USE_OWN_TR1_TUPLE
414// The user didn't tell us, so we need to figure it out.
415
416// We use our own TR1 tuple if we aren't sure the user has an
417// implementation of it already.  At this time, GCC 4.0.0+ and MSVC
418// 2010 are the only mainstream compilers that come with a TR1 tuple
419// implementation.  NVIDIA's CUDA NVCC compiler pretends to be GCC by
420// defining __GNUC__ and friends, but cannot compile GCC's tuple
421// implementation.  MSVC 2008 (9.0) provides TR1 tuple in a 323 MB
422// Feature Pack download, which we cannot assume the user has.
423#if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \
424    || _MSC_VER >= 1600
425#define GTEST_USE_OWN_TR1_TUPLE 0
426#else
427#define GTEST_USE_OWN_TR1_TUPLE 1
428#endif
429
430#endif  // GTEST_USE_OWN_TR1_TUPLE
431
432// To avoid conditional compilation everywhere, we make it
433// gtest-port.h's responsibility to #include the header implementing
434// tr1/tuple.
435#if GTEST_HAS_TR1_TUPLE
436
437#if GTEST_USE_OWN_TR1_TUPLE
438#include <gtest/internal/gtest-tuple.h>
439#elif GTEST_OS_SYMBIAN
440
441// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
442// use STLport's tuple implementation, which unfortunately doesn't
443// work as the copy of STLport distributed with Symbian is incomplete.
444// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
445// use its own tuple implementation.
446#ifdef BOOST_HAS_TR1_TUPLE
447#undef BOOST_HAS_TR1_TUPLE
448#endif  // BOOST_HAS_TR1_TUPLE
449
450// This prevents <boost/tr1/detail/config.hpp>, which defines
451// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
452#define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
453#include <tuple>
454
455#elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
456// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header.  This does
457// not conform to the TR1 spec, which requires the header to be <tuple>.
458
459#if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
460// Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
461// which is #included by <tr1/tuple>, to not compile when RTTI is
462// disabled.  _TR1_FUNCTIONAL is the header guard for
463// <tr1/functional>.  Hence the following #define is a hack to prevent
464// <tr1/functional> from being included.
465#define _TR1_FUNCTIONAL 1
466#include <tr1/tuple>
467#undef _TR1_FUNCTIONAL  // Allows the user to #include
468                        // <tr1/functional> if he chooses to.
469#else
470#include <tr1/tuple>  // NOLINT
471#endif  // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
472
473#else
474// If the compiler is not GCC 4.0+, we assume the user is using a
475// spec-conforming TR1 implementation.
476#include <tuple>  // NOLINT
477#endif  // GTEST_USE_OWN_TR1_TUPLE
478
479#endif  // GTEST_HAS_TR1_TUPLE
480
481// Determines whether clone(2) is supported.
482// Usually it will only be available on Linux, excluding
483// Linux on the Itanium architecture.
484// Also see http://linux.die.net/man/2/clone.
485#ifndef GTEST_HAS_CLONE
486// The user didn't tell us, so we need to figure it out.
487
488#if GTEST_OS_LINUX && !defined(__ia64__)
489#define GTEST_HAS_CLONE 1
490#else
491#define GTEST_HAS_CLONE 0
492#endif  // GTEST_OS_LINUX && !defined(__ia64__)
493
494#endif  // GTEST_HAS_CLONE
495
496// Determines whether to support stream redirection. This is used to test
497// output correctness and to implement death tests.
498#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
499#define GTEST_HAS_STREAM_REDIRECTION_ 1
500#endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
501
502// Determines whether to support death tests.
503// Google Test does not support death tests for VC 7.1 and earlier as
504// abort() in a VC 7.1 application compiled as GUI in debug config
505// pops up a dialog window that cannot be suppressed programmatically.
506#if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
507     (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
508     GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX)
509#define GTEST_HAS_DEATH_TEST 1
510#include <vector>  // NOLINT
511#endif
512
513// We don't support MSVC 7.1 with exceptions disabled now.  Therefore
514// all the compilers we care about are adequate for supporting
515// value-parameterized tests.
516#define GTEST_HAS_PARAM_TEST 1
517
518// Determines whether to support type-driven tests.
519
520// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
521// Sun Pro CC, and IBM Visual Age support.
522#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
523    defined(__IBMCPP__)
524#define GTEST_HAS_TYPED_TEST 1
525#define GTEST_HAS_TYPED_TEST_P 1
526#endif
527
528// Determines whether to support Combine(). This only makes sense when
529// value-parameterized tests are enabled.  The implementation doesn't
530// work on Sun Studio since it doesn't understand templated conversion
531// operators.
532#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
533#define GTEST_HAS_COMBINE 1
534#endif
535
536// Determines whether the system compiler uses UTF-16 for encoding wide strings.
537#define GTEST_WIDE_STRING_USES_UTF16_ \
538    (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
539
540// Defines some utility macros.
541
542// The GNU compiler emits a warning if nested "if" statements are followed by
543// an "else" statement and braces are not used to explicitly disambiguate the
544// "else" binding.  This leads to problems with code like:
545//
546//   if (gate)
547//     ASSERT_*(condition) << "Some message";
548//
549// The "switch (0) case 0:" idiom is used to suppress this.
550#ifdef __INTEL_COMPILER
551#define GTEST_AMBIGUOUS_ELSE_BLOCKER_
552#else
553#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:  // NOLINT
554#endif
555
556// Use this annotation at the end of a struct/class definition to
557// prevent the compiler from optimizing away instances that are never
558// used.  This is useful when all interesting logic happens inside the
559// c'tor and / or d'tor.  Example:
560//
561//   struct Foo {
562//     Foo() { ... }
563//   } GTEST_ATTRIBUTE_UNUSED_;
564//
565// Also use it after a variable or parameter declaration to tell the
566// compiler the variable/parameter does not have to be used.
567#if defined(__GNUC__) && !defined(COMPILER_ICC)
568#define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
569#else
570#define GTEST_ATTRIBUTE_UNUSED_
571#endif
572
573// A macro to disallow operator=
574// This should be used in the private: declarations for a class.
575#define GTEST_DISALLOW_ASSIGN_(type)\
576  void operator=(type const &)
577
578// A macro to disallow copy constructor and operator=
579// This should be used in the private: declarations for a class.
580#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
581  type(type const &);\
582  GTEST_DISALLOW_ASSIGN_(type)
583
584// Tell the compiler to warn about unused return values for functions declared
585// with this macro.  The macro should be used on function declarations
586// following the argument list:
587//
588//   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
589#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
590#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
591#else
592#define GTEST_MUST_USE_RESULT_
593#endif  // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
594
595// Determine whether the compiler supports Microsoft's Structured Exception
596// Handling.  This is supported by several Windows compilers but generally
597// does not exist on any other system.
598#ifndef GTEST_HAS_SEH
599// The user didn't tell us, so we need to figure it out.
600
601#if defined(_MSC_VER) || defined(__BORLANDC__)
602// These two compilers are known to support SEH.
603#define GTEST_HAS_SEH 1
604#else
605// Assume no SEH.
606#define GTEST_HAS_SEH 0
607#endif
608
609#endif  // GTEST_HAS_SEH
610
611#ifdef _MSC_VER
612
613#if GTEST_LINKED_AS_SHARED_LIBRARY
614#define GTEST_API_ __declspec(dllimport)
615#elif GTEST_CREATE_SHARED_LIBRARY
616#define GTEST_API_ __declspec(dllexport)
617#endif
618
619#endif  // _MSC_VER
620
621#ifndef GTEST_API_
622#define GTEST_API_
623#endif
624
625namespace testing {
626
627class Message;
628
629namespace internal {
630
631class String;
632
633// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
634// expression is true. For example, you could use it to verify the
635// size of a static array:
636//
637//   GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
638//                         content_type_names_incorrect_size);
639//
640// or to make sure a struct is smaller than a certain size:
641//
642//   GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
643//
644// The second argument to the macro is the name of the variable. If
645// the expression is false, most compilers will issue a warning/error
646// containing the name of the variable.
647
648template <bool>
649struct CompileAssert {
650};
651
652#define GTEST_COMPILE_ASSERT_(expr, msg) \
653  typedef ::testing::internal::CompileAssert<(bool(expr))> \
654      msg[bool(expr) ? 1 : -1]
655
656// Implementation details of GTEST_COMPILE_ASSERT_:
657//
658// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
659//   elements (and thus is invalid) when the expression is false.
660//
661// - The simpler definition
662//
663//    #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
664//
665//   does not work, as gcc supports variable-length arrays whose sizes
666//   are determined at run-time (this is gcc's extension and not part
667//   of the C++ standard).  As a result, gcc fails to reject the
668//   following code with the simple definition:
669//
670//     int foo;
671//     GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
672//                                      // not a compile-time constant.
673//
674// - By using the type CompileAssert<(bool(expr))>, we ensures that
675//   expr is a compile-time constant.  (Template arguments must be
676//   determined at compile-time.)
677//
678// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
679//   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written
680//
681//     CompileAssert<bool(expr)>
682//
683//   instead, these compilers will refuse to compile
684//
685//     GTEST_COMPILE_ASSERT_(5 > 0, some_message);
686//
687//   (They seem to think the ">" in "5 > 0" marks the end of the
688//   template argument list.)
689//
690// - The array size is (bool(expr) ? 1 : -1), instead of simply
691//
692//     ((expr) ? 1 : -1).
693//
694//   This is to avoid running into a bug in MS VC 7.1, which
695//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
696
697// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
698//
699// This template is declared, but intentionally undefined.
700template <typename T1, typename T2>
701struct StaticAssertTypeEqHelper;
702
703template <typename T>
704struct StaticAssertTypeEqHelper<T, T> {};
705
706#if GTEST_HAS_GLOBAL_STRING
707typedef ::string string;
708#else
709typedef ::std::string string;
710#endif  // GTEST_HAS_GLOBAL_STRING
711
712#if GTEST_HAS_GLOBAL_WSTRING
713typedef ::wstring wstring;
714#elif GTEST_HAS_STD_WSTRING
715typedef ::std::wstring wstring;
716#endif  // GTEST_HAS_GLOBAL_WSTRING
717
718typedef ::std::stringstream StrStream;
719
720// A helper for suppressing warnings on constant condition.  It just
721// returns 'condition'.
722GTEST_API_ bool IsTrue(bool condition);
723
724// Defines scoped_ptr.
725
726// This implementation of scoped_ptr is PARTIAL - it only contains
727// enough stuff to satisfy Google Test's need.
728template <typename T>
729class scoped_ptr {
730 public:
731  typedef T element_type;
732
733  explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
734  ~scoped_ptr() { reset(); }
735
736  T& operator*() const { return *ptr_; }
737  T* operator->() const { return ptr_; }
738  T* get() const { return ptr_; }
739
740  T* release() {
741    T* const ptr = ptr_;
742    ptr_ = NULL;
743    return ptr;
744  }
745
746  void reset(T* p = NULL) {
747    if (p != ptr_) {
748      if (IsTrue(sizeof(T) > 0)) {  // Makes sure T is a complete type.
749        delete ptr_;
750      }
751      ptr_ = p;
752    }
753  }
754 private:
755  T* ptr_;
756
757  GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
758};
759
760// Defines RE.
761
762// A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
763// Regular Expression syntax.
764class GTEST_API_ RE {
765 public:
766  // A copy constructor is required by the Standard to initialize object
767  // references from r-values.
768  RE(const RE& other) { Init(other.pattern()); }
769
770  // Constructs an RE from a string.
771  RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
772
773#if GTEST_HAS_GLOBAL_STRING
774  RE(const ::string& regex) { Init(regex.c_str()); }  // NOLINT
775#endif  // GTEST_HAS_GLOBAL_STRING
776
777  RE(const char* regex) { Init(regex); }  // NOLINT
778  ~RE();
779
780  // Returns the string representation of the regex.
781  const char* pattern() const { return pattern_; }
782
783  // FullMatch(str, re) returns true iff regular expression re matches
784  // the entire str.
785  // PartialMatch(str, re) returns true iff regular expression re
786  // matches a substring of str (including str itself).
787  //
788  // TODO(wan@google.com): make FullMatch() and PartialMatch() work
789  // when str contains NUL characters.
790  static bool FullMatch(const ::std::string& str, const RE& re) {
791    return FullMatch(str.c_str(), re);
792  }
793  static bool PartialMatch(const ::std::string& str, const RE& re) {
794    return PartialMatch(str.c_str(), re);
795  }
796
797#if GTEST_HAS_GLOBAL_STRING
798  static bool FullMatch(const ::string& str, const RE& re) {
799    return FullMatch(str.c_str(), re);
800  }
801  static bool PartialMatch(const ::string& str, const RE& re) {
802    return PartialMatch(str.c_str(), re);
803  }
804#endif  // GTEST_HAS_GLOBAL_STRING
805
806  static bool FullMatch(const char* str, const RE& re);
807  static bool PartialMatch(const char* str, const RE& re);
808
809 private:
810  void Init(const char* regex);
811
812  // We use a const char* instead of a string, as Google Test may be used
813  // where string is not available.  We also do not use Google Test's own
814  // String type here, in order to simplify dependencies between the
815  // files.
816  const char* pattern_;
817  bool is_valid_;
818#if GTEST_USES_POSIX_RE
819  regex_t full_regex_;     // For FullMatch().
820  regex_t partial_regex_;  // For PartialMatch().
821#else  // GTEST_USES_SIMPLE_RE
822  const char* full_pattern_;  // For FullMatch();
823#endif
824
825  GTEST_DISALLOW_ASSIGN_(RE);
826};
827
828// Defines logging utilities:
829//   GTEST_LOG_(severity) - logs messages at the specified severity level. The
830//                          message itself is streamed into the macro.
831//   LogToStderr()  - directs all log messages to stderr.
832//   FlushInfoLog() - flushes informational log messages.
833
834enum GTestLogSeverity {
835  GTEST_INFO,
836  GTEST_WARNING,
837  GTEST_ERROR,
838  GTEST_FATAL
839};
840
841// Formats log entry severity, provides a stream object for streaming the
842// log message, and terminates the message with a newline when going out of
843// scope.
844class GTEST_API_ GTestLog {
845 public:
846  GTestLog(GTestLogSeverity severity, const char* file, int line);
847
848  // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
849  ~GTestLog();
850
851  ::std::ostream& GetStream() { return ::std::cerr; }
852
853 private:
854  const GTestLogSeverity severity_;
855
856  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
857};
858
859#define GTEST_LOG_(severity) \
860    ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
861                                  __FILE__, __LINE__).GetStream()
862
863inline void LogToStderr() {}
864inline void FlushInfoLog() { fflush(NULL); }
865
866// INTERNAL IMPLEMENTATION - DO NOT USE.
867//
868// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
869// is not satisfied.
870//  Synopsys:
871//    GTEST_CHECK_(boolean_condition);
872//     or
873//    GTEST_CHECK_(boolean_condition) << "Additional message";
874//
875//    This checks the condition and if the condition is not satisfied
876//    it prints message about the condition violation, including the
877//    condition itself, plus additional message streamed into it, if any,
878//    and then it aborts the program. It aborts the program irrespective of
879//    whether it is built in the debug mode or not.
880#define GTEST_CHECK_(condition) \
881    GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
882    if (::testing::internal::IsTrue(condition)) \
883      ; \
884    else \
885      GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
886
887// An all-mode assert to verify that the given POSIX-style function
888// call returns 0 (indicating success).  Known limitation: this
889// doesn't expand to a balanced 'if' statement, so enclose the macro
890// in {} if you need to use it as the only statement in an 'if'
891// branch.
892#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
893  if (const int gtest_error = (posix_call)) \
894    GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
895                      << gtest_error
896
897// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
898//
899// Use implicit_cast as a safe version of static_cast for upcasting in
900// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
901// const Foo*).  When you use implicit_cast, the compiler checks that
902// the cast is safe.  Such explicit implicit_casts are necessary in
903// surprisingly many situations where C++ demands an exact type match
904// instead of an argument type convertable to a target type.
905//
906// The syntax for using implicit_cast is the same as for static_cast:
907//
908//   implicit_cast<ToType>(expr)
909//
910// implicit_cast would have been part of the C++ standard library,
911// but the proposal was submitted too late.  It will probably make
912// its way into the language in the future.
913template<typename To>
914inline To implicit_cast(To x) { return x; }
915
916// When you upcast (that is, cast a pointer from type Foo to type
917// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
918// always succeed.  When you downcast (that is, cast a pointer from
919// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
920// how do you know the pointer is really of type SubclassOfFoo?  It
921// could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,
922// when you downcast, you should use this macro.  In debug mode, we
923// use dynamic_cast<> to double-check the downcast is legal (we die
924// if it's not).  In normal mode, we do the efficient static_cast<>
925// instead.  Thus, it's important to test in debug mode to make sure
926// the cast is legal!
927//    This is the only place in the code we should use dynamic_cast<>.
928// In particular, you SHOULDN'T be using dynamic_cast<> in order to
929// do RTTI (eg code like this:
930//    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
931//    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
932// You should design the code some other way not to need this.
933template<typename To, typename From>  // use like this: down_cast<T*>(foo);
934inline To down_cast(From* f) {  // so we only accept pointers
935  // Ensures that To is a sub-type of From *.  This test is here only
936  // for compile-time type checking, and has no overhead in an
937  // optimized build at run-time, as it will be optimized away
938  // completely.
939  if (false) {
940    const To to = NULL;
941    ::testing::internal::implicit_cast<From*>(to);
942  }
943
944#if GTEST_HAS_RTTI
945  // RTTI: debug mode only!
946  GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
947#endif
948  return static_cast<To>(f);
949}
950
951// Downcasts the pointer of type Base to Derived.
952// Derived must be a subclass of Base. The parameter MUST
953// point to a class of type Derived, not any subclass of it.
954// When RTTI is available, the function performs a runtime
955// check to enforce this.
956template <class Derived, class Base>
957Derived* CheckedDowncastToActualType(Base* base) {
958#if GTEST_HAS_RTTI
959  GTEST_CHECK_(typeid(*base) == typeid(Derived));
960  return dynamic_cast<Derived*>(base);  // NOLINT
961#else
962  return static_cast<Derived*>(base);  // Poor man's downcast.
963#endif
964}
965
966#if GTEST_HAS_STREAM_REDIRECTION_
967
968// Defines the stderr capturer:
969//   CaptureStdout     - starts capturing stdout.
970//   GetCapturedStdout - stops capturing stdout and returns the captured string.
971//   CaptureStderr     - starts capturing stderr.
972//   GetCapturedStderr - stops capturing stderr and returns the captured string.
973//
974GTEST_API_ void CaptureStdout();
975GTEST_API_ String GetCapturedStdout();
976GTEST_API_ void CaptureStderr();
977GTEST_API_ String GetCapturedStderr();
978
979#endif  // GTEST_HAS_STREAM_REDIRECTION_
980
981
982#if GTEST_HAS_DEATH_TEST
983
984// A copy of all command line arguments.  Set by InitGoogleTest().
985extern ::std::vector<String> g_argvs;
986
987// GTEST_HAS_DEATH_TEST implies we have ::std::string.
988const ::std::vector<String>& GetArgvs();
989
990#endif  // GTEST_HAS_DEATH_TEST
991
992// Defines synchronization primitives.
993
994#if GTEST_HAS_PTHREAD
995
996// Sleeps for (roughly) n milli-seconds.  This function is only for
997// testing Google Test's own constructs.  Don't use it in user tests,
998// either directly or indirectly.
999inline void SleepMilliseconds(int n) {
1000  const timespec time = {
1001    0,                  // 0 seconds.
1002    n * 1000L * 1000L,  // And n ms.
1003  };
1004  nanosleep(&time, NULL);
1005}
1006
1007// Allows a controller thread to pause execution of newly created
1008// threads until notified.  Instances of this class must be created
1009// and destroyed in the controller thread.
1010//
1011// This class is only for testing Google Test's own constructs. Do not
1012// use it in user tests, either directly or indirectly.
1013class Notification {
1014 public:
1015  Notification() : notified_(false) {}
1016
1017  // Notifies all threads created with this notification to start. Must
1018  // be called from the controller thread.
1019  void Notify() { notified_ = true; }
1020
1021  // Blocks until the controller thread notifies. Must be called from a test
1022  // thread.
1023  void WaitForNotification() {
1024    while(!notified_) {
1025      SleepMilliseconds(10);
1026    }
1027  }
1028
1029 private:
1030  volatile bool notified_;
1031
1032  GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1033};
1034
1035// As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1036// Consequently, it cannot select a correct instantiation of ThreadWithParam
1037// in order to call its Run(). Introducing ThreadWithParamBase as a
1038// non-templated base class for ThreadWithParam allows us to bypass this
1039// problem.
1040class ThreadWithParamBase {
1041 public:
1042  virtual ~ThreadWithParamBase() {}
1043  virtual void Run() = 0;
1044};
1045
1046// pthread_create() accepts a pointer to a function type with the C linkage.
1047// According to the Standard (7.5/1), function types with different linkages
1048// are different even if they are otherwise identical.  Some compilers (for
1049// example, SunStudio) treat them as different types.  Since class methods
1050// cannot be defined with C-linkage we need to define a free C-function to
1051// pass into pthread_create().
1052extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1053  static_cast<ThreadWithParamBase*>(thread)->Run();
1054  return NULL;
1055}
1056
1057// Helper class for testing Google Test's multi-threading constructs.
1058// To use it, write:
1059//
1060//   void ThreadFunc(int param) { /* Do things with param */ }
1061//   Notification thread_can_start;
1062//   ...
1063//   // The thread_can_start parameter is optional; you can supply NULL.
1064//   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1065//   thread_can_start.Notify();
1066//
1067// These classes are only for testing Google Test's own constructs. Do
1068// not use them in user tests, either directly or indirectly.
1069template <typename T>
1070class ThreadWithParam : public ThreadWithParamBase {
1071 public:
1072  typedef void (*UserThreadFunc)(T);
1073
1074  ThreadWithParam(
1075      UserThreadFunc func, T param, Notification* thread_can_start)
1076      : func_(func),
1077        param_(param),
1078        thread_can_start_(thread_can_start),
1079        finished_(false) {
1080    ThreadWithParamBase* const base = this;
1081    // The thread can be created only after all fields except thread_
1082    // have been initialized.
1083    GTEST_CHECK_POSIX_SUCCESS_(
1084        pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
1085  }
1086  ~ThreadWithParam() { Join(); }
1087
1088  void Join() {
1089    if (!finished_) {
1090      GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
1091      finished_ = true;
1092    }
1093  }
1094
1095  virtual void Run() {
1096    if (thread_can_start_ != NULL)
1097      thread_can_start_->WaitForNotification();
1098    func_(param_);
1099  }
1100
1101 private:
1102  const UserThreadFunc func_;  // User-supplied thread function.
1103  const T param_;  // User-supplied parameter to the thread function.
1104  // When non-NULL, used to block execution until the controller thread
1105  // notifies.
1106  Notification* const thread_can_start_;
1107  bool finished_;  // true iff we know that the thread function has finished.
1108  pthread_t thread_;  // The native thread object.
1109
1110  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1111};
1112
1113// MutexBase and Mutex implement mutex on pthreads-based platforms. They
1114// are used in conjunction with class MutexLock:
1115//
1116//   Mutex mutex;
1117//   ...
1118//   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the end
1119//                            // of the current scope.
1120//
1121// MutexBase implements behavior for both statically and dynamically
1122// allocated mutexes.  Do not use MutexBase directly.  Instead, write
1123// the following to define a static mutex:
1124//
1125//   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1126//
1127// You can forward declare a static mutex like this:
1128//
1129//   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1130//
1131// To create a dynamic mutex, just define an object of type Mutex.
1132class MutexBase {
1133 public:
1134  // Acquires this mutex.
1135  void Lock() {
1136    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1137    owner_ = pthread_self();
1138  }
1139
1140  // Releases this mutex.
1141  void Unlock() {
1142    // We don't protect writing to owner_ here, as it's the caller's
1143    // responsibility to ensure that the current thread holds the
1144    // mutex when this is called.
1145    owner_ = 0;
1146    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1147  }
1148
1149  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1150  // with high probability.
1151  void AssertHeld() const {
1152    GTEST_CHECK_(owner_ == pthread_self())
1153        << "The current thread is not holding the mutex @" << this;
1154  }
1155
1156  // A static mutex may be used before main() is entered.  It may even
1157  // be used before the dynamic initialization stage.  Therefore we
1158  // must be able to initialize a static mutex object at link time.
1159  // This means MutexBase has to be a POD and its member variables
1160  // have to be public.
1161 public:
1162  pthread_mutex_t mutex_;  // The underlying pthread mutex.
1163  pthread_t owner_;  // The thread holding the mutex; 0 means no one holds it.
1164};
1165
1166// Forward-declares a static mutex.
1167#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1168    extern ::testing::internal::MutexBase mutex
1169
1170// Defines and statically (i.e. at link time) initializes a static mutex.
1171#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1172    ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
1173
1174// The Mutex class can only be used for mutexes created at runtime. It
1175// shares its API with MutexBase otherwise.
1176class Mutex : public MutexBase {
1177 public:
1178  Mutex() {
1179    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1180    owner_ = 0;
1181  }
1182  ~Mutex() {
1183    GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1184  }
1185
1186 private:
1187  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1188};
1189
1190// We cannot name this class MutexLock as the ctor declaration would
1191// conflict with a macro named MutexLock, which is defined on some
1192// platforms.  Hence the typedef trick below.
1193class GTestMutexLock {
1194 public:
1195  explicit GTestMutexLock(MutexBase* mutex)
1196      : mutex_(mutex) { mutex_->Lock(); }
1197
1198  ~GTestMutexLock() { mutex_->Unlock(); }
1199
1200 private:
1201  MutexBase* const mutex_;
1202
1203  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1204};
1205
1206typedef GTestMutexLock MutexLock;
1207
1208// Helpers for ThreadLocal.
1209
1210// pthread_key_create() requires DeleteThreadLocalValue() to have
1211// C-linkage.  Therefore it cannot be templatized to access
1212// ThreadLocal<T>.  Hence the need for class
1213// ThreadLocalValueHolderBase.
1214class ThreadLocalValueHolderBase {
1215 public:
1216  virtual ~ThreadLocalValueHolderBase() {}
1217};
1218
1219// Called by pthread to delete thread-local data stored by
1220// pthread_setspecific().
1221extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1222  delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1223}
1224
1225// Implements thread-local storage on pthreads-based systems.
1226//
1227//   // Thread 1
1228//   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
1229//
1230//   // Thread 2
1231//   tl.set(150);  // Changes the value for thread 2 only.
1232//   EXPECT_EQ(150, tl.get());
1233//
1234//   // Thread 1
1235//   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
1236//   tl.set(200);
1237//   EXPECT_EQ(200, tl.get());
1238//
1239// The template type argument T must have a public copy constructor.
1240// In addition, the default ThreadLocal constructor requires T to have
1241// a public default constructor.
1242//
1243// An object managed for a thread by a ThreadLocal instance is deleted
1244// when the thread exits.  Or, if the ThreadLocal instance dies in
1245// that thread, when the ThreadLocal dies.  It's the user's
1246// responsibility to ensure that all other threads using a ThreadLocal
1247// have exited when it dies, or the per-thread objects for those
1248// threads will not be deleted.
1249//
1250// Google Test only uses global ThreadLocal objects.  That means they
1251// will die after main() has returned.  Therefore, no per-thread
1252// object managed by Google Test will be leaked as long as all threads
1253// using Google Test have exited when main() returns.
1254template <typename T>
1255class ThreadLocal {
1256 public:
1257  ThreadLocal() : key_(CreateKey()),
1258                  default_() {}
1259  explicit ThreadLocal(const T& value) : key_(CreateKey()),
1260                                         default_(value) {}
1261
1262  ~ThreadLocal() {
1263    // Destroys the managed object for the current thread, if any.
1264    DeleteThreadLocalValue(pthread_getspecific(key_));
1265
1266    // Releases resources associated with the key.  This will *not*
1267    // delete managed objects for other threads.
1268    GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1269  }
1270
1271  T* pointer() { return GetOrCreateValue(); }
1272  const T* pointer() const { return GetOrCreateValue(); }
1273  const T& get() const { return *pointer(); }
1274  void set(const T& value) { *pointer() = value; }
1275
1276 private:
1277  // Holds a value of type T.
1278  class ValueHolder : public ThreadLocalValueHolderBase {
1279   public:
1280    explicit ValueHolder(const T& value) : value_(value) {}
1281
1282    T* pointer() { return &value_; }
1283
1284   private:
1285    T value_;
1286    GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1287  };
1288
1289  static pthread_key_t CreateKey() {
1290    pthread_key_t key;
1291    // When a thread exits, DeleteThreadLocalValue() will be called on
1292    // the object managed for that thread.
1293    GTEST_CHECK_POSIX_SUCCESS_(
1294        pthread_key_create(&key, &DeleteThreadLocalValue));
1295    return key;
1296  }
1297
1298  T* GetOrCreateValue() const {
1299    ThreadLocalValueHolderBase* const holder =
1300        static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1301    if (holder != NULL) {
1302      return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1303    }
1304
1305    ValueHolder* const new_holder = new ValueHolder(default_);
1306    ThreadLocalValueHolderBase* const holder_base = new_holder;
1307    GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1308    return new_holder->pointer();
1309  }
1310
1311  // A key pthreads uses for looking up per-thread values.
1312  const pthread_key_t key_;
1313  const T default_;  // The default value for each thread.
1314
1315  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1316};
1317
1318#define GTEST_IS_THREADSAFE 1
1319
1320#else  // GTEST_HAS_PTHREAD
1321
1322// A dummy implementation of synchronization primitives (mutex, lock,
1323// and thread-local variable).  Necessary for compiling Google Test where
1324// mutex is not supported - using Google Test in multiple threads is not
1325// supported on such platforms.
1326
1327class Mutex {
1328 public:
1329  Mutex() {}
1330  void AssertHeld() const {}
1331};
1332
1333#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1334  extern ::testing::internal::Mutex mutex
1335
1336#define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1337
1338class GTestMutexLock {
1339 public:
1340  explicit GTestMutexLock(Mutex*) {}  // NOLINT
1341};
1342
1343typedef GTestMutexLock MutexLock;
1344
1345template <typename T>
1346class ThreadLocal {
1347 public:
1348  ThreadLocal() : value_() {}
1349  explicit ThreadLocal(const T& value) : value_(value) {}
1350  T* pointer() { return &value_; }
1351  const T* pointer() const { return &value_; }
1352  const T& get() const { return value_; }
1353  void set(const T& value) { value_ = value; }
1354 private:
1355  T value_;
1356};
1357
1358// The above synchronization primitives have dummy implementations.
1359// Therefore Google Test is not thread-safe.
1360#define GTEST_IS_THREADSAFE 0
1361
1362#endif  // GTEST_HAS_PTHREAD
1363
1364// Returns the number of threads running in the process, or 0 to indicate that
1365// we cannot detect it.
1366GTEST_API_ size_t GetThreadCount();
1367
1368// Passing non-POD classes through ellipsis (...) crashes the ARM
1369// compiler and generates a warning in Sun Studio.  The Nokia Symbian
1370// and the IBM XL C/C++ compiler try to instantiate a copy constructor
1371// for objects passed through ellipsis (...), failing for uncopyable
1372// objects.  We define this to ensure that only POD is passed through
1373// ellipsis on these systems.
1374#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
1375// We lose support for NULL detection where the compiler doesn't like
1376// passing non-POD classes through ellipsis (...).
1377#define GTEST_ELLIPSIS_NEEDS_POD_ 1
1378#else
1379#define GTEST_CAN_COMPARE_NULL 1
1380#endif
1381
1382// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
1383// const T& and const T* in a function template.  These compilers
1384// _can_ decide between class template specializations for T and T*,
1385// so a tr1::type_traits-like is_pointer works.
1386#if defined(__SYMBIAN32__) || defined(__IBMCPP__)
1387#define GTEST_NEEDS_IS_POINTER_ 1
1388#endif
1389
1390template <bool bool_value>
1391struct bool_constant {
1392  typedef bool_constant<bool_value> type;
1393  static const bool value = bool_value;
1394};
1395template <bool bool_value> const bool bool_constant<bool_value>::value;
1396
1397typedef bool_constant<false> false_type;
1398typedef bool_constant<true> true_type;
1399
1400template <typename T>
1401struct is_pointer : public false_type {};
1402
1403template <typename T>
1404struct is_pointer<T*> : public true_type {};
1405
1406#if GTEST_OS_WINDOWS
1407#define GTEST_PATH_SEP_ "\\"
1408#define GTEST_HAS_ALT_PATH_SEP_ 1
1409// The biggest signed integer type the compiler supports.
1410typedef __int64 BiggestInt;
1411#else
1412#define GTEST_PATH_SEP_ "/"
1413#define GTEST_HAS_ALT_PATH_SEP_ 0
1414typedef long long BiggestInt;  // NOLINT
1415#endif  // GTEST_OS_WINDOWS
1416
1417// The testing::internal::posix namespace holds wrappers for common
1418// POSIX functions.  These wrappers hide the differences between
1419// Windows/MSVC and POSIX systems.  Since some compilers define these
1420// standard functions as macros, the wrapper cannot have the same name
1421// as the wrapped function.
1422
1423namespace posix {
1424
1425// Functions with a different name on Windows.
1426
1427#if GTEST_OS_WINDOWS
1428
1429typedef struct _stat StatStruct;
1430
1431#ifdef __BORLANDC__
1432inline int IsATTY(int fd) { return isatty(fd); }
1433inline int StrCaseCmp(const char* s1, const char* s2) {
1434  return stricmp(s1, s2);
1435}
1436inline char* StrDup(const char* src) { return strdup(src); }
1437#else  // !__BORLANDC__
1438#if GTEST_OS_WINDOWS_MOBILE
1439inline int IsATTY(int /* fd */) { return 0; }
1440#else
1441inline int IsATTY(int fd) { return _isatty(fd); }
1442#endif  // GTEST_OS_WINDOWS_MOBILE
1443inline int StrCaseCmp(const char* s1, const char* s2) {
1444  return _stricmp(s1, s2);
1445}
1446inline char* StrDup(const char* src) { return _strdup(src); }
1447#endif  // __BORLANDC__
1448
1449#if GTEST_OS_WINDOWS_MOBILE
1450inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
1451// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1452// time and thus not defined there.
1453#else
1454inline int FileNo(FILE* file) { return _fileno(file); }
1455inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
1456inline int RmDir(const char* dir) { return _rmdir(dir); }
1457inline bool IsDir(const StatStruct& st) {
1458  return (_S_IFDIR & st.st_mode) != 0;
1459}
1460#endif  // GTEST_OS_WINDOWS_MOBILE
1461
1462#else
1463
1464typedef struct stat StatStruct;
1465
1466inline int FileNo(FILE* file) { return fileno(file); }
1467inline int IsATTY(int fd) { return isatty(fd); }
1468inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
1469inline int StrCaseCmp(const char* s1, const char* s2) {
1470  return strcasecmp(s1, s2);
1471}
1472inline char* StrDup(const char* src) { return strdup(src); }
1473inline int RmDir(const char* dir) { return rmdir(dir); }
1474inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
1475
1476#endif  // GTEST_OS_WINDOWS
1477
1478// Functions deprecated by MSVC 8.0.
1479
1480#ifdef _MSC_VER
1481// Temporarily disable warning 4996 (deprecated function).
1482#pragma warning(push)
1483#pragma warning(disable:4996)
1484#endif
1485
1486inline const char* StrNCpy(char* dest, const char* src, size_t n) {
1487  return strncpy(dest, src, n);
1488}
1489
1490// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
1491// StrError() aren't needed on Windows CE at this time and thus not
1492// defined there.
1493
1494#if !GTEST_OS_WINDOWS_MOBILE
1495inline int ChDir(const char* dir) { return chdir(dir); }
1496#endif
1497inline FILE* FOpen(const char* path, const char* mode) {
1498  return fopen(path, mode);
1499}
1500#if !GTEST_OS_WINDOWS_MOBILE
1501inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
1502  return freopen(path, mode, stream);
1503}
1504inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
1505#endif
1506inline int FClose(FILE* fp) { return fclose(fp); }
1507#if !GTEST_OS_WINDOWS_MOBILE
1508inline int Read(int fd, void* buf, unsigned int count) {
1509  return static_cast<int>(read(fd, buf, count));
1510}
1511inline int Write(int fd, const void* buf, unsigned int count) {
1512  return static_cast<int>(write(fd, buf, count));
1513}
1514inline int Close(int fd) { return close(fd); }
1515inline const char* StrError(int errnum) { return strerror(errnum); }
1516#endif
1517inline const char* GetEnv(const char* name) {
1518#if GTEST_OS_WINDOWS_MOBILE
1519  // We are on Windows CE, which has no environment variables.
1520  return NULL;
1521#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1522  // Environment variables which we programmatically clear will be set to the
1523  // empty string rather than unset (NULL).  Handle that case.
1524  const char* const env = getenv(name);
1525  return (env != NULL && env[0] != '\0') ? env : NULL;
1526#else
1527  return getenv(name);
1528#endif
1529}
1530
1531#ifdef _MSC_VER
1532#pragma warning(pop)  // Restores the warning state.
1533#endif
1534
1535#if GTEST_OS_WINDOWS_MOBILE
1536// Windows CE has no C library. The abort() function is used in
1537// several places in Google Test. This implementation provides a reasonable
1538// imitation of standard behaviour.
1539void Abort();
1540#else
1541inline void Abort() { abort(); }
1542#endif  // GTEST_OS_WINDOWS_MOBILE
1543
1544}  // namespace posix
1545
1546// The maximum number a BiggestInt can represent.  This definition
1547// works no matter BiggestInt is represented in one's complement or
1548// two's complement.
1549//
1550// We cannot rely on numeric_limits in STL, as __int64 and long long
1551// are not part of standard C++ and numeric_limits doesn't need to be
1552// defined for them.
1553const BiggestInt kMaxBiggestInt =
1554    ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
1555
1556// This template class serves as a compile-time function from size to
1557// type.  It maps a size in bytes to a primitive type with that
1558// size. e.g.
1559//
1560//   TypeWithSize<4>::UInt
1561//
1562// is typedef-ed to be unsigned int (unsigned integer made up of 4
1563// bytes).
1564//
1565// Such functionality should belong to STL, but I cannot find it
1566// there.
1567//
1568// Google Test uses this class in the implementation of floating-point
1569// comparison.
1570//
1571// For now it only handles UInt (unsigned int) as that's all Google Test
1572// needs.  Other types can be easily added in the future if need
1573// arises.
1574template <size_t size>
1575class TypeWithSize {
1576 public:
1577  // This prevents the user from using TypeWithSize<N> with incorrect
1578  // values of N.
1579  typedef void UInt;
1580};
1581
1582// The specialization for size 4.
1583template <>
1584class TypeWithSize<4> {
1585 public:
1586  // unsigned int has size 4 in both gcc and MSVC.
1587  //
1588  // As base/basictypes.h doesn't compile on Windows, we cannot use
1589  // uint32, uint64, and etc here.
1590  typedef int Int;
1591  typedef unsigned int UInt;
1592};
1593
1594// The specialization for size 8.
1595template <>
1596class TypeWithSize<8> {
1597 public:
1598#if GTEST_OS_WINDOWS
1599  typedef __int64 Int;
1600  typedef unsigned __int64 UInt;
1601#else
1602  typedef long long Int;  // NOLINT
1603  typedef unsigned long long UInt;  // NOLINT
1604#endif  // GTEST_OS_WINDOWS
1605};
1606
1607// Integer types of known sizes.
1608typedef TypeWithSize<4>::Int Int32;
1609typedef TypeWithSize<4>::UInt UInt32;
1610typedef TypeWithSize<8>::Int Int64;
1611typedef TypeWithSize<8>::UInt UInt64;
1612typedef TypeWithSize<8>::Int TimeInMillis;  // Represents time in milliseconds.
1613
1614// Utilities for command line flags and environment variables.
1615
1616// Macro for referencing flags.
1617#define GTEST_FLAG(name) FLAGS_gtest_##name
1618
1619// Macros for declaring flags.
1620#define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
1621#define GTEST_DECLARE_int32_(name) \
1622    GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
1623#define GTEST_DECLARE_string_(name) \
1624    GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name)
1625
1626// Macros for defining flags.
1627#define GTEST_DEFINE_bool_(name, default_val, doc) \
1628    GTEST_API_ bool GTEST_FLAG(name) = (default_val)
1629#define GTEST_DEFINE_int32_(name, default_val, doc) \
1630    GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
1631#define GTEST_DEFINE_string_(name, default_val, doc) \
1632    GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val)
1633
1634// Parses 'str' for a 32-bit signed integer.  If successful, writes the result
1635// to *value and returns true; otherwise leaves *value unchanged and returns
1636// false.
1637// TODO(chandlerc): Find a better way to refactor flag and environment parsing
1638// out of both gtest-port.cc and gtest.cc to avoid exporting this utility
1639// function.
1640bool ParseInt32(const Message& src_text, const char* str, Int32* value);
1641
1642// Parses a bool/Int32/string from the environment variable
1643// corresponding to the given Google Test flag.
1644bool BoolFromGTestEnv(const char* flag, bool default_val);
1645GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
1646const char* StringFromGTestEnv(const char* flag, const char* default_val);
1647
1648}  // namespace internal
1649}  // namespace testing
1650
1651#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
1652