1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Authors: wan@google.com (Zhanyong Wan)
32 // Low-level types and utilities for porting Google Test to various
33 // platforms. They are subject to change without notice. DO NOT USE
36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
39 // The user can define the following macros in the build script to
40 // control Google Test's behavior. If the user doesn't define a macro
41 // in this list, Google Test will define it.
43 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
44 // is/isn't available.
45 // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
47 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
48 // is/isn't available (some systems define
49 // ::string, which is different to std::string).
50 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
51 // is/isn't available (some systems define
52 // ::wstring, which is different to std::wstring).
53 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
54 // is/isn't available.
55 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
57 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
58 // std::wstring does/doesn't work (Google Test can
59 // be used where std::wstring is unavailable).
60 // GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
61 // is/isn't available.
62 // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
63 // compiler supports Microsoft's "Structured
64 // Exception Handling".
65 // GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
66 // Test's own tr1 tuple implementation should be
67 // used. Unused when the user sets
68 // GTEST_HAS_TR1_TUPLE to 0.
69 // GTEST_LINKED_AS_SHARED_LIBRARY
70 // - Define to 1 when compiling tests that use
71 // Google Test as a shared library (known as
73 // GTEST_CREATE_SHARED_LIBRARY
74 // - Define to 1 when compiling Google Test itself
75 // as a shared library.
77 // This header defines the following utilities:
79 // Macros indicating the current platform (defined to 1 if compiled on
80 // the given platform; otherwise undefined):
81 // GTEST_OS_AIX - IBM AIX
82 // GTEST_OS_CYGWIN - Cygwin
83 // GTEST_OS_LINUX - Linux
84 // GTEST_OS_MAC - Mac OS X
85 // GTEST_OS_SOLARIS - Sun Solaris
86 // GTEST_OS_SYMBIAN - Symbian
87 // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
88 // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
89 // GTEST_OS_WINDOWS_MINGW - MinGW
90 // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
91 // GTEST_OS_ZOS - z/OS
93 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
94 // most stable support. Since core members of the Google Test project
95 // don't have access to other platforms, support for them may be less
96 // stable. If you notice any problems on your platform, please notify
97 // googletestframework@googlegroups.com (patches for fixing them are
98 // even more welcome!).
100 // Note that it is possible that none of the GTEST_OS_* macros are defined.
102 // Macros indicating available Google Test features (defined to 1 if
103 // the corresponding feature is supported; otherwise undefined):
104 // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
106 // GTEST_HAS_DEATH_TEST - death tests
107 // GTEST_HAS_PARAM_TEST - value-parameterized tests
108 // GTEST_HAS_TYPED_TEST - typed tests
109 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
110 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used.
111 // GTEST_USES_SIMPLE_RE - our own simple regex is used;
112 // the above two are mutually exclusive.
113 // GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
115 // Macros for basic C++ coding:
116 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
117 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
118 // variable don't have to be used.
119 // GTEST_DISALLOW_ASSIGN_ - disables operator=.
120 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
121 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
124 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
125 // - synchronization primitives.
126 // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
127 // synchronization primitives have real implementations
128 // and Google Test is thread-safe; or 0 otherwise.
130 // Template meta programming:
131 // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
134 // scoped_ptr - as in TR2.
136 // Regular expressions:
137 // RE - a simple regular expression class using the POSIX
138 // Extended Regular Expression syntax. Not available on
142 // GTEST_LOG_() - logs messages at the specified severity level.
143 // LogToStderr() - directs all log messages to stderr.
144 // FlushInfoLog() - flushes informational log messages.
146 // Stdout and stderr capturing:
147 // CaptureStdout() - starts capturing stdout.
148 // GetCapturedStdout() - stops capturing stdout and returns the captured
150 // CaptureStderr() - starts capturing stderr.
151 // GetCapturedStderr() - stops capturing stderr and returns the captured
155 // TypeWithSize - maps an integer to a int type.
156 // Int32, UInt32, Int64, UInt64, TimeInMillis
157 // - integers of known sizes.
158 // BiggestInt - the biggest signed integer type.
160 // Command-line utilities:
161 // GTEST_FLAG() - references a flag.
162 // GTEST_DECLARE_*() - declares a flag.
163 // GTEST_DEFINE_*() - defines a flag.
164 // GetArgvs() - returns the command line as a vector of strings.
166 // Environment variable utilities:
167 // GetEnv() - gets the value of an environment variable.
168 // BoolFromGTestEnv() - parses a bool environment variable.
169 // Int32FromGTestEnv() - parses an Int32 environment variable.
170 // StringFromGTestEnv() - parses a string environment variable.
172 #include <stddef.h> // For ptrdiff_t
177 #include <sys/stat.h>
178 #endif // !_WIN32_WCE
180 #include <iostream> // NOLINT
181 #include <sstream> // NOLINT
182 #include <string> // NOLINT
184 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
185 #define GTEST_FLAG_PREFIX_ "gtest_"
186 #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
187 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
188 #define GTEST_NAME_ "Google Test"
189 #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
191 // Determines the version of gcc that is used to compile this.
193 // 40302 means version 4.3.2.
194 #define GTEST_GCC_VER_ \
195 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
198 // Determines the platform on which Google Test is compiled.
200 #define GTEST_OS_CYGWIN 1
201 #elif defined __SYMBIAN32__
202 #define GTEST_OS_SYMBIAN 1
204 #define GTEST_OS_WINDOWS 1
206 #define GTEST_OS_WINDOWS_MOBILE 1
207 #elif defined(__MINGW__) || defined(__MINGW32__)
208 #define GTEST_OS_WINDOWS_MINGW 1
210 #define GTEST_OS_WINDOWS_DESKTOP 1
212 #elif defined __APPLE__
213 #define GTEST_OS_MAC 1
214 #elif defined __linux__
215 #define GTEST_OS_LINUX 1
216 #elif defined __MVS__
217 #define GTEST_OS_ZOS 1
218 #elif defined(__sun) && defined(__SVR4)
219 #define GTEST_OS_SOLARIS 1
221 #define GTEST_OS_AIX 1
224 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_SYMBIAN || \
225 GTEST_OS_SOLARIS || GTEST_OS_AIX
227 // On some platforms, <regex.h> needs someone to define size_t, and
228 // won't compile otherwise. We can #include it here as we already
229 // included <stdlib.h>, which is guaranteed to define size_t through
231 #include <regex.h> // NOLINT
232 #include <strings.h> // NOLINT
233 #include <sys/types.h> // NOLINT
234 #include <time.h> // NOLINT
235 #include <unistd.h> // NOLINT
237 #define GTEST_USES_POSIX_RE 1
239 #elif GTEST_OS_WINDOWS
241 #if !GTEST_OS_WINDOWS_MOBILE
242 #include <direct.h> // NOLINT
243 #include <io.h> // NOLINT
246 // <regex.h> is not available on Windows. Use our own simple regex
247 // implementation instead.
248 #define GTEST_USES_SIMPLE_RE 1
252 // <regex.h> may not be available on this platform. Use our own
253 // simple regex implementation instead.
254 #define GTEST_USES_SIMPLE_RE 1
256 #endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC ||
257 // GTEST_OS_SYMBIAN || GTEST_OS_SOLARIS || GTEST_OS_AIX
259 #ifndef GTEST_HAS_EXCEPTIONS
260 // The user didn't tell us whether exceptions are enabled, so we need
262 #if defined(_MSC_VER) || defined(__BORLANDC__)
263 // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
264 // macro to enable exceptions, so we'll do the same.
265 // Assumes that exceptions are enabled by default.
266 #ifndef _HAS_EXCEPTIONS
267 #define _HAS_EXCEPTIONS 1
268 #endif // _HAS_EXCEPTIONS
269 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
270 #elif defined(__GNUC__) && __EXCEPTIONS
271 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
272 #define GTEST_HAS_EXCEPTIONS 1
273 #elif defined(__SUNPRO_CC)
274 // Sun Pro CC supports exceptions. However, there is no compile-time way of
275 // detecting whether they are enabled or not. Therefore, we assume that
276 // they are enabled unless the user tells us otherwise.
277 #define GTEST_HAS_EXCEPTIONS 1
278 #elif defined(__IBMCPP__) && __EXCEPTIONS
279 // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
280 #define GTEST_HAS_EXCEPTIONS 1
282 // For other compilers, we assume exceptions are disabled to be
284 #define GTEST_HAS_EXCEPTIONS 0
285 #endif // defined(_MSC_VER) || defined(__BORLANDC__)
286 #endif // GTEST_HAS_EXCEPTIONS
288 #if !defined(GTEST_HAS_STD_STRING)
289 // Even though we don't use this macro any longer, we keep it in case
290 // some clients still depend on it.
291 #define GTEST_HAS_STD_STRING 1
292 #elif !GTEST_HAS_STD_STRING
293 // The user told us that ::std::string isn't available.
294 #error "Google Test cannot be used where ::std::string isn't available."
295 #endif // !defined(GTEST_HAS_STD_STRING)
297 #ifndef GTEST_HAS_GLOBAL_STRING
298 // The user didn't tell us whether ::string is available, so we need
301 #define GTEST_HAS_GLOBAL_STRING 0
303 #endif // GTEST_HAS_GLOBAL_STRING
305 #ifndef GTEST_HAS_STD_WSTRING
306 // The user didn't tell us whether ::std::wstring is available, so we need
308 // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
311 // Cygwin 1.5 and below doesn't support ::std::wstring.
312 // Cygwin 1.7 might add wstring support; this should be updated when clear.
313 // Solaris' libc++ doesn't support it either.
314 #define GTEST_HAS_STD_WSTRING (!(GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
316 #endif // GTEST_HAS_STD_WSTRING
318 #ifndef GTEST_HAS_GLOBAL_WSTRING
319 // The user didn't tell us whether ::wstring is available, so we need
321 #define GTEST_HAS_GLOBAL_WSTRING \
322 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
323 #endif // GTEST_HAS_GLOBAL_WSTRING
325 // Determines whether RTTI is available.
326 #ifndef GTEST_HAS_RTTI
327 // The user didn't tell us whether RTTI is enabled, so we need to
332 #ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
333 #define GTEST_HAS_RTTI 1
335 #define GTEST_HAS_RTTI 0
338 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
339 #elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
342 #define GTEST_HAS_RTTI 1
344 #define GTEST_HAS_RTTI 0
347 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
348 // both the typeid and dynamic_cast features are present.
349 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
352 #define GTEST_HAS_RTTI 1
354 #define GTEST_HAS_RTTI 0
359 // For all other compilers, we assume RTTI is enabled.
360 #define GTEST_HAS_RTTI 1
364 #endif // GTEST_HAS_RTTI
366 // It's this header's responsibility to #include <typeinfo> when RTTI
372 // Determines whether Google Test can use the pthreads library.
373 #ifndef GTEST_HAS_PTHREAD
374 // The user didn't tell us explicitly, so we assume pthreads support is
375 // available on Linux and Mac.
377 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
378 // to your compiler flags.
379 #define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC)
380 #endif // GTEST_HAS_PTHREAD
382 // Determines whether Google Test can use tr1/tuple. You can define
383 // this macro to 0 to prevent Google Test from using tuple (any
384 // feature depending on tuple with be disabled in this mode).
385 #ifndef GTEST_HAS_TR1_TUPLE
386 // The user didn't tell us not to do it, so we assume it's OK.
387 #define GTEST_HAS_TR1_TUPLE 1
388 #endif // GTEST_HAS_TR1_TUPLE
390 // Determines whether Google Test's own tr1 tuple implementation
392 #ifndef GTEST_USE_OWN_TR1_TUPLE
393 // The user didn't tell us, so we need to figure it out.
395 // We use our own TR1 tuple if we aren't sure the user has an
396 // implementation of it already. At this time, GCC 4.0.0+ and MSVC
397 // 2010 are the only mainstream compilers that come with a TR1 tuple
398 // implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by
399 // defining __GNUC__ and friends, but cannot compile GCC's tuple
400 // implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB
401 // Feature Pack download, which we cannot assume the user has.
402 #if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \
404 #define GTEST_USE_OWN_TR1_TUPLE 0
406 #define GTEST_USE_OWN_TR1_TUPLE 1
409 #endif // GTEST_USE_OWN_TR1_TUPLE
411 // To avoid conditional compilation everywhere, we make it
412 // gtest-port.h's responsibility to #include the header implementing
414 #if GTEST_HAS_TR1_TUPLE
416 #if GTEST_USE_OWN_TR1_TUPLE
417 #include <gtest/internal/gtest-tuple.h>
418 #elif GTEST_OS_SYMBIAN
420 // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
421 // use STLport's tuple implementation, which unfortunately doesn't
422 // work as the copy of STLport distributed with Symbian is incomplete.
423 // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
424 // use its own tuple implementation.
425 #ifdef BOOST_HAS_TR1_TUPLE
426 #undef BOOST_HAS_TR1_TUPLE
427 #endif // BOOST_HAS_TR1_TUPLE
429 // This prevents <boost/tr1/detail/config.hpp>, which defines
430 // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
431 #define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
434 #elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
435 // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
436 // not conform to the TR1 spec, which requires the header to be <tuple>.
438 #if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
439 // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
440 // which is #included by <tr1/tuple>, to not compile when RTTI is
441 // disabled. _TR1_FUNCTIONAL is the header guard for
442 // <tr1/functional>. Hence the following #define is a hack to prevent
443 // <tr1/functional> from being included.
444 #define _TR1_FUNCTIONAL 1
446 #undef _TR1_FUNCTIONAL // Allows the user to #include
447 // <tr1/functional> if he chooses to.
449 #include <tr1/tuple> // NOLINT
450 #endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
453 // If the compiler is not GCC 4.0+, we assume the user is using a
454 // spec-conforming TR1 implementation.
455 #include <tuple> // NOLINT
456 #endif // GTEST_USE_OWN_TR1_TUPLE
458 #endif // GTEST_HAS_TR1_TUPLE
460 // Determines whether clone(2) is supported.
461 // Usually it will only be available on Linux, excluding
462 // Linux on the Itanium architecture.
463 // Also see http://linux.die.net/man/2/clone.
464 #ifndef GTEST_HAS_CLONE
465 // The user didn't tell us, so we need to figure it out.
467 #if GTEST_OS_LINUX && !defined(__ia64__)
468 #define GTEST_HAS_CLONE 1
470 #define GTEST_HAS_CLONE 0
471 #endif // GTEST_OS_LINUX && !defined(__ia64__)
473 #endif // GTEST_HAS_CLONE
475 // Determines whether to support stream redirection. This is used to test
476 // output correctness and to implement death tests.
477 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
478 #define GTEST_HAS_STREAM_REDIRECTION_ 1
479 #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
481 // Determines whether to support death tests.
482 // Google Test does not support death tests for VC 7.1 and earlier as
483 // abort() in a VC 7.1 application compiled as GUI in debug config
484 // pops up a dialog window that cannot be suppressed programmatically.
485 #if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
486 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
487 GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX)
488 #define GTEST_HAS_DEATH_TEST 1
489 #include <vector> // NOLINT
492 // We don't support MSVC 7.1 with exceptions disabled now. Therefore
493 // all the compilers we care about are adequate for supporting
494 // value-parameterized tests.
495 #define GTEST_HAS_PARAM_TEST 1
497 // Determines whether to support type-driven tests.
499 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
500 // Sun Pro CC, and IBM Visual Age support.
501 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
503 #define GTEST_HAS_TYPED_TEST 1
504 #define GTEST_HAS_TYPED_TEST_P 1
507 // Determines whether to support Combine(). This only makes sense when
508 // value-parameterized tests are enabled. The implementation doesn't
509 // work on Sun Studio since it doesn't understand templated conversion
511 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
512 #define GTEST_HAS_COMBINE 1
515 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
516 #define GTEST_WIDE_STRING_USES_UTF16_ \
517 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
519 // Defines some utility macros.
521 // The GNU compiler emits a warning if nested "if" statements are followed by
522 // an "else" statement and braces are not used to explicitly disambiguate the
523 // "else" binding. This leads to problems with code like:
526 // ASSERT_*(condition) << "Some message";
528 // The "switch (0) case 0:" idiom is used to suppress this.
529 #ifdef __INTEL_COMPILER
530 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
532 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT
535 // Use this annotation at the end of a struct/class definition to
536 // prevent the compiler from optimizing away instances that are never
537 // used. This is useful when all interesting logic happens inside the
538 // c'tor and / or d'tor. Example:
542 // } GTEST_ATTRIBUTE_UNUSED_;
544 // Also use it after a variable or parameter declaration to tell the
545 // compiler the variable/parameter does not have to be used.
546 #if defined(__GNUC__) && !defined(COMPILER_ICC)
547 #define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
549 #define GTEST_ATTRIBUTE_UNUSED_
552 // A macro to disallow operator=
553 // This should be used in the private: declarations for a class.
554 #define GTEST_DISALLOW_ASSIGN_(type)\
555 void operator=(type const &)
557 // A macro to disallow copy constructor and operator=
558 // This should be used in the private: declarations for a class.
559 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
561 GTEST_DISALLOW_ASSIGN_(type)
563 // Tell the compiler to warn about unused return values for functions declared
564 // with this macro. The macro should be used on function declarations
565 // following the argument list:
567 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
568 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
569 #define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
571 #define GTEST_MUST_USE_RESULT_
572 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
574 // Determine whether the compiler supports Microsoft's Structured Exception
575 // Handling. This is supported by several Windows compilers but generally
576 // does not exist on any other system.
577 #ifndef GTEST_HAS_SEH
578 // The user didn't tell us, so we need to figure it out.
580 #if defined(_MSC_VER) || defined(__BORLANDC__)
581 // These two compilers are known to support SEH.
582 #define GTEST_HAS_SEH 1
585 #define GTEST_HAS_SEH 0
588 #endif // GTEST_HAS_SEH
592 #if GTEST_LINKED_AS_SHARED_LIBRARY
593 #define GTEST_API_ __declspec(dllimport)
594 #elif GTEST_CREATE_SHARED_LIBRARY
595 #define GTEST_API_ __declspec(dllexport)
612 class StrStream : public ::std::stringstream {
614 void* operator new(size_t, void* p) { return p; }
615 void* operator new[](size_t, void* p) { return p; }
617 void* operator new(size_t size) {
621 void operator delete(void* p) {
625 void* operator new[](size_t size) {
629 void operator delete[](void* p) {
636 // A helper for suppressing warnings on constant condition. It just
637 // returns 'condition'.
638 GTEST_API_ bool IsTrue(bool condition);
640 // Defines scoped_ptr.
642 // This implementation of scoped_ptr is PARTIAL - it only contains
643 // enough stuff to satisfy Google Test's need.
644 template <typename T>
647 typedef T element_type;
649 explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
650 ~scoped_ptr() { reset(); }
652 T& operator*() const { return *ptr_; }
653 T* operator->() const { return ptr_; }
654 T* get() const { return ptr_; }
662 void reset(T* p = NULL) {
664 if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
673 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
678 // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
679 // Regular Expression syntax.
680 class GTEST_API_ RE {
682 // A copy constructor is required by the Standard to initialize object
683 // references from r-values.
684 RE(const RE& other) { Init(other.pattern()); }
686 // Constructs an RE from a string.
687 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
689 #if GTEST_HAS_GLOBAL_STRING
690 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
691 #endif // GTEST_HAS_GLOBAL_STRING
693 RE(const char* regex) { Init(regex); } // NOLINT
696 // Returns the string representation of the regex.
697 const char* pattern() const { return pattern_; }
699 // FullMatch(str, re) returns true iff regular expression re matches
701 // PartialMatch(str, re) returns true iff regular expression re
702 // matches a substring of str (including str itself).
704 // TODO(wan@google.com): make FullMatch() and PartialMatch() work
705 // when str contains NUL characters.
706 static bool FullMatch(const ::std::string& str, const RE& re) {
707 return FullMatch(str.c_str(), re);
709 static bool PartialMatch(const ::std::string& str, const RE& re) {
710 return PartialMatch(str.c_str(), re);
713 #if GTEST_HAS_GLOBAL_STRING
714 static bool FullMatch(const ::string& str, const RE& re) {
715 return FullMatch(str.c_str(), re);
717 static bool PartialMatch(const ::string& str, const RE& re) {
718 return PartialMatch(str.c_str(), re);
720 #endif // GTEST_HAS_GLOBAL_STRING
722 static bool FullMatch(const char* str, const RE& re);
723 static bool PartialMatch(const char* str, const RE& re);
726 void Init(const char* regex);
728 // We use a const char* instead of a string, as Google Test may be used
729 // where string is not available. We also do not use Google Test's own
730 // String type here, in order to simplify dependencies between the
732 const char* pattern_;
734 #if GTEST_USES_POSIX_RE
735 regex_t full_regex_; // For FullMatch().
736 regex_t partial_regex_; // For PartialMatch().
737 #else // GTEST_USES_SIMPLE_RE
738 const char* full_pattern_; // For FullMatch();
741 GTEST_DISALLOW_ASSIGN_(RE);
744 // Defines logging utilities:
745 // GTEST_LOG_(severity) - logs messages at the specified severity level. The
746 // message itself is streamed into the macro.
747 // LogToStderr() - directs all log messages to stderr.
748 // FlushInfoLog() - flushes informational log messages.
750 enum GTestLogSeverity {
757 // Formats log entry severity, provides a stream object for streaming the
758 // log message, and terminates the message with a newline when going out of
760 class GTEST_API_ GTestLog {
762 GTestLog(GTestLogSeverity severity, const char* file, int line);
764 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
767 ::std::ostream& GetStream() { return ::std::cerr; }
770 const GTestLogSeverity severity_;
772 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
775 #define GTEST_LOG_(severity) \
776 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
777 __FILE__, __LINE__).GetStream()
779 inline void LogToStderr() {}
780 inline void FlushInfoLog() { fflush(NULL); }
782 // INTERNAL IMPLEMENTATION - DO NOT USE.
784 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
787 // GTEST_CHECK_(boolean_condition);
789 // GTEST_CHECK_(boolean_condition) << "Additional message";
791 // This checks the condition and if the condition is not satisfied
792 // it prints message about the condition violation, including the
793 // condition itself, plus additional message streamed into it, if any,
794 // and then it aborts the program. It aborts the program irrespective of
795 // whether it is built in the debug mode or not.
796 #define GTEST_CHECK_(condition) \
797 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
798 if (::testing::internal::IsTrue(condition)) \
801 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
803 // An all-mode assert to verify that the given POSIX-style function
804 // call returns 0 (indicating success). Known limitation: this
805 // doesn't expand to a balanced 'if' statement, so enclose the macro
806 // in {} if you need to use it as the only statement in an 'if'
808 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
809 if (const int gtest_error = (posix_call)) \
810 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
813 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
815 // Downcasts the pointer of type Base to Derived.
816 // Derived must be a subclass of Base. The parameter MUST
817 // point to a class of type Derived, not any subclass of it.
818 // When RTTI is available, the function performs a runtime
819 // check to enforce this.
820 template <class Derived, class Base>
821 Derived* CheckedDowncastToActualType(Base* base) {
823 GTEST_CHECK_(typeid(*base) == typeid(Derived));
824 return dynamic_cast<Derived*>(base); // NOLINT
826 return static_cast<Derived*>(base); // Poor man's downcast.
830 #if GTEST_HAS_STREAM_REDIRECTION_
832 // Defines the stderr capturer:
833 // CaptureStdout - starts capturing stdout.
834 // GetCapturedStdout - stops capturing stdout and returns the captured string.
835 // CaptureStderr - starts capturing stderr.
836 // GetCapturedStderr - stops capturing stderr and returns the captured string.
838 GTEST_API_ void CaptureStdout();
839 GTEST_API_ String GetCapturedStdout();
840 GTEST_API_ void CaptureStderr();
841 GTEST_API_ String GetCapturedStderr();
843 #endif // GTEST_HAS_STREAM_REDIRECTION_
846 #if GTEST_HAS_DEATH_TEST
848 // A copy of all command line arguments. Set by InitGoogleTest().
849 extern ::std::vector<String> g_argvs;
851 // GTEST_HAS_DEATH_TEST implies we have ::std::string.
852 const ::std::vector<String>& GetArgvs();
854 #endif // GTEST_HAS_DEATH_TEST
856 // Defines synchronization primitives.
858 #if GTEST_HAS_PTHREAD
860 // Sleeps for (roughly) n milli-seconds. This function is only for
861 // testing Google Test's own constructs. Don't use it in user tests,
862 // either directly or indirectly.
863 inline void SleepMilliseconds(int n) {
864 const timespec time = {
866 n * 1000L * 1000L, // And n ms.
868 nanosleep(&time, NULL);
871 // Allows a controller thread to pause execution of newly created
872 // threads until notified. Instances of this class must be created
873 // and destroyed in the controller thread.
875 // This class is only for testing Google Test's own constructs. Do not
876 // use it in user tests, either directly or indirectly.
879 Notification() : notified_(false) {}
881 // Notifies all threads created with this notification to start. Must
882 // be called from the controller thread.
883 void Notify() { notified_ = true; }
885 // Blocks until the controller thread notifies. Must be called from a test
887 void WaitForNotification() {
889 SleepMilliseconds(10);
894 volatile bool notified_;
896 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
899 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
900 // Consequently, it cannot select a correct instantiation of ThreadWithParam
901 // in order to call its Run(). Introducing ThreadWithParamBase as a
902 // non-templated base class for ThreadWithParam allows us to bypass this
904 class ThreadWithParamBase {
906 virtual ~ThreadWithParamBase() {}
907 virtual void Run() = 0;
910 // pthread_create() accepts a pointer to a function type with the C linkage.
911 // According to the Standard (7.5/1), function types with different linkages
912 // are different even if they are otherwise identical. Some compilers (for
913 // example, SunStudio) treat them as different types. Since class methods
914 // cannot be defined with C-linkage we need to define a free C-function to
915 // pass into pthread_create().
916 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
917 static_cast<ThreadWithParamBase*>(thread)->Run();
921 // Helper class for testing Google Test's multi-threading constructs.
924 // void ThreadFunc(int param) { /* Do things with param */ }
925 // Notification thread_can_start;
927 // // The thread_can_start parameter is optional; you can supply NULL.
928 // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
929 // thread_can_start.Notify();
931 // These classes are only for testing Google Test's own constructs. Do
932 // not use them in user tests, either directly or indirectly.
933 template <typename T>
934 class ThreadWithParam : public ThreadWithParamBase {
936 typedef void (*UserThreadFunc)(T);
939 UserThreadFunc func, T param, Notification* thread_can_start)
942 thread_can_start_(thread_can_start),
944 ThreadWithParamBase* const base = this;
945 // The thread can be created only after all fields except thread_
946 // have been initialized.
947 GTEST_CHECK_POSIX_SUCCESS_(
948 pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
950 ~ThreadWithParam() { Join(); }
954 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
960 if (thread_can_start_ != NULL)
961 thread_can_start_->WaitForNotification();
966 const UserThreadFunc func_; // User-supplied thread function.
967 const T param_; // User-supplied parameter to the thread function.
968 // When non-NULL, used to block execution until the controller thread
970 Notification* const thread_can_start_;
971 bool finished_; // true iff we know that the thread function has finished.
972 pthread_t thread_; // The native thread object.
974 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
977 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
981 // MutexBase and Mutex implement mutex on pthreads-based platforms. They
982 // are used in conjunction with class MutexLock:
986 // MutexLock lock(&mutex); // Acquires the mutex and releases it at the end
987 // // of the current scope.
989 // MutexBase implements behavior for both statically and dynamically
990 // allocated mutexes. Do not use MutexBase directly. Instead, write
991 // the following to define a static mutex:
993 // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
995 // You can forward declare a static mutex like this:
997 // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
999 // To create a dynamic mutex, just define an object of type Mutex.
1002 // Acquires this mutex.
1004 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1005 owner_ = pthread_self();
1008 // Releases this mutex.
1010 // We don't protect writing to owner_ here, as it's the caller's
1011 // responsibility to ensure that the current thread holds the
1012 // mutex when this is called.
1014 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1017 // Does nothing if the current thread holds the mutex. Otherwise, crashes
1018 // with high probability.
1019 void AssertHeld() const {
1020 GTEST_CHECK_(owner_ == pthread_self())
1021 << "The current thread is not holding the mutex @" << this;
1024 // A static mutex may be used before main() is entered. It may even
1025 // be used before the dynamic initialization stage. Therefore we
1026 // must be able to initialize a static mutex object at link time.
1027 // This means MutexBase has to be a POD and its member variables
1028 // have to be public.
1030 pthread_mutex_t mutex_; // The underlying pthread mutex.
1031 pthread_t owner_; // The thread holding the mutex; 0 means no one holds it.
1034 // Forward-declares a static mutex.
1035 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1036 extern ::testing::internal::MutexBase mutex
1038 // Defines and statically (i.e. at link time) initializes a static mutex.
1039 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1040 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
1042 // The Mutex class can only be used for mutexes created at runtime. It
1043 // shares its API with MutexBase otherwise.
1044 class Mutex : public MutexBase {
1047 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1051 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1055 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1058 // We cannot name this class MutexLock as the ctor declaration would
1059 // conflict with a macro named MutexLock, which is defined on some
1060 // platforms. Hence the typedef trick below.
1061 class GTestMutexLock {
1063 explicit GTestMutexLock(MutexBase* mutex)
1064 : mutex_(mutex) { mutex_->Lock(); }
1066 ~GTestMutexLock() { mutex_->Unlock(); }
1069 MutexBase* const mutex_;
1071 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1074 typedef GTestMutexLock MutexLock;
1076 // Helpers for ThreadLocal.
1078 // pthread_key_create() requires DeleteThreadLocalValue() to have
1079 // C-linkage. Therefore it cannot be templatized to access
1080 // ThreadLocal<T>. Hence the need for class
1081 // ThreadLocalValueHolderBase.
1082 class ThreadLocalValueHolderBase {
1084 virtual ~ThreadLocalValueHolderBase() {}
1087 // Called by pthread to delete thread-local data stored by
1088 // pthread_setspecific().
1089 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1090 delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1093 // Implements thread-local storage on pthreads-based systems.
1096 // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
1099 // tl.set(150); // Changes the value for thread 2 only.
1100 // EXPECT_EQ(150, tl.get());
1103 // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
1105 // EXPECT_EQ(200, tl.get());
1107 // The template type argument T must have a public copy constructor.
1108 // In addition, the default ThreadLocal constructor requires T to have
1109 // a public default constructor.
1111 // An object managed for a thread by a ThreadLocal instance is deleted
1112 // when the thread exits. Or, if the ThreadLocal instance dies in
1113 // that thread, when the ThreadLocal dies. It's the user's
1114 // responsibility to ensure that all other threads using a ThreadLocal
1115 // have exited when it dies, or the per-thread objects for those
1116 // threads will not be deleted.
1118 // Google Test only uses global ThreadLocal objects. That means they
1119 // will die after main() has returned. Therefore, no per-thread
1120 // object managed by Google Test will be leaked as long as all threads
1121 // using Google Test have exited when main() returns.
1122 template <typename T>
1125 ThreadLocal() : key_(CreateKey()),
1127 explicit ThreadLocal(const T& value) : key_(CreateKey()),
1131 // Destroys the managed object for the current thread, if any.
1132 DeleteThreadLocalValue(pthread_getspecific(key_));
1134 // Releases resources associated with the key. This will *not*
1135 // delete managed objects for other threads.
1136 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1139 T* pointer() { return GetOrCreateValue(); }
1140 const T* pointer() const { return GetOrCreateValue(); }
1141 const T& get() const { return *pointer(); }
1142 void set(const T& value) { *pointer() = value; }
1145 // Holds a value of type T.
1146 class ValueHolder : public ThreadLocalValueHolderBase {
1148 explicit ValueHolder(const T& value) : value_(value) {}
1150 T* pointer() { return &value_; }
1154 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1157 static pthread_key_t CreateKey() {
1159 // When a thread exits, DeleteThreadLocalValue() will be called on
1160 // the object managed for that thread.
1161 GTEST_CHECK_POSIX_SUCCESS_(
1162 pthread_key_create(&key, &DeleteThreadLocalValue));
1166 T* GetOrCreateValue() const {
1167 ThreadLocalValueHolderBase* const holder =
1168 static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1169 if (holder != NULL) {
1170 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1173 ValueHolder* const new_holder = new ValueHolder(default_);
1174 ThreadLocalValueHolderBase* const holder_base = new_holder;
1175 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1176 return new_holder->pointer();
1179 // A key pthreads uses for looking up per-thread values.
1180 const pthread_key_t key_;
1181 const T default_; // The default value for each thread.
1183 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1186 #define GTEST_IS_THREADSAFE 1
1188 #else // GTEST_HAS_PTHREAD
1190 // A dummy implementation of synchronization primitives (mutex, lock,
1191 // and thread-local variable). Necessary for compiling Google Test where
1192 // mutex is not supported - using Google Test in multiple threads is not
1193 // supported on such platforms.
1198 void AssertHeld() const {}
1201 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1202 extern ::testing::internal::Mutex mutex
1204 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1206 class GTestMutexLock {
1208 explicit GTestMutexLock(Mutex*) {} // NOLINT
1211 typedef GTestMutexLock MutexLock;
1213 template <typename T>
1216 ThreadLocal() : value_() {}
1217 explicit ThreadLocal(const T& value) : value_(value) {}
1218 T* pointer() { return &value_; }
1219 const T* pointer() const { return &value_; }
1220 const T& get() const { return value_; }
1221 void set(const T& value) { value_ = value; }
1226 // The above synchronization primitives have dummy implementations.
1227 // Therefore Google Test is not thread-safe.
1228 #define GTEST_IS_THREADSAFE 0
1230 #endif // GTEST_HAS_PTHREAD
1232 // Returns the number of threads running in the process, or 0 to indicate that
1233 // we cannot detect it.
1234 GTEST_API_ size_t GetThreadCount();
1236 // Passing non-POD classes through ellipsis (...) crashes the ARM
1237 // compiler and generates a warning in Sun Studio. The Nokia Symbian
1238 // and the IBM XL C/C++ compiler try to instantiate a copy constructor
1239 // for objects passed through ellipsis (...), failing for uncopyable
1240 // objects. We define this to ensure that only POD is passed through
1241 // ellipsis on these systems.
1242 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
1243 // We lose support for NULL detection where the compiler doesn't like
1244 // passing non-POD classes through ellipsis (...).
1245 #define GTEST_ELLIPSIS_NEEDS_POD_ 1
1247 #define GTEST_CAN_COMPARE_NULL 1
1250 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
1251 // const T& and const T* in a function template. These compilers
1252 // _can_ decide between class template specializations for T and T*,
1253 // so a tr1::type_traits-like is_pointer works.
1254 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
1255 #define GTEST_NEEDS_IS_POINTER_ 1
1258 template <bool bool_value>
1259 struct bool_constant {
1260 typedef bool_constant<bool_value> type;
1261 static const bool value = bool_value;
1263 template <bool bool_value> const bool bool_constant<bool_value>::value;
1265 typedef bool_constant<false> false_type;
1266 typedef bool_constant<true> true_type;
1268 template <typename T>
1269 struct is_pointer : public false_type {};
1271 template <typename T>
1272 struct is_pointer<T*> : public true_type {};
1274 #if GTEST_OS_WINDOWS
1275 #define GTEST_PATH_SEP_ "\\"
1276 #define GTEST_HAS_ALT_PATH_SEP_ 1
1277 // The biggest signed integer type the compiler supports.
1278 typedef __int64 BiggestInt;
1280 #define GTEST_PATH_SEP_ "/"
1281 #define GTEST_HAS_ALT_PATH_SEP_ 0
1282 typedef long long BiggestInt; // NOLINT
1283 #endif // GTEST_OS_WINDOWS
1285 // The testing::internal::posix namespace holds wrappers for common
1286 // POSIX functions. These wrappers hide the differences between
1287 // Windows/MSVC and POSIX systems. Since some compilers define these
1288 // standard functions as macros, the wrapper cannot have the same name
1289 // as the wrapped function.
1293 // Functions with a different name on Windows.
1295 #if GTEST_OS_WINDOWS
1297 typedef struct _stat StatStruct;
1300 inline int IsATTY(int fd) { return isatty(fd); }
1301 inline int StrCaseCmp(const char* s1, const char* s2) {
1302 return stricmp(s1, s2);
1304 inline char* StrDup(const char* src) { return strdup(src); }
1305 #else // !__BORLANDC__
1306 #if GTEST_OS_WINDOWS_MOBILE
1307 inline int IsATTY(int /* fd */) { return 0; }
1309 inline int IsATTY(int fd) { return _isatty(fd); }
1310 #endif // GTEST_OS_WINDOWS_MOBILE
1311 inline int StrCaseCmp(const char* s1, const char* s2) {
1312 return _stricmp(s1, s2);
1314 inline char* StrDup(const char* src) { return _strdup(src); }
1315 #endif // __BORLANDC__
1317 #if GTEST_OS_WINDOWS_MOBILE
1318 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
1319 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1320 // time and thus not defined there.
1322 inline int FileNo(FILE* file) { return _fileno(file); }
1323 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
1324 inline int RmDir(const char* dir) { return _rmdir(dir); }
1325 inline bool IsDir(const StatStruct& st) {
1326 return (_S_IFDIR & st.st_mode) != 0;
1328 #endif // GTEST_OS_WINDOWS_MOBILE
1332 typedef struct stat StatStruct;
1334 inline int FileNo(FILE* file) { return fileno(file); }
1335 inline int IsATTY(int fd) { return isatty(fd); }
1336 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
1337 inline int StrCaseCmp(const char* s1, const char* s2) {
1338 return strcasecmp(s1, s2);
1340 inline char* StrDup(const char* src) { return strdup(src); }
1341 inline int RmDir(const char* dir) { return rmdir(dir); }
1342 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
1344 #endif // GTEST_OS_WINDOWS
1346 // Functions deprecated by MSVC 8.0.
1349 // Temporarily disable warning 4996 (deprecated function).
1350 #pragma warning(push)
1351 #pragma warning(disable:4996)
1354 inline const char* StrNCpy(char* dest, const char* src, size_t n) {
1355 return strncpy(dest, src, n);
1358 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
1359 // StrError() aren't needed on Windows CE at this time and thus not
1362 #if !GTEST_OS_WINDOWS_MOBILE
1363 inline int ChDir(const char* dir) { return chdir(dir); }
1365 inline FILE* FOpen(const char* path, const char* mode) {
1366 return fopen(path, mode);
1368 #if !GTEST_OS_WINDOWS_MOBILE
1369 inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
1370 return freopen(path, mode, stream);
1372 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
1374 inline int FClose(FILE* fp) { return fclose(fp); }
1375 #if !GTEST_OS_WINDOWS_MOBILE
1376 inline int Read(int fd, void* buf, unsigned int count) {
1377 return static_cast<int>(read(fd, buf, count));
1379 inline int Write(int fd, const void* buf, unsigned int count) {
1380 return static_cast<int>(write(fd, buf, count));
1382 inline int Close(int fd) { return close(fd); }
1383 inline const char* StrError(int errnum) { return strerror(errnum); }
1385 inline const char* GetEnv(const char* name) {
1386 #if GTEST_OS_WINDOWS_MOBILE
1387 // We are on Windows CE, which has no environment variables.
1389 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1390 // Environment variables which we programmatically clear will be set to the
1391 // empty string rather than unset (NULL). Handle that case.
1392 const char* const env = getenv(name);
1393 return (env != NULL && env[0] != '\0') ? env : NULL;
1395 return getenv(name);
1400 #pragma warning(pop) // Restores the warning state.
1403 #if GTEST_OS_WINDOWS_MOBILE
1404 // Windows CE has no C library. The abort() function is used in
1405 // several places in Google Test. This implementation provides a reasonable
1406 // imitation of standard behaviour.
1409 inline void Abort() { abort(); }
1410 #endif // GTEST_OS_WINDOWS_MOBILE
1412 } // namespace posix
1414 // The maximum number a BiggestInt can represent. This definition
1415 // works no matter BiggestInt is represented in one's complement or
1416 // two's complement.
1418 // We cannot rely on numeric_limits in STL, as __int64 and long long
1419 // are not part of standard C++ and numeric_limits doesn't need to be
1420 // defined for them.
1421 const BiggestInt kMaxBiggestInt =
1422 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
1424 // This template class serves as a compile-time function from size to
1425 // type. It maps a size in bytes to a primitive type with that
1428 // TypeWithSize<4>::UInt
1430 // is typedef-ed to be unsigned int (unsigned integer made up of 4
1433 // Such functionality should belong to STL, but I cannot find it
1436 // Google Test uses this class in the implementation of floating-point
1439 // For now it only handles UInt (unsigned int) as that's all Google Test
1440 // needs. Other types can be easily added in the future if need
1442 template <size_t size>
1443 class TypeWithSize {
1445 // This prevents the user from using TypeWithSize<N> with incorrect
1450 // The specialization for size 4.
1452 class TypeWithSize<4> {
1454 // unsigned int has size 4 in both gcc and MSVC.
1456 // As base/basictypes.h doesn't compile on Windows, we cannot use
1457 // uint32, uint64, and etc here.
1459 typedef unsigned int UInt;
1462 // The specialization for size 8.
1464 class TypeWithSize<8> {
1466 #if GTEST_OS_WINDOWS
1467 typedef __int64 Int;
1468 typedef unsigned __int64 UInt;
1470 typedef long long Int; // NOLINT
1471 typedef unsigned long long UInt; // NOLINT
1472 #endif // GTEST_OS_WINDOWS
1475 // Integer types of known sizes.
1476 typedef TypeWithSize<4>::Int Int32;
1477 typedef TypeWithSize<4>::UInt UInt32;
1478 typedef TypeWithSize<8>::Int Int64;
1479 typedef TypeWithSize<8>::UInt UInt64;
1480 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
1482 // Utilities for command line flags and environment variables.
1484 // Macro for referencing flags.
1485 #define GTEST_FLAG(name) FLAGS_gtest_##name
1487 // Macros for declaring flags.
1488 #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
1489 #define GTEST_DECLARE_int32_(name) \
1490 GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
1491 #define GTEST_DECLARE_string_(name) \
1492 GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name)
1494 // Macros for defining flags.
1495 #define GTEST_DEFINE_bool_(name, default_val, doc) \
1496 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
1497 #define GTEST_DEFINE_int32_(name, default_val, doc) \
1498 GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
1499 #define GTEST_DEFINE_string_(name, default_val, doc) \
1500 GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val)
1502 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
1503 // to *value and returns true; otherwise leaves *value unchanged and returns
1505 // TODO(chandlerc): Find a better way to refactor flag and environment parsing
1506 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
1508 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
1510 // Parses a bool/Int32/string from the environment variable
1511 // corresponding to the given Google Test flag.
1512 bool BoolFromGTestEnv(const char* flag, bool default_val);
1513 GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
1514 const char* StringFromGTestEnv(const char* flag, const char* default_val);
1516 } // namespace internal
1517 } // namespace testing
1519 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_