1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Authors: wan@google.com (Zhanyong Wan)
32 // Low-level types and utilities for porting Google Test to various
33 // platforms. They are subject to change without notice. DO NOT USE
36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
39 // The user can define the following macros in the build script to
40 // control Google Test's behavior. If the user doesn't define a macro
41 // in this list, Google Test will define it.
43 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
44 // is/isn't available.
45 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
46 // is/isn't available (some systems define
47 // ::string, which is different to std::string).
48 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
49 // is/isn't available (some systems define
50 // ::wstring, which is different to std::wstring).
51 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
52 // is/isn't available.
53 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
55 // GTEST_HAS_STD_STRING - Define it to 1/0 to indicate that
56 // std::string does/doesn't work (Google Test can
57 // be used where std::string is unavailable).
58 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
59 // std::wstring does/doesn't work (Google Test can
60 // be used where std::wstring is unavailable).
61 // GTEST_HAS_TR1_TUPLE 1 - Define it to 1/0 to indicate tr1::tuple
62 // is/isn't available.
64 // This header defines the following utilities:
66 // Macros indicating the name of the Google C++ Testing Framework project:
67 // GTEST_NAME - a string literal of the project name.
68 // GTEST_FLAG_PREFIX - a string literal of the prefix all Google
69 // Test flag names share.
70 // GTEST_FLAG_PREFIX_UPPER - a string literal of the prefix all Google
71 // Test flag names share, in upper case.
73 // Macros indicating the current platform:
74 // GTEST_OS_CYGWIN - defined iff compiled on Cygwin.
75 // GTEST_OS_LINUX - defined iff compiled on Linux.
76 // GTEST_OS_MAC - defined iff compiled on Mac OS X.
77 // GTEST_OS_SOLARIS - defined iff compiled on Sun Solaris.
78 // GTEST_OS_SYMBIAN - defined iff compiled for Symbian.
79 // GTEST_OS_WINDOWS - defined iff compiled on Windows.
80 // GTEST_OS_ZOS - defined iff compiled on IBM z/OS.
82 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
83 // most stable support. Since core members of the Google Test project
84 // don't have access to other platforms, support for them may be less
85 // stable. If you notice any problems on your platform, please notify
86 // googletestframework@googlegroups.com (patches for fixing them are
87 // even more welcome!).
89 // Note that it is possible that none of the GTEST_OS_ macros are defined.
91 // Macros indicating available Google Test features:
92 // GTEST_HAS_COMBINE - defined iff Combine construct is supported
93 // in value-parameterized tests.
94 // GTEST_HAS_DEATH_TEST - defined iff death tests are supported.
95 // GTEST_HAS_PARAM_TEST - defined iff value-parameterized tests are
97 // GTEST_HAS_TYPED_TEST - defined iff typed tests are supported.
98 // GTEST_HAS_TYPED_TEST_P - defined iff type-parameterized tests are
101 // Macros for basic C++ coding:
102 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
103 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances don't have to
105 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
106 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
109 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
110 // - synchronization primitives.
111 // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
112 // synchronization primitives have real implementations
113 // and Google Test is thread-safe; or 0 otherwise.
115 // Template meta programming:
116 // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
119 // scoped_ptr - as in TR2.
121 // Regular expressions:
122 // RE - a simple regular expression class using the POSIX
123 // Extended Regular Expression syntax. Not available on
127 // GTEST_LOG_() - logs messages at the specified severity level.
128 // LogToStderr() - directs all log messages to stderr.
129 // FlushInfoLog() - flushes informational log messages.
132 // CaptureStderr() - starts capturing stderr.
133 // GetCapturedStderr() - stops capturing stderr and returns the captured
137 // TypeWithSize - maps an integer to a int type.
138 // Int32, UInt32, Int64, UInt64, TimeInMillis
139 // - integers of known sizes.
140 // BiggestInt - the biggest signed integer type.
142 // Command-line utilities:
143 // GTEST_FLAG() - references a flag.
144 // GTEST_DECLARE_*() - declares a flag.
145 // GTEST_DEFINE_*() - defines a flag.
146 // GetArgvs() - returns the command line as a vector of strings.
148 // Environment variable utilities:
149 // GetEnv() - gets the value of an environment variable.
150 // BoolFromGTestEnv() - parses a bool environment variable.
151 // Int32FromGTestEnv() - parses an Int32 environment variable.
152 // StringFromGTestEnv() - parses a string environment variable.
156 #include <iostream> // Used for GTEST_CHECK_
158 #define GTEST_NAME "Google Test"
159 #define GTEST_FLAG_PREFIX "gtest_"
160 #define GTEST_FLAG_PREFIX_UPPER "GTEST_"
162 // Determines the version of gcc that is used to compile this.
164 // 40302 means version 4.3.2.
165 #define GTEST_GCC_VER_ \
166 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
169 // Determines the platform on which Google Test is compiled.
171 #define GTEST_OS_CYGWIN
173 #define GTEST_OS_SYMBIAN
174 #elif defined _MSC_VER
175 // TODO(kenton@google.com): GTEST_OS_WINDOWS is currently used to mean
176 // both "The OS is Windows" and "The compiler is MSVC". These
177 // meanings really should be separated in order to better support
178 // Windows compilers other than MSVC.
179 #define GTEST_OS_WINDOWS
180 #elif defined __APPLE__
182 #elif defined __linux__
183 #define GTEST_OS_LINUX
184 #elif defined __MVS__
186 #elif defined(__sun) && defined(__SVR4)
187 #define GTEST_OS_SOLARIS
188 #elif defined(__HAIKU__)
189 #define GTEST_OS_HAIKU
192 // Determines whether ::std::string and ::string are available.
194 #ifndef GTEST_HAS_STD_STRING
195 // The user didn't tell us whether ::std::string is available, so we
196 // need to figure it out.
198 #ifdef GTEST_OS_WINDOWS
199 // Assumes that exceptions are enabled by default.
200 #ifndef _HAS_EXCEPTIONS
201 #define _HAS_EXCEPTIONS 1
202 #endif // _HAS_EXCEPTIONS
203 // GTEST_HAS_EXCEPTIONS is non-zero iff exceptions are enabled. It is
204 // always defined, while _HAS_EXCEPTIONS is defined only on Windows.
205 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
206 // On Windows, we can use ::std::string if the compiler version is VS
207 // 2005 or above, or if exceptions are enabled.
208 #define GTEST_HAS_STD_STRING ((_MSC_VER >= 1400) || GTEST_HAS_EXCEPTIONS)
209 #else // We are on Linux or Mac OS.
210 #define GTEST_HAS_EXCEPTIONS 0
211 #define GTEST_HAS_STD_STRING 1
212 #endif // GTEST_OS_WINDOWS
214 #endif // GTEST_HAS_STD_STRING
216 #ifndef GTEST_HAS_GLOBAL_STRING
217 // The user didn't tell us whether ::string is available, so we need
220 #define GTEST_HAS_GLOBAL_STRING 0
222 #endif // GTEST_HAS_GLOBAL_STRING
224 #ifndef GTEST_HAS_STD_WSTRING
225 // The user didn't tell us whether ::std::wstring is available, so we need
227 // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
230 #if defined(GTEST_OS_CYGWIN) || defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) || defined(_MINIX)
231 // At least some versions of cygwin don't support ::std::wstring.
232 // Solaris' libc++ doesn't support it either.
233 // Minix currently doesn't support it either.
234 #define GTEST_HAS_STD_WSTRING 0
236 #define GTEST_HAS_STD_WSTRING GTEST_HAS_STD_STRING
237 #endif // defined(GTEST_OS_CYGWIN) || defined(GTEST_OS_SOLARIS)
239 #endif // GTEST_HAS_STD_WSTRING
241 #ifndef GTEST_HAS_GLOBAL_WSTRING
242 // The user didn't tell us whether ::wstring is available, so we need
244 #define GTEST_HAS_GLOBAL_WSTRING GTEST_HAS_GLOBAL_STRING
245 #endif // GTEST_HAS_GLOBAL_WSTRING
247 #if GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || \
248 GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
249 #include <string> // NOLINT
250 #endif // GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING ||
251 // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
253 #if GTEST_HAS_STD_STRING
254 #include <sstream> // NOLINT
256 #include <strstream> // NOLINT
257 #endif // GTEST_HAS_STD_STRING
259 // Determines whether RTTI is available.
260 #ifndef GTEST_HAS_RTTI
261 // The user didn't tell us whether RTTI is enabled, so we need to
266 #ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
267 #define GTEST_HAS_RTTI 1
269 #define GTEST_HAS_RTTI 0
272 #elif defined(__GNUC__)
274 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
275 #if GTEST_GCC_VER_ >= 40302
277 #define GTEST_HAS_RTTI 1
279 #define GTEST_HAS_RTTI 0
282 // For gcc versions smaller than 4.3.2, we assume RTTI is enabled.
283 #define GTEST_HAS_RTTI 1
284 #endif // GTEST_GCC_VER >= 40302
288 // Unknown compiler - assume RTTI is enabled.
289 #define GTEST_HAS_RTTI 1
293 #endif // GTEST_HAS_RTTI
295 // Determines whether <pthread.h> is available.
296 #ifndef GTEST_HAS_PTHREAD
297 // The user didn't tell us, so we need to figure it out.
299 #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC)
300 #define GTEST_HAS_PTHREAD 1
302 #define GTEST_HAS_PTHREAD 0
303 #endif // GTEST_OS_LINUX || GTEST_OS_MAC
305 #endif // GTEST_HAS_PTHREAD
307 // Determines whether tr1/tuple is available. If you have tr1/tuple
308 // on your platform, define GTEST_HAS_TR1_TUPLE=1 for both the Google
309 // Test project and your tests. If you would like Google Test to detect
310 // tr1/tuple on your platform automatically, please open an issue
311 // ticket at http://code.google.com/p/googletest.
312 #ifndef GTEST_HAS_TR1_TUPLE
313 // The user didn't tell us, so we need to figure it out.
315 // GCC provides <tr1/tuple> since 4.0.0.
316 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
317 #define GTEST_HAS_TR1_TUPLE 1
319 #define GTEST_HAS_TR1_TUPLE 0
321 #endif // GTEST_HAS_TR1_TUPLE
323 // To avoid conditional compilation everywhere, we make it
324 // gtest-port.h's responsibility to #include the header implementing
326 #if GTEST_HAS_TR1_TUPLE
327 #if defined(__GNUC__)
328 // GCC implements tr1/tuple in the <tr1/tuple> header. This does not
329 // conform to the TR1 spec, which requires the header to be <tuple>.
332 // If the compiler is not GCC, we assume the user is using a
333 // spec-conforming TR1 implementation.
336 #endif // GTEST_HAS_TR1_TUPLE
338 // Determines whether clone(2) is supported.
339 // Usually it will only be available on Linux, excluding
340 // Linux on the Itanium architecture.
341 // Also see http://linux.die.net/man/2/clone.
342 #ifndef GTEST_HAS_CLONE
343 // The user didn't tell us, so we need to figure it out.
345 #if defined(GTEST_OS_LINUX) && !defined(__ia64__)
346 #define GTEST_HAS_CLONE 1
348 #define GTEST_HAS_CLONE 0
349 #endif // defined(GTEST_OS_LINUX) && !defined(__ia64__)
351 #endif // GTEST_HAS_CLONE
353 // Determines whether to support death tests.
354 #if GTEST_HAS_STD_STRING && GTEST_HAS_CLONE
355 #define GTEST_HAS_DEATH_TEST
356 // On some platforms, <regex.h> needs someone to define size_t, and
357 // won't compile otherwise. We can #include it here as we already
358 // included <stdlib.h>, which is guaranteed to define size_t through
363 #include <sys/mman.h>
364 #endif // GTEST_HAS_STD_STRING && GTEST_HAS_CLONE
366 // Determines whether to support value-parameterized tests.
368 #if defined(__GNUC__) || (_MSC_VER >= 1400)
369 // TODO(vladl@google.com): get the implementation rid of vector and list
370 // to compile on MSVC 7.1.
371 #define GTEST_HAS_PARAM_TEST
372 #endif // defined(__GNUC__) || (_MSC_VER >= 1400)
374 // Determines whether to support type-driven tests.
376 // Typed tests need <typeinfo> and variadic macros, which gcc and VC
378 #if defined(__GNUC__) || (_MSC_VER >= 1400)
379 #define GTEST_HAS_TYPED_TEST
380 #define GTEST_HAS_TYPED_TEST_P
381 #endif // defined(__GNUC__) || (_MSC_VER >= 1400)
383 // Determines whether to support Combine(). This only makes sense when
384 // value-parameterized tests are enabled.
385 #if defined(GTEST_HAS_PARAM_TEST) && GTEST_HAS_TR1_TUPLE
386 #define GTEST_HAS_COMBINE
387 #endif // defined(GTEST_HAS_PARAM_TEST) && GTEST_HAS_TR1_TUPLE
389 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
390 #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \
391 defined(GTEST_OS_SYMBIAN)
392 #define GTEST_WIDE_STRING_USES_UTF16_ 1
395 // Defines some utility macros.
397 // The GNU compiler emits a warning if nested "if" statements are followed by
398 // an "else" statement and braces are not used to explicitly disambiguate the
399 // "else" binding. This leads to problems with code like:
402 // ASSERT_*(condition) << "Some message";
404 // The "switch (0) case 0:" idiom is used to suppress this.
405 #ifdef __INTEL_COMPILER
406 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
408 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT
411 // Use this annotation at the end of a struct / class definition to
412 // prevent the compiler from optimizing away instances that are never
413 // used. This is useful when all interesting logic happens inside the
414 // c'tor and / or d'tor. Example:
418 // } GTEST_ATTRIBUTE_UNUSED_;
419 #if defined(__GNUC__) && !defined(COMPILER_ICC)
420 #define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
422 #define GTEST_ATTRIBUTE_UNUSED_
425 // A macro to disallow the evil copy constructor and operator= functions
426 // This should be used in the private: declarations for a class.
427 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
429 void operator=(const type &)
431 // Tell the compiler to warn about unused return values for functions declared
432 // with this macro. The macro should be used on function declarations
433 // following the argument list:
435 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
436 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
437 #define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
439 #define GTEST_MUST_USE_RESULT_
440 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
450 // std::strstream is deprecated. However, we have to use it on
451 // Windows as std::stringstream won't compile on Windows when
452 // exceptions are disabled. We use std::stringstream on other
453 // platforms to avoid compiler warnings there.
454 #if GTEST_HAS_STD_STRING
455 typedef ::std::stringstream StrStream;
457 typedef ::std::strstream StrStream;
458 #endif // GTEST_HAS_STD_STRING
460 // Defines scoped_ptr.
462 // This implementation of scoped_ptr is PARTIAL - it only contains
463 // enough stuff to satisfy Google Test's need.
464 template <typename T>
467 explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
468 ~scoped_ptr() { reset(); }
470 T& operator*() const { return *ptr_; }
471 T* operator->() const { return ptr_; }
472 T* get() const { return ptr_; }
480 void reset(T* p = NULL) {
482 if (sizeof(T) > 0) { // Makes sure T is a complete type.
491 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
494 #ifdef GTEST_HAS_DEATH_TEST
498 // A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended
499 // Regular Expression syntax.
502 // Constructs an RE from a string.
503 #if GTEST_HAS_STD_STRING
504 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
505 #endif // GTEST_HAS_STD_STRING
507 #if GTEST_HAS_GLOBAL_STRING
508 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
509 #endif // GTEST_HAS_GLOBAL_STRING
511 RE(const char* regex) { Init(regex); } // NOLINT
514 // Returns the string representation of the regex.
515 const char* pattern() const { return pattern_; }
517 // FullMatch(str, re) returns true iff regular expression re matches
519 // PartialMatch(str, re) returns true iff regular expression re
520 // matches a substring of str (including str itself).
522 // TODO(wan@google.com): make FullMatch() and PartialMatch() work
523 // when str contains NUL characters.
524 #if GTEST_HAS_STD_STRING
525 static bool FullMatch(const ::std::string& str, const RE& re) {
526 return FullMatch(str.c_str(), re);
528 static bool PartialMatch(const ::std::string& str, const RE& re) {
529 return PartialMatch(str.c_str(), re);
531 #endif // GTEST_HAS_STD_STRING
533 #if GTEST_HAS_GLOBAL_STRING
534 static bool FullMatch(const ::string& str, const RE& re) {
535 return FullMatch(str.c_str(), re);
537 static bool PartialMatch(const ::string& str, const RE& re) {
538 return PartialMatch(str.c_str(), re);
540 #endif // GTEST_HAS_GLOBAL_STRING
542 static bool FullMatch(const char* str, const RE& re);
543 static bool PartialMatch(const char* str, const RE& re);
546 void Init(const char* regex);
548 // We use a const char* instead of a string, as Google Test may be used
549 // where string is not available. We also do not use Google Test's own
550 // String type here, in order to simplify dependencies between the
552 const char* pattern_;
553 regex_t full_regex_; // For FullMatch().
554 regex_t partial_regex_; // For PartialMatch().
558 #endif // GTEST_HAS_DEATH_TEST
560 // Defines logging utilities:
561 // GTEST_LOG_() - logs messages at the specified severity level.
562 // LogToStderr() - directs all log messages to stderr.
563 // FlushInfoLog() - flushes informational log messages.
565 enum GTestLogSeverity {
572 void GTestLog(GTestLogSeverity severity, const char* file,
573 int line, const char* msg);
575 #define GTEST_LOG_(severity, msg)\
576 ::testing::internal::GTestLog(\
577 ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \
578 (::testing::Message() << (msg)).GetString().c_str())
580 inline void LogToStderr() {}
581 inline void FlushInfoLog() { fflush(NULL); }
583 // Defines the stderr capturer:
584 // CaptureStderr - starts capturing stderr.
585 // GetCapturedStderr - stops capturing stderr and returns the captured string.
587 #ifdef GTEST_HAS_DEATH_TEST
589 // A copy of all command line arguments. Set by InitGoogleTest().
590 extern ::std::vector<String> g_argvs;
592 void CaptureStderr();
593 // GTEST_HAS_DEATH_TEST implies we have ::std::string.
594 ::std::string GetCapturedStderr();
595 const ::std::vector<String>& GetArgvs();
597 #endif // GTEST_HAS_DEATH_TEST
599 // Defines synchronization primitives.
601 // A dummy implementation of synchronization primitives (mutex, lock,
602 // and thread-local variable). Necessary for compiling Google Test where
603 // mutex is not supported - using Google Test in multiple threads is not
604 // supported on such platforms.
609 explicit Mutex(int /*unused*/) {}
610 void AssertHeld() const {}
611 enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 };
614 // We cannot call it MutexLock directly as the ctor declaration would
615 // conflict with a macro named MutexLock, which is defined on some
616 // platforms. Hence the typedef trick below.
617 class GTestMutexLock {
619 explicit GTestMutexLock(Mutex*) {} // NOLINT
622 typedef GTestMutexLock MutexLock;
624 template <typename T>
627 ThreadLocal() : value_() {}
628 explicit ThreadLocal(const T& value) : value_(value) {}
629 T* pointer() { return &value_; }
630 const T* pointer() const { return &value_; }
631 const T& get() const { return value_; }
632 void set(const T& value) { value_ = value; }
637 // There's no portable way to detect the number of threads, so we just
638 // return 0 to indicate that we cannot detect it.
639 inline size_t GetThreadCount() { return 0; }
641 // The above synchronization primitives have dummy implementations.
642 // Therefore Google Test is not thread-safe.
643 #define GTEST_IS_THREADSAFE 0
645 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
647 // Passing non-POD classes through ellipsis (...) crashes the ARM
648 // compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to
649 // instantiate a copy constructor for objects passed through ellipsis
650 // (...), failing for uncopyable objects. We define this to indicate
652 #define GTEST_ELLIPSIS_NEEDS_COPY_ 1
654 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
655 // const T& and const T* in a function template. These compilers
656 // _can_ decide between class template specializations for T and T*,
657 // so a tr1::type_traits-like is_pointer works.
658 #define GTEST_NEEDS_IS_POINTER_ 1
660 #endif // defined(__SYMBIAN32__) || defined(__IBMCPP__)
662 template <bool bool_value>
663 struct bool_constant {
664 typedef bool_constant<bool_value> type;
665 static const bool value = bool_value;
667 template <bool bool_value> const bool bool_constant<bool_value>::value;
669 typedef bool_constant<false> false_type;
670 typedef bool_constant<true> true_type;
672 template <typename T>
673 struct is_pointer : public false_type {};
675 template <typename T>
676 struct is_pointer<T*> : public true_type {};
678 // Defines BiggestInt as the biggest signed integer type the compiler
681 #ifdef GTEST_OS_WINDOWS
682 typedef __int64 BiggestInt;
684 typedef long long BiggestInt; // NOLINT
685 #endif // GTEST_OS_WINDOWS
687 // The maximum number a BiggestInt can represent. This definition
688 // works no matter BiggestInt is represented in one's complement or
691 // We cannot rely on numeric_limits in STL, as __int64 and long long
692 // are not part of standard C++ and numeric_limits doesn't need to be
694 const BiggestInt kMaxBiggestInt =
695 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
697 // This template class serves as a compile-time function from size to
698 // type. It maps a size in bytes to a primitive type with that
701 // TypeWithSize<4>::UInt
703 // is typedef-ed to be unsigned int (unsigned integer made up of 4
706 // Such functionality should belong to STL, but I cannot find it
709 // Google Test uses this class in the implementation of floating-point
712 // For now it only handles UInt (unsigned int) as that's all Google Test
713 // needs. Other types can be easily added in the future if need
715 template <size_t size>
718 // This prevents the user from using TypeWithSize<N> with incorrect
723 // The specialization for size 4.
725 class TypeWithSize<4> {
727 // unsigned int has size 4 in both gcc and MSVC.
729 // As base/basictypes.h doesn't compile on Windows, we cannot use
730 // uint32, uint64, and etc here.
732 typedef unsigned int UInt;
735 // The specialization for size 8.
737 class TypeWithSize<8> {
739 #ifdef GTEST_OS_WINDOWS
741 typedef unsigned __int64 UInt;
743 typedef long long Int; // NOLINT
744 typedef unsigned long long UInt; // NOLINT
745 #endif // GTEST_OS_WINDOWS
748 // Integer types of known sizes.
749 typedef TypeWithSize<4>::Int Int32;
750 typedef TypeWithSize<4>::UInt UInt32;
751 typedef TypeWithSize<8>::Int Int64;
752 typedef TypeWithSize<8>::UInt UInt64;
753 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
755 // Utilities for command line flags and environment variables.
757 // A wrapper for getenv() that works on Linux, Windows, and Mac OS.
758 inline const char* GetEnv(const char* name) {
759 #ifdef _WIN32_WCE // We are on Windows CE.
760 // CE has no environment variables.
762 #elif defined(GTEST_OS_WINDOWS) // We are on Windows proper.
763 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996
764 // (deprecated function) there.
765 #pragma warning(push) // Saves the current warning state.
766 #pragma warning(disable:4996) // Temporarily disables warning 4996.
768 #pragma warning(pop) // Restores the warning state.
769 #else // We are on Linux or Mac OS.
775 // Windows CE has no C library. The abort() function is used in
776 // several places in Google Test. This implementation provides a reasonable
777 // imitation of standard behaviour.
780 inline void abort() { ::abort(); }
783 // INTERNAL IMPLEMENTATION - DO NOT USE.
785 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
788 // GTEST_CHECK_(boolean_condition);
790 // GTEST_CHECK_(boolean_condition) << "Additional message";
792 // This checks the condition and if the condition is not satisfied
793 // it prints message about the condition violation, including the
794 // condition itself, plus additional message streamed into it, if any,
795 // and then it aborts the program. It aborts the program irrespective of
796 // whether it is built in the debug mode or not.
797 class GTestCheckProvider {
799 GTestCheckProvider(const char* condition, const char* file, int line) {
800 FormatFileLocation(file, line);
801 ::std::cerr << " ERROR: Condition " << condition << " failed. ";
803 ~GTestCheckProvider() {
804 ::std::cerr << ::std::endl;
807 void FormatFileLocation(const char* file, int line) {
809 file = "unknown file";
811 ::std::cerr << file << ":";
814 ::std::cerr << file << "(" << line << "):";
816 ::std::cerr << file << ":" << line << ":";
820 ::std::ostream& GetStream() { return ::std::cerr; }
822 #define GTEST_CHECK_(condition) \
823 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
827 ::testing::internal::GTestCheckProvider(\
828 #condition, __FILE__, __LINE__).GetStream()
830 // Macro for referencing flags.
831 #define GTEST_FLAG(name) FLAGS_gtest_##name
833 // Macros for declaring flags.
834 #define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name)
835 #define GTEST_DECLARE_int32_(name) \
836 extern ::testing::internal::Int32 GTEST_FLAG(name)
837 #define GTEST_DECLARE_string_(name) \
838 extern ::testing::internal::String GTEST_FLAG(name)
840 // Macros for defining flags.
841 #define GTEST_DEFINE_bool_(name, default_val, doc) \
842 bool GTEST_FLAG(name) = (default_val)
843 #define GTEST_DEFINE_int32_(name, default_val, doc) \
844 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
845 #define GTEST_DEFINE_string_(name, default_val, doc) \
846 ::testing::internal::String GTEST_FLAG(name) = (default_val)
848 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
849 // to *value and returns true; otherwise leaves *value unchanged and returns
851 // TODO(chandlerc): Find a better way to refactor flag and environment parsing
852 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
854 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
856 // Parses a bool/Int32/string from the environment variable
857 // corresponding to the given Google Test flag.
858 bool BoolFromGTestEnv(const char* flag, bool default_val);
859 Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
860 const char* StringFromGTestEnv(const char* flag, const char* default_val);
862 } // namespace internal
863 } // namespace testing
865 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_