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