gtest-port.h revision 542b41e5d010cf0a18a37252d5f4b05cfa5408af
1// Copyright 2005, Google Inc. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 8// * Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// * Redistributions in binary form must reproduce the above 11// copyright notice, this list of conditions and the following disclaimer 12// in the documentation and/or other materials provided with the 13// distribution. 14// * Neither the name of Google Inc. nor the names of its 15// contributors may be used to endorse or promote products derived from 16// this software without specific prior written permission. 17// 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29// 30// Authors: wan@google.com (Zhanyong Wan) 31// 32// Low-level types and utilities for porting Google Test to various 33// platforms. They are subject to change without notice. DO NOT USE 34// THEM IN USER CODE. 35 36#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 37#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 38 39// The user can define the following macros in the build script to 40// control Google Test's behavior. If the user doesn't define a macro 41// in this list, Google Test will define it. 42// 43// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 44// is/isn't available. 45// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions 46// are enabled. 47// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string 48// is/isn't available (some systems define 49// ::string, which is different to std::string). 50// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string 51// is/isn't available (some systems define 52// ::wstring, which is different to std::wstring). 53// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 54// is/isn't available. 55// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 56// enabled. 57// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 58// std::wstring does/doesn't work (Google Test can 59// be used where std::wstring is unavailable). 60// GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple 61// is/isn't available. 62// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the 63// compiler supports Microsoft's "Structured 64// Exception Handling". 65// GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google 66// Test's own tr1 tuple implementation should be 67// used. Unused when the user sets 68// GTEST_HAS_TR1_TUPLE to 0. 69 70// This header defines the following utilities: 71// 72// Macros indicating the current platform (defined to 1 if compiled on 73// the given platform; otherwise undefined): 74// GTEST_OS_CYGWIN - Cygwin 75// GTEST_OS_LINUX - Linux 76// GTEST_OS_MAC - Mac OS X 77// GTEST_OS_SOLARIS - Sun Solaris 78// GTEST_OS_SYMBIAN - Symbian 79// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) 80// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop 81// GTEST_OS_WINDOWS_MINGW - MinGW 82// GTEST_OS_WINDOWS_MOBILE - Windows Mobile 83// GTEST_OS_ZOS - z/OS 84// 85// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the 86// most stable support. Since core members of the Google Test project 87// don't have access to other platforms, support for them may be less 88// stable. If you notice any problems on your platform, please notify 89// googletestframework@googlegroups.com (patches for fixing them are 90// even more welcome!). 91// 92// Note that it is possible that none of the GTEST_OS_* macros are defined. 93// 94// Macros indicating available Google Test features (defined to 1 if 95// the corresponding feature is supported; otherwise undefined): 96// GTEST_HAS_COMBINE - the Combine() function (for value-parameterized 97// tests) 98// GTEST_HAS_DEATH_TEST - death tests 99// GTEST_HAS_PARAM_TEST - value-parameterized tests 100// GTEST_HAS_TYPED_TEST - typed tests 101// GTEST_HAS_TYPED_TEST_P - type-parameterized tests 102// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. 103// GTEST_USES_SIMPLE_RE - our own simple regex is used; 104// the above two are mutually exclusive. 105// 106// Macros for basic C++ coding: 107// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 108// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a 109// variable don't have to be used. 110// GTEST_DISALLOW_ASSIGN_ - disables operator=. 111// GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=. 112// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 113// 114// Synchronization: 115// Mutex, MutexLock, ThreadLocal, GetThreadCount() 116// - synchronization primitives. 117// GTEST_IS_THREADSAFE - defined to 1 to indicate that the above 118// synchronization primitives have real implementations 119// and Google Test is thread-safe; or 0 otherwise. 120// 121// Template meta programming: 122// is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only. 123// 124// Smart pointers: 125// scoped_ptr - as in TR2. 126// 127// Regular expressions: 128// RE - a simple regular expression class using the POSIX 129// Extended Regular Expression syntax. Not available on 130// Windows. 131// 132// Logging: 133// GTEST_LOG_() - logs messages at the specified severity level. 134// LogToStderr() - directs all log messages to stderr. 135// FlushInfoLog() - flushes informational log messages. 136// 137// Stdout and stderr capturing: 138// CaptureStdout() - starts capturing stdout. 139// GetCapturedStdout() - stops capturing stdout and returns the captured 140// string. 141// CaptureStderr() - starts capturing stderr. 142// GetCapturedStderr() - stops capturing stderr and returns the captured 143// string. 144// 145// Integer types: 146// TypeWithSize - maps an integer to a int type. 147// Int32, UInt32, Int64, UInt64, TimeInMillis 148// - integers of known sizes. 149// BiggestInt - the biggest signed integer type. 150// 151// Command-line utilities: 152// GTEST_FLAG() - references a flag. 153// GTEST_DECLARE_*() - declares a flag. 154// GTEST_DEFINE_*() - defines a flag. 155// GetArgvs() - returns the command line as a vector of strings. 156// 157// Environment variable utilities: 158// GetEnv() - gets the value of an environment variable. 159// BoolFromGTestEnv() - parses a bool environment variable. 160// Int32FromGTestEnv() - parses an Int32 environment variable. 161// StringFromGTestEnv() - parses a string environment variable. 162 163#include <stddef.h> // For ptrdiff_t 164#include <stdlib.h> 165#include <stdio.h> 166#include <string.h> 167#ifndef _WIN32_WCE 168#include <sys/stat.h> 169#endif // !_WIN32_WCE 170 171#include <iostream> // NOLINT 172#include <sstream> // NOLINT 173#include <string> // NOLINT 174 175#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 176#define GTEST_FLAG_PREFIX_ "gtest_" 177#define GTEST_FLAG_PREFIX_DASH_ "gtest-" 178#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 179#define GTEST_NAME_ "Google Test" 180#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/" 181 182// Determines the version of gcc that is used to compile this. 183#ifdef __GNUC__ 184// 40302 means version 4.3.2. 185#define GTEST_GCC_VER_ \ 186 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) 187#endif // __GNUC__ 188 189// Determines the platform on which Google Test is compiled. 190#ifdef __CYGWIN__ 191#define GTEST_OS_CYGWIN 1 192#elif defined __SYMBIAN32__ 193#define GTEST_OS_SYMBIAN 1 194#elif defined _WIN32 195#define GTEST_OS_WINDOWS 1 196#ifdef _WIN32_WCE 197#define GTEST_OS_WINDOWS_MOBILE 1 198#elif defined(__MINGW__) || defined(__MINGW32__) 199#define GTEST_OS_WINDOWS_MINGW 1 200#else 201#define GTEST_OS_WINDOWS_DESKTOP 1 202#endif // _WIN32_WCE 203#elif defined __APPLE__ 204#define GTEST_OS_MAC 1 205#elif defined __linux__ 206#define GTEST_OS_LINUX 1 207#elif defined __MVS__ 208#define GTEST_OS_ZOS 1 209#elif defined(__sun) && defined(__SVR4) 210#define GTEST_OS_SOLARIS 1 211#endif // __CYGWIN__ 212 213#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_SYMBIAN || \ 214 GTEST_OS_SOLARIS 215 216// On some platforms, <regex.h> needs someone to define size_t, and 217// won't compile otherwise. We can #include it here as we already 218// included <stdlib.h>, which is guaranteed to define size_t through 219// <stddef.h>. 220#include <regex.h> // NOLINT 221#include <strings.h> // NOLINT 222#include <sys/types.h> // NOLINT 223#include <time.h> // NOLINT 224#include <unistd.h> // NOLINT 225 226#define GTEST_USES_POSIX_RE 1 227 228#elif GTEST_OS_WINDOWS 229 230#if !GTEST_OS_WINDOWS_MOBILE 231#include <direct.h> // NOLINT 232#include <io.h> // NOLINT 233#endif 234 235// <regex.h> is not available on Windows. Use our own simple regex 236// implementation instead. 237#define GTEST_USES_SIMPLE_RE 1 238 239#else 240 241// <regex.h> may not be available on this platform. Use our own 242// simple regex implementation instead. 243#define GTEST_USES_SIMPLE_RE 1 244 245#endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || 246 // GTEST_OS_SYMBIAN || GTEST_OS_SOLARIS 247 248#ifndef GTEST_HAS_EXCEPTIONS 249// The user didn't tell us whether exceptions are enabled, so we need 250// to figure it out. 251#if defined(_MSC_VER) || defined(__BORLANDC__) 252// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS 253// macro to enable exceptions, so we'll do the same. 254// Assumes that exceptions are enabled by default. 255#ifndef _HAS_EXCEPTIONS 256#define _HAS_EXCEPTIONS 1 257#endif // _HAS_EXCEPTIONS 258#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 259#elif defined(__GNUC__) && __EXCEPTIONS 260// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. 261#define GTEST_HAS_EXCEPTIONS 1 262#elif defined(__SUNPRO_CC) 263// Sun Pro CC supports exceptions. However, there is no compile-time way of 264// detecting whether they are enabled or not. Therefore, we assume that 265// they are enabled unless the user tells us otherwise. 266#define GTEST_HAS_EXCEPTIONS 1 267#else 268// For other compilers, we assume exceptions are disabled to be 269// conservative. 270#define GTEST_HAS_EXCEPTIONS 0 271#endif // defined(_MSC_VER) || defined(__BORLANDC__) 272#endif // GTEST_HAS_EXCEPTIONS 273 274#if !defined(GTEST_HAS_STD_STRING) 275// Even though we don't use this macro any longer, we keep it in case 276// some clients still depend on it. 277#define GTEST_HAS_STD_STRING 1 278#elif !GTEST_HAS_STD_STRING 279// The user told us that ::std::string isn't available. 280#error "Google Test cannot be used where ::std::string isn't available." 281#endif // !defined(GTEST_HAS_STD_STRING) 282 283#ifndef GTEST_HAS_GLOBAL_STRING 284// The user didn't tell us whether ::string is available, so we need 285// to figure it out. 286 287#define GTEST_HAS_GLOBAL_STRING 0 288 289#endif // GTEST_HAS_GLOBAL_STRING 290 291#ifndef GTEST_HAS_STD_WSTRING 292// The user didn't tell us whether ::std::wstring is available, so we need 293// to figure it out. 294// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring 295// is available. 296 297// Cygwin 1.5 and below doesn't support ::std::wstring. 298// Cygwin 1.7 might add wstring support; this should be updated when clear. 299// Solaris' libc++ doesn't support it either. 300#define GTEST_HAS_STD_WSTRING (!(GTEST_OS_CYGWIN || GTEST_OS_SOLARIS)) 301 302#endif // GTEST_HAS_STD_WSTRING 303 304#ifndef GTEST_HAS_GLOBAL_WSTRING 305// The user didn't tell us whether ::wstring is available, so we need 306// to figure it out. 307#define GTEST_HAS_GLOBAL_WSTRING \ 308 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING) 309#endif // GTEST_HAS_GLOBAL_WSTRING 310 311// Determines whether RTTI is available. 312#ifndef GTEST_HAS_RTTI 313// The user didn't tell us whether RTTI is enabled, so we need to 314// figure it out. 315 316#ifdef _MSC_VER 317 318#ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled. 319#define GTEST_HAS_RTTI 1 320#else 321#define GTEST_HAS_RTTI 0 322#endif // _CPPRTTI 323 324#elif defined(__GNUC__) 325 326// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled. 327#if GTEST_GCC_VER_ >= 40302 328#ifdef __GXX_RTTI 329#define GTEST_HAS_RTTI 1 330#else 331#define GTEST_HAS_RTTI 0 332#endif // __GXX_RTTI 333#else 334// For gcc versions smaller than 4.3.2, we assume RTTI is enabled. 335#define GTEST_HAS_RTTI 1 336#endif // GTEST_GCC_VER >= 40302 337 338#else 339 340// Unknown compiler - assume RTTI is enabled. 341#define GTEST_HAS_RTTI 1 342 343#endif // _MSC_VER 344 345#endif // GTEST_HAS_RTTI 346 347// Determines whether Google Test can use the pthreads library. 348#ifndef GTEST_HAS_PTHREAD 349// The user didn't tell us explicitly, so we assume pthreads support is 350// available on Linux and Mac. 351// 352// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 353// to your compiler flags. 354#define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC) 355#endif // GTEST_HAS_PTHREAD 356 357// Determines whether Google Test can use tr1/tuple. You can define 358// this macro to 0 to prevent Google Test from using tuple (any 359// feature depending on tuple with be disabled in this mode). 360#ifndef GTEST_HAS_TR1_TUPLE 361// The user didn't tell us not to do it, so we assume it's OK. 362#define GTEST_HAS_TR1_TUPLE 1 363#endif // GTEST_HAS_TR1_TUPLE 364 365// Determines whether Google Test's own tr1 tuple implementation 366// should be used. 367#ifndef GTEST_USE_OWN_TR1_TUPLE 368// The user didn't tell us, so we need to figure it out. 369 370// We use our own TR1 tuple if we aren't sure the user has an 371// implementation of it already. At this time, GCC 4.0.0+ and MSVC 372// 2010 are the only mainstream compilers that come with a TR1 tuple 373// implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by 374// defining __GNUC__ and friends, but cannot compile GCC's tuple 375// implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB 376// Feature Pack download, which we cannot assume the user has. 377#if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \ 378 || _MSC_VER >= 1600 379#define GTEST_USE_OWN_TR1_TUPLE 0 380#else 381#define GTEST_USE_OWN_TR1_TUPLE 1 382#endif 383 384#endif // GTEST_USE_OWN_TR1_TUPLE 385 386// To avoid conditional compilation everywhere, we make it 387// gtest-port.h's responsibility to #include the header implementing 388// tr1/tuple. 389#if GTEST_HAS_TR1_TUPLE 390 391#if GTEST_USE_OWN_TR1_TUPLE 392#include <gtest/internal/gtest-tuple.h> 393#elif GTEST_OS_SYMBIAN 394 395// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to 396// use STLport's tuple implementation, which unfortunately doesn't 397// work as the copy of STLport distributed with Symbian is incomplete. 398// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to 399// use its own tuple implementation. 400#ifdef BOOST_HAS_TR1_TUPLE 401#undef BOOST_HAS_TR1_TUPLE 402#endif // BOOST_HAS_TR1_TUPLE 403 404// This prevents <boost/tr1/detail/config.hpp>, which defines 405// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>. 406#define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED 407#include <tuple> 408 409#elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000) 410// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does 411// not conform to the TR1 spec, which requires the header to be <tuple>. 412 413#if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 414// Until version 4.3.2, gcc has a bug that causes <tr1/functional>, 415// which is #included by <tr1/tuple>, to not compile when RTTI is 416// disabled. _TR1_FUNCTIONAL is the header guard for 417// <tr1/functional>. Hence the following #define is a hack to prevent 418// <tr1/functional> from being included. 419#define _TR1_FUNCTIONAL 1 420#include <tr1/tuple> 421#undef _TR1_FUNCTIONAL // Allows the user to #include 422 // <tr1/functional> if he chooses to. 423#else 424#include <tr1/tuple> // NOLINT 425#endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 426 427#else 428// If the compiler is not GCC 4.0+, we assume the user is using a 429// spec-conforming TR1 implementation. 430#include <tuple> // NOLINT 431#endif // GTEST_USE_OWN_TR1_TUPLE 432 433#endif // GTEST_HAS_TR1_TUPLE 434 435// Determines whether clone(2) is supported. 436// Usually it will only be available on Linux, excluding 437// Linux on the Itanium architecture. 438// Also see http://linux.die.net/man/2/clone. 439#ifndef GTEST_HAS_CLONE 440// The user didn't tell us, so we need to figure it out. 441 442#if GTEST_OS_LINUX && !defined(__ia64__) 443#define GTEST_HAS_CLONE 1 444#else 445#define GTEST_HAS_CLONE 0 446#endif // GTEST_OS_LINUX && !defined(__ia64__) 447 448#endif // GTEST_HAS_CLONE 449 450// Determines whether to support stream redirection. This is used to test 451// output correctness and to implement death tests. 452#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN 453#define GTEST_HAS_STREAM_REDIRECTION_ 1 454#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN 455 456// Determines whether to support death tests. 457// Google Test does not support death tests for VC 7.1 and earlier as 458// abort() in a VC 7.1 application compiled as GUI in debug config 459// pops up a dialog window that cannot be suppressed programmatically. 460#if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ 461 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || GTEST_OS_WINDOWS_MINGW) 462#define GTEST_HAS_DEATH_TEST 1 463#include <vector> // NOLINT 464#endif 465 466// We don't support MSVC 7.1 with exceptions disabled now. Therefore 467// all the compilers we care about are adequate for supporting 468// value-parameterized tests. 469#define GTEST_HAS_PARAM_TEST 1 470 471// Determines whether to support type-driven tests. 472 473// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0, and 474// Sun Pro CC support. 475#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) 476#define GTEST_HAS_TYPED_TEST 1 477#define GTEST_HAS_TYPED_TEST_P 1 478#endif 479 480// Determines whether to support Combine(). This only makes sense when 481// value-parameterized tests are enabled. 482#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 483#define GTEST_HAS_COMBINE 1 484#endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 485 486// Determines whether the system compiler uses UTF-16 for encoding wide strings. 487#define GTEST_WIDE_STRING_USES_UTF16_ \ 488 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN) 489 490// Defines some utility macros. 491 492// The GNU compiler emits a warning if nested "if" statements are followed by 493// an "else" statement and braces are not used to explicitly disambiguate the 494// "else" binding. This leads to problems with code like: 495// 496// if (gate) 497// ASSERT_*(condition) << "Some message"; 498// 499// The "switch (0) case 0:" idiom is used to suppress this. 500#ifdef __INTEL_COMPILER 501#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 502#else 503#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT 504#endif 505 506// Use this annotation at the end of a struct/class definition to 507// prevent the compiler from optimizing away instances that are never 508// used. This is useful when all interesting logic happens inside the 509// c'tor and / or d'tor. Example: 510// 511// struct Foo { 512// Foo() { ... } 513// } GTEST_ATTRIBUTE_UNUSED_; 514// 515// Also use it after a variable or parameter declaration to tell the 516// compiler the variable/parameter does not have to be used. 517#if defined(__GNUC__) && !defined(COMPILER_ICC) 518#define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 519#else 520#define GTEST_ATTRIBUTE_UNUSED_ 521#endif 522 523// A macro to disallow operator= 524// This should be used in the private: declarations for a class. 525#define GTEST_DISALLOW_ASSIGN_(type)\ 526 void operator=(type const &) 527 528// A macro to disallow copy constructor and operator= 529// This should be used in the private: declarations for a class. 530#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\ 531 type(type const &);\ 532 GTEST_DISALLOW_ASSIGN_(type) 533 534// Tell the compiler to warn about unused return values for functions declared 535// with this macro. The macro should be used on function declarations 536// following the argument list: 537// 538// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 539#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC) 540#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) 541#else 542#define GTEST_MUST_USE_RESULT_ 543#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC 544 545// Determine whether the compiler supports Microsoft's Structured Exception 546// Handling. This is supported by several Windows compilers but generally 547// does not exist on any other system. 548#ifndef GTEST_HAS_SEH 549// The user didn't tell us, so we need to figure it out. 550 551#if defined(_MSC_VER) || defined(__BORLANDC__) 552// These two compilers are known to support SEH. 553#define GTEST_HAS_SEH 1 554#else 555// Assume no SEH. 556#define GTEST_HAS_SEH 0 557#endif 558 559#endif // GTEST_HAS_SEH 560 561namespace testing { 562 563class Message; 564 565namespace internal { 566 567class String; 568 569typedef ::std::stringstream StrStream; 570 571// A helper for suppressing warnings on constant condition. It just 572// returns 'condition'. 573bool IsTrue(bool condition); 574 575// Defines scoped_ptr. 576 577// This implementation of scoped_ptr is PARTIAL - it only contains 578// enough stuff to satisfy Google Test's need. 579template <typename T> 580class scoped_ptr { 581 public: 582 typedef T element_type; 583 584 explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 585 ~scoped_ptr() { reset(); } 586 587 T& operator*() const { return *ptr_; } 588 T* operator->() const { return ptr_; } 589 T* get() const { return ptr_; } 590 591 T* release() { 592 T* const ptr = ptr_; 593 ptr_ = NULL; 594 return ptr; 595 } 596 597 void reset(T* p = NULL) { 598 if (p != ptr_) { 599 if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type. 600 delete ptr_; 601 } 602 ptr_ = p; 603 } 604 } 605 private: 606 T* ptr_; 607 608 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr); 609}; 610 611// Defines RE. 612 613// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended 614// Regular Expression syntax. 615class RE { 616 public: 617 // Constructs an RE from a string. 618 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 619 620#if GTEST_HAS_GLOBAL_STRING 621 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 622#endif // GTEST_HAS_GLOBAL_STRING 623 624 RE(const char* regex) { Init(regex); } // NOLINT 625 ~RE(); 626 627 // Returns the string representation of the regex. 628 const char* pattern() const { return pattern_; } 629 630 // FullMatch(str, re) returns true iff regular expression re matches 631 // the entire str. 632 // PartialMatch(str, re) returns true iff regular expression re 633 // matches a substring of str (including str itself). 634 // 635 // TODO(wan@google.com): make FullMatch() and PartialMatch() work 636 // when str contains NUL characters. 637 static bool FullMatch(const ::std::string& str, const RE& re) { 638 return FullMatch(str.c_str(), re); 639 } 640 static bool PartialMatch(const ::std::string& str, const RE& re) { 641 return PartialMatch(str.c_str(), re); 642 } 643 644#if GTEST_HAS_GLOBAL_STRING 645 static bool FullMatch(const ::string& str, const RE& re) { 646 return FullMatch(str.c_str(), re); 647 } 648 static bool PartialMatch(const ::string& str, const RE& re) { 649 return PartialMatch(str.c_str(), re); 650 } 651#endif // GTEST_HAS_GLOBAL_STRING 652 653 static bool FullMatch(const char* str, const RE& re); 654 static bool PartialMatch(const char* str, const RE& re); 655 656 private: 657 void Init(const char* regex); 658 659 // We use a const char* instead of a string, as Google Test may be used 660 // where string is not available. We also do not use Google Test's own 661 // String type here, in order to simplify dependencies between the 662 // files. 663 const char* pattern_; 664 bool is_valid_; 665#if GTEST_USES_POSIX_RE 666 regex_t full_regex_; // For FullMatch(). 667 regex_t partial_regex_; // For PartialMatch(). 668#else // GTEST_USES_SIMPLE_RE 669 const char* full_pattern_; // For FullMatch(); 670#endif 671 672 GTEST_DISALLOW_COPY_AND_ASSIGN_(RE); 673}; 674 675// Defines logging utilities: 676// GTEST_LOG_(severity) - logs messages at the specified severity level. The 677// message itself is streamed into the macro. 678// LogToStderr() - directs all log messages to stderr. 679// FlushInfoLog() - flushes informational log messages. 680 681enum GTestLogSeverity { 682 GTEST_INFO, 683 GTEST_WARNING, 684 GTEST_ERROR, 685 GTEST_FATAL 686}; 687 688// Formats log entry severity, provides a stream object for streaming the 689// log message, and terminates the message with a newline when going out of 690// scope. 691class GTestLog { 692 public: 693 GTestLog(GTestLogSeverity severity, const char* file, int line); 694 695 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 696 ~GTestLog(); 697 698 ::std::ostream& GetStream() { return ::std::cerr; } 699 700 private: 701 const GTestLogSeverity severity_; 702 703 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog); 704}; 705 706#define GTEST_LOG_(severity) \ 707 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ 708 __FILE__, __LINE__).GetStream() 709 710inline void LogToStderr() {} 711inline void FlushInfoLog() { fflush(NULL); } 712 713// INTERNAL IMPLEMENTATION - DO NOT USE. 714// 715// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 716// is not satisfied. 717// Synopsys: 718// GTEST_CHECK_(boolean_condition); 719// or 720// GTEST_CHECK_(boolean_condition) << "Additional message"; 721// 722// This checks the condition and if the condition is not satisfied 723// it prints message about the condition violation, including the 724// condition itself, plus additional message streamed into it, if any, 725// and then it aborts the program. It aborts the program irrespective of 726// whether it is built in the debug mode or not. 727#define GTEST_CHECK_(condition) \ 728 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 729 if (::testing::internal::IsTrue(condition)) \ 730 ; \ 731 else \ 732 GTEST_LOG_(FATAL) << "Condition " #condition " failed. " 733 734// An all-mode assert to verify that the given POSIX-style function 735// call returns 0 (indicating success). Known limitation: this 736// doesn't expand to a balanced 'if' statement, so enclose the macro 737// in {} if you need to use it as the only statement in an 'if' 738// branch. 739#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ 740 if (const int gtest_error = (posix_call)) \ 741 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \ 742 << gtest_error 743 744#if GTEST_HAS_STREAM_REDIRECTION_ 745 746// Defines the stderr capturer: 747// CaptureStdout - starts capturing stdout. 748// GetCapturedStdout - stops capturing stdout and returns the captured string. 749// CaptureStderr - starts capturing stderr. 750// GetCapturedStderr - stops capturing stderr and returns the captured string. 751// 752void CaptureStdout(); 753String GetCapturedStdout(); 754void CaptureStderr(); 755String GetCapturedStderr(); 756 757#endif // GTEST_HAS_STREAM_REDIRECTION_ 758 759 760#if GTEST_HAS_DEATH_TEST 761 762// A copy of all command line arguments. Set by InitGoogleTest(). 763extern ::std::vector<String> g_argvs; 764 765// GTEST_HAS_DEATH_TEST implies we have ::std::string. 766const ::std::vector<String>& GetArgvs(); 767 768#endif // GTEST_HAS_DEATH_TEST 769 770// Defines synchronization primitives. 771 772#if GTEST_HAS_PTHREAD 773 774// Sleeps for (roughly) n milli-seconds. This function is only for 775// testing Google Test's own constructs. Don't use it in user tests, 776// either directly or indirectly. 777inline void SleepMilliseconds(int n) { 778 const timespec time = { 779 0, // 0 seconds. 780 n * 1000L * 1000L, // And n ms. 781 }; 782 nanosleep(&time, NULL); 783} 784 785// Allows a controller thread to pause execution of newly created 786// threads until notified. Instances of this class must be created 787// and destroyed in the controller thread. 788// 789// This class is only for testing Google Test's own constructs. Do not 790// use it in user tests, either directly or indirectly. 791class Notification { 792 public: 793 Notification() : notified_(false) {} 794 795 // Notifies all threads created with this notification to start. Must 796 // be called from the controller thread. 797 void Notify() { notified_ = true; } 798 799 // Blocks until the controller thread notifies. Must be called from a test 800 // thread. 801 void WaitForNotification() { 802 while(!notified_) { 803 SleepMilliseconds(10); 804 } 805 } 806 807 private: 808 volatile bool notified_; 809 810 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); 811}; 812 813// Helper class for testing Google Test's multi-threading constructs. 814// To use it, write: 815// 816// void ThreadFunc(int param) { /* Do things with param */ } 817// Notification thread_can_start; 818// ... 819// // The thread_can_start parameter is optional; you can supply NULL. 820// ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); 821// thread_can_start.Notify(); 822// 823// These classes are only for testing Google Test's own constructs. Do 824// not use them in user tests, either directly or indirectly. 825template <typename T> 826class ThreadWithParam { 827 public: 828 typedef void (*UserThreadFunc)(T); 829 830 ThreadWithParam( 831 UserThreadFunc func, T param, Notification* thread_can_start) 832 : func_(func), 833 param_(param), 834 thread_can_start_(thread_can_start), 835 finished_(false) { 836 // The thread can be created only after all fields except thread_ 837 // have been initialized. 838 GTEST_CHECK_POSIX_SUCCESS_( 839 pthread_create(&thread_, 0, ThreadMainStatic, this)); 840 } 841 ~ThreadWithParam() { Join(); } 842 843 void Join() { 844 if (!finished_) { 845 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0)); 846 finished_ = true; 847 } 848 } 849 850 private: 851 void ThreadMain() { 852 if (thread_can_start_ != NULL) 853 thread_can_start_->WaitForNotification(); 854 func_(param_); 855 } 856 857 static void* ThreadMainStatic(void* thread_with_param) { 858 static_cast<ThreadWithParam<T>*>(thread_with_param)->ThreadMain(); 859 return NULL; // We are not interested in the thread exit code. 860 } 861 862 const UserThreadFunc func_; // User-supplied thread function. 863 const T param_; // User-supplied parameter to the thread function. 864 // When non-NULL, used to block execution until the controller thread 865 // notifies. 866 Notification* const thread_can_start_; 867 bool finished_; // true iff we know that the thread function has finished. 868 pthread_t thread_; // The native thread object. 869 870 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); 871}; 872 873// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is 874// true. 875#include <pthread.h> 876 877// MutexBase and Mutex implement mutex on pthreads-based platforms. They 878// are used in conjunction with class MutexLock: 879// 880// Mutex mutex; 881// ... 882// MutexLock lock(&mutex); // Acquires the mutex and releases it at the end 883// // of the current scope. 884// 885// MutexBase implements behavior for both statically and dynamically 886// allocated mutexes. Do not use MutexBase directly. Instead, write 887// the following to define a static mutex: 888// 889// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); 890// 891// You can forward declare a static mutex like this: 892// 893// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); 894// 895// To create a dynamic mutex, just define an object of type Mutex. 896class MutexBase { 897 public: 898 // Acquires this mutex. 899 void Lock() { 900 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); 901 owner_ = pthread_self(); 902 } 903 904 // Releases this mutex. 905 void Unlock() { 906 // We don't protect writing to owner_ here, as it's the caller's 907 // responsibility to ensure that the current thread holds the 908 // mutex when this is called. 909 owner_ = 0; 910 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); 911 } 912 913 // Does nothing if the current thread holds the mutex. Otherwise, crashes 914 // with high probability. 915 void AssertHeld() const { 916 GTEST_CHECK_(owner_ == pthread_self()) 917 << "The current thread is not holding the mutex @" << this; 918 } 919 920 // A static mutex may be used before main() is entered. It may even 921 // be used before the dynamic initialization stage. Therefore we 922 // must be able to initialize a static mutex object at link time. 923 // This means MutexBase has to be a POD and its member variables 924 // have to be public. 925 public: 926 pthread_mutex_t mutex_; // The underlying pthread mutex. 927 pthread_t owner_; // The thread holding the mutex; 0 means no one holds it. 928}; 929 930// Forward-declares a static mutex. 931#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 932 extern ::testing::internal::MutexBase mutex 933 934// Defines and statically (i.e. at link time) initializes a static mutex. 935#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 936 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 } 937 938// The Mutex class can only be used for mutexes created at runtime. It 939// shares its API with MutexBase otherwise. 940class Mutex : public MutexBase { 941 public: 942 Mutex() { 943 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL)); 944 owner_ = 0; 945 } 946 ~Mutex() { 947 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); 948 } 949 950 private: 951 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); 952}; 953 954// We cannot name this class MutexLock as the ctor declaration would 955// conflict with a macro named MutexLock, which is defined on some 956// platforms. Hence the typedef trick below. 957class GTestMutexLock { 958 public: 959 explicit GTestMutexLock(MutexBase* mutex) 960 : mutex_(mutex) { mutex_->Lock(); } 961 962 ~GTestMutexLock() { mutex_->Unlock(); } 963 964 private: 965 MutexBase* const mutex_; 966 967 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); 968}; 969 970typedef GTestMutexLock MutexLock; 971 972// Implements thread-local storage on pthreads-based systems. 973// 974// // Thread 1 975// ThreadLocal<int> tl(100); // 100 is the default value for each thread. 976// 977// // Thread 2 978// tl.set(150); // Changes the value for thread 2 only. 979// EXPECT_EQ(150, tl.get()); 980// 981// // Thread 1 982// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. 983// tl.set(200); 984// EXPECT_EQ(200, tl.get()); 985// 986// The template type argument T must have a public copy constructor. 987// In addition, the default ThreadLocal constructor requires T to have 988// a public default constructor. An object managed by a ThreadLocal 989// instance for a thread is guaranteed to exist at least until the 990// earliest of the two events: (a) the thread terminates or (b) the 991// ThreadLocal object is destroyed. 992template <typename T> 993class ThreadLocal { 994 public: 995 ThreadLocal() : key_(CreateKey()), 996 default_() {} 997 explicit ThreadLocal(const T& value) : key_(CreateKey()), 998 default_(value) {} 999 1000 ~ThreadLocal() { 1001 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); 1002 } 1003 1004 T* pointer() { return GetOrCreateValue(); } 1005 const T* pointer() const { return GetOrCreateValue(); } 1006 const T& get() const { return *pointer(); } 1007 void set(const T& value) { *pointer() = value; } 1008 1009 private: 1010 static pthread_key_t CreateKey() { 1011 pthread_key_t key; 1012 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_create(&key, &DeleteData)); 1013 return key; 1014 } 1015 1016 T* GetOrCreateValue() const { 1017 T* const value = static_cast<T*>(pthread_getspecific(key_)); 1018 if (value != NULL) 1019 return value; 1020 1021 T* const new_value = new T(default_); 1022 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, new_value)); 1023 return new_value; 1024 } 1025 1026 static void DeleteData(void* data) { delete static_cast<T*>(data); } 1027 1028 // A key pthreads uses for looking up per-thread values. 1029 const pthread_key_t key_; 1030 const T default_; // The default value for each thread. 1031 1032 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); 1033}; 1034 1035#define GTEST_IS_THREADSAFE 1 1036 1037#else // GTEST_HAS_PTHREAD 1038 1039// A dummy implementation of synchronization primitives (mutex, lock, 1040// and thread-local variable). Necessary for compiling Google Test where 1041// mutex is not supported - using Google Test in multiple threads is not 1042// supported on such platforms. 1043 1044class Mutex { 1045 public: 1046 Mutex() {} 1047 void AssertHeld() const {} 1048}; 1049 1050#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1051 extern ::testing::internal::Mutex mutex 1052 1053#define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex 1054 1055class GTestMutexLock { 1056 public: 1057 explicit GTestMutexLock(Mutex*) {} // NOLINT 1058}; 1059 1060typedef GTestMutexLock MutexLock; 1061 1062template <typename T> 1063class ThreadLocal { 1064 public: 1065 ThreadLocal() : value_() {} 1066 explicit ThreadLocal(const T& value) : value_(value) {} 1067 T* pointer() { return &value_; } 1068 const T* pointer() const { return &value_; } 1069 const T& get() const { return value_; } 1070 void set(const T& value) { value_ = value; } 1071 private: 1072 T value_; 1073}; 1074 1075// The above synchronization primitives have dummy implementations. 1076// Therefore Google Test is not thread-safe. 1077#define GTEST_IS_THREADSAFE 0 1078 1079#endif // GTEST_HAS_PTHREAD 1080 1081// Returns the number of threads running in the process, or 0 to indicate that 1082// we cannot detect it. 1083size_t GetThreadCount(); 1084 1085// Passing non-POD classes through ellipsis (...) crashes the ARM 1086// compiler and generates a warning in Sun Studio. The Nokia Symbian 1087// and the IBM XL C/C++ compiler try to instantiate a copy constructor 1088// for objects passed through ellipsis (...), failing for uncopyable 1089// objects. We define this to ensure that only POD is passed through 1090// ellipsis on these systems. 1091#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC) 1092#define GTEST_ELLIPSIS_NEEDS_POD_ 1 1093#endif 1094 1095// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between 1096// const T& and const T* in a function template. These compilers 1097// _can_ decide between class template specializations for T and T*, 1098// so a tr1::type_traits-like is_pointer works. 1099#if defined(__SYMBIAN32__) || defined(__IBMCPP__) 1100#define GTEST_NEEDS_IS_POINTER_ 1 1101#endif 1102 1103template <bool bool_value> 1104struct bool_constant { 1105 typedef bool_constant<bool_value> type; 1106 static const bool value = bool_value; 1107}; 1108template <bool bool_value> const bool bool_constant<bool_value>::value; 1109 1110typedef bool_constant<false> false_type; 1111typedef bool_constant<true> true_type; 1112 1113template <typename T> 1114struct is_pointer : public false_type {}; 1115 1116template <typename T> 1117struct is_pointer<T*> : public true_type {}; 1118 1119#if GTEST_OS_WINDOWS 1120#define GTEST_PATH_SEP_ "\\" 1121#define GTEST_HAS_ALT_PATH_SEP_ 1 1122// The biggest signed integer type the compiler supports. 1123typedef __int64 BiggestInt; 1124#else 1125#define GTEST_PATH_SEP_ "/" 1126#define GTEST_HAS_ALT_PATH_SEP_ 0 1127typedef long long BiggestInt; // NOLINT 1128#endif // GTEST_OS_WINDOWS 1129 1130// The testing::internal::posix namespace holds wrappers for common 1131// POSIX functions. These wrappers hide the differences between 1132// Windows/MSVC and POSIX systems. Since some compilers define these 1133// standard functions as macros, the wrapper cannot have the same name 1134// as the wrapped function. 1135 1136namespace posix { 1137 1138// Functions with a different name on Windows. 1139 1140#if GTEST_OS_WINDOWS 1141 1142typedef struct _stat StatStruct; 1143 1144#ifdef __BORLANDC__ 1145inline int IsATTY(int fd) { return isatty(fd); } 1146inline int StrCaseCmp(const char* s1, const char* s2) { 1147 return stricmp(s1, s2); 1148} 1149inline char* StrDup(const char* src) { return strdup(src); } 1150#else // !__BORLANDC__ 1151#if GTEST_OS_WINDOWS_MOBILE 1152inline int IsATTY(int /* fd */) { return 0; } 1153#else 1154inline int IsATTY(int fd) { return _isatty(fd); } 1155#endif // GTEST_OS_WINDOWS_MOBILE 1156inline int StrCaseCmp(const char* s1, const char* s2) { 1157 return _stricmp(s1, s2); 1158} 1159inline char* StrDup(const char* src) { return _strdup(src); } 1160#endif // __BORLANDC__ 1161 1162#if GTEST_OS_WINDOWS_MOBILE 1163inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } 1164// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this 1165// time and thus not defined there. 1166#else 1167inline int FileNo(FILE* file) { return _fileno(file); } 1168inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } 1169inline int RmDir(const char* dir) { return _rmdir(dir); } 1170inline bool IsDir(const StatStruct& st) { 1171 return (_S_IFDIR & st.st_mode) != 0; 1172} 1173#endif // GTEST_OS_WINDOWS_MOBILE 1174 1175#else 1176 1177typedef struct stat StatStruct; 1178 1179inline int FileNo(FILE* file) { return fileno(file); } 1180inline int IsATTY(int fd) { return isatty(fd); } 1181inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } 1182inline int StrCaseCmp(const char* s1, const char* s2) { 1183 return strcasecmp(s1, s2); 1184} 1185inline char* StrDup(const char* src) { return strdup(src); } 1186inline int RmDir(const char* dir) { return rmdir(dir); } 1187inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 1188 1189#endif // GTEST_OS_WINDOWS 1190 1191// Functions deprecated by MSVC 8.0. 1192 1193#ifdef _MSC_VER 1194// Temporarily disable warning 4996 (deprecated function). 1195#pragma warning(push) 1196#pragma warning(disable:4996) 1197#endif 1198 1199inline const char* StrNCpy(char* dest, const char* src, size_t n) { 1200 return strncpy(dest, src, n); 1201} 1202 1203// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and 1204// StrError() aren't needed on Windows CE at this time and thus not 1205// defined there. 1206 1207#if !GTEST_OS_WINDOWS_MOBILE 1208inline int ChDir(const char* dir) { return chdir(dir); } 1209#endif 1210inline FILE* FOpen(const char* path, const char* mode) { 1211 return fopen(path, mode); 1212} 1213#if !GTEST_OS_WINDOWS_MOBILE 1214inline FILE *FReopen(const char* path, const char* mode, FILE* stream) { 1215 return freopen(path, mode, stream); 1216} 1217inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } 1218#endif 1219inline int FClose(FILE* fp) { return fclose(fp); } 1220#if !GTEST_OS_WINDOWS_MOBILE 1221inline int Read(int fd, void* buf, unsigned int count) { 1222 return static_cast<int>(read(fd, buf, count)); 1223} 1224inline int Write(int fd, const void* buf, unsigned int count) { 1225 return static_cast<int>(write(fd, buf, count)); 1226} 1227inline int Close(int fd) { return close(fd); } 1228inline const char* StrError(int errnum) { return strerror(errnum); } 1229#endif 1230inline const char* GetEnv(const char* name) { 1231#if GTEST_OS_WINDOWS_MOBILE 1232 // We are on Windows CE, which has no environment variables. 1233 return NULL; 1234#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 1235 // Environment variables which we programmatically clear will be set to the 1236 // empty string rather than unset (NULL). Handle that case. 1237 const char* const env = getenv(name); 1238 return (env != NULL && env[0] != '\0') ? env : NULL; 1239#else 1240 return getenv(name); 1241#endif 1242} 1243 1244#ifdef _MSC_VER 1245#pragma warning(pop) // Restores the warning state. 1246#endif 1247 1248#if GTEST_OS_WINDOWS_MOBILE 1249// Windows CE has no C library. The abort() function is used in 1250// several places in Google Test. This implementation provides a reasonable 1251// imitation of standard behaviour. 1252void Abort(); 1253#else 1254inline void Abort() { abort(); } 1255#endif // GTEST_OS_WINDOWS_MOBILE 1256 1257} // namespace posix 1258 1259// The maximum number a BiggestInt can represent. This definition 1260// works no matter BiggestInt is represented in one's complement or 1261// two's complement. 1262// 1263// We cannot rely on numeric_limits in STL, as __int64 and long long 1264// are not part of standard C++ and numeric_limits doesn't need to be 1265// defined for them. 1266const BiggestInt kMaxBiggestInt = 1267 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 1268 1269// This template class serves as a compile-time function from size to 1270// type. It maps a size in bytes to a primitive type with that 1271// size. e.g. 1272// 1273// TypeWithSize<4>::UInt 1274// 1275// is typedef-ed to be unsigned int (unsigned integer made up of 4 1276// bytes). 1277// 1278// Such functionality should belong to STL, but I cannot find it 1279// there. 1280// 1281// Google Test uses this class in the implementation of floating-point 1282// comparison. 1283// 1284// For now it only handles UInt (unsigned int) as that's all Google Test 1285// needs. Other types can be easily added in the future if need 1286// arises. 1287template <size_t size> 1288class TypeWithSize { 1289 public: 1290 // This prevents the user from using TypeWithSize<N> with incorrect 1291 // values of N. 1292 typedef void UInt; 1293}; 1294 1295// The specialization for size 4. 1296template <> 1297class TypeWithSize<4> { 1298 public: 1299 // unsigned int has size 4 in both gcc and MSVC. 1300 // 1301 // As base/basictypes.h doesn't compile on Windows, we cannot use 1302 // uint32, uint64, and etc here. 1303 typedef int Int; 1304 typedef unsigned int UInt; 1305}; 1306 1307// The specialization for size 8. 1308template <> 1309class TypeWithSize<8> { 1310 public: 1311#if GTEST_OS_WINDOWS 1312 typedef __int64 Int; 1313 typedef unsigned __int64 UInt; 1314#else 1315 typedef long long Int; // NOLINT 1316 typedef unsigned long long UInt; // NOLINT 1317#endif // GTEST_OS_WINDOWS 1318}; 1319 1320// Integer types of known sizes. 1321typedef TypeWithSize<4>::Int Int32; 1322typedef TypeWithSize<4>::UInt UInt32; 1323typedef TypeWithSize<8>::Int Int64; 1324typedef TypeWithSize<8>::UInt UInt64; 1325typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 1326 1327// Utilities for command line flags and environment variables. 1328 1329// Macro for referencing flags. 1330#define GTEST_FLAG(name) FLAGS_gtest_##name 1331 1332// Macros for declaring flags. 1333#define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name) 1334#define GTEST_DECLARE_int32_(name) \ 1335 extern ::testing::internal::Int32 GTEST_FLAG(name) 1336#define GTEST_DECLARE_string_(name) \ 1337 extern ::testing::internal::String GTEST_FLAG(name) 1338 1339// Macros for defining flags. 1340#define GTEST_DEFINE_bool_(name, default_val, doc) \ 1341 bool GTEST_FLAG(name) = (default_val) 1342#define GTEST_DEFINE_int32_(name, default_val, doc) \ 1343 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 1344#define GTEST_DEFINE_string_(name, default_val, doc) \ 1345 ::testing::internal::String GTEST_FLAG(name) = (default_val) 1346 1347// Parses 'str' for a 32-bit signed integer. If successful, writes the result 1348// to *value and returns true; otherwise leaves *value unchanged and returns 1349// false. 1350// TODO(chandlerc): Find a better way to refactor flag and environment parsing 1351// out of both gtest-port.cc and gtest.cc to avoid exporting this utility 1352// function. 1353bool ParseInt32(const Message& src_text, const char* str, Int32* value); 1354 1355// Parses a bool/Int32/string from the environment variable 1356// corresponding to the given Google Test flag. 1357bool BoolFromGTestEnv(const char* flag, bool default_val); 1358Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 1359const char* StringFromGTestEnv(const char* flag, const char* default_val); 1360 1361} // namespace internal 1362} // namespace testing 1363 1364#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 1365