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


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:10