43 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
44 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
265 # include <sys/types.h>
266 # include <sys/stat.h>
267 #endif // !_WIN32_WCE
269 #if defined __APPLE__
270 # include <AvailabilityMacros.h>
271 # include <TargetConditionals.h>
284 #if !defined(GTEST_DEV_EMAIL_)
285 # define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
286 # define GTEST_FLAG_PREFIX_ "gtest_"
287 # define GTEST_FLAG_PREFIX_DASH_ "gtest-"
288 # define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
289 # define GTEST_NAME_ "Google Test"
290 # define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
291 #endif // !defined(GTEST_DEV_EMAIL_)
293 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
294 # define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
295 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
300 # define GTEST_GCC_VER_ \
301 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
310 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
311 __pragma(warning(push)) \
312 __pragma(warning(disable: warnings))
313 # define GTEST_DISABLE_MSC_WARNINGS_POP_() \
314 __pragma(warning(pop))
317 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
318 # define GTEST_DISABLE_MSC_WARNINGS_POP_()
321 #ifndef GTEST_LANG_CXX11
326 # if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L
328 # define GTEST_LANG_CXX11 1
330 # define GTEST_LANG_CXX11 0
343 #if GTEST_LANG_CXX11 && \
344 (!defined(__GLIBCXX__) || ( \
345 __GLIBCXX__ >= 20110325ul && \
347 __GLIBCXX__ != 20110416ul && \
348 __GLIBCXX__ != 20120313ul && \
349 __GLIBCXX__ != 20110428ul && \
350 __GLIBCXX__ != 20120702ul))
351 # define GTEST_STDLIB_CXX11 1
355 #if GTEST_STDLIB_CXX11
356 # define GTEST_HAS_STD_BEGIN_AND_END_ 1
357 # define GTEST_HAS_STD_FORWARD_LIST_ 1
358 # define GTEST_HAS_STD_FUNCTION_ 1
359 # define GTEST_HAS_STD_INITIALIZER_LIST_ 1
360 # define GTEST_HAS_STD_MOVE_ 1
361 # define GTEST_HAS_STD_SHARED_PTR_ 1
362 # define GTEST_HAS_STD_TYPE_TRAITS_ 1
363 # define GTEST_HAS_STD_UNIQUE_PTR_ 1
369 # define GTEST_HAS_STD_TUPLE_ 1
370 # if defined(__clang__)
372 # if defined(__has_include) && !__has_include(<tuple>)
373 # undef GTEST_HAS_STD_TUPLE_
375 # elif defined(_MSC_VER)
377 # if defined(_CPPLIB_VER) && _CPPLIB_VER < 520
378 # undef GTEST_HAS_STD_TUPLE_
380 # elif defined(__GLIBCXX__)
384 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)
385 # undef GTEST_HAS_STD_TUPLE_
394 # if !GTEST_OS_WINDOWS_MOBILE
399 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
402 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
407 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
414 # include <strings.h>
415 #endif // GTEST_OS_WINDOWS
417 #if GTEST_OS_LINUX_ANDROID
419 # include <android/api-level.h>
423 #ifndef GTEST_HAS_POSIX_RE
424 # if GTEST_OS_LINUX_ANDROID
426 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
428 # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
435 #elif GTEST_HAS_POSIX_RE
443 # define GTEST_USES_POSIX_RE 1
445 #elif GTEST_OS_WINDOWS
449 # define GTEST_USES_SIMPLE_RE 1
455 # define GTEST_USES_SIMPLE_RE 1
457 #endif // GTEST_USES_PCRE
459 #ifndef GTEST_HAS_EXCEPTIONS
462 # if defined(_MSC_VER) || defined(__BORLANDC__)
466 # ifndef _HAS_EXCEPTIONS
467 # define _HAS_EXCEPTIONS 1
468 # endif // _HAS_EXCEPTIONS
469 # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
470 # elif defined(__clang__)
478 # define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
479 # elif defined(__GNUC__) && __EXCEPTIONS
481 # define GTEST_HAS_EXCEPTIONS 1
482 # elif defined(__SUNPRO_CC)
486 # define GTEST_HAS_EXCEPTIONS 1
487 # elif defined(__IBMCPP__) && __EXCEPTIONS
489 # define GTEST_HAS_EXCEPTIONS 1
490 # elif defined(__HP_aCC)
493 # define GTEST_HAS_EXCEPTIONS 1
497 # define GTEST_HAS_EXCEPTIONS 0
498 # endif // defined(_MSC_VER) || defined(__BORLANDC__)
499 #endif // GTEST_HAS_EXCEPTIONS
501 #if !defined(GTEST_HAS_STD_STRING)
504 # define GTEST_HAS_STD_STRING 1
505 #elif !GTEST_HAS_STD_STRING
507 # error "Google Test cannot be used where ::std::string isn't available."
508 #endif // !defined(GTEST_HAS_STD_STRING)
510 #ifndef GTEST_HAS_GLOBAL_STRING
514 # define GTEST_HAS_GLOBAL_STRING 0
516 #endif // GTEST_HAS_GLOBAL_STRING
518 #ifndef GTEST_HAS_STD_WSTRING
527 # define GTEST_HAS_STD_WSTRING \
528 (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
530 #endif // GTEST_HAS_STD_WSTRING
532 #ifndef GTEST_HAS_GLOBAL_WSTRING
535 # define GTEST_HAS_GLOBAL_WSTRING \
536 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
537 #endif // GTEST_HAS_GLOBAL_WSTRING
540 #ifndef GTEST_HAS_RTTI
546 # ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
547 # define GTEST_HAS_RTTI 1
549 # define GTEST_HAS_RTTI 0
553 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
560 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
561 !defined(__EXCEPTIONS)
562 # define GTEST_HAS_RTTI 0
564 # define GTEST_HAS_RTTI 1
565 # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
567 # define GTEST_HAS_RTTI 0
568 # endif // __GXX_RTTI
573 # elif defined(__clang__)
575 # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
579 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
582 # define GTEST_HAS_RTTI 1
584 # define GTEST_HAS_RTTI 0
590 # define GTEST_HAS_RTTI 1
594 #endif // GTEST_HAS_RTTI
603 #ifndef GTEST_HAS_PTHREAD
609 # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \
610 || GTEST_OS_QNX || GTEST_OS_FREEBSD || GTEST_OS_NACL)
611 #endif // GTEST_HAS_PTHREAD
613 #if GTEST_HAS_PTHREAD
616 # include <pthread.h>
624 #if !defined(GTEST_HAS_HASH_MAP_)
626 # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
627 # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
629 #endif // !defined(GTEST_HAS_HASH_MAP_)
634 #ifndef GTEST_HAS_TR1_TUPLE
635 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
637 # define GTEST_HAS_TR1_TUPLE 0
640 # define GTEST_HAS_TR1_TUPLE 1
642 #endif // GTEST_HAS_TR1_TUPLE
646 #ifndef GTEST_USE_OWN_TR1_TUPLE
659 # if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
660 && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600
661 # define GTEST_ENV_HAS_TR1_TUPLE_ 1
667 # if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
668 # define GTEST_ENV_HAS_STD_TUPLE_ 1
671 # if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
672 # define GTEST_USE_OWN_TR1_TUPLE 0
674 # define GTEST_USE_OWN_TR1_TUPLE 1
677 #endif // GTEST_USE_OWN_TR1_TUPLE
682 #if GTEST_HAS_STD_TUPLE_
684 # define GTEST_TUPLE_NAMESPACE_ ::std
685 #endif // GTEST_HAS_STD_TUPLE_
689 #if GTEST_HAS_TR1_TUPLE
690 # ifndef GTEST_TUPLE_NAMESPACE_
691 # define GTEST_TUPLE_NAMESPACE_ ::std::tr1
692 # endif // GTEST_TUPLE_NAMESPACE_
694 # if GTEST_USE_OWN_TR1_TUPLE
696 # elif GTEST_ENV_HAS_STD_TUPLE_
707 using ::std::tuple_element;
708 using ::std::tuple_size;
712 # elif GTEST_OS_SYMBIAN
719 # ifdef BOOST_HAS_TR1_TUPLE
720 # undef BOOST_HAS_TR1_TUPLE
721 # endif // BOOST_HAS_TR1_TUPLE
725 # define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
728 # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
732 # if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
738 # define _TR1_FUNCTIONAL 1
739 # include <tr1/tuple>
740 # undef _TR1_FUNCTIONAL // Allows the user to #include
743 # include <tr1/tuple>
744 # endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
750 # endif // GTEST_USE_OWN_TR1_TUPLE
752 #endif // GTEST_HAS_TR1_TUPLE
758 #ifndef GTEST_HAS_CLONE
761 # if GTEST_OS_LINUX && !defined(__ia64__)
762 # if GTEST_OS_LINUX_ANDROID
765 # if defined(__LP64__) || \
766 (defined(__arm__) && __ANDROID_API__ >= 9) || \
767 (defined(__mips__) && __ANDROID_API__ >= 12) || \
768 (defined(__i386__) && __ANDROID_API__ >= 17)
769 # define GTEST_HAS_CLONE 1
771 # define GTEST_HAS_CLONE 0
774 # define GTEST_HAS_CLONE 1
777 # define GTEST_HAS_CLONE 0
778 # endif // GTEST_OS_LINUX && !defined(__ia64__)
780 #endif // GTEST_HAS_CLONE
784 #ifndef GTEST_HAS_STREAM_REDIRECTION
787 # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || \
788 GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
789 # define GTEST_HAS_STREAM_REDIRECTION 0
791 # define GTEST_HAS_STREAM_REDIRECTION 1
792 # endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
793 #endif // GTEST_HAS_STREAM_REDIRECTION
799 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
800 (GTEST_OS_MAC && !GTEST_OS_IOS) || \
801 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
802 GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
803 GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD)
804 # define GTEST_HAS_DEATH_TEST 1
810 #define GTEST_HAS_PARAM_TEST 1
816 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
817 defined(__IBMCPP__) || defined(__HP_aCC)
818 # define GTEST_HAS_TYPED_TEST 1
819 # define GTEST_HAS_TYPED_TEST_P 1
826 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
827 # define GTEST_HAS_COMBINE 1
831 #define GTEST_WIDE_STRING_USES_UTF16_ \
832 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
836 # define GTEST_CAN_STREAM_RESULTS_ 1
849 #ifdef __INTEL_COMPILER
850 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
852 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
866 #if defined(__GNUC__) && !defined(COMPILER_ICC)
867 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
868 #elif defined(__clang__)
869 # if __has_attribute(unused)
870 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
873 #ifndef GTEST_ATTRIBUTE_UNUSED_
874 # define GTEST_ATTRIBUTE_UNUSED_
879 #define GTEST_DISALLOW_ASSIGN_(type)\
880 void operator=(type const &)
884 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
886 GTEST_DISALLOW_ASSIGN_(type)
893 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
894 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
896 # define GTEST_MUST_USE_RESULT_
897 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
907 # define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
908 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
909 # define GTEST_INTENTIONAL_CONST_COND_POP_() \
910 GTEST_DISABLE_MSC_WARNINGS_POP_()
915 #ifndef GTEST_HAS_SEH
918 # if defined(_MSC_VER) || defined(__BORLANDC__)
920 # define GTEST_HAS_SEH 1
923 # define GTEST_HAS_SEH 0
926 #define GTEST_IS_THREADSAFE \
927 (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ \
928 || (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) \
929 || GTEST_HAS_PTHREAD)
931 #endif // GTEST_HAS_SEH
934 # if GTEST_LINKED_AS_SHARED_LIBRARY
935 # define GTEST_API_ __declspec(dllimport)
936 # elif GTEST_CREATE_SHARED_LIBRARY
937 # define GTEST_API_ __declspec(dllexport)
939 #elif __GNUC__ >= 4 || defined(__clang__)
940 # define GTEST_API_ __attribute__((visibility ("default")))
949 # define GTEST_NO_INLINE_ __attribute__((noinline))
951 # define GTEST_NO_INLINE_
955 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
956 # define GTEST_HAS_CXXABI_H_ 1
958 # define GTEST_HAS_CXXABI_H_ 0
963 #if defined(__clang__)
964 # if __has_feature(memory_sanitizer)
965 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
966 __attribute__((no_sanitize_memory))
968 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
969 # endif // __has_feature(memory_sanitizer)
971 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
975 #if defined(__clang__)
976 # if __has_feature(address_sanitizer)
977 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
978 __attribute__((no_sanitize_address))
980 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
981 # endif // __has_feature(address_sanitizer)
983 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
987 #if defined(__clang__)
988 # if __has_feature(thread_sanitizer)
989 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
990 __attribute__((no_sanitize_thread))
992 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
993 # endif // __has_feature(thread_sanitizer)
995 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
1002 #if defined(GTEST_TUPLE_NAMESPACE_)
1006 using GTEST_TUPLE_NAMESPACE_::get;
1008 using GTEST_TUPLE_NAMESPACE_::tuple;
1009 using GTEST_TUPLE_NAMESPACE_::tuple_size;
1010 using GTEST_TUPLE_NAMESPACE_::tuple_element;
1011 #endif // defined(GTEST_TUPLE_NAMESPACE_)
1013 namespace internal {
1035 #if GTEST_LANG_CXX11
1036 # define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
1037 #else // !GTEST_LANG_CXX11
1042 # define GTEST_COMPILE_ASSERT_(expr, msg) \
1043 typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
1044 msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
1045 #endif // !GTEST_LANG_CXX11
1093 template <
typename T1,
typename T2>
1096 template <
typename T>
1102 #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
1104 #if GTEST_HAS_GLOBAL_STRING
1108 #endif // GTEST_HAS_GLOBAL_STRING
1110 #if GTEST_HAS_GLOBAL_WSTRING
1112 #elif GTEST_HAS_STD_WSTRING
1114 #endif // GTEST_HAS_GLOBAL_WSTRING
1124 template <
typename T>
1137 T*
const ptr =
ptr_;
1175 #if GTEST_HAS_GLOBAL_STRING
1179 #endif // GTEST_HAS_GLOBAL_STRING
1195 return FullMatch(str.c_str(), re);
1198 return PartialMatch(str.c_str(), re);
1201 #if GTEST_HAS_GLOBAL_STRING
1204 return FullMatch(str.c_str(), re);
1207 return PartialMatch(str.c_str(), re);
1210 #endif // GTEST_HAS_GLOBAL_STRING
1212 static bool FullMatch(
const char* str,
const RE& re);
1213 static bool PartialMatch(
const char* str,
const RE& re);
1216 void Init(
const char* regex);
1224 #if GTEST_USES_POSIX_RE
1229 #else // GTEST_USES_SIMPLE_RE
1231 const char* full_pattern_;
1279 #if !defined(GTEST_LOG_)
1281 # define GTEST_LOG_(severity) \
1282 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1283 __FILE__, __LINE__).GetStream()
1288 #endif // !defined(GTEST_LOG_)
1290 #if !defined(GTEST_CHECK_)
1305 # define GTEST_CHECK_(condition) \
1306 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1307 if (::testing::internal::IsTrue(condition)) \
1310 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1311 #endif // !defined(GTEST_CHECK_)
1318 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1319 if (const int gtest_error = (posix_call)) \
1320 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1323 #if GTEST_HAS_STD_MOVE_
1325 #else // GTEST_HAS_STD_MOVE_
1326 template <
typename T>
1330 #endif // GTEST_HAS_STD_MOVE_
1352 template<
typename To>
1376 template<
typename To,
typename From>
1386 ::testing::internal::ImplicitCast_<From*>(to);
1391 GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
1393 return static_cast<To>(
f);
1401 template <
class Derived,
class Base>
1407 #if GTEST_HAS_DOWNCAST_
1408 return ::down_cast<Derived*>(base);
1409 #elif GTEST_HAS_RTTI
1410 return dynamic_cast<Derived*
>(base);
1412 return static_cast<Derived*
>(base);
1416 #if GTEST_HAS_STREAM_REDIRECTION
1429 #endif // GTEST_HAS_STREAM_REDIRECTION
1443 #if GTEST_HAS_DEATH_TEST
1445 const ::std::vector<testing::internal::string>& GetInjectableArgvs();
1446 void SetInjectableArgvs(const ::std::vector<testing::internal::string>*
1450 #endif // GTEST_HAS_DEATH_TEST
1453 #if GTEST_IS_THREADSAFE
1454 # if GTEST_HAS_PTHREAD
1458 inline void SleepMilliseconds(
int n) {
1459 const timespec
time = {
1463 nanosleep(&time, NULL);
1465 # endif // GTEST_HAS_PTHREAD
1467 # if GTEST_HAS_NOTIFICATION_
1471 # elif GTEST_HAS_PTHREAD
1478 class Notification {
1480 Notification() : notified_(
false) {
1484 pthread_mutex_destroy(&mutex_);
1490 pthread_mutex_lock(&mutex_);
1492 pthread_mutex_unlock(&mutex_);
1497 void WaitForNotification() {
1499 pthread_mutex_lock(&mutex_);
1500 const bool notified = notified_;
1501 pthread_mutex_unlock(&mutex_);
1504 SleepMilliseconds(10);
1509 pthread_mutex_t mutex_;
1515 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1528 typedef void* Handle;
1530 explicit AutoHandle(Handle handle);
1536 void Reset(Handle handle);
1540 bool IsCloseable()
const;
1557 void WaitForNotification();
1564 # endif // GTEST_HAS_NOTIFICATION_
1569 # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1576 class ThreadWithParamBase {
1578 virtual ~ThreadWithParamBase() {}
1579 virtual void Run() = 0;
1588 extern "C" inline void* ThreadFuncWithCLinkage(
void* thread) {
1589 static_cast<ThreadWithParamBase*
>(thread)->
Run();
1605 template <
typename T>
1606 class ThreadWithParam :
public ThreadWithParamBase {
1608 typedef void UserThreadFunc(
T);
1610 ThreadWithParam(UserThreadFunc* func,
T param, Notification* thread_can_start)
1613 thread_can_start_(thread_can_start),
1615 ThreadWithParamBase*
const base =
this;
1619 pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
1621 ~ThreadWithParam() { Join(); }
1630 virtual void Run() {
1631 if (thread_can_start_ != NULL)
1632 thread_can_start_->WaitForNotification();
1637 UserThreadFunc*
const func_;
1641 Notification*
const thread_can_start_;
1647 # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1650 # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1654 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1672 enum MutexType { kStatic = 0, kDynamic = 1 };
1676 enum StaticConstructorSelector { kStaticMutex = 0 };
1681 explicit Mutex(StaticConstructorSelector ) {}
1696 void ThreadSafeLazyInit();
1700 unsigned int owner_thread_id_;
1705 long critical_section_init_phase_;
1706 GTEST_CRITICAL_SECTION* critical_section_;
1711 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1712 extern ::testing::internal::Mutex mutex
1714 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1715 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1722 class GTestMutexLock {
1725 : mutex_(mutex) { mutex_->Lock(); }
1730 Mutex*
const mutex_;
1739 class ThreadLocalValueHolderBase {
1741 virtual ~ThreadLocalValueHolderBase() {}
1746 class ThreadLocalBase {
1752 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread()
const = 0;
1755 ThreadLocalBase() {}
1756 virtual ~ThreadLocalBase() {}
1769 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1770 const ThreadLocalBase* thread_local_instance);
1773 static void OnThreadLocalDestroyed(
1774 const ThreadLocalBase* thread_local_instance);
1784 virtual ~Runnable() {}
1785 virtual void Run() = 0;
1788 ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
1789 virtual ~ThreadWithParamBase();
1796 template <
typename T>
1797 class ThreadWithParam :
public ThreadWithParamBase {
1799 typedef void UserThreadFunc(
T);
1801 ThreadWithParam(UserThreadFunc* func,
T param, Notification* thread_can_start)
1802 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
1804 virtual ~ThreadWithParam() {}
1807 class RunnableImpl :
public Runnable {
1809 RunnableImpl(UserThreadFunc* func,
T param)
1813 virtual ~RunnableImpl() {}
1814 virtual void Run() {
1819 UserThreadFunc*
const func_;
1855 template <
typename T>
1856 class ThreadLocal :
public ThreadLocalBase {
1858 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1860 : default_factory_(new InstanceValueHolderFactory(value)) {}
1862 ~
ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(
this); }
1864 T*
pointer() {
return GetOrCreateValue(); }
1865 const T*
pointer()
const {
return GetOrCreateValue(); }
1866 const T&
get()
const {
return *
pointer(); }
1872 class ValueHolder :
public ThreadLocalValueHolderBase {
1874 ValueHolder() :
value_() {}
1875 explicit ValueHolder(
const T&
value) :
value_(value) {}
1885 T* GetOrCreateValue()
const {
1886 return static_cast<ValueHolder*
>(
1887 ThreadLocalRegistry::GetValueOnCurrentThread(
this))->
pointer();
1890 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread()
const {
1891 return default_factory_->MakeNewHolder();
1894 class ValueHolderFactory {
1896 ValueHolderFactory() {}
1897 virtual ~ValueHolderFactory() {}
1898 virtual ValueHolder* MakeNewHolder()
const = 0;
1904 class DefaultValueHolderFactory :
public ValueHolderFactory {
1906 DefaultValueHolderFactory() {}
1907 virtual ValueHolder* MakeNewHolder()
const {
return new ValueHolder(); }
1913 class InstanceValueHolderFactory :
public ValueHolderFactory {
1915 explicit InstanceValueHolderFactory(
const T&
value) :
value_(value) {}
1916 virtual ValueHolder* MakeNewHolder()
const {
1917 return new ValueHolder(
value_);
1926 scoped_ptr<ValueHolderFactory> default_factory_;
1931 # elif GTEST_HAS_PTHREAD
1939 owner_ = pthread_self();
1955 void AssertHeld()
const {
1956 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1957 <<
"The current thread is not holding the mutex @" <<
this;
1966 pthread_mutex_t mutex_;
1978 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1979 extern ::testing::internal::MutexBase mutex
1982 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1983 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false, pthread_t() }
1987 class Mutex :
public MutexBase {
2006 class GTestMutexLock {
2009 : mutex_(mutex) { mutex_->Lock(); }
2014 MutexBase*
const mutex_;
2027 class ThreadLocalValueHolderBase {
2029 virtual ~ThreadLocalValueHolderBase() {}
2034 extern "C" inline void DeleteThreadLocalValue(
void* value_holder) {
2035 delete static_cast<ThreadLocalValueHolderBase*
>(value_holder);
2039 template <
typename T>
2043 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
2045 : key_(CreateKey()),
2046 default_factory_(new InstanceValueHolderFactory(value)) {}
2050 DeleteThreadLocalValue(pthread_getspecific(key_));
2057 T*
pointer() {
return GetOrCreateValue(); }
2058 const T*
pointer()
const {
return GetOrCreateValue(); }
2059 const T&
get()
const {
return *
pointer(); }
2064 class ValueHolder :
public ThreadLocalValueHolderBase {
2066 ValueHolder() :
value_() {}
2067 explicit ValueHolder(
const T&
value) :
value_(value) {}
2076 static pthread_key_t CreateKey() {
2081 pthread_key_create(&key, &DeleteThreadLocalValue));
2085 T* GetOrCreateValue()
const {
2086 ThreadLocalValueHolderBase*
const holder =
2087 static_cast<ThreadLocalValueHolderBase*
>(pthread_getspecific(key_));
2088 if (holder != NULL) {
2089 return CheckedDowncastToActualType<ValueHolder>(holder)->
pointer();
2092 ValueHolder*
const new_holder = default_factory_->MakeNewHolder();
2093 ThreadLocalValueHolderBase*
const holder_base = new_holder;
2095 return new_holder->pointer();
2098 class ValueHolderFactory {
2100 ValueHolderFactory() {}
2101 virtual ~ValueHolderFactory() {}
2102 virtual ValueHolder* MakeNewHolder()
const = 0;
2108 class DefaultValueHolderFactory :
public ValueHolderFactory {
2110 DefaultValueHolderFactory() {}
2111 virtual ValueHolder* MakeNewHolder()
const {
return new ValueHolder(); }
2117 class InstanceValueHolderFactory :
public ValueHolderFactory {
2119 explicit InstanceValueHolderFactory(
const T&
value) :
value_(value) {}
2120 virtual ValueHolder* MakeNewHolder()
const {
2121 return new ValueHolder(
value_);
2131 const pthread_key_t key_;
2132 scoped_ptr<ValueHolderFactory> default_factory_;
2137 # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
2139 #else // GTEST_IS_THREADSAFE
2154 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
2155 extern ::testing::internal::Mutex mutex
2157 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
2171 template <
typename T>
2184 #endif // GTEST_IS_THREADSAFE
2196 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
2199 # define GTEST_ELLIPSIS_NEEDS_POD_ 1
2201 # define GTEST_CAN_COMPARE_NULL 1
2208 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
2209 # define GTEST_NEEDS_IS_POINTER_ 1
2212 template <
bool bool_value>
2222 template <
typename T>
2225 template <
typename T>
2228 template <
typename Iterator>
2233 template <
typename T>
2238 template <
typename T>
2243 #if GTEST_OS_WINDOWS
2244 # define GTEST_PATH_SEP_ "\\"
2245 # define GTEST_HAS_ALT_PATH_SEP_ 1
2249 # define GTEST_PATH_SEP_ "/"
2250 # define GTEST_HAS_ALT_PATH_SEP_ 0
2252 #endif // GTEST_OS_WINDOWS
2262 return isalpha(static_cast<unsigned char>(ch)) != 0;
2265 return isalnum(static_cast<unsigned char>(ch)) != 0;
2268 return isdigit(static_cast<unsigned char>(ch)) != 0;
2271 return islower(static_cast<unsigned char>(ch)) != 0;
2274 return isspace(static_cast<unsigned char>(ch)) != 0;
2277 return isupper(static_cast<unsigned char>(ch)) != 0;
2280 return isxdigit(static_cast<unsigned char>(ch)) != 0;
2283 const unsigned char low_byte =
static_cast<unsigned char>(ch);
2284 return ch == low_byte && isxdigit(low_byte) != 0;
2288 return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
2291 return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
2295 std::string::iterator
it = str.end();
2296 while (it != str.begin() &&
IsSpace(*--it))
2311 #if GTEST_OS_WINDOWS
2313 typedef struct _stat StatStruct;
2315 # ifdef __BORLANDC__
2316 inline int IsATTY(
int fd) {
return isatty(fd); }
2317 inline int StrCaseCmp(
const char* s1,
const char* s2) {
2318 return stricmp(s1, s2);
2320 inline char*
StrDup(
const char* src) {
return strdup(src); }
2321 # else // !__BORLANDC__
2322 # if GTEST_OS_WINDOWS_MOBILE
2323 inline int IsATTY(
int ) {
return 0; }
2325 inline int IsATTY(
int fd) {
return _isatty(fd); }
2326 # endif // GTEST_OS_WINDOWS_MOBILE
2327 inline int StrCaseCmp(
const char* s1,
const char* s2) {
2328 return _stricmp(s1, s2);
2330 inline char*
StrDup(
const char* src) {
return _strdup(src); }
2331 # endif // __BORLANDC__
2333 # if GTEST_OS_WINDOWS_MOBILE
2334 inline int FileNo(FILE*
file) {
return reinterpret_cast<int>(_fileno(file)); }
2338 inline int FileNo(FILE* file) {
return _fileno(file); }
2339 inline int Stat(
const char*
path, StatStruct* buf) {
return _stat(path, buf); }
2340 inline int RmDir(
const char* dir) {
return _rmdir(dir); }
2341 inline bool IsDir(
const StatStruct& st) {
2342 return (_S_IFDIR & st.st_mode) != 0;
2344 # endif // GTEST_OS_WINDOWS_MOBILE
2348 typedef struct stat StatStruct;
2350 inline int FileNo(FILE* file) {
return fileno(file); }
2351 inline int IsATTY(
int fd) {
return isatty(fd); }
2352 inline int Stat(
const char* path, StatStruct* buf) {
return stat(path, buf); }
2354 return strcasecmp(s1, s2);
2357 inline int RmDir(
const char* dir) {
return rmdir(dir); }
2358 inline bool IsDir(
const StatStruct& st) {
return S_ISDIR(st.st_mode); }
2360 #endif // GTEST_OS_WINDOWS
2367 return strncpy(dest, src, n);
2374 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
2375 inline int ChDir(
const char* dir) {
return chdir(dir); }
2378 return fopen(path, mode);
2380 #if !GTEST_OS_WINDOWS_MOBILE
2381 inline FILE *
FReopen(
const char* path,
const char*
mode, FILE* stream) {
2382 return freopen(path, mode, stream);
2384 inline FILE*
FDOpen(
int fd,
const char*
mode) {
return fdopen(fd, mode); }
2387 #if !GTEST_OS_WINDOWS_MOBILE
2389 return static_cast<int>(
read(fd, buf, count));
2392 return static_cast<int>(
write(fd, buf, count));
2394 inline int Close(
int fd) {
return close(fd); }
2395 inline const char*
StrError(
int errnum) {
return strerror(errnum); }
2398 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE | GTEST_OS_WINDOWS_RT
2400 static_cast<void>(
name);
2402 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2405 const char*
const env = getenv(name);
2406 return (env != NULL && env[0] !=
'\0') ? env : NULL;
2408 return getenv(name);
2414 #if GTEST_OS_WINDOWS_MOBILE
2421 #endif // GTEST_OS_WINDOWS_MOBILE
2430 #if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
2432 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
2433 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2434 #elif defined(_MSC_VER)
2437 # define GTEST_SNPRINTF_ _snprintf
2439 # define GTEST_SNPRINTF_ snprintf
2470 template <
size_t size>
2494 #if GTEST_OS_WINDOWS
2495 typedef __int64
Int;
2496 typedef unsigned __int64
UInt;
2500 #endif // GTEST_OS_WINDOWS
2513 #if !defined(GTEST_FLAG)
2514 # define GTEST_FLAG(name) FLAGS_gtest_##name
2515 #endif // !defined(GTEST_FLAG)
2517 #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2518 # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2519 #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2521 #if !defined(GTEST_DECLARE_bool_)
2522 # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2525 # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2526 # define GTEST_DECLARE_int32_(name) \
2527 GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
2528 #define GTEST_DECLARE_string_(name) \
2529 GTEST_API_ extern ::std::string GTEST_FLAG(name)
2532 #define GTEST_DEFINE_bool_(name, default_val, doc) \
2533 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2534 #define GTEST_DEFINE_int32_(name, default_val, doc) \
2535 GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
2536 #define GTEST_DEFINE_string_(name, default_val, doc) \
2537 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2539 #endif // !defined(GTEST_DECLARE_bool_)
2542 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2543 # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2544 # define GTEST_LOCK_EXCLUDED_(locks)
2545 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2564 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_