Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gtest-port.h
Go to the documentation of this file.
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. All macros ending with _ and symbols defined in an
34// internal namespace are subject to change without notice. Code
35// outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't
36// end with _ are part of Google Test's public API and can be used by
37// code outside Google Test.
38//
39// This file is fundamental to Google Test. All other Google Test source
40// files are expected to #include this. Therefore, it cannot #include
41// any other Google Test header.
42
43#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
44#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
45
46// Environment-describing macros
47// -----------------------------
48//
49// Google Test can be used in many different environments. Macros in
50// this section tell Google Test what kind of environment it is being
51// used in, such that Google Test can provide environment-specific
52// features and implementations.
53//
54// Google Test tries to automatically detect the properties of its
55// environment, so users usually don't need to worry about these
56// macros. However, the automatic detection is not perfect.
57// Sometimes it's necessary for a user to define some of the following
58// macros in the build script to override Google Test's decisions.
59//
60// If the user doesn't define a macro in the list, Google Test will
61// provide a default definition. After this header is #included, all
62// macros in this list will be defined to either 1 or 0.
63//
64// Notes to maintainers:
65// - Each macro here is a user-tweakable knob; do not grow the list
66// lightly.
67// - Use #if to key off these macros. Don't use #ifdef or "#if
68// defined(...)", which will not work as these macros are ALWAYS
69// defined.
70//
71// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
72// is/isn't available.
73// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
74// are enabled.
75// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
76// is/isn't available
77// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::wstring
78// is/isn't available
79// GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular
80// expressions are/aren't available.
81// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
82// is/isn't available.
83// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
84// enabled.
85// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
86// std::wstring does/doesn't work (Google Test can
87// be used where std::wstring is unavailable).
88// GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
89// is/isn't available.
90// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
91// compiler supports Microsoft's "Structured
92// Exception Handling".
93// GTEST_HAS_STREAM_REDIRECTION
94// - Define it to 1/0 to indicate whether the
95// platform supports I/O stream redirection using
96// dup() and dup2().
97// GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
98// Test's own tr1 tuple implementation should be
99// used. Unused when the user sets
100// GTEST_HAS_TR1_TUPLE to 0.
101// GTEST_LANG_CXX11 - Define it to 1/0 to indicate that Google Test
102// is building in C++11/C++98 mode.
103// GTEST_LINKED_AS_SHARED_LIBRARY
104// - Define to 1 when compiling tests that use
105// Google Test as a shared library (known as
106// DLL on Windows).
107// GTEST_CREATE_SHARED_LIBRARY
108// - Define to 1 when compiling Google Test itself
109// as a shared library.
110// GTEST_DEFAULT_DEATH_TEST_STYLE
111// - The default value of --gtest_death_test_style.
112// The legacy default has been "fast" in the open
113// source version since 2008. The recommended value
114// is "threadsafe", and can be set in
115// custom/gtest-port.h.
116
117// Platform-indicating macros
118// --------------------------
119//
120// Macros indicating the platform on which Google Test is being used
121// (a macro is defined to 1 if compiled on the given platform;
122// otherwise UNDEFINED -- it's never defined to 0.). Google Test
123// defines these macros automatically. Code outside Google Test MUST
124// NOT define them.
125//
126// GTEST_OS_AIX - IBM AIX
127// GTEST_OS_CYGWIN - Cygwin
128// GTEST_OS_FREEBSD - FreeBSD
129// GTEST_OS_FUCHSIA - Fuchsia
130// GTEST_OS_HPUX - HP-UX
131// GTEST_OS_LINUX - Linux
132// GTEST_OS_LINUX_ANDROID - Google Android
133// GTEST_OS_MAC - Mac OS X
134// GTEST_OS_IOS - iOS
135// GTEST_OS_NACL - Google Native Client (NaCl)
136// GTEST_OS_NETBSD - NetBSD
137// GTEST_OS_OPENBSD - OpenBSD
138// GTEST_OS_QNX - QNX
139// GTEST_OS_SOLARIS - Sun Solaris
140// GTEST_OS_SYMBIAN - Symbian
141// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
142// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
143// GTEST_OS_WINDOWS_MINGW - MinGW
144// GTEST_OS_WINDOWS_MOBILE - Windows Mobile
145// GTEST_OS_WINDOWS_PHONE - Windows Phone
146// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
147// GTEST_OS_ZOS - z/OS
148//
149// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
150// most stable support. Since core members of the Google Test project
151// don't have access to other platforms, support for them may be less
152// stable. If you notice any problems on your platform, please notify
153// googletestframework@googlegroups.com (patches for fixing them are
154// even more welcome!).
155//
156// It is possible that none of the GTEST_OS_* macros are defined.
157
158// Feature-indicating macros
159// -------------------------
160//
161// Macros indicating which Google Test features are available (a macro
162// is defined to 1 if the corresponding feature is supported;
163// otherwise UNDEFINED -- it's never defined to 0.). Google Test
164// defines these macros automatically. Code outside Google Test MUST
165// NOT define them.
166//
167// These macros are public so that portable tests can be written.
168// Such tests typically surround code using a feature with an #if
169// which controls that code. For example:
170//
171// #if GTEST_HAS_DEATH_TEST
172// EXPECT_DEATH(DoSomethingDeadly());
173// #endif
174//
175// GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
176// tests)
177// GTEST_HAS_DEATH_TEST - death tests
178// GTEST_HAS_TYPED_TEST - typed tests
179// GTEST_HAS_TYPED_TEST_P - type-parameterized tests
180// GTEST_IS_THREADSAFE - Google Test is thread-safe.
181// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
182// GTEST_HAS_POSIX_RE (see above) which users can
183// define themselves.
184// GTEST_USES_SIMPLE_RE - our own simple regex is used;
185// the above RE\b(s) are mutually exclusive.
186// GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
187
188// Misc public macros
189// ------------------
190//
191// GTEST_FLAG(flag_name) - references the variable corresponding to
192// the given Google Test flag.
193
194// Internal utilities
195// ------------------
196//
197// The following macros and utilities are for Google Test's INTERNAL
198// use only. Code outside Google Test MUST NOT USE THEM DIRECTLY.
199//
200// Macros for basic C++ coding:
201// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
202// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
203// variable don't have to be used.
204// GTEST_DISALLOW_ASSIGN_ - disables operator=.
205// GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
206// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
207// GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
208// suppressed (constant conditional).
209// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
210// is suppressed.
211//
212// C++11 feature wrappers:
213//
214// testing::internal::forward - portability wrapper for std::forward.
215// testing::internal::move - portability wrapper for std::move.
216//
217// Synchronization:
218// Mutex, MutexLock, ThreadLocal, GetThreadCount()
219// - synchronization primitives.
220//
221// Template meta programming:
222// is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
223// IteratorTraits - partial implementation of std::iterator_traits, which
224// is not available in libCstd when compiled with Sun C++.
225//
226// Smart pointers:
227// scoped_ptr - as in TR2.
228//
229// Regular expressions:
230// RE - a simple regular expression class using the POSIX
231// Extended Regular Expression syntax on UNIX-like platforms
232// or a reduced regular exception syntax on other
233// platforms, including Windows.
234// Logging:
235// GTEST_LOG_() - logs messages at the specified severity level.
236// LogToStderr() - directs all log messages to stderr.
237// FlushInfoLog() - flushes informational log messages.
238//
239// Stdout and stderr capturing:
240// CaptureStdout() - starts capturing stdout.
241// GetCapturedStdout() - stops capturing stdout and returns the captured
242// string.
243// CaptureStderr() - starts capturing stderr.
244// GetCapturedStderr() - stops capturing stderr and returns the captured
245// string.
246//
247// Integer types:
248// TypeWithSize - maps an integer to a int type.
249// Int32, UInt32, Int64, UInt64, TimeInMillis
250// - integers of known sizes.
251// BiggestInt - the biggest signed integer type.
252//
253// Command-line utilities:
254// GTEST_DECLARE_*() - declares a flag.
255// GTEST_DEFINE_*() - defines a flag.
256// GetInjectableArgvs() - returns the command line as a vector of strings.
257//
258// Environment variable utilities:
259// GetEnv() - gets the value of an environment variable.
260// BoolFromGTestEnv() - parses a bool environment variable.
261// Int32FromGTestEnv() - parses an Int32 environment variable.
262// StringFromGTestEnv() - parses a string environment variable.
263
264#include <ctype.h> // for isspace, etc
265#include <stddef.h> // for ptrdiff_t
266#include <stdlib.h>
267#include <stdio.h>
268#include <string.h>
269#ifndef _WIN32_WCE
270# include <sys/types.h>
271# include <sys/stat.h>
272#endif // !_WIN32_WCE
273
274#if defined __APPLE__
275# include <AvailabilityMacros.h>
276# include <TargetConditionals.h>
277#endif
278
279// Brings in the definition of HAS_GLOBAL_STRING. This must be done
280// BEFORE we test HAS_GLOBAL_STRING.
281#include <string> // NOLINT
282#include <algorithm> // NOLINT
283#include <iostream> // NOLINT
284#include <sstream> // NOLINT
285#include <utility>
286#include <vector> // NOLINT
287
290
291#if !defined(GTEST_DEV_EMAIL_)
292# define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
293# define GTEST_FLAG_PREFIX_ "gtest_"
294# define GTEST_FLAG_PREFIX_DASH_ "gtest-"
295# define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
296# define GTEST_NAME_ "Google Test"
297# define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
298#endif // !defined(GTEST_DEV_EMAIL_)
299
300#if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
301# define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
302#endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
303
304// Determines the version of gcc that is used to compile this.
305#ifdef __GNUC__
306// 40302 means version 4.3.2.
307# define GTEST_GCC_VER_ \
308 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
309#endif // __GNUC__
310
311// Macros for disabling Microsoft Visual C++ warnings.
312//
313// GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)
314// /* code that triggers warnings C4800 and C4385 */
315// GTEST_DISABLE_MSC_WARNINGS_POP_()
316#if _MSC_VER >= 1500
317# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
318 __pragma(warning(push)) \
319 __pragma(warning(disable: warnings))
320# define GTEST_DISABLE_MSC_WARNINGS_POP_() \
321 __pragma(warning(pop))
322#else
323// Older versions of MSVC don't have __pragma.
324# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
325# define GTEST_DISABLE_MSC_WARNINGS_POP_()
326#endif
327
328#ifndef GTEST_LANG_CXX11
329// gcc and clang define __GXX_EXPERIMENTAL_CXX0X__ when
330// -std={c,gnu}++{0x,11} is passed. The C++11 standard specifies a
331// value for __cplusplus, and recent versions of clang, gcc, and
332// probably other compilers set that too in C++11 mode.
333# if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L || _MSC_VER >= 1900
334// Compiling in at least C++11 mode.
335# define GTEST_LANG_CXX11 1
336# else
337# define GTEST_LANG_CXX11 0
338# endif
339#endif
340
341// Distinct from C++11 language support, some environments don't provide
342// proper C++11 library support. Notably, it's possible to build in
343// C++11 mode when targeting Mac OS X 10.6, which has an old libstdc++
344// with no C++11 support.
345//
346// libstdc++ has sufficient C++11 support as of GCC 4.6.0, __GLIBCXX__
347// 20110325, but maintenance releases in the 4.4 and 4.5 series followed
348// this date, so check for those versions by their date stamps.
349// https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html#abi.versioning
350#if GTEST_LANG_CXX11 && \
351 (!defined(__GLIBCXX__) || ( \
352 __GLIBCXX__ >= 20110325ul && /* GCC >= 4.6.0 */ \
353 /* Blacklist of patch releases of older branches: */ \
354 __GLIBCXX__ != 20110416ul && /* GCC 4.4.6 */ \
355 __GLIBCXX__ != 20120313ul && /* GCC 4.4.7 */ \
356 __GLIBCXX__ != 20110428ul && /* GCC 4.5.3 */ \
357 __GLIBCXX__ != 20120702ul)) /* GCC 4.5.4 */
358# define GTEST_STDLIB_CXX11 1
359#endif
360
361// Only use C++11 library features if the library provides them.
362#if GTEST_STDLIB_CXX11
363# define GTEST_HAS_STD_BEGIN_AND_END_ 1
364# define GTEST_HAS_STD_FORWARD_LIST_ 1
365# if !defined(_MSC_VER) || (_MSC_FULL_VER >= 190023824)
366// works only with VS2015U2 and better
367# define GTEST_HAS_STD_FUNCTION_ 1
368# endif
369# define GTEST_HAS_STD_INITIALIZER_LIST_ 1
370# define GTEST_HAS_STD_MOVE_ 1
371# define GTEST_HAS_STD_UNIQUE_PTR_ 1
372# define GTEST_HAS_STD_SHARED_PTR_ 1
373# define GTEST_HAS_UNORDERED_MAP_ 1
374# define GTEST_HAS_UNORDERED_SET_ 1
375#endif
376
377// C++11 specifies that <tuple> provides std::tuple.
378// Some platforms still might not have it, however.
379#if GTEST_LANG_CXX11
380# define GTEST_HAS_STD_TUPLE_ 1
381# if defined(__clang__)
382// Inspired by http://clang.llvm.org/docs/LanguageExtensions.html#__has_include
383# if defined(__has_include) && !__has_include(<tuple>)
384# undef GTEST_HAS_STD_TUPLE_
385# endif
386# elif defined(_MSC_VER)
387// Inspired by boost/config/stdlib/dinkumware.hpp
388# if defined(_CPPLIB_VER) && _CPPLIB_VER < 520
389# undef GTEST_HAS_STD_TUPLE_
390# endif
391# elif defined(__GLIBCXX__)
392// Inspired by boost/config/stdlib/libstdcpp3.hpp,
393// http://gcc.gnu.org/gcc-4.2/changes.html and
394// http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch01.html#manual.intro.status.standard.200x
395# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)
396# undef GTEST_HAS_STD_TUPLE_
397# endif
398# endif
399#endif
400
401// Brings in definitions for functions used in the testing::internal::posix
402// namespace (read, write, close, chdir, isatty, stat). We do not currently
403// use them on Windows Mobile.
404#if GTEST_OS_WINDOWS
405# if !GTEST_OS_WINDOWS_MOBILE
406# include <direct.h>
407# include <io.h>
408# endif
409// In order to avoid having to include <windows.h>, use forward declaration
410#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
411// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
412// separate (equivalent) structs, instead of using typedef
413typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
414#else
415// Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
416// This assumption is verified by
417// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
418typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
419#endif
420#else
421// This assumes that non-Windows OSes provide unistd.h. For OSes where this
422// is not the case, we need to include headers that provide the functions
423// mentioned above.
424# include <unistd.h>
425# include <strings.h>
426#endif // GTEST_OS_WINDOWS
427
428#if GTEST_OS_LINUX_ANDROID
429// Used to define __ANDROID_API__ matching the target NDK API level.
430# include <android/api-level.h> // NOLINT
431#endif
432
433// Defines this to true iff Google Test can use POSIX regular expressions.
434#ifndef GTEST_HAS_POSIX_RE
435# if GTEST_OS_LINUX_ANDROID
436// On Android, <regex.h> is only available starting with Gingerbread.
437# define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
438# else
439# define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
440# endif
441#endif
442
443#if GTEST_USES_PCRE
444// The appropriate headers have already been included.
445
446#elif GTEST_HAS_POSIX_RE
447
448// On some platforms, <regex.h> needs someone to define size_t, and
449// won't compile otherwise. We can #include it here as we already
450// included <stdlib.h>, which is guaranteed to define size_t through
451// <stddef.h>.
452# include <regex.h> // NOLINT
453
454# define GTEST_USES_POSIX_RE 1
455
456#elif GTEST_OS_WINDOWS
457
458// <regex.h> is not available on Windows. Use our own simple regex
459// implementation instead.
460# define GTEST_USES_SIMPLE_RE 1
461
462#else
463
464// <regex.h> may not be available on this platform. Use our own
465// simple regex implementation instead.
466# define GTEST_USES_SIMPLE_RE 1
467
468#endif // GTEST_USES_PCRE
469
470#ifndef GTEST_HAS_EXCEPTIONS
471// The user didn't tell us whether exceptions are enabled, so we need
472// to figure it out.
473# if defined(_MSC_VER) && defined(_CPPUNWIND)
474// MSVC defines _CPPUNWIND to 1 iff exceptions are enabled.
475# define GTEST_HAS_EXCEPTIONS 1
476# elif defined(__BORLANDC__)
477// C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS
478// macro to enable exceptions, so we'll do the same.
479// Assumes that exceptions are enabled by default.
480# ifndef _HAS_EXCEPTIONS
481# define _HAS_EXCEPTIONS 1
482# endif // _HAS_EXCEPTIONS
483# define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
484# elif defined(__clang__)
485// clang defines __EXCEPTIONS iff exceptions are enabled before clang 220714,
486// but iff cleanups are enabled after that. In Obj-C++ files, there can be
487// cleanups for ObjC exceptions which also need cleanups, even if C++ exceptions
488// are disabled. clang has __has_feature(cxx_exceptions) which checks for C++
489// exceptions starting at clang r206352, but which checked for cleanups prior to
490// that. To reliably check for C++ exception availability with clang, check for
491// __EXCEPTIONS && __has_feature(cxx_exceptions).
492# define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
493# elif defined(__GNUC__) && __EXCEPTIONS
494// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
495# define GTEST_HAS_EXCEPTIONS 1
496# elif defined(__SUNPRO_CC)
497// Sun Pro CC supports exceptions. However, there is no compile-time way of
498// detecting whether they are enabled or not. Therefore, we assume that
499// they are enabled unless the user tells us otherwise.
500# define GTEST_HAS_EXCEPTIONS 1
501# elif defined(__IBMCPP__) && __EXCEPTIONS
502// xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
503# define GTEST_HAS_EXCEPTIONS 1
504# elif defined(__HP_aCC)
505// Exception handling is in effect by default in HP aCC compiler. It has to
506// be turned of by +noeh compiler option if desired.
507# define GTEST_HAS_EXCEPTIONS 1
508# else
509// For other compilers, we assume exceptions are disabled to be
510// conservative.
511# define GTEST_HAS_EXCEPTIONS 0
512# endif // defined(_MSC_VER) || defined(__BORLANDC__)
513#endif // GTEST_HAS_EXCEPTIONS
514
515#if !defined(GTEST_HAS_STD_STRING)
516// Even though we don't use this macro any longer, we keep it in case
517// some clients still depend on it.
518# define GTEST_HAS_STD_STRING 1
519#elif !GTEST_HAS_STD_STRING
520// The user told us that ::std::string isn't available.
521# error "::std::string isn't available."
522#endif // !defined(GTEST_HAS_STD_STRING)
523
524#ifndef GTEST_HAS_GLOBAL_STRING
525// The user didn't tell us whether ::string is available, so we need
526// to figure it out.
527
528# define GTEST_HAS_GLOBAL_STRING 0
529
530#endif // GTEST_HAS_GLOBAL_STRING
531
532#ifndef GTEST_HAS_STD_WSTRING
533// The user didn't tell us whether ::std::wstring is available, so we need
534// to figure it out.
535// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
536// is available.
537
538// Cygwin 1.7 and below doesn't support ::std::wstring.
539// Solaris' libc++ doesn't support it either. Android has
540// no support for it at least as recent as Froyo (2.2).
541# define GTEST_HAS_STD_WSTRING \
542 (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
543
544#endif // GTEST_HAS_STD_WSTRING
545
546#ifndef GTEST_HAS_GLOBAL_WSTRING
547// The user didn't tell us whether ::wstring is available, so we need
548// to figure it out.
549# define GTEST_HAS_GLOBAL_WSTRING \
550 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
551#endif // GTEST_HAS_GLOBAL_WSTRING
552
553// Determines whether RTTI is available.
554#ifndef GTEST_HAS_RTTI
555// The user didn't tell us whether RTTI is enabled, so we need to
556// figure it out.
557
558# ifdef _MSC_VER
559
560# ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
561# define GTEST_HAS_RTTI 1
562# else
563# define GTEST_HAS_RTTI 0
564# endif
565
566// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
567# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
568
569# ifdef __GXX_RTTI
570// When building against STLport with the Android NDK and with
571// -frtti -fno-exceptions, the build fails at link time with undefined
572// references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
573// so disable RTTI when detected.
574# if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
575 !defined(__EXCEPTIONS)
576# define GTEST_HAS_RTTI 0
577# else
578# define GTEST_HAS_RTTI 1
579# endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
580# else
581# define GTEST_HAS_RTTI 0
582# endif // __GXX_RTTI
583
584// Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
585// using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
586// first version with C++ support.
587# elif defined(__clang__)
588
589# define GTEST_HAS_RTTI __has_feature(cxx_rtti)
590
591// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
592// both the typeid and dynamic_cast features are present.
593# elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
594
595# ifdef __RTTI_ALL__
596# define GTEST_HAS_RTTI 1
597# else
598# define GTEST_HAS_RTTI 0
599# endif
600
601# else
602
603// For all other compilers, we assume RTTI is enabled.
604# define GTEST_HAS_RTTI 1
605
606# endif // _MSC_VER
607
608#endif // GTEST_HAS_RTTI
609
610// It's this header's responsibility to #include <typeinfo> when RTTI
611// is enabled.
612#if GTEST_HAS_RTTI
613# include <typeinfo>
614#endif
615
616// Determines whether Google Test can use the pthreads library.
617#ifndef GTEST_HAS_PTHREAD
618// The user didn't tell us explicitly, so we make reasonable assumptions about
619// which platforms have pthreads support.
620//
621// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
622// to your compiler flags.
623#define GTEST_HAS_PTHREAD \
624 (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \
625 GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA)
626#endif // GTEST_HAS_PTHREAD
627
628#if GTEST_HAS_PTHREAD
629// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
630// true.
631# include <pthread.h> // NOLINT
632
633// For timespec and nanosleep, used below.
634# include <time.h> // NOLINT
635#endif
636
637// Determines if hash_map/hash_set are available.
638// Only used for testing against those containers.
639#if !defined(GTEST_HAS_HASH_MAP_)
640# if defined(_MSC_VER) && (_MSC_VER < 1900)
641# define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
642# define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
643# endif // _MSC_VER
644#endif // !defined(GTEST_HAS_HASH_MAP_)
645
646// Determines whether Google Test can use tr1/tuple. You can define
647// this macro to 0 to prevent Google Test from using tuple (any
648// feature depending on tuple with be disabled in this mode).
649#ifndef GTEST_HAS_TR1_TUPLE
650# if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
651// STLport, provided with the Android NDK, has neither <tr1/tuple> or <tuple>.
652# define GTEST_HAS_TR1_TUPLE 0
653# elif defined(_MSC_VER) && (_MSC_VER >= 1910)
654// Prevent `warning C4996: 'std::tr1': warning STL4002:
655// The non-Standard std::tr1 namespace and TR1-only machinery
656// are deprecated and will be REMOVED.`
657# define GTEST_HAS_TR1_TUPLE 0
658# elif GTEST_LANG_CXX11 && defined(_LIBCPP_VERSION)
659// libc++ doesn't support TR1.
660# define GTEST_HAS_TR1_TUPLE 0
661# else
662// The user didn't tell us not to do it, so we assume it's OK.
663# define GTEST_HAS_TR1_TUPLE 1
664# endif
665#endif // GTEST_HAS_TR1_TUPLE
666
667// Determines whether Google Test's own tr1 tuple implementation
668// should be used.
669#ifndef GTEST_USE_OWN_TR1_TUPLE
670// We use our own tuple implementation on Symbian.
671# if GTEST_OS_SYMBIAN
672# define GTEST_USE_OWN_TR1_TUPLE 1
673# else
674// The user didn't tell us, so we need to figure it out.
675
676// We use our own TR1 tuple if we aren't sure the user has an
677// implementation of it already. At this time, libstdc++ 4.0.0+ and
678// MSVC 2010 are the only mainstream standard libraries that come
679// with a TR1 tuple implementation. NVIDIA's CUDA NVCC compiler
680// pretends to be GCC by defining __GNUC__ and friends, but cannot
681// compile GCC's tuple implementation. MSVC 2008 (9.0) provides TR1
682// tuple in a 323 MB Feature Pack download, which we cannot assume the
683// user has. QNX's QCC compiler is a modified GCC but it doesn't
684// support TR1 tuple. libc++ only provides std::tuple, in C++11 mode,
685// and it can be used with some compilers that define __GNUC__.
686# if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
687 && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) \
688 || (_MSC_VER >= 1600 && _MSC_VER < 1900)
689# define GTEST_ENV_HAS_TR1_TUPLE_ 1
690# endif
691
692// C++11 specifies that <tuple> provides std::tuple. Use that if gtest is used
693// in C++11 mode and libstdc++ isn't very old (binaries targeting OS X 10.6
694// can build with clang but need to use gcc4.2's libstdc++).
695# if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
696# define GTEST_ENV_HAS_STD_TUPLE_ 1
697# endif
698
699# if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
700# define GTEST_USE_OWN_TR1_TUPLE 0
701# else
702# define GTEST_USE_OWN_TR1_TUPLE 1
703# endif
704# endif // GTEST_OS_SYMBIAN
705#endif // GTEST_USE_OWN_TR1_TUPLE
706
707// To avoid conditional compilation we make it gtest-port.h's responsibility
708// to #include the header implementing tuple.
709#if GTEST_HAS_STD_TUPLE_
710# include <tuple> // IWYU pragma: export
711# define GTEST_TUPLE_NAMESPACE_ ::std
712#endif // GTEST_HAS_STD_TUPLE_
713
714// We include tr1::tuple even if std::tuple is available to define printers for
715// them.
716#if GTEST_HAS_TR1_TUPLE
717# ifndef GTEST_TUPLE_NAMESPACE_
718# define GTEST_TUPLE_NAMESPACE_ ::std::tr1
719# endif // GTEST_TUPLE_NAMESPACE_
720
721# if GTEST_USE_OWN_TR1_TUPLE
722# include "gtest/internal/gtest-tuple.h" // IWYU pragma: export // NOLINT
723# elif GTEST_OS_SYMBIAN
724
725// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
726// use STLport's tuple implementation, which unfortunately doesn't
727// work as the copy of STLport distributed with Symbian is incomplete.
728// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
729// use its own tuple implementation.
730# ifdef BOOST_HAS_TR1_TUPLE
731# undef BOOST_HAS_TR1_TUPLE
732# endif // BOOST_HAS_TR1_TUPLE
733
734// This prevents <boost/tr1/detail/config.hpp>, which defines
735// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
736# define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
737# include <tuple> // IWYU pragma: export // NOLINT
738
739# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
740// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
741// not conform to the TR1 spec, which requires the header to be <tuple>.
742
743# if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
744// Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
745// which is #included by <tr1/tuple>, to not compile when RTTI is
746// disabled. _TR1_FUNCTIONAL is the header guard for
747// <tr1/functional>. Hence the following #define is a hack to prevent
748// <tr1/functional> from being included.
749# define _TR1_FUNCTIONAL 1
750# include <tr1/tuple>
751# undef _TR1_FUNCTIONAL // Allows the user to #include
752 // <tr1/functional> if they choose to.
753# else
754# include <tr1/tuple> // NOLINT
755# endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
756
757// VS 2010 now has tr1 support.
758# elif _MSC_VER >= 1600
759# include <tuple> // IWYU pragma: export // NOLINT
760
761# else // GTEST_USE_OWN_TR1_TUPLE
762# include <tr1/tuple> // IWYU pragma: export // NOLINT
763# endif // GTEST_USE_OWN_TR1_TUPLE
764
765#endif // GTEST_HAS_TR1_TUPLE
766
767// Determines whether clone(2) is supported.
768// Usually it will only be available on Linux, excluding
769// Linux on the Itanium architecture.
770// Also see http://linux.die.net/man/2/clone.
771#ifndef GTEST_HAS_CLONE
772// The user didn't tell us, so we need to figure it out.
773
774# if GTEST_OS_LINUX && !defined(__ia64__)
775# if GTEST_OS_LINUX_ANDROID
776// On Android, clone() became available at different API levels for each 32-bit
777// architecture.
778# if defined(__LP64__) || \
779 (defined(__arm__) && __ANDROID_API__ >= 9) || \
780 (defined(__mips__) && __ANDROID_API__ >= 12) || \
781 (defined(__i386__) && __ANDROID_API__ >= 17)
782# define GTEST_HAS_CLONE 1
783# else
784# define GTEST_HAS_CLONE 0
785# endif
786# else
787# define GTEST_HAS_CLONE 1
788# endif
789# else
790# define GTEST_HAS_CLONE 0
791# endif // GTEST_OS_LINUX && !defined(__ia64__)
792
793#endif // GTEST_HAS_CLONE
794
795// Determines whether to support stream redirection. This is used to test
796// output correctness and to implement death tests.
797#ifndef GTEST_HAS_STREAM_REDIRECTION
798// By default, we assume that stream redirection is supported on all
799// platforms except known mobile ones.
800# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || \
801 GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
802# define GTEST_HAS_STREAM_REDIRECTION 0
803# else
804# define GTEST_HAS_STREAM_REDIRECTION 1
805# endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
806#endif // GTEST_HAS_STREAM_REDIRECTION
807
808// Determines whether to support death tests.
809// Google Test does not support death tests for VC 7.1 and earlier as
810// abort() in a VC 7.1 application compiled as GUI in debug config
811// pops up a dialog window that cannot be suppressed programmatically.
812#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
813 (GTEST_OS_MAC && !GTEST_OS_IOS) || \
814 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
815 GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
816 GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD || \
817 GTEST_OS_NETBSD || GTEST_OS_FUCHSIA)
818# define GTEST_HAS_DEATH_TEST 1
819#endif
820
821// Determines whether to support type-driven tests.
822
823// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
824// Sun Pro CC, IBM Visual Age, and HP aCC support.
825#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
826 defined(__IBMCPP__) || defined(__HP_aCC)
827# define GTEST_HAS_TYPED_TEST 1
828# define GTEST_HAS_TYPED_TEST_P 1
829#endif
830
831// Determines whether to support Combine(). This only makes sense when
832// value-parameterized tests are enabled. The implementation doesn't
833// work on Sun Studio since it doesn't understand templated conversion
834// operators.
835#if (GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_) && !defined(__SUNPRO_CC)
836# define GTEST_HAS_COMBINE 1
837#endif
838
839// Determines whether the system compiler uses UTF-16 for encoding wide strings.
840#define GTEST_WIDE_STRING_USES_UTF16_ \
841 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
842
843// Determines whether test results can be streamed to a socket.
844#if GTEST_OS_LINUX
845# define GTEST_CAN_STREAM_RESULTS_ 1
846#endif
847
848// Defines some utility macros.
849
850// The GNU compiler emits a warning if nested "if" statements are followed by
851// an "else" statement and braces are not used to explicitly disambiguate the
852// "else" binding. This leads to problems with code like:
853//
854// if (gate)
855// ASSERT_*(condition) << "Some message";
856//
857// The "switch (0) case 0:" idiom is used to suppress this.
858#ifdef __INTEL_COMPILER
859# define GTEST_AMBIGUOUS_ELSE_BLOCKER_
860#else
861# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
862#endif
863
864// Use this annotation at the end of a struct/class definition to
865// prevent the compiler from optimizing away instances that are never
866// used. This is useful when all interesting logic happens inside the
867// c'tor and / or d'tor. Example:
868//
869// struct Foo {
870// Foo() { ... }
871// } GTEST_ATTRIBUTE_UNUSED_;
872//
873// Also use it after a variable or parameter declaration to tell the
874// compiler the variable/parameter does not have to be used.
875#if defined(__GNUC__) && !defined(COMPILER_ICC)
876# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
877#elif defined(__clang__)
878# if __has_attribute(unused)
879# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
880# endif
881#endif
882#ifndef GTEST_ATTRIBUTE_UNUSED_
883# define GTEST_ATTRIBUTE_UNUSED_
884#endif
885
886#if GTEST_LANG_CXX11
887# define GTEST_CXX11_EQUALS_DELETE_ = delete
888#else // GTEST_LANG_CXX11
889# define GTEST_CXX11_EQUALS_DELETE_
890#endif // GTEST_LANG_CXX11
891
892// Use this annotation before a function that takes a printf format string.
893#if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC)
894# if defined(__MINGW_PRINTF_FORMAT)
895// MinGW has two different printf implementations. Ensure the format macro
896// matches the selected implementation. See
897// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
898# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
899 __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \
900 first_to_check)))
901# else
902# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
903 __attribute__((__format__(__printf__, string_index, first_to_check)))
904# endif
905#else
906# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
907#endif
908
909
910// A macro to disallow operator=
911// This should be used in the private: declarations for a class.
912#define GTEST_DISALLOW_ASSIGN_(type) \
913 void operator=(type const &) GTEST_CXX11_EQUALS_DELETE_
914
915// A macro to disallow copy constructor and operator=
916// This should be used in the private: declarations for a class.
917#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type) \
918 type(type const &) GTEST_CXX11_EQUALS_DELETE_; \
919 GTEST_DISALLOW_ASSIGN_(type)
920
921// Tell the compiler to warn about unused return values for functions declared
922// with this macro. The macro should be used on function declarations
923// following the argument list:
924//
925// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
926#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
927# define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
928#else
929# define GTEST_MUST_USE_RESULT_
930#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
931
932// MS C++ compiler emits warning when a conditional expression is compile time
933// constant. In some contexts this warning is false positive and needs to be
934// suppressed. Use the following two macros in such cases:
935//
936// GTEST_INTENTIONAL_CONST_COND_PUSH_()
937// while (true) {
938// GTEST_INTENTIONAL_CONST_COND_POP_()
939// }
940# define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
941 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
942# define GTEST_INTENTIONAL_CONST_COND_POP_() \
943 GTEST_DISABLE_MSC_WARNINGS_POP_()
944
945// Determine whether the compiler supports Microsoft's Structured Exception
946// Handling. This is supported by several Windows compilers but generally
947// does not exist on any other system.
948#ifndef GTEST_HAS_SEH
949// The user didn't tell us, so we need to figure it out.
950
951# if defined(_MSC_VER) || defined(__BORLANDC__)
952// These two compilers are known to support SEH.
953# define GTEST_HAS_SEH 1
954# else
955// Assume no SEH.
956# define GTEST_HAS_SEH 0
957# endif
958
959#define GTEST_IS_THREADSAFE \
960 (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ \
961 || (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) \
962 || GTEST_HAS_PTHREAD)
963
964#endif // GTEST_HAS_SEH
965
966// GTEST_API_ qualifies all symbols that must be exported. The definitions below
967// are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
968// gtest/internal/custom/gtest-port.h
969#ifndef GTEST_API_
970
971#ifdef _MSC_VER
972# if GTEST_LINKED_AS_SHARED_LIBRARY
973# define GTEST_API_ __declspec(dllimport)
974# elif GTEST_CREATE_SHARED_LIBRARY
975# define GTEST_API_ __declspec(dllexport)
976# endif
977#elif __GNUC__ >= 4 || defined(__clang__)
978# define GTEST_API_ __attribute__((visibility ("default")))
979#endif // _MSC_VER
980
981#endif // GTEST_API_
982
983#ifndef GTEST_API_
984# define GTEST_API_
985#endif // GTEST_API_
986
987#ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
988# define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
989#endif // GTEST_DEFAULT_DEATH_TEST_STYLE
990
991#ifdef __GNUC__
992// Ask the compiler to never inline a given function.
993# define GTEST_NO_INLINE_ __attribute__((noinline))
994#else
995# define GTEST_NO_INLINE_
996#endif
997
998// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
999#if !defined(GTEST_HAS_CXXABI_H_)
1000# if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
1001# define GTEST_HAS_CXXABI_H_ 1
1002# else
1003# define GTEST_HAS_CXXABI_H_ 0
1004# endif
1005#endif
1006
1007// A function level attribute to disable checking for use of uninitialized
1008// memory when built with MemorySanitizer.
1009#if defined(__clang__)
1010# if __has_feature(memory_sanitizer)
1011# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
1012 __attribute__((no_sanitize_memory))
1013# else
1014# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
1015# endif // __has_feature(memory_sanitizer)
1016#else
1017# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
1018#endif // __clang__
1019
1020// A function level attribute to disable AddressSanitizer instrumentation.
1021#if defined(__clang__)
1022# if __has_feature(address_sanitizer)
1023# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
1024 __attribute__((no_sanitize_address))
1025# else
1026# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
1027# endif // __has_feature(address_sanitizer)
1028#else
1029# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
1030#endif // __clang__
1031
1032// A function level attribute to disable ThreadSanitizer instrumentation.
1033#if defined(__clang__)
1034# if __has_feature(thread_sanitizer)
1035# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
1036 __attribute__((no_sanitize_thread))
1037# else
1038# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
1039# endif // __has_feature(thread_sanitizer)
1040#else
1041# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
1042#endif // __clang__
1043
1044namespace testing {
1045
1046class Message;
1047
1048#if defined(GTEST_TUPLE_NAMESPACE_)
1049// Import tuple and friends into the ::testing namespace.
1050// It is part of our interface, having them in ::testing allows us to change
1051// their types as needed.
1052using GTEST_TUPLE_NAMESPACE_::get;
1053using GTEST_TUPLE_NAMESPACE_::make_tuple;
1054using GTEST_TUPLE_NAMESPACE_::tuple;
1055using GTEST_TUPLE_NAMESPACE_::tuple_size;
1056using GTEST_TUPLE_NAMESPACE_::tuple_element;
1057#endif // defined(GTEST_TUPLE_NAMESPACE_)
1058
1059namespace internal {
1060
1061// A secret type that Google Test users don't know about. It has no
1062// definition on purpose. Therefore it's impossible to create a
1063// Secret object, which is what we want.
1064class Secret;
1065
1066// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
1067// expression is true. For example, you could use it to verify the
1068// size of a static array:
1069//
1070// GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,
1071// names_incorrect_size);
1072//
1073// or to make sure a struct is smaller than a certain size:
1074//
1075// GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
1076//
1077// The second argument to the macro is the name of the variable. If
1078// the expression is false, most compilers will issue a warning/error
1079// containing the name of the variable.
1080
1081#if GTEST_LANG_CXX11
1082# define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
1083#else // !GTEST_LANG_CXX11
1084template <bool>
1086};
1087
1088# define GTEST_COMPILE_ASSERT_(expr, msg) \
1089 typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
1090 msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
1091#endif // !GTEST_LANG_CXX11
1092
1093// Implementation details of GTEST_COMPILE_ASSERT_:
1094//
1095// (In C++11, we simply use static_assert instead of the following)
1096//
1097// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
1098// elements (and thus is invalid) when the expression is false.
1099//
1100// - The simpler definition
1101//
1102// #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
1103//
1104// does not work, as gcc supports variable-length arrays whose sizes
1105// are determined at run-time (this is gcc's extension and not part
1106// of the C++ standard). As a result, gcc fails to reject the
1107// following code with the simple definition:
1108//
1109// int foo;
1110// GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
1111// // not a compile-time constant.
1112//
1113// - By using the type CompileAssert<(bool(expr))>, we ensures that
1114// expr is a compile-time constant. (Template arguments must be
1115// determined at compile-time.)
1116//
1117// - The outter parentheses in CompileAssert<(bool(expr))> are necessary
1118// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
1119//
1120// CompileAssert<bool(expr)>
1121//
1122// instead, these compilers will refuse to compile
1123//
1124// GTEST_COMPILE_ASSERT_(5 > 0, some_message);
1125//
1126// (They seem to think the ">" in "5 > 0" marks the end of the
1127// template argument list.)
1128//
1129// - The array size is (bool(expr) ? 1 : -1), instead of simply
1130//
1131// ((expr) ? 1 : -1).
1132//
1133// This is to avoid running into a bug in MS VC 7.1, which
1134// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
1135
1136// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
1137//
1138// This template is declared, but intentionally undefined.
1139template <typename T1, typename T2>
1141
1142template <typename T>
1144 enum { value = true };
1145};
1146
1147// Same as std::is_same<>.
1148template <typename T, typename U>
1149struct IsSame {
1150 enum { value = false };
1151};
1152template <typename T>
1153struct IsSame<T, T> {
1154 enum { value = true };
1155};
1156
1157// Evaluates to the number of elements in 'array'.
1158#define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
1159
1160#if GTEST_HAS_GLOBAL_STRING
1161typedef ::string string;
1162#else
1163typedef ::std::string string;
1164#endif // GTEST_HAS_GLOBAL_STRING
1165
1166#if GTEST_HAS_GLOBAL_WSTRING
1167typedef ::wstring wstring;
1168#elif GTEST_HAS_STD_WSTRING
1169typedef ::std::wstring wstring;
1170#endif // GTEST_HAS_GLOBAL_WSTRING
1171
1172// A helper for suppressing warnings on constant condition. It just
1173// returns 'condition'.
1174GTEST_API_ bool IsTrue(bool condition);
1175
1176// Defines scoped_ptr.
1177
1178// This implementation of scoped_ptr is PARTIAL - it only contains
1179// enough stuff to satisfy Google Test's need.
1180template <typename T>
1182 public:
1184
1185 explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
1187
1188 T& operator*() const { return *ptr_; }
1189 T* operator->() const { return ptr_; }
1190 T* get() const { return ptr_; }
1191
1193 T* const ptr = ptr_;
1194 ptr_ = NULL;
1195 return ptr;
1196 }
1197
1198 void reset(T* p = NULL) {
1199 if (p != ptr_) {
1200 if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
1201 delete ptr_;
1202 }
1203 ptr_ = p;
1204 }
1205 }
1206
1207 friend void swap(scoped_ptr& a, scoped_ptr& b) {
1208 using std::swap;
1209 swap(a.ptr_, b.ptr_);
1210 }
1211
1212 private:
1213 T* ptr_;
1214
1216};
1217
1218// Defines RE.
1219
1220#if GTEST_USES_PCRE
1221using ::RE;
1222#elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE
1223
1224// A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
1225// Regular Expression syntax.
1227 public:
1228 // A copy constructor is required by the Standard to initialize object
1229 // references from r-values.
1230 RE(const RE& other) { Init(other.pattern()); }
1231
1232 // Constructs an RE from a string.
1233 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
1234
1235# if GTEST_HAS_GLOBAL_STRING
1236
1237 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
1238
1239# endif // GTEST_HAS_GLOBAL_STRING
1240
1241 RE(const char* regex) { Init(regex); } // NOLINT
1243
1244 // Returns the string representation of the regex.
1245 const char* pattern() const { return pattern_; }
1246
1247 // FullMatch(str, re) returns true iff regular expression re matches
1248 // the entire str.
1249 // PartialMatch(str, re) returns true iff regular expression re
1250 // matches a substring of str (including str itself).
1251 //
1252 // TODO(wan@google.com): make FullMatch() and PartialMatch() work
1253 // when str contains NUL characters.
1254 static bool FullMatch(const ::std::string& str, const RE& re) {
1255 return FullMatch(str.c_str(), re);
1256 }
1257 static bool PartialMatch(const ::std::string& str, const RE& re) {
1258 return PartialMatch(str.c_str(), re);
1259 }
1260
1261# if GTEST_HAS_GLOBAL_STRING
1262
1263 static bool FullMatch(const ::string& str, const RE& re) {
1264 return FullMatch(str.c_str(), re);
1265 }
1266 static bool PartialMatch(const ::string& str, const RE& re) {
1267 return PartialMatch(str.c_str(), re);
1268 }
1269
1270# endif // GTEST_HAS_GLOBAL_STRING
1271
1272 static bool FullMatch(const char* str, const RE& re);
1273 static bool PartialMatch(const char* str, const RE& re);
1274
1275 private:
1276 void Init(const char* regex);
1277
1278 // We use a const char* instead of an std::string, as Google Test used to be
1279 // used where std::string is not available. TODO(wan@google.com): change to
1280 // std::string.
1281 const char* pattern_;
1282 bool is_valid_;
1283
1284# if GTEST_USES_POSIX_RE
1285
1286 regex_t full_regex_; // For FullMatch().
1287 regex_t partial_regex_; // For PartialMatch().
1288
1289# else // GTEST_USES_SIMPLE_RE
1290
1291 const char* full_pattern_; // For FullMatch();
1292
1293# endif
1294
1296};
1297
1298#endif // GTEST_USES_PCRE
1299
1300// Formats a source file path and a line number as they would appear
1301// in an error message from the compiler used to compile this code.
1302GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
1303
1304// Formats a file location for compiler-independent XML output.
1305// Although this function is not platform dependent, we put it next to
1306// FormatFileLocation in order to contrast the two functions.
1307GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
1308 int line);
1309
1310// Defines logging utilities:
1311// GTEST_LOG_(severity) - logs messages at the specified severity level. The
1312// message itself is streamed into the macro.
1313// LogToStderr() - directs all log messages to stderr.
1314// FlushInfoLog() - flushes informational log messages.
1315
1322
1323// Formats log entry severity, provides a stream object for streaming the
1324// log message, and terminates the message with a newline when going out of
1325// scope.
1327 public:
1328 GTestLog(GTestLogSeverity severity, const char* file, int line);
1329
1330 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1331 ~GTestLog();
1332
1333 ::std::ostream& GetStream() { return ::std::cerr; }
1334
1335 private:
1336 const GTestLogSeverity severity_;
1337
1339};
1340
1341#if !defined(GTEST_LOG_)
1342
1343# define GTEST_LOG_(severity) \
1344 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1345 __FILE__, __LINE__).GetStream()
1346
1347inline void LogToStderr() {}
1348inline void FlushInfoLog() { fflush(NULL); }
1349
1350#endif // !defined(GTEST_LOG_)
1351
1352#if !defined(GTEST_CHECK_)
1353// INTERNAL IMPLEMENTATION - DO NOT USE.
1354//
1355// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1356// is not satisfied.
1357// Synopsys:
1358// GTEST_CHECK_(boolean_condition);
1359// or
1360// GTEST_CHECK_(boolean_condition) << "Additional message";
1361//
1362// This checks the condition and if the condition is not satisfied
1363// it prints message about the condition violation, including the
1364// condition itself, plus additional message streamed into it, if any,
1365// and then it aborts the program. It aborts the program irrespective of
1366// whether it is built in the debug mode or not.
1367# define GTEST_CHECK_(condition) \
1368 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1369 if (::testing::internal::IsTrue(condition)) \
1370 ; \
1371 else \
1372 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1373#endif // !defined(GTEST_CHECK_)
1374
1375// An all-mode assert to verify that the given POSIX-style function
1376// call returns 0 (indicating success). Known limitation: this
1377// doesn't expand to a balanced 'if' statement, so enclose the macro
1378// in {} if you need to use it as the only statement in an 'if'
1379// branch.
1380#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1381 if (const int gtest_error = (posix_call)) \
1382 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1383 << gtest_error
1384
1385// Adds reference to a type if it is not a reference type,
1386// otherwise leaves it unchanged. This is the same as
1387// tr1::add_reference, which is not widely available yet.
1388template <typename T>
1389struct AddReference { typedef T& type; }; // NOLINT
1390template <typename T>
1391struct AddReference<T&> { typedef T& type; }; // NOLINT
1392
1393// A handy wrapper around AddReference that works when the argument T
1394// depends on template parameters.
1395#define GTEST_ADD_REFERENCE_(T) \
1396 typename ::testing::internal::AddReference<T>::type
1397
1398// Transforms "T" into "const T&" according to standard reference collapsing
1399// rules (this is only needed as a backport for C++98 compilers that do not
1400// support reference collapsing). Specifically, it transforms:
1401//
1402// char ==> const char&
1403// const char ==> const char&
1404// char& ==> char&
1405// const char& ==> const char&
1406//
1407// Note that the non-const reference will not have "const" added. This is
1408// standard, and necessary so that "T" can always bind to "const T&".
1409template <typename T>
1410struct ConstRef { typedef const T& type; };
1411template <typename T>
1412struct ConstRef<T&> { typedef T& type; };
1413
1414// The argument T must depend on some template parameters.
1415#define GTEST_REFERENCE_TO_CONST_(T) \
1416 typename ::testing::internal::ConstRef<T>::type
1417
1418#if GTEST_HAS_STD_MOVE_
1419using std::forward;
1420using std::move;
1421
1422template <typename T>
1423struct RvalueRef {
1424 typedef T&& type;
1425};
1426#else // GTEST_HAS_STD_MOVE_
1427template <typename T>
1428const T& move(const T& t) {
1429 return t;
1430}
1431template <typename T>
1433
1434template <typename T>
1436 typedef const T& type;
1437};
1438#endif // GTEST_HAS_STD_MOVE_
1439
1440// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1441//
1442// Use ImplicitCast_ as a safe version of static_cast for upcasting in
1443// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1444// const Foo*). When you use ImplicitCast_, the compiler checks that
1445// the cast is safe. Such explicit ImplicitCast_s are necessary in
1446// surprisingly many situations where C++ demands an exact type match
1447// instead of an argument type convertable to a target type.
1448//
1449// The syntax for using ImplicitCast_ is the same as for static_cast:
1450//
1451// ImplicitCast_<ToType>(expr)
1452//
1453// ImplicitCast_ would have been part of the C++ standard library,
1454// but the proposal was submitted too late. It will probably make
1455// its way into the language in the future.
1456//
1457// This relatively ugly name is intentional. It prevents clashes with
1458// similar functions users may have (e.g., implicit_cast). The internal
1459// namespace alone is not enough because the function can be found by ADL.
1460template<typename To>
1461inline To ImplicitCast_(To x) { return x; }
1462
1463// When you upcast (that is, cast a pointer from type Foo to type
1464// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
1465// always succeed. When you downcast (that is, cast a pointer from
1466// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
1467// how do you know the pointer is really of type SubclassOfFoo? It
1468// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
1469// when you downcast, you should use this macro. In debug mode, we
1470// use dynamic_cast<> to double-check the downcast is legal (we die
1471// if it's not). In normal mode, we do the efficient static_cast<>
1472// instead. Thus, it's important to test in debug mode to make sure
1473// the cast is legal!
1474// This is the only place in the code we should use dynamic_cast<>.
1475// In particular, you SHOULDN'T be using dynamic_cast<> in order to
1476// do RTTI (eg code like this:
1477// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
1478// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
1479// You should design the code some other way not to need this.
1480//
1481// This relatively ugly name is intentional. It prevents clashes with
1482// similar functions users may have (e.g., down_cast). The internal
1483// namespace alone is not enough because the function can be found by ADL.
1484template<typename To, typename From> // use like this: DownCast_<T*>(foo);
1485inline To DownCast_(From* f) { // so we only accept pointers
1486 // Ensures that To is a sub-type of From *. This test is here only
1487 // for compile-time type checking, and has no overhead in an
1488 // optimized build at run-time, as it will be optimized away
1489 // completely.
1491 if (false) {
1493 const To to = NULL;
1495 }
1496
1497#if GTEST_HAS_RTTI
1498 // RTTI: debug mode only!
1499 GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
1500#endif
1501 return static_cast<To>(f);
1502}
1503
1504// Downcasts the pointer of type Base to Derived.
1505// Derived must be a subclass of Base. The parameter MUST
1506// point to a class of type Derived, not any subclass of it.
1507// When RTTI is available, the function performs a runtime
1508// check to enforce this.
1509template <class Derived, class Base>
1510Derived* CheckedDowncastToActualType(Base* base) {
1511#if GTEST_HAS_RTTI
1512 GTEST_CHECK_(typeid(*base) == typeid(Derived));
1513#endif
1514
1515#if GTEST_HAS_DOWNCAST_
1516 return ::down_cast<Derived*>(base);
1517#elif GTEST_HAS_RTTI
1518 return dynamic_cast<Derived*>(base); // NOLINT
1519#else
1520 return static_cast<Derived*>(base); // Poor man's downcast.
1521#endif
1522}
1523
1524#if GTEST_HAS_STREAM_REDIRECTION
1525
1526// Defines the stderr capturer:
1527// CaptureStdout - starts capturing stdout.
1528// GetCapturedStdout - stops capturing stdout and returns the captured string.
1529// CaptureStderr - starts capturing stderr.
1530// GetCapturedStderr - stops capturing stderr and returns the captured string.
1531//
1536
1537#endif // GTEST_HAS_STREAM_REDIRECTION
1538// Returns the size (in bytes) of a file.
1539GTEST_API_ size_t GetFileSize(FILE* file);
1540
1541// Reads the entire content of a file as a string.
1542GTEST_API_ std::string ReadEntireFile(FILE* file);
1543
1544// All command line arguments.
1545GTEST_API_ std::vector<std::string> GetArgvs();
1546
1547#if GTEST_HAS_DEATH_TEST
1548
1549std::vector<std::string> GetInjectableArgvs();
1550// Deprecated: pass the args vector by value instead.
1551void SetInjectableArgvs(const std::vector<std::string>* new_argvs);
1552void SetInjectableArgvs(const std::vector<std::string>& new_argvs);
1553#if GTEST_HAS_GLOBAL_STRING
1554void SetInjectableArgvs(const std::vector< ::string>& new_argvs);
1555#endif // GTEST_HAS_GLOBAL_STRING
1556void ClearInjectableArgvs();
1557
1558#endif // GTEST_HAS_DEATH_TEST
1559
1560// Defines synchronization primitives.
1561#if GTEST_IS_THREADSAFE
1562# if GTEST_HAS_PTHREAD
1563// Sleeps for (roughly) n milliseconds. This function is only for testing
1564// Google Test's own constructs. Don't use it in user tests, either
1565// directly or indirectly.
1566inline void SleepMilliseconds(int n) {
1567 const timespec time = {
1568 0, // 0 seconds.
1569 n * 1000L * 1000L, // And n ms.
1570 };
1571 nanosleep(&time, NULL);
1572}
1573# endif // GTEST_HAS_PTHREAD
1574
1575# if GTEST_HAS_NOTIFICATION_
1576// Notification has already been imported into the namespace.
1577// Nothing to do here.
1578
1579# elif GTEST_HAS_PTHREAD
1580// Allows a controller thread to pause execution of newly created
1581// threads until notified. Instances of this class must be created
1582// and destroyed in the controller thread.
1583//
1584// This class is only for testing Google Test's own constructs. Do not
1585// use it in user tests, either directly or indirectly.
1586class Notification {
1587 public:
1588 Notification() : notified_(false) {
1589 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1590 }
1591 ~Notification() {
1592 pthread_mutex_destroy(&mutex_);
1593 }
1594
1595 // Notifies all threads created with this notification to start. Must
1596 // be called from the controller thread.
1597 void Notify() {
1598 pthread_mutex_lock(&mutex_);
1599 notified_ = true;
1600 pthread_mutex_unlock(&mutex_);
1601 }
1602
1603 // Blocks until the controller thread notifies. Must be called from a test
1604 // thread.
1605 void WaitForNotification() {
1606 for (;;) {
1607 pthread_mutex_lock(&mutex_);
1608 const bool notified = notified_;
1609 pthread_mutex_unlock(&mutex_);
1610 if (notified)
1611 break;
1612 SleepMilliseconds(10);
1613 }
1614 }
1615
1616 private:
1617 pthread_mutex_t mutex_;
1618 bool notified_;
1619
1620 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1621};
1622
1623# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1624
1625GTEST_API_ void SleepMilliseconds(int n);
1626
1627// Provides leak-safe Windows kernel handle ownership.
1628// Used in death tests and in threading support.
1629class GTEST_API_ AutoHandle {
1630 public:
1631 // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1632 // avoid including <windows.h> in this header file. Including <windows.h> is
1633 // undesirable because it defines a lot of symbols and macros that tend to
1634 // conflict with client code. This assumption is verified by
1635 // WindowsTypesTest.HANDLEIsVoidStar.
1636 typedef void* Handle;
1637 AutoHandle();
1638 explicit AutoHandle(Handle handle);
1639
1640 ~AutoHandle();
1641
1642 Handle Get() const;
1643 void Reset();
1644 void Reset(Handle handle);
1645
1646 private:
1647 // Returns true iff the handle is a valid handle object that can be closed.
1648 bool IsCloseable() const;
1649
1650 Handle handle_;
1651
1653};
1654
1655// Allows a controller thread to pause execution of newly created
1656// threads until notified. Instances of this class must be created
1657// and destroyed in the controller thread.
1658//
1659// This class is only for testing Google Test's own constructs. Do not
1660// use it in user tests, either directly or indirectly.
1661class GTEST_API_ Notification {
1662 public:
1663 Notification();
1664 void Notify();
1665 void WaitForNotification();
1666
1667 private:
1668 AutoHandle event_;
1669
1670 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1671};
1672# endif // GTEST_HAS_NOTIFICATION_
1673
1674// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1675// defined, but we don't want to use MinGW's pthreads implementation, which
1676// has conformance problems with some versions of the POSIX standard.
1677# if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1678
1679// As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1680// Consequently, it cannot select a correct instantiation of ThreadWithParam
1681// in order to call its Run(). Introducing ThreadWithParamBase as a
1682// non-templated base class for ThreadWithParam allows us to bypass this
1683// problem.
1684class ThreadWithParamBase {
1685 public:
1686 virtual ~ThreadWithParamBase() {}
1687 virtual void Run() = 0;
1688};
1689
1690// pthread_create() accepts a pointer to a function type with the C linkage.
1691// According to the Standard (7.5/1), function types with different linkages
1692// are different even if they are otherwise identical. Some compilers (for
1693// example, SunStudio) treat them as different types. Since class methods
1694// cannot be defined with C-linkage we need to define a free C-function to
1695// pass into pthread_create().
1696extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1697 static_cast<ThreadWithParamBase*>(thread)->Run();
1698 return NULL;
1699}
1700
1701// Helper class for testing Google Test's multi-threading constructs.
1702// To use it, write:
1703//
1704// void ThreadFunc(int param) { /* Do things with param */ }
1705// Notification thread_can_start;
1706// ...
1707// // The thread_can_start parameter is optional; you can supply NULL.
1708// ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1709// thread_can_start.Notify();
1710//
1711// These classes are only for testing Google Test's own constructs. Do
1712// not use them in user tests, either directly or indirectly.
1713template <typename T>
1714class ThreadWithParam : public ThreadWithParamBase {
1715 public:
1716 typedef void UserThreadFunc(T);
1717
1718 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1719 : func_(func),
1720 param_(param),
1721 thread_can_start_(thread_can_start),
1722 finished_(false) {
1723 ThreadWithParamBase* const base = this;
1724 // The thread can be created only after all fields except thread_
1725 // have been initialized.
1727 pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
1728 }
1729 ~ThreadWithParam() { Join(); }
1730
1731 void Join() {
1732 if (!finished_) {
1733 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
1734 finished_ = true;
1735 }
1736 }
1737
1738 virtual void Run() {
1739 if (thread_can_start_ != NULL)
1740 thread_can_start_->WaitForNotification();
1741 func_(param_);
1742 }
1743
1744 private:
1745 UserThreadFunc* const func_; // User-supplied thread function.
1746 const T param_; // User-supplied parameter to the thread function.
1747 // When non-NULL, used to block execution until the controller thread
1748 // notifies.
1749 Notification* const thread_can_start_;
1750 bool finished_; // true iff we know that the thread function has finished.
1751 pthread_t thread_; // The native thread object.
1752
1753 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1754};
1755# endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1756 // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1757
1758# if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1759// Mutex and ThreadLocal have already been imported into the namespace.
1760// Nothing to do here.
1761
1762# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1763
1764// Mutex implements mutex on Windows platforms. It is used in conjunction
1765// with class MutexLock:
1766//
1767// Mutex mutex;
1768// ...
1769// MutexLock lock(&mutex); // Acquires the mutex and releases it at the
1770// // end of the current scope.
1771//
1772// A static Mutex *must* be defined or declared using one of the following
1773// macros:
1774// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1775// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1776//
1777// (A non-static Mutex is defined/declared in the usual way).
1778class GTEST_API_ Mutex {
1779 public:
1780 enum MutexType { kStatic = 0, kDynamic = 1 };
1781 // We rely on kStaticMutex being 0 as it is to what the linker initializes
1782 // type_ in static mutexes. critical_section_ will be initialized lazily
1783 // in ThreadSafeLazyInit().
1784 enum StaticConstructorSelector { kStaticMutex = 0 };
1785
1786 // This constructor intentionally does nothing. It relies on type_ being
1787 // statically initialized to 0 (effectively setting it to kStatic) and on
1788 // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1789 explicit Mutex(StaticConstructorSelector /*dummy*/) {}
1790
1791 Mutex();
1792 ~Mutex();
1793
1794 void Lock();
1795
1796 void Unlock();
1797
1798 // Does nothing if the current thread holds the mutex. Otherwise, crashes
1799 // with high probability.
1800 void AssertHeld();
1801
1802 private:
1803 // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1804 void ThreadSafeLazyInit();
1805
1806 // Per http://blogs.msdn.com/b/oldnewthing/archive/2004/02/23/78395.aspx,
1807 // we assume that 0 is an invalid value for thread IDs.
1808 unsigned int owner_thread_id_;
1809
1810 // For static mutexes, we rely on these members being initialized to zeros
1811 // by the linker.
1812 MutexType type_;
1813 long critical_section_init_phase_; // NOLINT
1814 GTEST_CRITICAL_SECTION* critical_section_;
1815
1817};
1818
1819# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1820 extern ::testing::internal::Mutex mutex
1821
1822# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1823 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1824
1825// We cannot name this class MutexLock because the ctor declaration would
1826// conflict with a macro named MutexLock, which is defined on some
1827// platforms. That macro is used as a defensive measure to prevent against
1828// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1829// "MutexLock l(&mu)". Hence the typedef trick below.
1830class GTestMutexLock {
1831 public:
1832 explicit GTestMutexLock(Mutex* mutex)
1833 : mutex_(mutex) { mutex_->Lock(); }
1834
1835 ~GTestMutexLock() { mutex_->Unlock(); }
1836
1837 private:
1838 Mutex* const mutex_;
1839
1841};
1842
1843typedef GTestMutexLock MutexLock;
1844
1845// Base class for ValueHolder<T>. Allows a caller to hold and delete a value
1846// without knowing its type.
1847class ThreadLocalValueHolderBase {
1848 public:
1849 virtual ~ThreadLocalValueHolderBase() {}
1850};
1851
1852// Provides a way for a thread to send notifications to a ThreadLocal
1853// regardless of its parameter type.
1854class ThreadLocalBase {
1855 public:
1856 // Creates a new ValueHolder<T> object holding a default value passed to
1857 // this ThreadLocal<T>'s constructor and returns it. It is the caller's
1858 // responsibility not to call this when the ThreadLocal<T> instance already
1859 // has a value on the current thread.
1860 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1861
1862 protected:
1863 ThreadLocalBase() {}
1864 virtual ~ThreadLocalBase() {}
1865
1866 private:
1867 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase);
1868};
1869
1870// Maps a thread to a set of ThreadLocals that have values instantiated on that
1871// thread and notifies them when the thread exits. A ThreadLocal instance is
1872// expected to persist until all threads it has values on have terminated.
1873class GTEST_API_ ThreadLocalRegistry {
1874 public:
1875 // Registers thread_local_instance as having value on the current thread.
1876 // Returns a value that can be used to identify the thread from other threads.
1877 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1878 const ThreadLocalBase* thread_local_instance);
1879
1880 // Invoked when a ThreadLocal instance is destroyed.
1881 static void OnThreadLocalDestroyed(
1882 const ThreadLocalBase* thread_local_instance);
1883};
1884
1885class GTEST_API_ ThreadWithParamBase {
1886 public:
1887 void Join();
1888
1889 protected:
1890 class Runnable {
1891 public:
1892 virtual ~Runnable() {}
1893 virtual void Run() = 0;
1894 };
1895
1896 ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
1897 virtual ~ThreadWithParamBase();
1898
1899 private:
1900 AutoHandle thread_;
1901};
1902
1903// Helper class for testing Google Test's multi-threading constructs.
1904template <typename T>
1905class ThreadWithParam : public ThreadWithParamBase {
1906 public:
1907 typedef void UserThreadFunc(T);
1908
1909 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1910 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
1911 }
1912 virtual ~ThreadWithParam() {}
1913
1914 private:
1915 class RunnableImpl : public Runnable {
1916 public:
1917 RunnableImpl(UserThreadFunc* func, T param)
1918 : func_(func),
1919 param_(param) {
1920 }
1921 virtual ~RunnableImpl() {}
1922 virtual void Run() {
1923 func_(param_);
1924 }
1925
1926 private:
1927 UserThreadFunc* const func_;
1928 const T param_;
1929
1930 GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl);
1931 };
1932
1933 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1934};
1935
1936// Implements thread-local storage on Windows systems.
1937//
1938// // Thread 1
1939// ThreadLocal<int> tl(100); // 100 is the default value for each thread.
1940//
1941// // Thread 2
1942// tl.set(150); // Changes the value for thread 2 only.
1943// EXPECT_EQ(150, tl.get());
1944//
1945// // Thread 1
1946// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
1947// tl.set(200);
1948// EXPECT_EQ(200, tl.get());
1949//
1950// The template type argument T must have a public copy constructor.
1951// In addition, the default ThreadLocal constructor requires T to have
1952// a public default constructor.
1953//
1954// The users of a TheadLocal instance have to make sure that all but one
1955// threads (including the main one) using that instance have exited before
1956// destroying it. Otherwise, the per-thread objects managed for them by the
1957// ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1958//
1959// Google Test only uses global ThreadLocal objects. That means they
1960// will die after main() has returned. Therefore, no per-thread
1961// object managed by Google Test will be leaked as long as all threads
1962// using Google Test have exited when main() returns.
1963template <typename T>
1964class ThreadLocal : public ThreadLocalBase {
1965 public:
1966 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1967 explicit ThreadLocal(const T& value)
1968 : default_factory_(new InstanceValueHolderFactory(value)) {}
1969
1970 ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1971
1972 T* pointer() { return GetOrCreateValue(); }
1973 const T* pointer() const { return GetOrCreateValue(); }
1974 const T& get() const { return *pointer(); }
1975 void set(const T& value) { *pointer() = value; }
1976
1977 private:
1978 // Holds a value of T. Can be deleted via its base class without the caller
1979 // knowing the type of T.
1980 class ValueHolder : public ThreadLocalValueHolderBase {
1981 public:
1982 ValueHolder() : value_() {}
1983 explicit ValueHolder(const T& value) : value_(value) {}
1984
1985 T* pointer() { return &value_; }
1986
1987 private:
1988 T value_;
1990 };
1991
1992
1993 T* GetOrCreateValue() const {
1994 return static_cast<ValueHolder*>(
1995 ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();
1996 }
1997
1998 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const {
1999 return default_factory_->MakeNewHolder();
2000 }
2001
2002 class ValueHolderFactory {
2003 public:
2004 ValueHolderFactory() {}
2005 virtual ~ValueHolderFactory() {}
2006 virtual ValueHolder* MakeNewHolder() const = 0;
2007
2008 private:
2009 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
2010 };
2011
2012 class DefaultValueHolderFactory : public ValueHolderFactory {
2013 public:
2014 DefaultValueHolderFactory() {}
2015 virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
2016
2017 private:
2018 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
2019 };
2020
2021 class InstanceValueHolderFactory : public ValueHolderFactory {
2022 public:
2023 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
2024 virtual ValueHolder* MakeNewHolder() const {
2025 return new ValueHolder(value_);
2026 }
2027
2028 private:
2029 const T value_; // The value for each thread.
2030
2031 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
2032 };
2033
2034 scoped_ptr<ValueHolderFactory> default_factory_;
2035
2037};
2038
2039# elif GTEST_HAS_PTHREAD
2040
2041// MutexBase and Mutex implement mutex on pthreads-based platforms.
2042class MutexBase {
2043 public:
2044 // Acquires this mutex.
2045 void Lock() {
2046 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
2047 owner_ = pthread_self();
2048 has_owner_ = true;
2049 }
2050
2051 // Releases this mutex.
2052 void Unlock() {
2053 // Since the lock is being released the owner_ field should no longer be
2054 // considered valid. We don't protect writing to has_owner_ here, as it's
2055 // the caller's responsibility to ensure that the current thread holds the
2056 // mutex when this is called.
2057 has_owner_ = false;
2058 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
2059 }
2060
2061 // Does nothing if the current thread holds the mutex. Otherwise, crashes
2062 // with high probability.
2063 void AssertHeld() const {
2064 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
2065 << "The current thread is not holding the mutex @" << this;
2066 }
2067
2068 // A static mutex may be used before main() is entered. It may even
2069 // be used before the dynamic initialization stage. Therefore we
2070 // must be able to initialize a static mutex object at link time.
2071 // This means MutexBase has to be a POD and its member variables
2072 // have to be public.
2073 public:
2074 pthread_mutex_t mutex_; // The underlying pthread mutex.
2075 // has_owner_ indicates whether the owner_ field below contains a valid thread
2076 // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
2077 // accesses to the owner_ field should be protected by a check of this field.
2078 // An alternative might be to memset() owner_ to all zeros, but there's no
2079 // guarantee that a zero'd pthread_t is necessarily invalid or even different
2080 // from pthread_self().
2081 bool has_owner_;
2082 pthread_t owner_; // The thread holding the mutex.
2083};
2084
2085// Forward-declares a static mutex.
2086# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
2087 extern ::testing::internal::MutexBase mutex
2088
2089// Defines and statically (i.e. at link time) initializes a static mutex.
2090// The initialization list here does not explicitly initialize each field,
2091// instead relying on default initialization for the unspecified fields. In
2092// particular, the owner_ field (a pthread_t) is not explicitly initialized.
2093// This allows initialization to work whether pthread_t is a scalar or struct.
2094// The flag -Wmissing-field-initializers must not be specified for this to work.
2095# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
2096 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
2097
2098// The Mutex class can only be used for mutexes created at runtime. It
2099// shares its API with MutexBase otherwise.
2100class Mutex : public MutexBase {
2101 public:
2102 Mutex() {
2103 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
2104 has_owner_ = false;
2105 }
2106 ~Mutex() {
2107 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
2108 }
2109
2110 private:
2112};
2113
2114// We cannot name this class MutexLock because the ctor declaration would
2115// conflict with a macro named MutexLock, which is defined on some
2116// platforms. That macro is used as a defensive measure to prevent against
2117// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
2118// "MutexLock l(&mu)". Hence the typedef trick below.
2119class GTestMutexLock {
2120 public:
2121 explicit GTestMutexLock(MutexBase* mutex)
2122 : mutex_(mutex) { mutex_->Lock(); }
2123
2124 ~GTestMutexLock() { mutex_->Unlock(); }
2125
2126 private:
2127 MutexBase* const mutex_;
2128
2130};
2131
2132typedef GTestMutexLock MutexLock;
2133
2134// Helpers for ThreadLocal.
2135
2136// pthread_key_create() requires DeleteThreadLocalValue() to have
2137// C-linkage. Therefore it cannot be templatized to access
2138// ThreadLocal<T>. Hence the need for class
2139// ThreadLocalValueHolderBase.
2140class ThreadLocalValueHolderBase {
2141 public:
2142 virtual ~ThreadLocalValueHolderBase() {}
2143};
2144
2145// Called by pthread to delete thread-local data stored by
2146// pthread_setspecific().
2147extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
2148 delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
2149}
2150
2151// Implements thread-local storage on pthreads-based systems.
2152template <typename T>
2153class GTEST_API_ ThreadLocal {
2154 public:
2155 ThreadLocal()
2156 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
2157 explicit ThreadLocal(const T& value)
2158 : key_(CreateKey()),
2159 default_factory_(new InstanceValueHolderFactory(value)) {}
2160
2161 ~ThreadLocal() {
2162 // Destroys the managed object for the current thread, if any.
2163 DeleteThreadLocalValue(pthread_getspecific(key_));
2164
2165 // Releases resources associated with the key. This will *not*
2166 // delete managed objects for other threads.
2167 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
2168 }
2169
2170 T* pointer() { return GetOrCreateValue(); }
2171 const T* pointer() const { return GetOrCreateValue(); }
2172 const T& get() const { return *pointer(); }
2173 void set(const T& value) { *pointer() = value; }
2174
2175 private:
2176 // Holds a value of type T.
2177 class ValueHolder : public ThreadLocalValueHolderBase {
2178 public:
2179 ValueHolder() : value_() {}
2180 explicit ValueHolder(const T& value) : value_(value) {}
2181
2182 T* pointer() { return &value_; }
2183
2184 private:
2185 T value_;
2187 };
2188
2189 static pthread_key_t CreateKey() {
2190 pthread_key_t key;
2191 // When a thread exits, DeleteThreadLocalValue() will be called on
2192 // the object managed for that thread.
2194 pthread_key_create(&key, &DeleteThreadLocalValue));
2195 return key;
2196 }
2197
2198 T* GetOrCreateValue() const {
2199 ThreadLocalValueHolderBase* const holder =
2200 static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
2201 if (holder != NULL) {
2202 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
2203 }
2204
2205 ValueHolder* const new_holder = default_factory_->MakeNewHolder();
2206 ThreadLocalValueHolderBase* const holder_base = new_holder;
2207 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
2208 return new_holder->pointer();
2209 }
2210
2211 class ValueHolderFactory {
2212 public:
2213 ValueHolderFactory() {}
2214 virtual ~ValueHolderFactory() {}
2215 virtual ValueHolder* MakeNewHolder() const = 0;
2216
2217 private:
2218 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
2219 };
2220
2221 class DefaultValueHolderFactory : public ValueHolderFactory {
2222 public:
2223 DefaultValueHolderFactory() {}
2224 virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
2225
2226 private:
2227 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
2228 };
2229
2230 class InstanceValueHolderFactory : public ValueHolderFactory {
2231 public:
2232 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
2233 virtual ValueHolder* MakeNewHolder() const {
2234 return new ValueHolder(value_);
2235 }
2236
2237 private:
2238 const T value_; // The value for each thread.
2239
2240 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
2241 };
2242
2243 // A key pthreads uses for looking up per-thread values.
2244 const pthread_key_t key_;
2245 scoped_ptr<ValueHolderFactory> default_factory_;
2246
2248};
2249
2250# endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
2251
2252#else // GTEST_IS_THREADSAFE
2253
2254// A dummy implementation of synchronization primitives (mutex, lock,
2255// and thread-local variable). Necessary for compiling Google Test where
2256// mutex is not supported - using Google Test in multiple threads is not
2257// supported on such platforms.
2258
2259class Mutex {
2260 public:
2262 void Lock() {}
2263 void Unlock() {}
2264 void AssertHeld() const {}
2265};
2266
2267# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
2268 extern ::testing::internal::Mutex mutex
2269
2270# define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
2271
2272// We cannot name this class MutexLock because the ctor declaration would
2273// conflict with a macro named MutexLock, which is defined on some
2274// platforms. That macro is used as a defensive measure to prevent against
2275// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
2276// "MutexLock l(&mu)". Hence the typedef trick below.
2278 public:
2279 explicit GTestMutexLock(Mutex*) {} // NOLINT
2280};
2281
2283
2284template <typename T>
2286 public:
2287 ThreadLocal() : value_() {}
2288 explicit ThreadLocal(const T& value) : value_(value) {}
2289 T* pointer() { return &value_; }
2290 const T* pointer() const { return &value_; }
2291 const T& get() const { return value_; }
2292 void set(const T& value) { value_ = value; }
2293 private:
2294 T value_;
2295};
2296
2297#endif // GTEST_IS_THREADSAFE
2298
2299// Returns the number of threads running in the process, or 0 to indicate that
2300// we cannot detect it.
2301GTEST_API_ size_t GetThreadCount();
2302
2303// Passing non-POD classes through ellipsis (...) crashes the ARM
2304// compiler and generates a warning in Sun Studio before 12u4. The Nokia Symbian
2305// and the IBM XL C/C++ compiler try to instantiate a copy constructor
2306// for objects passed through ellipsis (...), failing for uncopyable
2307// objects. We define this to ensure that only POD is passed through
2308// ellipsis on these systems.
2309#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || \
2310 (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x5130)
2311// We lose support for NULL detection where the compiler doesn't like
2312// passing non-POD classes through ellipsis (...).
2313# define GTEST_ELLIPSIS_NEEDS_POD_ 1
2314#else
2315# define GTEST_CAN_COMPARE_NULL 1
2316#endif
2317
2318// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
2319// const T& and const T* in a function template. These compilers
2320// _can_ decide between class template specializations for T and T*,
2321// so a tr1::type_traits-like is_pointer works.
2322#if defined(__SYMBIAN32__) || defined(__IBMCPP__)
2323# define GTEST_NEEDS_IS_POINTER_ 1
2324#endif
2325
2326template <bool bool_value>
2329 static const bool value = bool_value;
2330};
2331template <bool bool_value> const bool bool_constant<bool_value>::value;
2332
2335
2336template <typename T, typename U>
2337struct is_same : public false_type {};
2338
2339template <typename T>
2340struct is_same<T, T> : public true_type {};
2341
2342
2343template <typename T>
2344struct is_pointer : public false_type {};
2345
2346template <typename T>
2347struct is_pointer<T*> : public true_type {};
2348
2349template <typename Iterator>
2351 typedef typename Iterator::value_type value_type;
2352};
2353
2354
2355template <typename T>
2357 typedef T value_type;
2358};
2359
2360template <typename T>
2361struct IteratorTraits<const T*> {
2362 typedef T value_type;
2363};
2364
2365#if GTEST_OS_WINDOWS
2366# define GTEST_PATH_SEP_ "\\"
2367# define GTEST_HAS_ALT_PATH_SEP_ 1
2368// The biggest signed integer type the compiler supports.
2369typedef __int64 BiggestInt;
2370#else
2371# define GTEST_PATH_SEP_ "/"
2372# define GTEST_HAS_ALT_PATH_SEP_ 0
2373typedef long long BiggestInt; // NOLINT
2374#endif // GTEST_OS_WINDOWS
2375
2376// Utilities for char.
2377
2378// isspace(int ch) and friends accept an unsigned char or EOF. char
2379// may be signed, depending on the compiler (or compiler flags).
2380// Therefore we need to cast a char to unsigned char before calling
2381// isspace(), etc.
2382
2383inline bool IsAlpha(char ch) {
2384 return isalpha(static_cast<unsigned char>(ch)) != 0;
2385}
2386inline bool IsAlNum(char ch) {
2387 return isalnum(static_cast<unsigned char>(ch)) != 0;
2388}
2389inline bool IsDigit(char ch) {
2390 return isdigit(static_cast<unsigned char>(ch)) != 0;
2391}
2392inline bool IsLower(char ch) {
2393 return islower(static_cast<unsigned char>(ch)) != 0;
2394}
2395inline bool IsSpace(char ch) {
2396 return isspace(static_cast<unsigned char>(ch)) != 0;
2397}
2398inline bool IsUpper(char ch) {
2399 return isupper(static_cast<unsigned char>(ch)) != 0;
2400}
2401inline bool IsXDigit(char ch) {
2402 return isxdigit(static_cast<unsigned char>(ch)) != 0;
2403}
2404inline bool IsXDigit(wchar_t ch) {
2405 const unsigned char low_byte = static_cast<unsigned char>(ch);
2406 return ch == low_byte && isxdigit(low_byte) != 0;
2407}
2408
2409inline char ToLower(char ch) {
2410 return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
2411}
2412inline char ToUpper(char ch) {
2413 return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
2414}
2415
2416inline std::string StripTrailingSpaces(std::string str) {
2417 std::string::iterator it = str.end();
2418 while (it != str.begin() && IsSpace(*--it))
2419 it = str.erase(it);
2420 return str;
2421}
2422
2423// The testing::internal::posix namespace holds wrappers for common
2424// POSIX functions. These wrappers hide the differences between
2425// Windows/MSVC and POSIX systems. Since some compilers define these
2426// standard functions as macros, the wrapper cannot have the same name
2427// as the wrapped function.
2428
2429namespace posix {
2430
2431// Functions with a different name on Windows.
2432
2433#if GTEST_OS_WINDOWS
2434
2435typedef struct _stat StatStruct;
2436
2437# ifdef __BORLANDC__
2438inline int IsATTY(int fd) { return isatty(fd); }
2439inline int StrCaseCmp(const char* s1, const char* s2) {
2440 return stricmp(s1, s2);
2441}
2442inline char* StrDup(const char* src) { return strdup(src); }
2443# else // !__BORLANDC__
2444# if GTEST_OS_WINDOWS_MOBILE
2445inline int IsATTY(int /* fd */) { return 0; }
2446# else
2447inline int IsATTY(int fd) { return _isatty(fd); }
2448# endif // GTEST_OS_WINDOWS_MOBILE
2449inline int StrCaseCmp(const char* s1, const char* s2) {
2450 return _stricmp(s1, s2);
2451}
2452inline char* StrDup(const char* src) { return _strdup(src); }
2453# endif // __BORLANDC__
2454
2455# if GTEST_OS_WINDOWS_MOBILE
2456inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
2457// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
2458// time and thus not defined there.
2459# else
2460inline int FileNo(FILE* file) { return _fileno(file); }
2461inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2462inline int RmDir(const char* dir) { return _rmdir(dir); }
2463inline bool IsDir(const StatStruct& st) {
2464 return (_S_IFDIR & st.st_mode) != 0;
2465}
2466# endif // GTEST_OS_WINDOWS_MOBILE
2467
2468#else
2469
2470typedef struct stat StatStruct;
2471
2472inline int FileNo(FILE* file) { return fileno(file); }
2473inline int IsATTY(int fd) { return isatty(fd); }
2474inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
2475inline int StrCaseCmp(const char* s1, const char* s2) {
2476 return strcasecmp(s1, s2);
2477}
2478inline char* StrDup(const char* src) { return strdup(src); }
2479inline int RmDir(const char* dir) { return rmdir(dir); }
2480inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2481
2482#endif // GTEST_OS_WINDOWS
2483
2484// Functions deprecated by MSVC 8.0.
2485
2486GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */)
2487
2488inline const char* StrNCpy(char* dest, const char* src, size_t n) {
2489 return strncpy(dest, src, n);
2490}
2491
2492// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2493// StrError() aren't needed on Windows CE at this time and thus not
2494// defined there.
2495
2496#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
2497inline int ChDir(const char* dir) { return chdir(dir); }
2498#endif
2499inline FILE* FOpen(const char* path, const char* mode) {
2500 return fopen(path, mode);
2501}
2502#if !GTEST_OS_WINDOWS_MOBILE
2503inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
2504 return freopen(path, mode, stream);
2505}
2506inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2507#endif
2508inline int FClose(FILE* fp) { return fclose(fp); }
2509#if !GTEST_OS_WINDOWS_MOBILE
2510inline int Read(int fd, void* buf, unsigned int count) {
2511 return static_cast<int>(read(fd, buf, count));
2512}
2513inline int Write(int fd, const void* buf, unsigned int count) {
2514 return static_cast<int>(write(fd, buf, count));
2515}
2516inline int Close(int fd) { return close(fd); }
2517inline const char* StrError(int errnum) { return strerror(errnum); }
2518#endif
2519inline const char* GetEnv(const char* name) {
2520#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
2521 // We are on Windows CE, which has no environment variables.
2522 static_cast<void>(name); // To prevent 'unused argument' warning.
2523 return NULL;
2524#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2525 // Environment variables which we programmatically clear will be set to the
2526 // empty string rather than unset (NULL). Handle that case.
2527 const char* const env = getenv(name);
2528 return (env != NULL && env[0] != '\0') ? env : NULL;
2529#else
2530 return getenv(name);
2531#endif
2532}
2533
2535
2536#if GTEST_OS_WINDOWS_MOBILE
2537// Windows CE has no C library. The abort() function is used in
2538// several places in Google Test. This implementation provides a reasonable
2539// imitation of standard behaviour.
2540void Abort();
2541#else
2542inline void Abort() { abort(); }
2543#endif // GTEST_OS_WINDOWS_MOBILE
2544
2545} // namespace posix
2546
2547// MSVC "deprecates" snprintf and issues warnings wherever it is used. In
2548// order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2549// MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
2550// function in order to achieve that. We use macro definition here because
2551// snprintf is a variadic function.
2552#if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
2553// MSVC 2005 and above support variadic macros.
2554# define GTEST_SNPRINTF_(buffer, size, format, ...) \
2555 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2556#elif defined(_MSC_VER)
2557// Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't
2558// complain about _snprintf.
2559# define GTEST_SNPRINTF_ _snprintf
2560#else
2561# define GTEST_SNPRINTF_ snprintf
2562#endif
2563
2564// The maximum number a BiggestInt can represent. This definition
2565// works no matter BiggestInt is represented in one's complement or
2566// two's complement.
2567//
2568// We cannot rely on numeric_limits in STL, as __int64 and long long
2569// are not part of standard C++ and numeric_limits doesn't need to be
2570// defined for them.
2572 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
2573
2574// This template class serves as a compile-time function from size to
2575// type. It maps a size in bytes to a primitive type with that
2576// size. e.g.
2577//
2578// TypeWithSize<4>::UInt
2579//
2580// is typedef-ed to be unsigned int (unsigned integer made up of 4
2581// bytes).
2582//
2583// Such functionality should belong to STL, but I cannot find it
2584// there.
2585//
2586// Google Test uses this class in the implementation of floating-point
2587// comparison.
2588//
2589// For now it only handles UInt (unsigned int) as that's all Google Test
2590// needs. Other types can be easily added in the future if need
2591// arises.
2592template <size_t size>
2594 public:
2595 // This prevents the user from using TypeWithSize<N> with incorrect
2596 // values of N.
2597 typedef void UInt;
2598};
2599
2600// The specialization for size 4.
2601template <>
2603 public:
2604 // unsigned int has size 4 in both gcc and MSVC.
2605 //
2606 // As base/basictypes.h doesn't compile on Windows, we cannot use
2607 // uint32, uint64, and etc here.
2608 typedef int Int;
2609 typedef unsigned int UInt;
2610};
2611
2612// The specialization for size 8.
2613template <>
2615 public:
2616#if GTEST_OS_WINDOWS
2617 typedef __int64 Int;
2618 typedef unsigned __int64 UInt;
2619#else
2620 typedef long long Int; // NOLINT
2621 typedef unsigned long long UInt; // NOLINT
2622#endif // GTEST_OS_WINDOWS
2623};
2624
2625// Integer types of known sizes.
2630typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
2631
2632// Utilities for command line flags and environment variables.
2633
2634// Macro for referencing flags.
2635#if !defined(GTEST_FLAG)
2636# define GTEST_FLAG(name) FLAGS_gtest_##name
2637#endif // !defined(GTEST_FLAG)
2638
2639#if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2640# define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2641#endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2642
2643#if !defined(GTEST_DECLARE_bool_)
2644# define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2645
2646// Macros for declaring flags.
2647# define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2648# define GTEST_DECLARE_int32_(name) \
2649 GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
2650# define GTEST_DECLARE_string_(name) \
2651 GTEST_API_ extern ::std::string GTEST_FLAG(name)
2652
2653// Macros for defining flags.
2654# define GTEST_DEFINE_bool_(name, default_val, doc) \
2655 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2656# define GTEST_DEFINE_int32_(name, default_val, doc) \
2657 GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
2658# define GTEST_DEFINE_string_(name, default_val, doc) \
2659 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2660
2661#endif // !defined(GTEST_DECLARE_bool_)
2662
2663// Thread annotations
2664#if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2665# define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2666# define GTEST_LOCK_EXCLUDED_(locks)
2667#endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2668
2669// Parses 'str' for a 32-bit signed integer. If successful, writes the result
2670// to *value and returns true; otherwise leaves *value unchanged and returns
2671// false.
2672// TODO(chandlerc): Find a better way to refactor flag and environment parsing
2673// out of both gtest-port.cc and gtest.cc to avoid exporting this utility
2674// function.
2675bool ParseInt32(const Message& src_text, const char* str, Int32* value);
2676
2677// Parses a bool/Int32/string from the environment variable
2678// corresponding to the given Google Test flag.
2679bool BoolFromGTestEnv(const char* flag, bool default_val);
2680GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
2681std::string OutputFlagAlsoCheckEnvVar();
2682const char* StringFromGTestEnv(const char* flag, const char* default_val);
2683
2684} // namespace internal
2685} // namespace testing
2686
2687#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
const mie::Vuint & p
Definition bn.cpp:27
std::string name
::std::ostream & GetStream()
static bool PartialMatch(const ::std::string &str, const RE &re)
static bool FullMatch(const char *str, const RE &re)
const char * pattern() const
RE(const ::std::string &regex)
RE(const char *regex)
static bool PartialMatch(const char *str, const RE &re)
static bool FullMatch(const ::std::string &str, const RE &re)
RE(const RE &other)
void set(const T &value)
friend void swap(scoped_ptr &a, scoped_ptr &b)
int * count
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition gtest-port.h:940
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition gtest-port.h:324
#define GTEST_API_
Definition gtest-port.h:984
#define GTEST_ADD_REFERENCE_(T)
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition gtest-port.h:325
#define GTEST_CHECK_(condition)
#define GTEST_DISALLOW_ASSIGN_(type)
Definition gtest-port.h:912
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition gtest-port.h:942
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition gtest-port.h:917
void close(T *e, websocketpp::connection_hdl hdl)
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
void swap(picojson::value &x, picojson::value &y)
int ChDir(const char *dir)
int StrCaseCmp(const char *s1, const char *s2)
int Stat(const char *path, StatStruct *buf)
int FileNo(FILE *file)
int Read(int fd, void *buf, unsigned int count)
const char * StrNCpy(char *dest, const char *src, size_t n)
char * StrDup(const char *src)
const char * StrError(int errnum)
FILE * FReopen(const char *path, const char *mode, FILE *stream)
FILE * FDOpen(int fd, const char *mode)
const char * GetEnv(const char *name)
int RmDir(const char *dir)
bool IsDir(const StatStruct &st)
int Write(int fd, const void *buf, unsigned int count)
FILE * FOpen(const char *path, const char *mode)
long long BiggestInt
GTEST_API_ size_t GetFileSize(FILE *file)
GTestMutexLock MutexLock
std::string OutputFlagAlsoCheckEnvVar()
GTEST_API_ Int32 Int32FromGTestEnv(const char *flag, Int32 default_val)
To DownCast_(From *f)
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
TypeWithSize< 8 >::Int Int64
GTEST_API_ std::string ReadEntireFile(FILE *file)
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
GTEST_API_ std::string GetCapturedStderr()
GTEST_API_ size_t GetThreadCount()
::std::wstring wstring
TypeWithSize< 4 >::UInt UInt32
bool IsDigit(char ch)
GTEST_API_ bool IsTrue(bool condition)
Definition gtest.cc:5404
bool_constant< true > true_type
TypeWithSize< 8 >::Int TimeInMillis
bool BoolFromGTestEnv(const char *flag, bool default_val)
const char * StringFromGTestEnv(const char *flag, const char *default_val)
bool IsAlNum(char ch)
bool IsUpper(char ch)
::std::string string
GTEST_API_ void CaptureStderr()
TypeWithSize< 4 >::Int Int32
GTEST_API_ std::vector< std::string > GetArgvs()
Definition gtest.cc:398
To ImplicitCast_(To x)
const T & move(const T &t)
bool IsXDigit(char ch)
TypeWithSize< 8 >::UInt UInt64
std::string StripTrailingSpaces(std::string str)
bool_constant< false > false_type
bool ParseInt32(const Message &src_text, const char *str, Int32 *value)
char ToUpper(char ch)
bool IsLower(char ch)
GTEST_API_ void CaptureStdout()
const BiggestInt kMaxBiggestInt
char ToLower(char ch)
bool IsAlpha(char ch)
GTEST_API_ std::string GetCapturedStdout()
bool IsSpace(char ch)
Derived * CheckedDowncastToActualType(Base *base)
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1181
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
Definition lib.h:54
Iterator::value_type value_type
bool_constant< bool_value > type
uint8_t key[16]
Definition yubico_otp.c:41
CK_SLOT_ID CK_FLAGS CK_VOID_PTR CK_NOTIFY Notify
uint8_t buf[2048]
bool set