1
// Copyright 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Authors: wan@google.com (Zhanyong Wan)
32
// Low-level types and utilities for porting Google Test to various
33
// platforms. They are subject to change without notice. DO NOT USE
36
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
37
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
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.
43
// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
44
// is/isn't available.
45
// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
46
// is/isn't available (some systems define
47
// ::string, which is different to std::string).
48
// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
49
// is/isn't available (some systems define
50
// ::wstring, which is different to std::wstring).
51
// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
52
// is/isn't available.
53
// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
55
// GTEST_HAS_STD_STRING - Define it to 1/0 to indicate that
56
// std::string does/doesn't work (Google Test can
57
// be used where std::string is unavailable).
58
// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
59
// std::wstring does/doesn't work (Google Test can
60
// be used where std::wstring is unavailable).
61
// GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
62
// is/isn't available.
63
// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
64
// compiler supports Microsoft's "Structured
65
// Exception Handling".
66
// GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
67
// Test's own tr1 tuple implementation should be
68
// used. Unused when the user sets
69
// GTEST_HAS_TR1_TUPLE to 0.
71
// This header defines the following utilities:
73
// Macros indicating the current platform (defined to 1 if compiled on
74
// the given platform; otherwise undefined):
75
// GTEST_OS_CYGWIN - Cygwin
76
// GTEST_OS_LINUX - Linux
77
// GTEST_OS_MAC - Mac OS X
78
// GTEST_OS_SOLARIS - Sun Solaris
79
// GTEST_OS_SYMBIAN - Symbian
80
// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
81
// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
82
// GTEST_OS_WINDOWS_MINGW - MinGW
83
// GTEST_OS_WINODWS_MOBILE - Windows Mobile
84
// GTEST_OS_ZOS - z/OS
86
// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
87
// most stable support. Since core members of the Google Test project
88
// don't have access to other platforms, support for them may be less
89
// stable. If you notice any problems on your platform, please notify
90
// googletestframework@googlegroups.com (patches for fixing them are
91
// even more welcome!).
93
// Note that it is possible that none of the GTEST_OS_* macros are defined.
95
// Macros indicating available Google Test features (defined to 1 if
96
// the corresponding feature is supported; otherwise undefined):
97
// GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
99
// GTEST_HAS_DEATH_TEST - death tests
100
// GTEST_HAS_PARAM_TEST - value-parameterized tests
101
// GTEST_HAS_TYPED_TEST - typed tests
102
// GTEST_HAS_TYPED_TEST_P - type-parameterized tests
103
// GTEST_USES_POSIX_RE - enhanced POSIX regex is used.
104
// GTEST_USES_SIMPLE_RE - our own simple regex is used;
105
// the above two are mutually exclusive.
107
// Macros for basic C++ coding:
108
// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
109
// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
110
// variable don't have to be used.
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.
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.
121
// Template meta programming:
122
// is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
125
// scoped_ptr - as in TR2.
127
// Regular expressions:
128
// RE - a simple regular expression class using the POSIX
129
// Extended Regular Expression syntax. Not available on
133
// GTEST_LOG_() - logs messages at the specified severity level.
134
// LogToStderr() - directs all log messages to stderr.
135
// FlushInfoLog() - flushes informational log messages.
138
// CaptureStderr() - starts capturing stderr.
139
// GetCapturedStderr() - stops capturing stderr and returns the captured
143
// TypeWithSize - maps an integer to a int type.
144
// Int32, UInt32, Int64, UInt64, TimeInMillis
145
// - integers of known sizes.
146
// BiggestInt - the biggest signed integer type.
148
// Command-line utilities:
149
// GTEST_FLAG() - references a flag.
150
// GTEST_DECLARE_*() - declares a flag.
151
// GTEST_DEFINE_*() - defines a flag.
152
// GetArgvs() - returns the command line as a vector of strings.
154
// Environment variable utilities:
155
// GetEnv() - gets the value of an environment variable.
156
// BoolFromGTestEnv() - parses a bool environment variable.
157
// Int32FromGTestEnv() - parses an Int32 environment variable.
158
// StringFromGTestEnv() - parses a string environment variable.
160
#include <stddef.h> // For ptrdiff_t
165
#include <sys/stat.h>
166
#endif // !_WIN32_WCE
168
#include <iostream> // NOLINT
170
#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
171
#define GTEST_FLAG_PREFIX_ "gtest_"
172
#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
173
#define GTEST_NAME_ "Google Test"
174
#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
176
// Determines the version of gcc that is used to compile this.
178
// 40302 means version 4.3.2.
179
#define GTEST_GCC_VER_ \
180
(__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
183
// Determines the platform on which Google Test is compiled.
185
#define GTEST_OS_CYGWIN 1
186
#elif defined __SYMBIAN32__
187
#define GTEST_OS_SYMBIAN 1
189
#define GTEST_OS_WINDOWS 1
191
#define GTEST_OS_WINDOWS_MOBILE 1
192
#elif defined(__MINGW__) || defined(__MINGW32__)
193
#define GTEST_OS_WINDOWS_MINGW 1
195
#define GTEST_OS_WINDOWS_DESKTOP 1
197
#elif defined __APPLE__
198
#define GTEST_OS_MAC 1
199
#elif defined __linux__
200
#define GTEST_OS_LINUX 1
201
#elif defined __MVS__
202
#define GTEST_OS_ZOS 1
203
#elif defined(__sun) && defined(__SVR4)
204
#define GTEST_OS_SOLARIS 1
207
#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_SYMBIAN || \
210
// On some platforms, <regex.h> needs someone to define size_t, and
211
// won't compile otherwise. We can #include it here as we already
212
// included <stdlib.h>, which is guaranteed to define size_t through
214
#include <regex.h> // NOLINT
215
#include <strings.h> // NOLINT
216
#include <sys/types.h> // NOLINT
217
#include <unistd.h> // NOLINT
219
#define GTEST_USES_POSIX_RE 1
221
#elif GTEST_OS_WINDOWS
223
#if !GTEST_OS_WINDOWS_MOBILE
224
#include <direct.h> // NOLINT
225
#include <io.h> // NOLINT
228
// <regex.h> is not available on Windows. Use our own simple regex
229
// implementation instead.
230
#define GTEST_USES_SIMPLE_RE 1
234
// <regex.h> may not be available on this platform. Use our own
235
// simple regex implementation instead.
236
#define GTEST_USES_SIMPLE_RE 1
238
#endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC ||
239
// GTEST_OS_SYMBIAN || GTEST_OS_SOLARIS
241
// Defines GTEST_HAS_EXCEPTIONS to 1 if exceptions are enabled, or 0
244
#if defined(_MSC_VER) || defined(__BORLANDC__)
245
// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
246
// macro to enable exceptions, so we'll do the same.
247
// Assumes that exceptions are enabled by default.
248
#ifndef _HAS_EXCEPTIONS
249
#define _HAS_EXCEPTIONS 1
250
#endif // _HAS_EXCEPTIONS
251
#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
252
#else // The compiler is not MSVC or C++Builder.
253
// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. For
254
// other compilers, we assume exceptions are disabled to be
256
#if defined(__GNUC__) && __EXCEPTIONS
257
#define GTEST_HAS_EXCEPTIONS 1
259
#define GTEST_HAS_EXCEPTIONS 0
260
#endif // defined(__GNUC__) && __EXCEPTIONS
261
#endif // defined(_MSC_VER) || defined(__BORLANDC__)
263
// Determines whether ::std::string and ::string are available.
265
#ifndef GTEST_HAS_STD_STRING
266
// The user didn't tell us whether ::std::string is available, so we
267
// need to figure it out. The only environment that we know
268
// ::std::string is not available is MSVC 7.1 or lower with exceptions
270
#if defined(_MSC_VER) && (_MSC_VER < 1400) && !GTEST_HAS_EXCEPTIONS
271
#define GTEST_HAS_STD_STRING 0
273
#define GTEST_HAS_STD_STRING 1
275
#endif // GTEST_HAS_STD_STRING
277
#ifndef GTEST_HAS_GLOBAL_STRING
278
// The user didn't tell us whether ::string is available, so we need
281
#define GTEST_HAS_GLOBAL_STRING 0
283
#endif // GTEST_HAS_GLOBAL_STRING
285
#ifndef GTEST_HAS_STD_WSTRING
286
// The user didn't tell us whether ::std::wstring is available, so we need
288
// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
291
#if GTEST_OS_CYGWIN || GTEST_OS_SOLARIS
292
// Cygwin 1.5 and below doesn't support ::std::wstring.
293
// Cygwin 1.7 might add wstring support; this should be updated when clear.
294
// Solaris' libc++ doesn't support it either.
295
#define GTEST_HAS_STD_WSTRING 0
297
#define GTEST_HAS_STD_WSTRING GTEST_HAS_STD_STRING
298
#endif // GTEST_OS_CYGWIN || GTEST_OS_SOLARIS
300
#endif // GTEST_HAS_STD_WSTRING
302
#ifndef GTEST_HAS_GLOBAL_WSTRING
303
// The user didn't tell us whether ::wstring is available, so we need
305
#define GTEST_HAS_GLOBAL_WSTRING \
306
(GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
307
#endif // GTEST_HAS_GLOBAL_WSTRING
309
#if GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || \
310
GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
311
#include <string> // NOLINT
312
#endif // GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING ||
313
// GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
315
#if GTEST_HAS_STD_STRING
316
#include <sstream> // NOLINT
318
#include <strstream> // NOLINT
319
#endif // GTEST_HAS_STD_STRING
321
// Determines whether RTTI is available.
322
#ifndef GTEST_HAS_RTTI
323
// The user didn't tell us whether RTTI is enabled, so we need to
328
#ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
329
#define GTEST_HAS_RTTI 1
331
#define GTEST_HAS_RTTI 0
334
#elif defined(__GNUC__)
336
// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
337
#if GTEST_GCC_VER_ >= 40302
339
#define GTEST_HAS_RTTI 1
341
#define GTEST_HAS_RTTI 0
344
// For gcc versions smaller than 4.3.2, we assume RTTI is enabled.
345
#define GTEST_HAS_RTTI 1
346
#endif // GTEST_GCC_VER >= 40302
350
// Unknown compiler - assume RTTI is enabled.
351
#define GTEST_HAS_RTTI 1
355
#endif // GTEST_HAS_RTTI
357
// Determines whether <pthread.h> is available.
358
#ifndef GTEST_HAS_PTHREAD
359
// The user didn't tell us, so we need to figure it out.
360
#define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC)
361
#endif // GTEST_HAS_PTHREAD
363
// Determines whether Google Test can use tr1/tuple. You can define
364
// this macro to 0 to prevent Google Test from using tuple (any
365
// feature depending on tuple with be disabled in this mode).
366
#ifndef GTEST_HAS_TR1_TUPLE
367
// The user didn't tell us not to do it, so we assume it's OK.
368
#define GTEST_HAS_TR1_TUPLE 1
369
#endif // GTEST_HAS_TR1_TUPLE
371
// Determines whether Google Test's own tr1 tuple implementation
373
#ifndef GTEST_USE_OWN_TR1_TUPLE
374
// The user didn't tell us, so we need to figure it out.
376
// We use our own tr1 tuple if we aren't sure the user has an
377
// implementation of it already. At this time, GCC 4.0.0+ is the only
378
// mainstream compiler that comes with a TR1 tuple implementation.
379
// MSVC 2008 (9.0) provides TR1 tuple in a 323 MB Feature Pack
380
// download, which we cannot assume the user has. MSVC 2010 isn't
382
#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
383
#define GTEST_USE_OWN_TR1_TUPLE 0
385
#define GTEST_USE_OWN_TR1_TUPLE 1
386
#endif // defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
388
#endif // GTEST_USE_OWN_TR1_TUPLE
390
// To avoid conditional compilation everywhere, we make it
391
// gtest-port.h's responsibility to #include the header implementing
393
#if GTEST_HAS_TR1_TUPLE
395
#if GTEST_USE_OWN_TR1_TUPLE
396
#include <gtest/internal/gtest-tuple.h>
397
#elif GTEST_OS_SYMBIAN
399
// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
400
// use STLport's tuple implementation, which unfortunately doesn't
401
// work as the copy of STLport distributed with Symbian is incomplete.
402
// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
403
// use its own tuple implementation.
404
#ifdef BOOST_HAS_TR1_TUPLE
405
#undef BOOST_HAS_TR1_TUPLE
406
#endif // BOOST_HAS_TR1_TUPLE
408
// This prevents <boost/tr1/detail/config.hpp>, which defines
409
// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
410
#define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
413
#elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
414
// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
415
// not conform to the TR1 spec, which requires the header to be <tuple>.
417
#if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
418
// Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
419
// which is #included by <tr1/tuple>, to not compile when RTTI is
420
// disabled. _TR1_FUNCTIONAL is the header guard for
421
// <tr1/functional>. Hence the following #define is a hack to prevent
422
// <tr1/functional> from being included.
423
#define _TR1_FUNCTIONAL 1
425
#undef _TR1_FUNCTIONAL // Allows the user to #include
426
// <tr1/functional> if he chooses to.
429
#endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
432
// If the compiler is not GCC 4.0+, we assume the user is using a
433
// spec-conforming TR1 implementation.
435
#endif // GTEST_USE_OWN_TR1_TUPLE
437
#endif // GTEST_HAS_TR1_TUPLE
439
// Determines whether clone(2) is supported.
440
// Usually it will only be available on Linux, excluding
441
// Linux on the Itanium architecture.
442
// Also see http://linux.die.net/man/2/clone.
443
#ifndef GTEST_HAS_CLONE
444
// The user didn't tell us, so we need to figure it out.
446
#if GTEST_OS_LINUX && !defined(__ia64__)
447
#define GTEST_HAS_CLONE 1
449
#define GTEST_HAS_CLONE 0
450
#endif // GTEST_OS_LINUX && !defined(__ia64__)
452
#endif // GTEST_HAS_CLONE
454
// Determines whether to support death tests.
455
// Google Test does not support death tests for VC 7.1 and earlier for
457
// 1. std::vector does not build in VC 7.1 when exceptions are disabled.
458
// 2. std::string does not build in VC 7.1 when exceptions are disabled
459
// (this is covered by GTEST_HAS_STD_STRING guard).
460
// 3. abort() in a VC 7.1 application compiled as GUI in debug config
461
// pops up a dialog window that cannot be suppressed programmatically.
462
#if GTEST_HAS_STD_STRING && \
463
(GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || \
464
(GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || GTEST_OS_WINDOWS_MINGW)
465
#define GTEST_HAS_DEATH_TEST 1
466
#include <vector> // NOLINT
469
// Determines whether to support value-parameterized tests.
471
#if defined(__GNUC__) || (_MSC_VER >= 1400)
472
// TODO(vladl@google.com): get the implementation rid of vector and list
473
// to compile on MSVC 7.1.
474
#define GTEST_HAS_PARAM_TEST 1
475
#endif // defined(__GNUC__) || (_MSC_VER >= 1400)
477
// Determines whether to support type-driven tests.
479
// Typed tests need <typeinfo> and variadic macros, which gcc and VC
481
#if defined(__GNUC__) || (_MSC_VER >= 1400)
482
#define GTEST_HAS_TYPED_TEST 1
483
#define GTEST_HAS_TYPED_TEST_P 1
484
#endif // defined(__GNUC__) || (_MSC_VER >= 1400)
486
// Determines whether to support Combine(). This only makes sense when
487
// value-parameterized tests are enabled.
488
#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE
489
#define GTEST_HAS_COMBINE 1
490
#endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE
492
// Determines whether the system compiler uses UTF-16 for encoding wide strings.
493
#define GTEST_WIDE_STRING_USES_UTF16_ \
494
(GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN)
496
// Defines some utility macros.
498
// The GNU compiler emits a warning if nested "if" statements are followed by
499
// an "else" statement and braces are not used to explicitly disambiguate the
500
// "else" binding. This leads to problems with code like:
503
// ASSERT_*(condition) << "Some message";
505
// The "switch (0) case 0:" idiom is used to suppress this.
506
#ifdef __INTEL_COMPILER
507
#define GTEST_AMBIGUOUS_ELSE_BLOCKER_
509
#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT
512
// Use this annotation at the end of a struct/class definition to
513
// prevent the compiler from optimizing away instances that are never
514
// used. This is useful when all interesting logic happens inside the
515
// c'tor and / or d'tor. Example:
519
// } GTEST_ATTRIBUTE_UNUSED_;
521
// Also use it after a variable or parameter declaration to tell the
522
// compiler the variable/parameter does not have to be used.
523
#if defined(__GNUC__) && !defined(COMPILER_ICC)
524
#define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
526
#define GTEST_ATTRIBUTE_UNUSED_
529
// A macro to disallow the evil copy constructor and operator= functions
530
// This should be used in the private: declarations for a class.
531
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
533
void operator=(const type &)
535
// Tell the compiler to warn about unused return values for functions declared
536
// with this macro. The macro should be used on function declarations
537
// following the argument list:
539
// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
540
#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
541
#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
543
#define GTEST_MUST_USE_RESULT_
544
#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
546
// Determine whether the compiler supports Microsoft's Structured Exception
547
// Handling. This is supported by several Windows compilers but generally
548
// does not exist on any other system.
549
#ifndef GTEST_HAS_SEH
550
// The user didn't tell us, so we need to figure it out.
552
#if defined(_MSC_VER) || defined(__BORLANDC__)
553
// These two compilers are known to support SEH.
554
#define GTEST_HAS_SEH 1
557
#define GTEST_HAS_SEH 0
560
#endif // GTEST_HAS_SEH
570
// std::strstream is deprecated. However, we have to use it on
571
// Windows as std::stringstream won't compile on Windows when
572
// exceptions are disabled. We use std::stringstream on other
573
// platforms to avoid compiler warnings there.
574
#if GTEST_HAS_STD_STRING
575
typedef ::std::stringstream StrStream;
577
typedef ::std::strstream StrStream;
578
#endif // GTEST_HAS_STD_STRING
580
// A helper for suppressing warnings on constant condition. It just
581
// returns 'condition'.
582
bool IsTrue(bool condition);
584
// Defines scoped_ptr.
586
// This implementation of scoped_ptr is PARTIAL - it only contains
587
// enough stuff to satisfy Google Test's need.
588
template <typename T>
591
explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
592
~scoped_ptr() { reset(); }
594
T& operator*() const { return *ptr_; }
595
T* operator->() const { return ptr_; }
596
T* get() const { return ptr_; }
604
void reset(T* p = NULL) {
606
if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
615
GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
620
// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended
621
// Regular Expression syntax.
624
// Constructs an RE from a string.
625
#if GTEST_HAS_STD_STRING
626
RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
627
#endif // GTEST_HAS_STD_STRING
629
#if GTEST_HAS_GLOBAL_STRING
630
RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
631
#endif // GTEST_HAS_GLOBAL_STRING
633
RE(const char* regex) { Init(regex); } // NOLINT
636
// Returns the string representation of the regex.
637
const char* pattern() const { return pattern_; }
639
// FullMatch(str, re) returns true iff regular expression re matches
641
// PartialMatch(str, re) returns true iff regular expression re
642
// matches a substring of str (including str itself).
644
// TODO(wan@google.com): make FullMatch() and PartialMatch() work
645
// when str contains NUL characters.
646
#if GTEST_HAS_STD_STRING
647
static bool FullMatch(const ::std::string& str, const RE& re) {
648
return FullMatch(str.c_str(), re);
650
static bool PartialMatch(const ::std::string& str, const RE& re) {
651
return PartialMatch(str.c_str(), re);
653
#endif // GTEST_HAS_STD_STRING
655
#if GTEST_HAS_GLOBAL_STRING
656
static bool FullMatch(const ::string& str, const RE& re) {
657
return FullMatch(str.c_str(), re);
659
static bool PartialMatch(const ::string& str, const RE& re) {
660
return PartialMatch(str.c_str(), re);
662
#endif // GTEST_HAS_GLOBAL_STRING
664
static bool FullMatch(const char* str, const RE& re);
665
static bool PartialMatch(const char* str, const RE& re);
668
void Init(const char* regex);
670
// We use a const char* instead of a string, as Google Test may be used
671
// where string is not available. We also do not use Google Test's own
672
// String type here, in order to simplify dependencies between the
674
const char* pattern_;
676
#if GTEST_USES_POSIX_RE
677
regex_t full_regex_; // For FullMatch().
678
regex_t partial_regex_; // For PartialMatch().
679
#else // GTEST_USES_SIMPLE_RE
680
const char* full_pattern_; // For FullMatch();
683
GTEST_DISALLOW_COPY_AND_ASSIGN_(RE);
686
// Defines logging utilities:
687
// GTEST_LOG_(severity) - logs messages at the specified severity level. The
688
// message itself is streamed into the macro.
689
// LogToStderr() - directs all log messages to stderr.
690
// FlushInfoLog() - flushes informational log messages.
692
enum GTestLogSeverity {
699
// Formats log entry severity, provides a stream object for streaming the
700
// log message, and terminates the message with a newline when going out of
704
GTestLog(GTestLogSeverity severity, const char* file, int line);
706
// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
709
::std::ostream& GetStream() { return ::std::cerr; }
712
const GTestLogSeverity severity_;
714
GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
717
#define GTEST_LOG_(severity) \
718
::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
719
__FILE__, __LINE__).GetStream()
721
inline void LogToStderr() {}
722
inline void FlushInfoLog() { fflush(NULL); }
724
// Defines the stderr capturer:
725
// CaptureStderr - starts capturing stderr.
726
// GetCapturedStderr - stops capturing stderr and returns the captured string.
728
void CaptureStderr();
729
String GetCapturedStderr();
731
#if GTEST_HAS_DEATH_TEST
733
// A copy of all command line arguments. Set by InitGoogleTest().
734
extern ::std::vector<String> g_argvs;
736
// GTEST_HAS_DEATH_TEST implies we have ::std::string.
737
const ::std::vector<String>& GetArgvs();
739
#endif // GTEST_HAS_DEATH_TEST
741
// Defines synchronization primitives.
743
// A dummy implementation of synchronization primitives (mutex, lock,
744
// and thread-local variable). Necessary for compiling Google Test where
745
// mutex is not supported - using Google Test in multiple threads is not
746
// supported on such platforms.
751
explicit Mutex(int /*unused*/) {}
752
void AssertHeld() const {}
753
enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 };
756
// We cannot call it MutexLock directly as the ctor declaration would
757
// conflict with a macro named MutexLock, which is defined on some
758
// platforms. Hence the typedef trick below.
759
class GTestMutexLock {
761
explicit GTestMutexLock(Mutex*) {} // NOLINT
764
typedef GTestMutexLock MutexLock;
766
template <typename T>
769
ThreadLocal() : value_() {}
770
explicit ThreadLocal(const T& value) : value_(value) {}
771
T* pointer() { return &value_; }
772
const T* pointer() const { return &value_; }
773
const T& get() const { return value_; }
774
void set(const T& value) { value_ = value; }
779
// Returns the number of threads running in the process, or 0 to indicate that
780
// we cannot detect it.
781
size_t GetThreadCount();
783
// The above synchronization primitives have dummy implementations.
784
// Therefore Google Test is not thread-safe.
785
#define GTEST_IS_THREADSAFE 0
787
#if defined(__SYMBIAN32__) || defined(__IBMCPP__)
789
// Passing non-POD classes through ellipsis (...) crashes the ARM
790
// compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to
791
// instantiate a copy constructor for objects passed through ellipsis
792
// (...), failing for uncopyable objects. We define this to indicate
794
#define GTEST_ELLIPSIS_NEEDS_COPY_ 1
796
// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
797
// const T& and const T* in a function template. These compilers
798
// _can_ decide between class template specializations for T and T*,
799
// so a tr1::type_traits-like is_pointer works.
800
#define GTEST_NEEDS_IS_POINTER_ 1
802
#endif // defined(__SYMBIAN32__) || defined(__IBMCPP__)
804
template <bool bool_value>
805
struct bool_constant {
806
typedef bool_constant<bool_value> type;
807
static const bool value = bool_value;
809
template <bool bool_value> const bool bool_constant<bool_value>::value;
811
typedef bool_constant<false> false_type;
812
typedef bool_constant<true> true_type;
814
template <typename T>
815
struct is_pointer : public false_type {};
817
template <typename T>
818
struct is_pointer<T*> : public true_type {};
821
#define GTEST_PATH_SEP_ "\\"
822
// The biggest signed integer type the compiler supports.
823
typedef __int64 BiggestInt;
825
#define GTEST_PATH_SEP_ "/"
826
typedef long long BiggestInt; // NOLINT
827
#endif // GTEST_OS_WINDOWS
829
// The testing::internal::posix namespace holds wrappers for common
830
// POSIX functions. These wrappers hide the differences between
831
// Windows/MSVC and POSIX systems. Since some compilers define these
832
// standard functions as macros, the wrapper cannot have the same name
833
// as the wrapped function.
837
// Functions with a different name on Windows.
841
typedef struct _stat StatStruct;
844
inline int IsATTY(int fd) { return isatty(fd); }
845
inline int StrCaseCmp(const char* s1, const char* s2) {
846
return stricmp(s1, s2);
848
inline char* StrDup(const char* src) { return strdup(src); }
849
#else // !__BORLANDC__
850
#if GTEST_OS_WINDOWS_MOBILE
851
inline int IsATTY(int /* fd */) { return 0; }
853
inline int IsATTY(int fd) { return _isatty(fd); }
854
#endif // GTEST_OS_WINDOWS_MOBILE
855
inline int StrCaseCmp(const char* s1, const char* s2) {
856
return _stricmp(s1, s2);
858
inline char* StrDup(const char* src) { return _strdup(src); }
859
#endif // __BORLANDC__
861
#if GTEST_OS_WINDOWS_MOBILE
862
inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
863
// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
864
// time and thus not defined there.
866
inline int FileNo(FILE* file) { return _fileno(file); }
867
inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
868
inline int RmDir(const char* dir) { return _rmdir(dir); }
869
inline bool IsDir(const StatStruct& st) {
870
return (_S_IFDIR & st.st_mode) != 0;
872
#endif // GTEST_OS_WINDOWS_MOBILE
876
typedef struct stat StatStruct;
878
inline int FileNo(FILE* file) { return fileno(file); }
879
inline int IsATTY(int fd) { return isatty(fd); }
880
inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
881
inline int StrCaseCmp(const char* s1, const char* s2) {
882
return strcasecmp(s1, s2);
884
inline char* StrDup(const char* src) { return strdup(src); }
885
inline int RmDir(const char* dir) { return rmdir(dir); }
886
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
888
#endif // GTEST_OS_WINDOWS
890
// Functions deprecated by MSVC 8.0.
893
// Temporarily disable warning 4996 (deprecated function).
894
#pragma warning(push)
895
#pragma warning(disable:4996)
898
inline const char* StrNCpy(char* dest, const char* src, size_t n) {
899
return strncpy(dest, src, n);
902
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
903
// StrError() aren't needed on Windows CE at this time and thus not
906
#if !GTEST_OS_WINDOWS_MOBILE
907
inline int ChDir(const char* dir) { return chdir(dir); }
909
inline FILE* FOpen(const char* path, const char* mode) {
910
return fopen(path, mode);
912
#if !GTEST_OS_WINDOWS_MOBILE
913
inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
914
return freopen(path, mode, stream);
916
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
918
inline int FClose(FILE* fp) { return fclose(fp); }
919
#if !GTEST_OS_WINDOWS_MOBILE
920
inline int Read(int fd, void* buf, unsigned int count) {
921
return static_cast<int>(read(fd, buf, count));
923
inline int Write(int fd, const void* buf, unsigned int count) {
924
return static_cast<int>(write(fd, buf, count));
926
inline int Close(int fd) { return close(fd); }
927
inline const char* StrError(int errnum) { return strerror(errnum); }
929
inline const char* GetEnv(const char* name) {
930
#if GTEST_OS_WINDOWS_MOBILE
931
// We are on Windows CE, which has no environment variables.
933
#elif defined(__BORLANDC__)
934
// Environment variables which we programmatically clear will be set to the
935
// empty string rather than unset (NULL). Handle that case.
936
const char* const env = getenv(name);
937
return (env != NULL && env[0] != '\0') ? env : NULL;
944
#pragma warning(pop) // Restores the warning state.
947
#if GTEST_OS_WINDOWS_MOBILE
948
// Windows CE has no C library. The abort() function is used in
949
// several places in Google Test. This implementation provides a reasonable
950
// imitation of standard behaviour.
953
inline void Abort() { abort(); }
954
#endif // GTEST_OS_WINDOWS_MOBILE
958
// The maximum number a BiggestInt can represent. This definition
959
// works no matter BiggestInt is represented in one's complement or
962
// We cannot rely on numeric_limits in STL, as __int64 and long long
963
// are not part of standard C++ and numeric_limits doesn't need to be
965
const BiggestInt kMaxBiggestInt =
966
~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
968
// This template class serves as a compile-time function from size to
969
// type. It maps a size in bytes to a primitive type with that
972
// TypeWithSize<4>::UInt
974
// is typedef-ed to be unsigned int (unsigned integer made up of 4
977
// Such functionality should belong to STL, but I cannot find it
980
// Google Test uses this class in the implementation of floating-point
983
// For now it only handles UInt (unsigned int) as that's all Google Test
984
// needs. Other types can be easily added in the future if need
986
template <size_t size>
989
// This prevents the user from using TypeWithSize<N> with incorrect
994
// The specialization for size 4.
996
class TypeWithSize<4> {
998
// unsigned int has size 4 in both gcc and MSVC.
1000
// As base/basictypes.h doesn't compile on Windows, we cannot use
1001
// uint32, uint64, and etc here.
1003
typedef unsigned int UInt;
1006
// The specialization for size 8.
1008
class TypeWithSize<8> {
1010
#if GTEST_OS_WINDOWS
1011
typedef __int64 Int;
1012
typedef unsigned __int64 UInt;
1014
typedef long long Int; // NOLINT
1015
typedef unsigned long long UInt; // NOLINT
1016
#endif // GTEST_OS_WINDOWS
1019
// Integer types of known sizes.
1020
typedef TypeWithSize<4>::Int Int32;
1021
typedef TypeWithSize<4>::UInt UInt32;
1022
typedef TypeWithSize<8>::Int Int64;
1023
typedef TypeWithSize<8>::UInt UInt64;
1024
typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
1026
// Utilities for command line flags and environment variables.
1028
// INTERNAL IMPLEMENTATION - DO NOT USE.
1030
// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1031
// is not satisfied.
1033
// GTEST_CHECK_(boolean_condition);
1035
// GTEST_CHECK_(boolean_condition) << "Additional message";
1037
// This checks the condition and if the condition is not satisfied
1038
// it prints message about the condition violation, including the
1039
// condition itself, plus additional message streamed into it, if any,
1040
// and then it aborts the program. It aborts the program irrespective of
1041
// whether it is built in the debug mode or not.
1042
#define GTEST_CHECK_(condition) \
1043
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1044
if (::testing::internal::IsTrue(condition)) \
1047
GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1049
// Macro for referencing flags.
1050
#define GTEST_FLAG(name) FLAGS_gtest_##name
1052
// Macros for declaring flags.
1053
#define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name)
1054
#define GTEST_DECLARE_int32_(name) \
1055
extern ::testing::internal::Int32 GTEST_FLAG(name)
1056
#define GTEST_DECLARE_string_(name) \
1057
extern ::testing::internal::String GTEST_FLAG(name)
1059
// Macros for defining flags.
1060
#define GTEST_DEFINE_bool_(name, default_val, doc) \
1061
bool GTEST_FLAG(name) = (default_val)
1062
#define GTEST_DEFINE_int32_(name, default_val, doc) \
1063
::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
1064
#define GTEST_DEFINE_string_(name, default_val, doc) \
1065
::testing::internal::String GTEST_FLAG(name) = (default_val)
1067
// Parses 'str' for a 32-bit signed integer. If successful, writes the result
1068
// to *value and returns true; otherwise leaves *value unchanged and returns
1070
// TODO(chandlerc): Find a better way to refactor flag and environment parsing
1071
// out of both gtest-port.cc and gtest.cc to avoid exporting this utility
1073
bool ParseInt32(const Message& src_text, const char* str, Int32* value);
1075
// Parses a bool/Int32/string from the environment variable
1076
// corresponding to the given Google Test flag.
1077
bool BoolFromGTestEnv(const char* flag, bool default_val);
1078
Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
1079
const char* StringFromGTestEnv(const char* flag, const char* default_val);
1081
} // namespace internal
1082
} // namespace testing
1084
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_