1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Copyright 2005, Google Inc. 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// All rights reserved. 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Redistribution and use in source and binary forms, with or without 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// modification, are permitted provided that the following conditions are 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// met: 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// * Redistributions of source code must retain the above copyright 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// notice, this list of conditions and the following disclaimer. 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// * Redistributions in binary form must reproduce the above 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// copyright notice, this list of conditions and the following disclaimer 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in the documentation and/or other materials provided with the 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// distribution. 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// * Neither the name of Google Inc. nor the names of its 15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// contributors may be used to endorse or promote products derived from 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// this software without specific prior written permission. 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Authors: wan@google.com (Zhanyong Wan) 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Low-level types and utilities for porting Google Test to various 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// platforms. They are subject to change without notice. DO NOT USE 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// THEM IN USER CODE. 35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user can define the following macros in the build script to 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// control Google Test's behavior. If the user doesn't define a macro 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in this list, Google Test will define it. 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is/isn't available. 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are enabled. 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is/isn't available (some systems define 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ::string, which is different to std::string). 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is/isn't available (some systems define 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ::wstring, which is different to std::wstring). 5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular 5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// expressions are/aren't available. 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is/isn't available. 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// enabled. 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// std::wstring does/doesn't work (Google Test can 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// be used where std::wstring is unavailable). 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is/isn't available. 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// compiler supports Microsoft's "Structured 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Exception Handling". 6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_HAS_STREAM_REDIRECTION 6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - Define it to 1/0 to indicate whether the 6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// platform supports I/O stream redirection using 7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// dup() and dup2(). 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Test's own tr1 tuple implementation should be 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// used. Unused when the user sets 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_TR1_TUPLE to 0. 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_LINKED_AS_SHARED_LIBRARY 76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// - Define to 1 when compiling tests that use 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Google Test as a shared library (known as 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// DLL on Windows). 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_CREATE_SHARED_LIBRARY 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// - Define to 1 when compiling Google Test itself 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// as a shared library. 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This header defines the following utilities: 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macros indicating the current platform (defined to 1 if compiled on 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the given platform; otherwise undefined): 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_AIX - IBM AIX 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_CYGWIN - Cygwin 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_HAIKU - Haiku 9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_OS_HPUX - HP-UX 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_LINUX - Linux 9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_OS_LINUX_ANDROID - Google Android 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_MAC - Mac OS X 9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_OS_NACL - Google Native Client (NaCl) 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_SOLARIS - Sun Solaris 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_SYMBIAN - Symbian 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) 98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop 99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_WINDOWS_MINGW - MinGW 100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_WINDOWS_MOBILE - Windows Mobile 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_OS_ZOS - z/OS 102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// most stable support. Since core members of the Google Test project 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// don't have access to other platforms, support for them may be less 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// stable. If you notice any problems on your platform, please notify 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// googletestframework@googlegroups.com (patches for fixing them are 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// even more welcome!). 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Note that it is possible that none of the GTEST_OS_* macros are defined. 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macros indicating available Google Test features (defined to 1 if 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the corresponding feature is supported; otherwise undefined): 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_COMBINE - the Combine() function (for value-parameterized 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// tests) 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_DEATH_TEST - death tests 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_PARAM_TEST - value-parameterized tests 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_TYPED_TEST - typed tests 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_TYPED_TEST_P - type-parameterized tests 12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with 12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_HAS_POSIX_RE (see above) which users can 12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// define themselves. 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_USES_SIMPLE_RE - our own simple regex is used; 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the above two are mutually exclusive. 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ(). 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macros for basic C++ coding: 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// variable don't have to be used. 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DISALLOW_ASSIGN_ - disables operator=. 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=. 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Synchronization: 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Mutex, MutexLock, ThreadLocal, GetThreadCount() 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// - synchronization primitives. 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_IS_THREADSAFE - defined to 1 to indicate that the above 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// synchronization primitives have real implementations 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and Google Test is thread-safe; or 0 otherwise. 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Template meta programming: 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only. 14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// IteratorTraits - partial implementation of std::iterator_traits, which 14519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// is not available in libCstd when compiled with Sun C++. 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Smart pointers: 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// scoped_ptr - as in TR2. 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Regular expressions: 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// RE - a simple regular expression class using the POSIX 15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Extended Regular Expression syntax on UNIX-like 15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// platforms, or a reduced regular exception syntax on 15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// other platforms, including Windows. 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Logging: 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_LOG_() - logs messages at the specified severity level. 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LogToStderr() - directs all log messages to stderr. 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FlushInfoLog() - flushes informational log messages. 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Stdout and stderr capturing: 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// CaptureStdout() - starts capturing stdout. 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetCapturedStdout() - stops capturing stdout and returns the captured 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// string. 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// CaptureStderr() - starts capturing stderr. 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetCapturedStderr() - stops capturing stderr and returns the captured 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// string. 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Integer types: 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TypeWithSize - maps an integer to a int type. 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Int32, UInt32, Int64, UInt64, TimeInMillis 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// - integers of known sizes. 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// BiggestInt - the biggest signed integer type. 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Command-line utilities: 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_FLAG() - references a flag. 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DECLARE_*() - declares a flag. 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DEFINE_*() - defines a flag. 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetArgvs() - returns the command line as a vector of strings. 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Environment variable utilities: 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetEnv() - gets the value of an environment variable. 183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// BoolFromGTestEnv() - parses a bool environment variable. 184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Int32FromGTestEnv() - parses an Int32 environment variable. 185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// StringFromGTestEnv() - parses a string environment variable. 186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <ctype.h> // for isspace, etc 18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include <stddef.h> // for ptrdiff_t 189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <stdlib.h> 190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <stdio.h> 191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <string.h> 192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef _WIN32_WCE 19319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <sys/types.h> 19419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <sys/stat.h> 195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // !_WIN32_WCE 196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <iostream> // NOLINT 198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <sstream> // NOLINT 199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <string> // NOLINT 200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_FLAG_PREFIX_ "gtest_" 203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_FLAG_PREFIX_DASH_ "gtest-" 204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_NAME_ "Google Test" 206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/" 207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines the version of gcc that is used to compile this. 209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef __GNUC__ 210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 40302 means version 4.3.2. 21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_GCC_VER_ \ 212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) 213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // __GNUC__ 214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines the platform on which Google Test is compiled. 216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef __CYGWIN__ 21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_CYGWIN 1 218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined __SYMBIAN32__ 21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_SYMBIAN 1 220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined _WIN32 22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_WINDOWS 1 22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef _WIN32_WCE 22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_WINDOWS_MOBILE 1 22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__MINGW__) || defined(__MINGW32__) 22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_WINDOWS_MINGW 1 22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_WINDOWS_DESKTOP 1 22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // _WIN32_WCE 229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined __APPLE__ 23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_MAC 1 231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined __linux__ 23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_LINUX 1 23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef ANDROID 23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_LINUX_ANDROID 1 23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // ANDROID 236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined __MVS__ 23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_ZOS 1 238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined(__sun) && defined(__SVR4) 23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_SOLARIS 1 240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined(_AIX) 24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_AIX 1 24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#elif defined(__hpux) 24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_HPUX 1 24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#elif defined __native_client__ 24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_NACL 1 246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined(__HAIKU__) 24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_OS_HAIKU 1 248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // __CYGWIN__ 249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Brings in definitions for functions used in the testing::internal::posix 25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// namespace (read, write, close, chdir, isatty, stat). We do not currently 25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// use them on Windows Mobile. 25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if !GTEST_OS_WINDOWS 25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This assumes that non-Windows OSes provide unistd.h. For OSes where this 25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// is not the case, we need to include headers that provide the functions 25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// mentioned above. 25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <unistd.h> 25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if !GTEST_OS_NACL 25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// TODO(vladl@google.com): Remove this condition when Native Client SDK adds 26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// strings.h (tracked in 26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// http://code.google.com/p/nativeclient/issues/detail?id=1175). 26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <strings.h> // Native Client doesn't provide strings.h. 26319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#elif !GTEST_OS_WINDOWS_MOBILE 26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <direct.h> 26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <io.h> 26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Defines this to true iff Google Test can use POSIX regular expressions. 27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#ifndef GTEST_HAS_POSIX_RE 27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS) 27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_POSIX_RE 275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// On some platforms, <regex.h> needs someone to define size_t, and 277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// won't compile otherwise. We can #include it here as we already 278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// included <stdlib.h>, which is guaranteed to define size_t through 279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// <stddef.h>. 28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <regex.h> // NOLINT 281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_USES_POSIX_RE 1 283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif GTEST_OS_WINDOWS 285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// <regex.h> is not available on Windows. Use our own simple regex 287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// implementation instead. 28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_USES_SIMPLE_RE 1 289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// <regex.h> may not be available on this platform. Use our own 293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// simple regex implementation instead. 29419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_USES_SIMPLE_RE 1 295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 29619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif // GTEST_HAS_POSIX_RE 297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_EXCEPTIONS 299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us whether exceptions are enabled, so we need 300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to figure it out. 30119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if defined(_MSC_VER) || defined(__BORLANDC__) 302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS 303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// macro to enable exceptions, so we'll do the same. 304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Assumes that exceptions are enabled by default. 30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifndef _HAS_EXCEPTIONS 30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define _HAS_EXCEPTIONS 1 30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // _HAS_EXCEPTIONS 30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__GNUC__) && __EXCEPTIONS 310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. 31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS 1 31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__SUNPRO_CC) 313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Sun Pro CC supports exceptions. However, there is no compile-time way of 314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// detecting whether they are enabled or not. Therefore, we assume that 315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// they are enabled unless the user tells us otherwise. 31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS 1 31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__IBMCPP__) && __EXCEPTIONS 318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// xlC defines __EXCEPTIONS to 1 iff exceptions are enabled. 31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS 1 32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__HP_aCC) 32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Exception handling is in effect by default in HP aCC compiler. It has to 32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// be turned of by +noeh compiler option if desired. 32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS 1 32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// For other compilers, we assume exceptions are disabled to be 326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// conservative. 32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_EXCEPTIONS 0 32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // defined(_MSC_VER) || defined(__BORLANDC__) 329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_EXCEPTIONS 330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if !defined(GTEST_HAS_STD_STRING) 332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Even though we don't use this macro any longer, we keep it in case 333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// some clients still depend on it. 33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_STD_STRING 1 335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif !GTEST_HAS_STD_STRING 336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user told us that ::std::string isn't available. 33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# error "Google Test cannot be used where ::std::string isn't available." 338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // !defined(GTEST_HAS_STD_STRING) 339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_GLOBAL_STRING 341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us whether ::string is available, so we need 342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to figure it out. 343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_GLOBAL_STRING 0 345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_GLOBAL_STRING 347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_STD_WSTRING 349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us whether ::std::wstring is available, so we need 350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to figure it out. 351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring 352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is available. 353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Cygwin 1.7 and below doesn't support ::std::wstring. 35519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Solaris' libc++ doesn't support it either. Android has 35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// no support for it at least as recent as Froyo (2.2). 357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Minix currently doesn't support it either. 35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_STD_WSTRING \ 35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || GTEST_OS_HAIKU || defined(_MINIX))) 360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_STD_WSTRING 362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_GLOBAL_WSTRING 364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us whether ::wstring is available, so we need 365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to figure it out. 36619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_GLOBAL_WSTRING \ 367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING) 368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_GLOBAL_WSTRING 369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether RTTI is available. 371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_RTTI 372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us whether RTTI is enabled, so we need to 373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// figure it out. 374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef _MSC_VER 376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled. 37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 1 37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 38019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 0 38119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled. 38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302) 385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef __GXX_RTTI 38719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 1 38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 0 39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // __GXX_RTTI 391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if 393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// both the typeid and dynamic_cast features are present. 39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__IBMCPP__) && (__IBMCPP__ >= 900) 395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 39619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef __RTTI_ALL__ 39719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 1 39819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 39919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 0 40019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// For all other compilers, we assume RTTI is enabled. 40519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_RTTI 1 406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 40719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // _MSC_VER 408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_RTTI 410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// It's this header's responsibility to #include <typeinfo> when RTTI 412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is enabled. 413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_RTTI 41419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <typeinfo> 415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether Google Test can use the pthreads library. 418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_PTHREAD 419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us explicitly, so we assume pthreads support is 420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// available on Linux and Mac. 421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to your compiler flags. 42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX) 425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_PTHREAD 426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_PTHREAD 42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is 42919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// true. 43019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <pthread.h> // NOLINT 43119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 43219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// For timespec and nanosleep, used below. 43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <time.h> // NOLINT 43419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 43519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether Google Test can use tr1/tuple. You can define 437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// this macro to 0 to prevent Google Test from using tuple (any 438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// feature depending on tuple with be disabled in this mode). 439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_TR1_TUPLE 440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us not to do it, so we assume it's OK. 44119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_TR1_TUPLE 1 442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_TR1_TUPLE 443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether Google Test's own tr1 tuple implementation 445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// should be used. 446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_USE_OWN_TR1_TUPLE 447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us, so we need to figure it out. 448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We use our own TR1 tuple if we aren't sure the user has an 450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// implementation of it already. At this time, GCC 4.0.0+ and MSVC 451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 2010 are the only mainstream compilers that come with a TR1 tuple 452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by 453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// defining __GNUC__ and friends, but cannot compile GCC's tuple 454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB 455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Feature Pack download, which we cannot assume the user has. 45619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if (defined(__GNUC__) && !(defined(__CUDACC__) || defined(__clang__)) \ 45719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman && (GTEST_GCC_VER_ >= 40000)) \ 458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman || _MSC_VER >= 1600 45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_USE_OWN_TR1_TUPLE 0 46019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 46119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_USE_OWN_TR1_TUPLE 1 46219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_USE_OWN_TR1_TUPLE 465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// To avoid conditional compilation everywhere, we make it 467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// gtest-port.h's responsibility to #include the header implementing 468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// tr1/tuple. 469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_TR1_TUPLE 470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_USE_OWN_TR1_TUPLE 47219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include "gtest/internal/gtest-tuple.h" 47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif GTEST_OS_SYMBIAN 474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to 476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// use STLport's tuple implementation, which unfortunately doesn't 477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// work as the copy of STLport distributed with Symbian is incomplete. 478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to 479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// use its own tuple implementation. 48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef BOOST_HAS_TR1_TUPLE 48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# undef BOOST_HAS_TR1_TUPLE 48219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // BOOST_HAS_TR1_TUPLE 483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This prevents <boost/tr1/detail/config.hpp>, which defines 485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>. 48619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED 48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <tuple> 488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000) 490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does 491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// not conform to the TR1 spec, which requires the header to be <tuple>. 492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Until version 4.3.2, gcc has a bug that causes <tr1/functional>, 495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// which is #included by <tr1/tuple>, to not compile when RTTI is 496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// disabled. _TR1_FUNCTIONAL is the header guard for 497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// <tr1/functional>. Hence the following #define is a hack to prevent 498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// <tr1/functional> from being included. 49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define _TR1_FUNCTIONAL 1 50019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <tr1/tuple> 50119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# undef _TR1_FUNCTIONAL // Allows the user to #include 502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // <tr1/functional> if he chooses to. 50319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 50419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <tr1/tuple> // NOLINT 50519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 50719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// If the compiler is not GCC 4.0+, we assume the user is using a 509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// spec-conforming TR1 implementation. 51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <tuple> // NOLINT 51119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // GTEST_USE_OWN_TR1_TUPLE 512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_TR1_TUPLE 514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether clone(2) is supported. 516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Usually it will only be available on Linux, excluding 517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Linux on the Itanium architecture. 518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Also see http://linux.die.net/man/2/clone. 519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_CLONE 520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us, so we need to figure it out. 521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 52219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_OS_LINUX && !defined(__ia64__) 52319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_CLONE 1 52419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 52519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_CLONE 0 52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // GTEST_OS_LINUX && !defined(__ia64__) 527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_CLONE 529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether to support stream redirection. This is used to test 531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// output correctness and to implement death tests. 53219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#ifndef GTEST_HAS_STREAM_REDIRECTION 53319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// By default, we assume that stream redirection is supported on all 53419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// platforms except known mobile ones. 53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN 53619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_STREAM_REDIRECTION 0 53719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 53819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_STREAM_REDIRECTION 1 53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN 54019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif // GTEST_HAS_STREAM_REDIRECTION 541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether to support death tests. 543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Google Test does not support death tests for VC 7.1 and earlier as 544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// abort() in a VC 7.1 application compiled as GUI in debug config 545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pops up a dialog window that cannot be suppressed programmatically. 546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ 547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \ 54819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX) 54919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_DEATH_TEST 1 55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# include <vector> // NOLINT 551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We don't support MSVC 7.1 with exceptions disabled now. Therefore 554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// all the compilers we care about are adequate for supporting 555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// value-parameterized tests. 556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_HAS_PARAM_TEST 1 557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether to support type-driven tests. 559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0, 56119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Sun Pro CC, IBM Visual Age, and HP aCC support. 562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \ 56319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman defined(__IBMCPP__) || defined(__HP_aCC) 56419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_TYPED_TEST 1 56519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_TYPED_TEST_P 1 566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether to support Combine(). This only makes sense when 569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// value-parameterized tests are enabled. The implementation doesn't 570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// work on Sun Studio since it doesn't understand templated conversion 571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// operators. 572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC) 57319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_COMBINE 1 574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determines whether the system compiler uses UTF-16 for encoding wide strings. 577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_WIDE_STRING_USES_UTF16_ \ 578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX) 579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 58019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Determines whether test results can be streamed to a socket. 58119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_OS_LINUX 58219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_CAN_STREAM_RESULTS_ 1 58319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 58419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines some utility macros. 586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The GNU compiler emits a warning if nested "if" statements are followed by 588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// an "else" statement and braces are not used to explicitly disambiguate the 589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// "else" binding. This leads to problems with code like: 590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// if (gate) 592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ASSERT_*(condition) << "Some message"; 593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The "switch (0) case 0:" idiom is used to suppress this. 595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef __INTEL_COMPILER 59619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 59819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT 599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Use this annotation at the end of a struct/class definition to 602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// prevent the compiler from optimizing away instances that are never 603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// used. This is useful when all interesting logic happens inside the 604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// c'tor and / or d'tor. Example: 605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// struct Foo { 607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Foo() { ... } 608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// } GTEST_ATTRIBUTE_UNUSED_; 609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Also use it after a variable or parameter declaration to tell the 611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// compiler the variable/parameter does not have to be used. 612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if defined(__GNUC__) && !defined(COMPILER_ICC) 61319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 61519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_ATTRIBUTE_UNUSED_ 616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A macro to disallow operator= 619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This should be used in the private: declarations for a class. 620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DISALLOW_ASSIGN_(type)\ 621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void operator=(type const &) 622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A macro to disallow copy constructor and operator= 624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This should be used in the private: declarations for a class. 625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\ 626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman type(type const &);\ 627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_ASSIGN_(type) 628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Tell the compiler to warn about unused return values for functions declared 630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// with this macro. The macro should be used on function declarations 631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// following the argument list: 632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC) 63519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) 636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 63719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_MUST_USE_RESULT_ 638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC 639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Determine whether the compiler supports Microsoft's Structured Exception 641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Handling. This is supported by several Windows compilers but generally 642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// does not exist on any other system. 643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_HAS_SEH 644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The user didn't tell us, so we need to figure it out. 645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 64619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if defined(_MSC_VER) || defined(__BORLANDC__) 647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// These two compilers are known to support SEH. 64819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_SEH 1 64919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Assume no SEH. 65119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_SEH 0 65219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_SEH 655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef _MSC_VER 657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 65819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_LINKED_AS_SHARED_LIBRARY 65919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_API_ __declspec(dllimport) 66019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# elif GTEST_CREATE_SHARED_LIBRARY 66119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_API_ __declspec(dllexport) 66219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif 663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // _MSC_VER 665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef GTEST_API_ 66719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_API_ 66819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 66919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 67019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#ifdef __GNUC__ 67119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Ask the compiler to never inline a given function. 67219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_NO_INLINE_ __attribute__((noinline)) 67319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#else 67419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_NO_INLINE_ 675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace testing { 678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Message; 680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace internal { 682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass String; 684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 68519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time 68619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// expression is true. For example, you could use it to verify the 68719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// size of a static array: 68819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 68919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES, 69019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// content_type_names_incorrect_size); 69119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 69219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// or to make sure a struct is smaller than a certain size: 69319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 69419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large); 69519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 69619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// The second argument to the macro is the name of the variable. If 69719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the expression is false, most compilers will issue a warning/error 69819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// containing the name of the variable. 69919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 70019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <bool> 70119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct CompileAssert { 70219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}; 70319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 70419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#define GTEST_COMPILE_ASSERT_(expr, msg) \ 70519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef ::testing::internal::CompileAssert<(bool(expr))> \ 70619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman msg[bool(expr) ? 1 : -1] 70719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 70819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Implementation details of GTEST_COMPILE_ASSERT_: 70919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 71019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1 71119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// elements (and thus is invalid) when the expression is false. 71219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 71319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - The simpler definition 71419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 71519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1] 71619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 71719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// does not work, as gcc supports variable-length arrays whose sizes 71819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// are determined at run-time (this is gcc's extension and not part 71919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// of the C++ standard). As a result, gcc fails to reject the 72019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// following code with the simple definition: 72119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 72219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// int foo; 72319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is 72419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// // not a compile-time constant. 72519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 72619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - By using the type CompileAssert<(bool(expr))>, we ensures that 72719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// expr is a compile-time constant. (Template arguments must be 72819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// determined at compile-time.) 72919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 73019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - The outter parentheses in CompileAssert<(bool(expr))> are necessary 73119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written 73219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 73319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// CompileAssert<bool(expr)> 73419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 73519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// instead, these compilers will refuse to compile 73619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 73719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// GTEST_COMPILE_ASSERT_(5 > 0, some_message); 73819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 73919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// (They seem to think the ">" in "5 > 0" marks the end of the 74019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// template argument list.) 74119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 74219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// - The array size is (bool(expr) ? 1 : -1), instead of simply 74319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 74419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// ((expr) ? 1 : -1). 74519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 74619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This is to avoid running into a bug in MS VC 7.1, which 74719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 74819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 74919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h. 75019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 75119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This template is declared, but intentionally undefined. 75219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename T1, typename T2> 75319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct StaticAssertTypeEqHelper; 75419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 75519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename T> 75619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct StaticAssertTypeEqHelper<T, T> {}; 75719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 75819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_GLOBAL_STRING 75919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantypedef ::string string; 76019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#else 76119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantypedef ::std::string string; 76219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif // GTEST_HAS_GLOBAL_STRING 76319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 76419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_GLOBAL_WSTRING 76519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantypedef ::wstring wstring; 76619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#elif GTEST_HAS_STD_WSTRING 76719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantypedef ::std::wstring wstring; 76819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif // GTEST_HAS_GLOBAL_WSTRING 769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A helper for suppressing warnings on constant condition. It just 771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// returns 'condition'. 772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ bool IsTrue(bool condition); 773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines scoped_ptr. 775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This implementation of scoped_ptr is PARTIAL - it only contains 777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// enough stuff to satisfy Google Test's need. 778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass scoped_ptr { 780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef T element_type; 782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~scoped_ptr() { reset(); } 785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T& operator*() const { return *ptr_; } 787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* operator->() const { return ptr_; } 788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* get() const { return ptr_; } 789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* release() { 791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* const ptr = ptr_; 792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ptr_ = NULL; 793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return ptr; 794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void reset(T* p = NULL) { 797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (p != ptr_) { 798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type. 799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delete ptr_; 800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ptr_ = p; 802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* ptr_; 806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr); 808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines RE. 811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A simple C++ wrapper for <regex.h>. It uses the POSIX Extended 813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Regular Expression syntax. 814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTEST_API_ RE { 815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // A copy constructor is required by the Standard to initialize object 817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // references from r-values. 818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RE(const RE& other) { Init(other.pattern()); } 819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Constructs an RE from a string. 821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_GLOBAL_STRING 82419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 82619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_GLOBAL_STRING 828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman RE(const char* regex) { Init(regex); } // NOLINT 830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~RE(); 831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Returns the string representation of the regex. 833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const char* pattern() const { return pattern_; } 834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // FullMatch(str, re) returns true iff regular expression re matches 836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the entire str. 837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // PartialMatch(str, re) returns true iff regular expression re 838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // matches a substring of str (including str itself). 839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // TODO(wan@google.com): make FullMatch() and PartialMatch() work 841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // when str contains NUL characters. 842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool FullMatch(const ::std::string& str, const RE& re) { 843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return FullMatch(str.c_str(), re); 844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool PartialMatch(const ::std::string& str, const RE& re) { 846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return PartialMatch(str.c_str(), re); 847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_GLOBAL_STRING 85019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool FullMatch(const ::string& str, const RE& re) { 852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return FullMatch(str.c_str(), re); 853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool PartialMatch(const ::string& str, const RE& re) { 855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return PartialMatch(str.c_str(), re); 856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 85719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_GLOBAL_STRING 859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool FullMatch(const char* str, const RE& re); 861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static bool PartialMatch(const char* str, const RE& re); 862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void Init(const char* regex); 865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We use a const char* instead of a string, as Google Test may be used 867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // where string is not available. We also do not use Google Test's own 868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // String type here, in order to simplify dependencies between the 869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // files. 870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const char* pattern_; 871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool is_valid_; 87219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_USES_POSIX_RE 87419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regex_t full_regex_; // For FullMatch(). 876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman regex_t partial_regex_; // For PartialMatch(). 87719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else // GTEST_USES_SIMPLE_RE 87919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const char* full_pattern_; // For FullMatch(); 88119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_ASSIGN_(RE); 885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 88719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Formats a source file path and a line number as they would appear 88819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// in an error message from the compiler used to compile this code. 88919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanGTEST_API_ ::std::string FormatFileLocation(const char* file, int line); 89019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 89119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Formats a file location for compiler-independent XML output. 89219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Although this function is not platform dependent, we put it next to 89319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// FormatFileLocation in order to contrast the two functions. 89419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanGTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, 89519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman int line); 89619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines logging utilities: 898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_LOG_(severity) - logs messages at the specified severity level. The 899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// message itself is streamed into the macro. 900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LogToStderr() - directs all log messages to stderr. 901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FlushInfoLog() - flushes informational log messages. 902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanenum GTestLogSeverity { 904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_INFO, 905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_WARNING, 906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_ERROR, 907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_FATAL 908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Formats log entry severity, provides a stream object for streaming the 911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// log message, and terminates the message with a newline when going out of 912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// scope. 913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTEST_API_ GTestLog { 914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTestLog(GTestLogSeverity severity, const char* file, int line); 916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~GTestLog(); 919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ::std::ostream& GetStream() { return ::std::cerr; } 921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const GTestLogSeverity severity_; 924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog); 926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_LOG_(severity) \ 929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ 930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman __FILE__, __LINE__).GetStream() 931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline void LogToStderr() {} 933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline void FlushInfoLog() { fflush(NULL); } 934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// INTERNAL IMPLEMENTATION - DO NOT USE. 936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is not satisfied. 939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Synopsys: 940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_CHECK_(boolean_condition); 941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// or 942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_CHECK_(boolean_condition) << "Additional message"; 943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This checks the condition and if the condition is not satisfied 945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// it prints message about the condition violation, including the 946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// condition itself, plus additional message streamed into it, if any, 947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and then it aborts the program. It aborts the program irrespective of 948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// whether it is built in the debug mode or not. 949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_CHECK_(condition) \ 950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (::testing::internal::IsTrue(condition)) \ 952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ; \ 953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman else \ 954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_LOG_(FATAL) << "Condition " #condition " failed. " 955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// An all-mode assert to verify that the given POSIX-style function 957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// call returns 0 (indicating success). Known limitation: this 958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// doesn't expand to a balanced 'if' statement, so enclose the macro 959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in {} if you need to use it as the only statement in an 'if' 960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// branch. 961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ 962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (const int gtest_error = (posix_call)) \ 963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_LOG_(FATAL) << #posix_call << "failed with error " \ 964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << gtest_error 965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 96819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Use ImplicitCast_ as a safe version of static_cast for upcasting in 96919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a 97019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// const Foo*). When you use ImplicitCast_, the compiler checks that 97119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the cast is safe. Such explicit ImplicitCast_s are necessary in 97219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// surprisingly many situations where C++ demands an exact type match 97319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// instead of an argument type convertable to a target type. 97419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 97519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// The syntax for using ImplicitCast_ is the same as for static_cast: 97619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 97719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// ImplicitCast_<ToType>(expr) 97819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 97919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// ImplicitCast_ would have been part of the C++ standard library, 98019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// but the proposal was submitted too late. It will probably make 98119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// its way into the language in the future. 98219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 98319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This relatively ugly name is intentional. It prevents clashes with 98419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// similar functions users may have (e.g., implicit_cast). The internal 98519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// namespace alone is not enough because the function can be found by ADL. 98619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate<typename To> 98719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline To ImplicitCast_(To x) { return x; } 98819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 98919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// When you upcast (that is, cast a pointer from type Foo to type 99019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts 99119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// always succeed. When you downcast (that is, cast a pointer from 99219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because 99319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// how do you know the pointer is really of type SubclassOfFoo? It 99419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, 99519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// when you downcast, you should use this macro. In debug mode, we 99619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// use dynamic_cast<> to double-check the downcast is legal (we die 99719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// if it's not). In normal mode, we do the efficient static_cast<> 99819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// instead. Thus, it's important to test in debug mode to make sure 99919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the cast is legal! 100019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This is the only place in the code we should use dynamic_cast<>. 100119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// In particular, you SHOULDN'T be using dynamic_cast<> in order to 100219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// do RTTI (eg code like this: 100319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); 100419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); 100519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// You should design the code some other way not to need this. 100619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// 100719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This relatively ugly name is intentional. It prevents clashes with 100819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// similar functions users may have (e.g., down_cast). The internal 100919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// namespace alone is not enough because the function can be found by ADL. 101019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate<typename To, typename From> // use like this: DownCast_<T*>(foo); 101119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline To DownCast_(From* f) { // so we only accept pointers 101219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // Ensures that To is a sub-type of From *. This test is here only 101319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // for compile-time type checking, and has no overhead in an 101419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // optimized build at run-time, as it will be optimized away 101519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // completely. 101619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman if (false) { 101719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const To to = NULL; 101819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman ::testing::internal::ImplicitCast_<From*>(to); 101919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman } 102019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 102119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_RTTI 102219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman // RTTI: debug mode only! 102319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL); 102419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif 102519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return static_cast<To>(f); 102619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 102719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Downcasts the pointer of type Base to Derived. 1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Derived must be a subclass of Base. The parameter MUST 1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// point to a class of type Derived, not any subclass of it. 1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// When RTTI is available, the function performs a runtime 1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// check to enforce this. 1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class Derived, class Base> 1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanDerived* CheckedDowncastToActualType(Base* base) { 1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_RTTI 1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_(typeid(*base) == typeid(Derived)); 1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return dynamic_cast<Derived*>(base); // NOLINT 1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<Derived*>(base); // Poor man's downcast. 1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 104319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#if GTEST_HAS_STREAM_REDIRECTION 1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines the stderr capturer: 1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// CaptureStdout - starts capturing stdout. 1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetCapturedStdout - stops capturing stdout and returns the captured string. 1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// CaptureStderr - starts capturing stderr. 1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GetCapturedStderr - stops capturing stderr and returns the captured string. 1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ void CaptureStdout(); 1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ String GetCapturedStdout(); 1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ void CaptureStderr(); 1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ String GetCapturedStderr(); 1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 105619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#endif // GTEST_HAS_STREAM_REDIRECTION 1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_DEATH_TEST 1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A copy of all command line arguments. Set by InitGoogleTest(). 1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern ::std::vector<String> g_argvs; 1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_HAS_DEATH_TEST implies we have ::std::string. 1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst ::std::vector<String>& GetArgvs(); 1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_DEATH_TEST 1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines synchronization primitives. 1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_HAS_PTHREAD 1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Sleeps for (roughly) n milli-seconds. This function is only for 1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// testing Google Test's own constructs. Don't use it in user tests, 1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// either directly or indirectly. 1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline void SleepMilliseconds(int n) { 1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const timespec time = { 1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 0, // 0 seconds. 1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman n * 1000L * 1000L, // And n ms. 1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman nanosleep(&time, NULL); 1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Allows a controller thread to pause execution of newly created 1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// threads until notified. Instances of this class must be created 1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and destroyed in the controller thread. 1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This class is only for testing Google Test's own constructs. Do not 1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// use it in user tests, either directly or indirectly. 1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Notification { 1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Notification() : notified_(false) {} 1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Notifies all threads created with this notification to start. Must 1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // be called from the controller thread. 1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void Notify() { notified_ = true; } 1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Blocks until the controller thread notifies. Must be called from a test 1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // thread. 1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void WaitForNotification() { 1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman while(!notified_) { 1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman SleepMilliseconds(10); 1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman volatile bool notified_; 1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); 1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// As a C-function, ThreadFuncWithCLinkage cannot be templated itself. 1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Consequently, it cannot select a correct instantiation of ThreadWithParam 1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// in order to call its Run(). Introducing ThreadWithParamBase as a 1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// non-templated base class for ThreadWithParam allows us to bypass this 1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// problem. 1117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ThreadWithParamBase { 1118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual ~ThreadWithParamBase() {} 1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual void Run() = 0; 1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pthread_create() accepts a pointer to a function type with the C linkage. 1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// According to the Standard (7.5/1), function types with different linkages 1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are different even if they are otherwise identical. Some compilers (for 1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// example, SunStudio) treat them as different types. Since class methods 1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// cannot be defined with C-linkage we need to define a free C-function to 1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pass into pthread_create(). 1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern "C" inline void* ThreadFuncWithCLinkage(void* thread) { 1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static_cast<ThreadWithParamBase*>(thread)->Run(); 1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return NULL; 1132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Helper class for testing Google Test's multi-threading constructs. 1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// To use it, write: 1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// void ThreadFunc(int param) { /* Do things with param */ } 1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Notification thread_can_start; 1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ... 1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// // The thread_can_start parameter is optional; you can supply NULL. 1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); 1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// thread_can_start.Notify(); 1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// These classes are only for testing Google Test's own constructs. Do 1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// not use them in user tests, either directly or indirectly. 1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ThreadWithParam : public ThreadWithParamBase { 1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef void (*UserThreadFunc)(T); 1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadWithParam( 1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman UserThreadFunc func, T param, Notification* thread_can_start) 1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : func_(func), 1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman param_(param), 1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman thread_can_start_(thread_can_start), 1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman finished_(false) { 1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadWithParamBase* const base = this; 1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // The thread can be created only after all fields except thread_ 1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // have been initialized. 1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_( 1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base)); 1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~ThreadWithParam() { Join(); } 1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void Join() { 1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (!finished_) { 1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0)); 1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman finished_ = true; 1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual void Run() { 1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (thread_can_start_ != NULL) 1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman thread_can_start_->WaitForNotification(); 1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman func_(param_); 1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const UserThreadFunc func_; // User-supplied thread function. 1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T param_; // User-supplied parameter to the thread function. 1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // When non-NULL, used to block execution until the controller thread 1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // notifies. 1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Notification* const thread_can_start_; 1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool finished_; // true iff we know that the thread function has finished. 1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_t thread_; // The native thread object. 1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); 1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MutexBase and Mutex implement mutex on pthreads-based platforms. They 1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are used in conjunction with class MutexLock: 1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Mutex mutex; 1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ... 1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MutexLock lock(&mutex); // Acquires the mutex and releases it at the end 1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// // of the current scope. 1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MutexBase implements behavior for both statically and dynamically 1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// allocated mutexes. Do not use MutexBase directly. Instead, write 1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the following to define a static mutex: 1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); 1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// You can forward declare a static mutex like this: 1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); 1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// To create a dynamic mutex, just define an object of type Mutex. 1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MutexBase { 1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Acquires this mutex. 1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void Lock() { 1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); 1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman owner_ = pthread_self(); 1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Releases this mutex. 1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void Unlock() { 1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We don't protect writing to owner_ here, as it's the caller's 1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // responsibility to ensure that the current thread holds the 1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // mutex when this is called. 1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman owner_ = 0; 1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); 1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Does nothing if the current thread holds the mutex. Otherwise, crashes 1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // with high probability. 1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void AssertHeld() const { 1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_(owner_ == pthread_self()) 1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman << "The current thread is not holding the mutex @" << this; 1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // A static mutex may be used before main() is entered. It may even 1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // be used before the dynamic initialization stage. Therefore we 1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // must be able to initialize a static mutex object at link time. 1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This means MutexBase has to be a POD and its member variables 1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // have to be public. 1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_mutex_t mutex_; // The underlying pthread mutex. 1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_t owner_; // The thread holding the mutex; 0 means no one holds it. 1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Forward-declares a static mutex. 124419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman extern ::testing::internal::MutexBase mutex 1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Defines and statically (i.e. at link time) initializes a static mutex. 124819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 } 1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The Mutex class can only be used for mutexes created at runtime. It 1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// shares its API with MutexBase otherwise. 1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Mutex : public MutexBase { 1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Mutex() { 1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL)); 1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman owner_ = 0; 1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~Mutex() { 1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); 1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); 1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We cannot name this class MutexLock as the ctor declaration would 1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// conflict with a macro named MutexLock, which is defined on some 1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// platforms. Hence the typedef trick below. 1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTestMutexLock { 1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit GTestMutexLock(MutexBase* mutex) 1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman : mutex_(mutex) { mutex_->Lock(); } 1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~GTestMutexLock() { mutex_->Unlock(); } 1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman MutexBase* const mutex_; 1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); 1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef GTestMutexLock MutexLock; 1284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Helpers for ThreadLocal. 1286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pthread_key_create() requires DeleteThreadLocalValue() to have 1288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// C-linkage. Therefore it cannot be templatized to access 1289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ThreadLocal<T>. Hence the need for class 1290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ThreadLocalValueHolderBase. 1291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ThreadLocalValueHolderBase { 1292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman virtual ~ThreadLocalValueHolderBase() {} 1294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Called by pthread to delete thread-local data stored by 1297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// pthread_setspecific(). 1298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanextern "C" inline void DeleteThreadLocalValue(void* value_holder) { 1299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman delete static_cast<ThreadLocalValueHolderBase*>(value_holder); 1300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Implements thread-local storage on pthreads-based systems. 1303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// // Thread 1 1305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ThreadLocal<int> tl(100); // 100 is the default value for each thread. 1306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// // Thread 2 1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// tl.set(150); // Changes the value for thread 2 only. 1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_EQ(150, tl.get()); 1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// // Thread 1 1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. 1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// tl.set(200); 1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EXPECT_EQ(200, tl.get()); 1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The template type argument T must have a public copy constructor. 1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// In addition, the default ThreadLocal constructor requires T to have 1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// a public default constructor. 1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// An object managed for a thread by a ThreadLocal instance is deleted 1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// when the thread exits. Or, if the ThreadLocal instance dies in 1322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// that thread, when the ThreadLocal dies. It's the user's 1323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// responsibility to ensure that all other threads using a ThreadLocal 1324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// have exited when it dies, or the per-thread objects for those 1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// threads will not be deleted. 1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Google Test only uses global ThreadLocal objects. That means they 1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// will die after main() has returned. Therefore, no per-thread 1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// object managed by Google Test will be leaked as long as all threads 1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// using Google Test have exited when main() returns. 1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ThreadLocal { 1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadLocal() : key_(CreateKey()), 1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default_() {} 1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit ThreadLocal(const T& value) : key_(CreateKey()), 1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman default_(value) {} 1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~ThreadLocal() { 1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Destroys the managed object for the current thread, if any. 1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman DeleteThreadLocalValue(pthread_getspecific(key_)); 1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Releases resources associated with the key. This will *not* 1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // delete managed objects for other threads. 1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); 1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* pointer() { return GetOrCreateValue(); } 1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T* pointer() const { return GetOrCreateValue(); } 1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T& get() const { return *pointer(); } 1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void set(const T& value) { *pointer() = value; } 1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Holds a value of type T. 1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman class ValueHolder : public ThreadLocalValueHolderBase { 1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit ValueHolder(const T& value) : value_(value) {} 1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* pointer() { return &value_; } 1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T value_; 1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder); 1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman }; 1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static pthread_key_t CreateKey() { 1367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_key_t key; 1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // When a thread exits, DeleteThreadLocalValue() will be called on 1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // the object managed for that thread. 1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_( 1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman pthread_key_create(&key, &DeleteThreadLocalValue)); 1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return key; 1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* GetOrCreateValue() const { 1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadLocalValueHolderBase* const holder = 1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_)); 1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman if (holder != NULL) { 1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return CheckedDowncastToActualType<ValueHolder>(holder)->pointer(); 1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ValueHolder* const new_holder = new ValueHolder(default_); 1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadLocalValueHolderBase* const holder_base = new_holder; 1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); 1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return new_holder->pointer(); 1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman } 1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // A key pthreads uses for looking up per-thread values. 1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const pthread_key_t key_; 1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T default_; // The default value for each thread. 1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); 1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 139519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_IS_THREADSAFE 1 1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else // GTEST_HAS_PTHREAD 1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// A dummy implementation of synchronization primitives (mutex, lock, 1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and thread-local variable). Necessary for compiling Google Test where 1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// mutex is not supported - using Google Test in multiple threads is not 1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// supported on such platforms. 1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Mutex { 1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman Mutex() {} 1407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void AssertHeld() const {} 1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 141019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman extern ::testing::internal::Mutex mutex 1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 141319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex 1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GTestMutexLock { 1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit GTestMutexLock(Mutex*) {} // NOLINT 1418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef GTestMutexLock MutexLock; 1421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 1423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ThreadLocal { 1424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ThreadLocal() : value_() {} 1426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman explicit ThreadLocal(const T& value) : value_(value) {} 1427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T* pointer() { return &value_; } 1428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T* pointer() const { return &value_; } 1429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const T& get() const { return value_; } 1430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman void set(const T& value) { value_ = value; } 1431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman private: 1432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman T value_; 1433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The above synchronization primitives have dummy implementations. 1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Therefore Google Test is not thread-safe. 143719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_IS_THREADSAFE 0 1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_HAS_PTHREAD 1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Returns the number of threads running in the process, or 0 to indicate that 1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// we cannot detect it. 1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ size_t GetThreadCount(); 1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Passing non-POD classes through ellipsis (...) crashes the ARM 1446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// compiler and generates a warning in Sun Studio. The Nokia Symbian 1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and the IBM XL C/C++ compiler try to instantiate a copy constructor 1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// for objects passed through ellipsis (...), failing for uncopyable 1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// objects. We define this to ensure that only POD is passed through 1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ellipsis on these systems. 1451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC) 1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We lose support for NULL detection where the compiler doesn't like 1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// passing non-POD classes through ellipsis (...). 145419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_ELLIPSIS_NEEDS_POD_ 1 1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 145619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_CAN_COMPARE_NULL 1 1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between 1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// const T& and const T* in a function template. These compilers 1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// _can_ decide between class template specializations for T and T*, 1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// so a tr1::type_traits-like is_pointer works. 1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if defined(__SYMBIAN32__) || defined(__IBMCPP__) 146419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_NEEDS_IS_POINTER_ 1 1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <bool bool_value> 1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct bool_constant { 1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef bool_constant<bool_value> type; 1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman static const bool value = bool_value; 1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <bool bool_value> const bool bool_constant<bool_value>::value; 1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef bool_constant<false> false_type; 1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef bool_constant<true> true_type; 1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct is_pointer : public false_type {}; 1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename T> 1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct is_pointer<T*> : public true_type {}; 1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 148319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename Iterator> 148419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct IteratorTraits { 148519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef typename Iterator::value_type value_type; 148619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}; 148719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 148819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename T> 148919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct IteratorTraits<T*> { 149019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef T value_type; 149119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}; 149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 149319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename T> 149419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct IteratorTraits<const T*> { 149519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman typedef T value_type; 149619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}; 149719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_OS_WINDOWS 149919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_PATH_SEP_ "\\" 150019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_ALT_PATH_SEP_ 1 1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The biggest signed integer type the compiler supports. 1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef __int64 BiggestInt; 1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 150419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_PATH_SEP_ "/" 150519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# define GTEST_HAS_ALT_PATH_SEP_ 0 1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef long long BiggestInt; // NOLINT 1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_OS_WINDOWS 1508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 150919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Utilities for char. 151019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 151119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// isspace(int ch) and friends accept an unsigned char or EOF. char 151219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// may be signed, depending on the compiler (or compiler flags). 151319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Therefore we need to cast a char to unsigned char before calling 151419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// isspace(), etc. 151519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 151619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsAlpha(char ch) { 151719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isalpha(static_cast<unsigned char>(ch)) != 0; 151819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 151919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsAlNum(char ch) { 152019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isalnum(static_cast<unsigned char>(ch)) != 0; 152119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 152219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsDigit(char ch) { 152319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isdigit(static_cast<unsigned char>(ch)) != 0; 152419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 152519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsLower(char ch) { 152619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return islower(static_cast<unsigned char>(ch)) != 0; 152719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 152819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsSpace(char ch) { 152919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isspace(static_cast<unsigned char>(ch)) != 0; 153019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 153119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsUpper(char ch) { 153219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isupper(static_cast<unsigned char>(ch)) != 0; 153319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 153419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline bool IsXDigit(char ch) { 153519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return isxdigit(static_cast<unsigned char>(ch)) != 0; 153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 153819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline char ToLower(char ch) { 153919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return static_cast<char>(tolower(static_cast<unsigned char>(ch))); 154019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 154119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline char ToUpper(char ch) { 154219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman return static_cast<char>(toupper(static_cast<unsigned char>(ch))); 154319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} 154419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The testing::internal::posix namespace holds wrappers for common 1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// POSIX functions. These wrappers hide the differences between 1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Windows/MSVC and POSIX systems. Since some compilers define these 1548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// standard functions as macros, the wrapper cannot have the same name 1549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// as the wrapped function. 1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace posix { 1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Functions with a different name on Windows. 1554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_OS_WINDOWS 1556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef struct _stat StatStruct; 1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 155919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# ifdef __BORLANDC__ 1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int IsATTY(int fd) { return isatty(fd); } 1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int StrCaseCmp(const char* s1, const char* s2) { 1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return stricmp(s1, s2); 1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline char* StrDup(const char* src) { return strdup(src); } 156519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else // !__BORLANDC__ 156619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_OS_WINDOWS_MOBILE 1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int IsATTY(int /* fd */) { return 0; } 156819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int IsATTY(int fd) { return _isatty(fd); } 157019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // GTEST_OS_WINDOWS_MOBILE 1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int StrCaseCmp(const char* s1, const char* s2) { 1572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return _stricmp(s1, s2); 1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline char* StrDup(const char* src) { return _strdup(src); } 157519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // __BORLANDC__ 1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 157719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# if GTEST_OS_WINDOWS_MOBILE 1578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } 1579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this 1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// time and thus not defined there. 158119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# else 1582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int FileNo(FILE* file) { return _fileno(file); } 1583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } 1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int RmDir(const char* dir) { return _rmdir(dir); } 1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline bool IsDir(const StatStruct& st) { 1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return (_S_IFDIR & st.st_mode) != 0; 1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 158819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# endif // GTEST_OS_WINDOWS_MOBILE 1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef struct stat StatStruct; 1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int FileNo(FILE* file) { return fileno(file); } 1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int IsATTY(int fd) { return isatty(fd); } 1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } 1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int StrCaseCmp(const char* s1, const char* s2) { 1598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return strcasecmp(s1, s2); 1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline char* StrDup(const char* src) { return strdup(src); } 1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int RmDir(const char* dir) { return rmdir(dir); } 1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 1603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_OS_WINDOWS 1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Functions deprecated by MSVC 8.0. 1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef _MSC_VER 1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Temporarily disable warning 4996 (deprecated function). 161019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# pragma warning(push) 161119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# pragma warning(disable:4996) 1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline const char* StrNCpy(char* dest, const char* src, size_t n) { 1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return strncpy(dest, src, n); 1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and 1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// StrError() aren't needed on Windows CE at this time and thus not 1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// defined there. 1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if !GTEST_OS_WINDOWS_MOBILE 1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int ChDir(const char* dir) { return chdir(dir); } 1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline FILE* FOpen(const char* path, const char* mode) { 1626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return fopen(path, mode); 1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if !GTEST_OS_WINDOWS_MOBILE 1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline FILE *FReopen(const char* path, const char* mode, FILE* stream) { 1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return freopen(path, mode, stream); 1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } 1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int FClose(FILE* fp) { return fclose(fp); } 1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if !GTEST_OS_WINDOWS_MOBILE 1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int Read(int fd, void* buf, unsigned int count) { 1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<int>(read(fd, buf, count)); 1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int Write(int fd, const void* buf, unsigned int count) { 1640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return static_cast<int>(write(fd, buf, count)); 1641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline int Close(int fd) { return close(fd); } 1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline const char* StrError(int errnum) { return strerror(errnum); } 1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline const char* GetEnv(const char* name) { 1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_OS_WINDOWS_MOBILE 1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // We are on Windows CE, which has no environment variables. 1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return NULL; 1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // Environment variables which we programmatically clear will be set to the 1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // empty string rather than unset (NULL). Handle that case. 1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const char* const env = getenv(name); 1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return (env != NULL && env[0] != '\0') ? env : NULL; 1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman return getenv(name); 1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} 1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef _MSC_VER 166019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman# pragma warning(pop) // Restores the warning state. 1661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 1662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_OS_WINDOWS_MOBILE 1664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Windows CE has no C library. The abort() function is used in 1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// several places in Google Test. This implementation provides a reasonable 1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// imitation of standard behaviour. 1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Abort(); 1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 1669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline void Abort() { abort(); } 1670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_OS_WINDOWS_MOBILE 1671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // namespace posix 1673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The maximum number a BiggestInt can represent. This definition 1675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// works no matter BiggestInt is represented in one's complement or 1676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// two's complement. 1677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We cannot rely on numeric_limits in STL, as __int64 and long long 1679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are not part of standard C++ and numeric_limits doesn't need to be 1680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// defined for them. 1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst BiggestInt kMaxBiggestInt = 1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This template class serves as a compile-time function from size to 1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// type. It maps a size in bytes to a primitive type with that 1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// size. e.g. 1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TypeWithSize<4>::UInt 1689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// is typedef-ed to be unsigned int (unsigned integer made up of 4 1691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// bytes). 1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Such functionality should belong to STL, but I cannot find it 1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// there. 1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Google Test uses this class in the implementation of floating-point 1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// comparison. 1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 1699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// For now it only handles UInt (unsigned int) as that's all Google Test 1700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// needs. Other types can be easily added in the future if need 1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// arises. 1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <size_t size> 1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass TypeWithSize { 1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // This prevents the user from using TypeWithSize<N> with incorrect 1706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // values of N. 1707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef void UInt; 1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The specialization for size 4. 1711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> 1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass TypeWithSize<4> { 1713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 1714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // unsigned int has size 4 in both gcc and MSVC. 1715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // 1716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // As base/basictypes.h doesn't compile on Windows, we cannot use 1717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman // uint32, uint64, and etc here. 1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef int Int; 1719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef unsigned int UInt; 1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The specialization for size 8. 1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> 1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass TypeWithSize<8> { 1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman public: 172619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman 1727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if GTEST_OS_WINDOWS 1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef __int64 Int; 1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef unsigned __int64 UInt; 1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else 1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef long long Int; // NOLINT 1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef unsigned long long UInt; // NOLINT 1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_OS_WINDOWS 1734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Integer types of known sizes. 1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef TypeWithSize<4>::Int Int32; 1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef TypeWithSize<4>::UInt UInt32; 1739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef TypeWithSize<8>::Int Int64; 1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef TypeWithSize<8>::UInt UInt64; 1741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 1742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Utilities for command line flags and environment variables. 1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macro for referencing flags. 1746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_FLAG(name) FLAGS_gtest_##name 1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macros for declaring flags. 1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name) 1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DECLARE_int32_(name) \ 1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name) 1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DECLARE_string_(name) \ 1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name) 1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Macros for defining flags. 1756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DEFINE_bool_(name, default_val, doc) \ 1757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_API_ bool GTEST_FLAG(name) = (default_val) 1758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DEFINE_int32_(name, default_val, doc) \ 1759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 1760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define GTEST_DEFINE_string_(name, default_val, doc) \ 1761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val) 1762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Parses 'str' for a 32-bit signed integer. If successful, writes the result 1764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to *value and returns true; otherwise leaves *value unchanged and returns 1765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// false. 1766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TODO(chandlerc): Find a better way to refactor flag and environment parsing 1767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// out of both gtest-port.cc and gtest.cc to avoid exporting this utility 1768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// function. 1769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ParseInt32(const Message& src_text, const char* str, Int32* value); 1770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Parses a bool/Int32/string from the environment variable 1772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// corresponding to the given Google Test flag. 1773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool BoolFromGTestEnv(const char* flag, bool default_val); 1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 1775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char* StringFromGTestEnv(const char* flag, const char* default_val); 1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // namespace internal 1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // namespace testing 1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 1781