43 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
44 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
268 #include <type_traits>
271 # include <sys/types.h>
272 # include <sys/stat.h>
273 #endif // !_WIN32_WCE
275 #if defined __APPLE__
276 # include <AvailabilityMacros.h>
277 # include <TargetConditionals.h>
290 #if !defined(GTEST_DEV_EMAIL_)
291 # define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
292 # define GTEST_FLAG_PREFIX_ "gtest_"
293 # define GTEST_FLAG_PREFIX_DASH_ "gtest-"
294 # define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
295 # define GTEST_NAME_ "Google Test"
296 # define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
297 #endif // !defined(GTEST_DEV_EMAIL_)
299 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
300 # define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
301 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
306 # define GTEST_GCC_VER_ \
307 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
315 #if defined(_MSC_VER)
316 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
317 __pragma(warning(push)) \
318 __pragma(warning(disable: warnings))
319 # define GTEST_DISABLE_MSC_WARNINGS_POP_() \
320 __pragma(warning(pop))
323 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
324 # define GTEST_DISABLE_MSC_WARNINGS_POP_()
330 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
331 _Pragma("clang diagnostic push") \
332 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
333 _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
334 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
335 _Pragma("clang diagnostic pop")
337 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
338 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
339 # define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
340 GTEST_DISABLE_MSC_WARNINGS_POP_()
347 # if !GTEST_OS_WINDOWS_MOBILE
352 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
355 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
360 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
367 # include <strings.h>
368 #endif // GTEST_OS_WINDOWS
370 #if GTEST_OS_LINUX_ANDROID
372 # include <android/api-level.h>
377 #ifndef GTEST_HAS_POSIX_RE
378 # if GTEST_OS_LINUX_ANDROID
380 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
382 # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
389 #elif GTEST_HAS_POSIX_RE
397 # define GTEST_USES_POSIX_RE 1
399 #elif GTEST_OS_WINDOWS
403 # define GTEST_USES_SIMPLE_RE 1
409 # define GTEST_USES_SIMPLE_RE 1
411 #endif // GTEST_USES_PCRE
413 #ifndef GTEST_HAS_EXCEPTIONS
416 # if defined(_MSC_VER) && defined(_CPPUNWIND)
418 # define GTEST_HAS_EXCEPTIONS 1
419 # elif defined(__BORLANDC__)
423 # ifndef _HAS_EXCEPTIONS
424 # define _HAS_EXCEPTIONS 1
425 # endif // _HAS_EXCEPTIONS
426 # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
427 # elif defined(__clang__)
436 # define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
437 # elif defined(__GNUC__) && __EXCEPTIONS
439 # define GTEST_HAS_EXCEPTIONS 1
440 # elif defined(__SUNPRO_CC)
444 # define GTEST_HAS_EXCEPTIONS 1
445 # elif defined(__IBMCPP__) && __EXCEPTIONS
447 # define GTEST_HAS_EXCEPTIONS 1
448 # elif defined(__HP_aCC)
451 # define GTEST_HAS_EXCEPTIONS 1
455 # define GTEST_HAS_EXCEPTIONS 0
456 # endif // defined(_MSC_VER) || defined(__BORLANDC__)
457 #endif // GTEST_HAS_EXCEPTIONS
459 #ifndef GTEST_HAS_STD_WSTRING
465 #define GTEST_HAS_STD_WSTRING \
466 (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
467 GTEST_OS_HAIKU || GTEST_OS_ESP32 || GTEST_OS_ESP8266))
469 #endif // GTEST_HAS_STD_WSTRING
472 #ifndef GTEST_HAS_RTTI
478 #ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled.
479 # define GTEST_HAS_RTTI 1
481 # define GTEST_HAS_RTTI 0
486 # elif defined(__GNUC__)
493 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
494 !defined(__EXCEPTIONS)
495 # define GTEST_HAS_RTTI 0
497 # define GTEST_HAS_RTTI 1
498 # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
500 # define GTEST_HAS_RTTI 0
501 # endif // __GXX_RTTI
506 # elif defined(__clang__)
508 # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
512 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
515 # define GTEST_HAS_RTTI 1
517 # define GTEST_HAS_RTTI 0
523 # define GTEST_HAS_RTTI 1
527 #endif // GTEST_HAS_RTTI
536 #ifndef GTEST_HAS_PTHREAD
542 #define GTEST_HAS_PTHREAD \
543 (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \
544 GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
545 GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || \
547 #endif // GTEST_HAS_PTHREAD
549 #if GTEST_HAS_PTHREAD
552 # include <pthread.h>
562 #ifndef GTEST_HAS_CLONE
565 # if GTEST_OS_LINUX && !defined(__ia64__)
566 # if GTEST_OS_LINUX_ANDROID
569 # if defined(__LP64__) || \
570 (defined(__arm__) && __ANDROID_API__ >= 9) || \
571 (defined(__mips__) && __ANDROID_API__ >= 12) || \
572 (defined(__i386__) && __ANDROID_API__ >= 17)
573 # define GTEST_HAS_CLONE 1
575 # define GTEST_HAS_CLONE 0
578 # define GTEST_HAS_CLONE 1
581 # define GTEST_HAS_CLONE 0
582 # endif // GTEST_OS_LINUX && !defined(__ia64__)
584 #endif // GTEST_HAS_CLONE
588 #ifndef GTEST_HAS_STREAM_REDIRECTION
591 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
592 GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266
593 # define GTEST_HAS_STREAM_REDIRECTION 0
595 # define GTEST_HAS_STREAM_REDIRECTION 1
596 # endif // !GTEST_OS_WINDOWS_MOBILE
597 #endif // GTEST_HAS_STREAM_REDIRECTION
601 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
602 (GTEST_OS_MAC && !GTEST_OS_IOS) || \
603 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW || \
604 GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \
605 GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
606 GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU)
607 # define GTEST_HAS_DEATH_TEST 1
614 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
615 defined(__IBMCPP__) || defined(__HP_aCC)
616 # define GTEST_HAS_TYPED_TEST 1
617 # define GTEST_HAS_TYPED_TEST_P 1
621 #define GTEST_WIDE_STRING_USES_UTF16_ \
622 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2)
625 #if GTEST_OS_LINUX || GTEST_OS_GNU_KFREEBSD || GTEST_OS_DRAGONFLY || \
626 GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_OPENBSD
627 # define GTEST_CAN_STREAM_RESULTS_ 1
640 #ifdef __INTEL_COMPILER
641 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
643 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
657 #if defined(__GNUC__) && !defined(COMPILER_ICC)
658 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
659 #elif defined(__clang__)
660 # if __has_attribute(unused)
661 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
664 #ifndef GTEST_ATTRIBUTE_UNUSED_
665 # define GTEST_ATTRIBUTE_UNUSED_
669 #if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC)
670 # if defined(__MINGW_PRINTF_FORMAT)
674 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
675 __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \
678 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
679 __attribute__((__format__(__printf__, string_index, first_to_check)))
682 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
688 #define GTEST_DISALLOW_ASSIGN_(type) \
689 type& operator=(type const &) = delete
693 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type) \
694 type(type const&) = delete; \
695 type& operator=(type const&) = delete
699 #define GTEST_DISALLOW_MOVE_ASSIGN_(type) \
700 type& operator=(type &&) noexcept = delete
704 #define GTEST_DISALLOW_MOVE_AND_ASSIGN_(type) \
705 type(type&&) noexcept = delete; \
706 type& operator=(type&&) noexcept = delete
713 #if defined(__GNUC__) && !defined(COMPILER_ICC)
714 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
716 # define GTEST_MUST_USE_RESULT_
717 #endif // __GNUC__ && !COMPILER_ICC
727 # define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
728 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
729 # define GTEST_INTENTIONAL_CONST_COND_POP_() \
730 GTEST_DISABLE_MSC_WARNINGS_POP_()
735 #ifndef GTEST_HAS_SEH
738 # if defined(_MSC_VER) || defined(__BORLANDC__)
740 # define GTEST_HAS_SEH 1
743 # define GTEST_HAS_SEH 0
746 #endif // GTEST_HAS_SEH
748 #ifndef GTEST_IS_THREADSAFE
750 #define GTEST_IS_THREADSAFE \
751 (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \
752 (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \
755 #endif // GTEST_IS_THREADSAFE
763 # if GTEST_LINKED_AS_SHARED_LIBRARY
764 # define GTEST_API_ __declspec(dllimport)
765 # elif GTEST_CREATE_SHARED_LIBRARY
766 # define GTEST_API_ __declspec(dllexport)
768 #elif __GNUC__ >= 4 || defined(__clang__)
769 # define GTEST_API_ __attribute__((visibility ("default")))
778 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
779 # define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
780 #endif // GTEST_DEFAULT_DEATH_TEST_STYLE
784 # define GTEST_NO_INLINE_ __attribute__((noinline))
786 # define GTEST_NO_INLINE_
790 #if !defined(GTEST_HAS_CXXABI_H_)
791 # if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
792 # define GTEST_HAS_CXXABI_H_ 1
794 # define GTEST_HAS_CXXABI_H_ 0
800 #if defined(__clang__)
801 # if __has_feature(memory_sanitizer)
802 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
803 __attribute__((no_sanitize_memory))
805 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
806 # endif // __has_feature(memory_sanitizer)
808 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
812 #if defined(__clang__)
813 # if __has_feature(address_sanitizer)
814 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
815 __attribute__((no_sanitize_address))
817 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
818 # endif // __has_feature(address_sanitizer)
820 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
824 #if defined(__clang__)
825 # if __has_feature(hwaddress_sanitizer)
826 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \
827 __attribute__((no_sanitize("hwaddress")))
829 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
830 # endif // __has_feature(hwaddress_sanitizer)
832 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
836 #if defined(__clang__)
837 # if __has_feature(thread_sanitizer)
838 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
839 __attribute__((no_sanitize_thread))
841 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
842 # endif // __has_feature(thread_sanitizer)
844 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
854 using std::make_tuple;
856 using std::tuple_element;
857 using std::tuple_size;
875 #define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
885 #elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE
896 RE(const ::std::string& regex) { Init(regex.c_str()); }
898 RE(
const char* regex) { Init(regex); }
902 const char*
pattern()
const {
return pattern_; }
909 return FullMatch(str.c_str(), re);
912 return PartialMatch(str.c_str(), re);
915 static bool FullMatch(
const char* str,
const RE& re);
916 static bool PartialMatch(
const char* str,
const RE& re);
919 void Init(
const char* regex);
923 # if GTEST_USES_POSIX_RE
928 # else // GTEST_USES_SIMPLE_RE
930 const char* full_pattern_;
935 #endif // GTEST_USES_PCRE
978 #if !defined(GTEST_LOG_)
980 # define GTEST_LOG_(severity) \
981 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
982 __FILE__, __LINE__).GetStream()
987 #endif // !defined(GTEST_LOG_)
989 #if !defined(GTEST_CHECK_)
1004 # define GTEST_CHECK_(condition) \
1005 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1006 if (::testing::internal::IsTrue(condition)) \
1009 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1010 #endif // !defined(GTEST_CHECK_)
1017 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1018 if (const int gtest_error = (posix_call)) \
1019 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1033 template <
typename T>
1035 template <
typename T>
1039 #define GTEST_REFERENCE_TO_CONST_(T) \
1040 typename ::testing::internal::ConstRef<T>::type
1062 template<
typename To>
1086 template<
typename To,
typename From>
1095 const To to =
nullptr;
1096 ::testing::internal::ImplicitCast_<From*>(to);
1101 GTEST_CHECK_(f ==
nullptr || dynamic_cast<To>(f) !=
nullptr);
1103 return static_cast<To>(f);
1111 template <
class Derived,
class Base>
1117 #if GTEST_HAS_DOWNCAST_
1118 return ::down_cast<Derived*>(base);
1119 #elif GTEST_HAS_RTTI
1120 return dynamic_cast<Derived*
>(base);
1122 return static_cast<Derived*
>(base);
1126 #if GTEST_HAS_STREAM_REDIRECTION
1139 #endif // GTEST_HAS_STREAM_REDIRECTION
1149 #if GTEST_HAS_DEATH_TEST
1151 std::vector<std::string> GetInjectableArgvs();
1153 void SetInjectableArgvs(
const std::vector<std::string>* new_argvs);
1154 void SetInjectableArgvs(
const std::vector<std::string>& new_argvs);
1155 void ClearInjectableArgvs();
1157 #endif // GTEST_HAS_DEATH_TEST
1160 #if GTEST_IS_THREADSAFE
1161 # if GTEST_HAS_PTHREAD
1165 inline void SleepMilliseconds(
int n) {
1166 const timespec time = {
1170 nanosleep(&time,
nullptr);
1172 # endif // GTEST_HAS_PTHREAD
1174 # if GTEST_HAS_NOTIFICATION_
1178 # elif GTEST_HAS_PTHREAD
1185 class Notification {
1187 Notification() : notified_(
false) {
1191 pthread_mutex_destroy(&mutex_);
1197 pthread_mutex_lock(&mutex_);
1199 pthread_mutex_unlock(&mutex_);
1204 void WaitForNotification() {
1206 pthread_mutex_lock(&mutex_);
1207 const bool notified = notified_;
1208 pthread_mutex_unlock(&mutex_);
1211 SleepMilliseconds(10);
1216 pthread_mutex_t mutex_;
1222 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1235 typedef void* Handle;
1237 explicit AutoHandle(Handle handle);
1243 void Reset(Handle handle);
1248 bool IsCloseable()
const;
1265 void WaitForNotification();
1272 # endif // GTEST_HAS_NOTIFICATION_
1277 # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1284 class ThreadWithParamBase {
1286 virtual ~ThreadWithParamBase() {}
1287 virtual void Run() = 0;
1296 extern "C" inline void* ThreadFuncWithCLinkage(
void* thread) {
1297 static_cast<ThreadWithParamBase*
>(thread)->Run();
1313 template <
typename T>
1314 class ThreadWithParam :
public ThreadWithParamBase {
1316 typedef void UserThreadFunc(
T);
1318 ThreadWithParam(UserThreadFunc*
func,
T param, Notification* thread_can_start)
1321 thread_can_start_(thread_can_start),
1323 ThreadWithParamBase*
const base =
this;
1327 pthread_create(&thread_,
nullptr, &ThreadFuncWithCLinkage, base));
1329 ~ThreadWithParam()
override { Join(); }
1338 void Run()
override {
1339 if (thread_can_start_ !=
nullptr) thread_can_start_->WaitForNotification();
1344 UserThreadFunc*
const func_;
1348 Notification*
const thread_can_start_;
1355 # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1358 # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1362 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1380 enum MutexType { kStatic = 0, kDynamic = 1 };
1384 enum StaticConstructorSelector { kStaticMutex = 0 };
1389 explicit Mutex(StaticConstructorSelector ) {}
1404 void ThreadSafeLazyInit();
1408 unsigned int owner_thread_id_;
1413 long critical_section_init_phase_;
1414 GTEST_CRITICAL_SECTION* critical_section_;
1419 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1420 extern ::testing::internal::Mutex mutex
1422 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1423 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1430 class GTestMutexLock {
1433 : mutex_(mutex) { mutex_->Lock(); }
1438 Mutex*
const mutex_;
1447 class ThreadLocalValueHolderBase {
1449 virtual ~ThreadLocalValueHolderBase() {}
1454 class ThreadLocalBase {
1460 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread()
const = 0;
1463 ThreadLocalBase() {}
1464 virtual ~ThreadLocalBase() {}
1477 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1478 const ThreadLocalBase* thread_local_instance);
1481 static void OnThreadLocalDestroyed(
1482 const ThreadLocalBase* thread_local_instance);
1492 virtual ~Runnable() {}
1493 virtual void Run() = 0;
1496 ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
1497 virtual ~ThreadWithParamBase();
1504 template <
typename T>
1505 class ThreadWithParam :
public ThreadWithParamBase {
1507 typedef void UserThreadFunc(
T);
1509 ThreadWithParam(UserThreadFunc* func,
T param, Notification* thread_can_start)
1510 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
1512 virtual ~ThreadWithParam() {}
1515 class RunnableImpl :
public Runnable {
1517 RunnableImpl(UserThreadFunc* func,
T param)
1521 virtual ~RunnableImpl() {}
1522 virtual void Run() {
1527 UserThreadFunc*
const func_;
1563 template <
typename T>
1564 class ThreadLocal :
public ThreadLocalBase {
1566 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1568 : default_factory_(new InstanceValueHolderFactory(value)) {}
1570 ~
ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(
this); }
1572 T*
pointer() {
return GetOrCreateValue(); }
1573 const T*
pointer()
const {
return GetOrCreateValue(); }
1574 const T&
get()
const {
return *
pointer(); }
1580 class ValueHolder :
public ThreadLocalValueHolderBase {
1582 ValueHolder() :
value_() {}
1583 explicit ValueHolder(
const T&
value) :
value_(value) {}
1593 T* GetOrCreateValue()
const {
1594 return static_cast<ValueHolder*
>(
1595 ThreadLocalRegistry::GetValueOnCurrentThread(
this))->
pointer();
1598 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread()
const {
1599 return default_factory_->MakeNewHolder();
1602 class ValueHolderFactory {
1604 ValueHolderFactory() {}
1605 virtual ~ValueHolderFactory() {}
1606 virtual ValueHolder* MakeNewHolder()
const = 0;
1612 class DefaultValueHolderFactory :
public ValueHolderFactory {
1614 DefaultValueHolderFactory() {}
1615 ValueHolder* MakeNewHolder()
const override {
return new ValueHolder(); }
1621 class InstanceValueHolderFactory :
public ValueHolderFactory {
1623 explicit InstanceValueHolderFactory(
const T&
value) :
value_(value) {}
1624 ValueHolder* MakeNewHolder()
const override {
1625 return new ValueHolder(
value_);
1634 std::unique_ptr<ValueHolderFactory> default_factory_;
1639 # elif GTEST_HAS_PTHREAD
1647 owner_ = pthread_self();
1663 void AssertHeld()
const {
1664 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1665 <<
"The current thread is not holding the mutex @" <<
this;
1674 pthread_mutex_t mutex_;
1686 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1687 extern ::testing::internal::MutexBase mutex
1695 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1696 ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1700 class Mutex :
public MutexBase {
1719 class GTestMutexLock {
1722 : mutex_(mutex) { mutex_->Lock(); }
1727 MutexBase*
const mutex_;
1740 class ThreadLocalValueHolderBase {
1742 virtual ~ThreadLocalValueHolderBase() {}
1747 extern "C" inline void DeleteThreadLocalValue(
void* value_holder) {
1748 delete static_cast<ThreadLocalValueHolderBase*
>(value_holder);
1752 template <
typename T>
1756 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1757 explicit ThreadLocal(
const T&
value)
1758 : key_(CreateKey()),
1759 default_factory_(new InstanceValueHolderFactory(value)) {}
1763 DeleteThreadLocalValue(pthread_getspecific(key_));
1770 T* pointer() {
return GetOrCreateValue(); }
1771 const T* pointer()
const {
return GetOrCreateValue(); }
1772 const T&
get()
const {
return *pointer(); }
1777 class ValueHolder :
public ThreadLocalValueHolderBase {
1779 ValueHolder() :
value_() {}
1780 explicit ValueHolder(
const T&
value) :
value_(value) {}
1782 T* pointer() {
return &
value_; }
1789 static pthread_key_t CreateKey() {
1794 pthread_key_create(&key, &DeleteThreadLocalValue));
1798 T* GetOrCreateValue()
const {
1799 ThreadLocalValueHolderBase*
const holder =
1800 static_cast<ThreadLocalValueHolderBase*
>(pthread_getspecific(key_));
1801 if (holder !=
nullptr) {
1802 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1805 ValueHolder*
const new_holder = default_factory_->MakeNewHolder();
1806 ThreadLocalValueHolderBase*
const holder_base = new_holder;
1808 return new_holder->pointer();
1811 class ValueHolderFactory {
1813 ValueHolderFactory() {}
1814 virtual ~ValueHolderFactory() {}
1815 virtual ValueHolder* MakeNewHolder()
const = 0;
1821 class DefaultValueHolderFactory :
public ValueHolderFactory {
1823 DefaultValueHolderFactory() {}
1824 ValueHolder* MakeNewHolder()
const override {
return new ValueHolder(); }
1830 class InstanceValueHolderFactory :
public ValueHolderFactory {
1832 explicit InstanceValueHolderFactory(
const T&
value) :
value_(value) {}
1833 ValueHolder* MakeNewHolder()
const override {
1834 return new ValueHolder(
value_);
1844 const pthread_key_t key_;
1845 std::unique_ptr<ValueHolderFactory> default_factory_;
1850 # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1852 #else // GTEST_IS_THREADSAFE
1867 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1868 extern ::testing::internal::Mutex mutex
1870 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1884 template <
typename T>
1897 #endif // GTEST_IS_THREADSAFE
1903 #if GTEST_OS_WINDOWS
1904 # define GTEST_PATH_SEP_ "\\"
1905 # define GTEST_HAS_ALT_PATH_SEP_ 1
1907 # define GTEST_PATH_SEP_ "/"
1908 # define GTEST_HAS_ALT_PATH_SEP_ 0
1909 #endif // GTEST_OS_WINDOWS
1919 return isalpha(static_cast<unsigned char>(ch)) != 0;
1922 return isalnum(static_cast<unsigned char>(ch)) != 0;
1925 return isdigit(static_cast<unsigned char>(ch)) != 0;
1928 return islower(static_cast<unsigned char>(ch)) != 0;
1931 return isspace(static_cast<unsigned char>(ch)) != 0;
1934 return isupper(static_cast<unsigned char>(ch)) != 0;
1937 return isxdigit(static_cast<unsigned char>(ch)) != 0;
1940 const unsigned char low_byte =
static_cast<unsigned char>(
ch);
1941 return ch == low_byte && isxdigit(low_byte) != 0;
1945 return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1948 return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1952 std::string::iterator it = str.end();
1953 while (it != str.begin() &&
IsSpace(*--it))
1968 #if GTEST_OS_WINDOWS
1970 typedef struct _stat StatStruct;
1972 # ifdef __BORLANDC__
1973 inline int DoIsATTY(
int fd) {
return isatty(fd); }
1974 inline int StrCaseCmp(
const char* s1,
const char* s2) {
1975 return stricmp(s1, s2);
1977 inline char*
StrDup(
const char* src) {
return strdup(src); }
1978 # else // !__BORLANDC__
1979 # if GTEST_OS_WINDOWS_MOBILE
1980 inline int DoIsATTY(
int ) {
return 0; }
1982 inline int DoIsATTY(
int fd) {
return _isatty(fd); }
1983 # endif // GTEST_OS_WINDOWS_MOBILE
1984 inline int StrCaseCmp(
const char* s1,
const char* s2) {
1985 return _stricmp(s1, s2);
1987 inline char*
StrDup(
const char* src) {
return _strdup(src); }
1988 # endif // __BORLANDC__
1990 # if GTEST_OS_WINDOWS_MOBILE
1991 inline int FileNo(FILE* file) {
return reinterpret_cast<int>(_fileno(file)); }
1995 inline int FileNo(FILE* file) {
return _fileno(file); }
1996 inline int Stat(
const char* path, StatStruct* buf) {
return _stat(path, buf); }
1997 inline int RmDir(
const char* dir) {
return _rmdir(dir); }
1998 inline bool IsDir(
const StatStruct& st) {
1999 return (_S_IFDIR & st.st_mode) != 0;
2001 # endif // GTEST_OS_WINDOWS_MOBILE
2003 #elif GTEST_OS_ESP8266
2004 typedef struct stat StatStruct;
2006 inline int FileNo(FILE* file) {
return fileno(file); }
2007 inline int DoIsATTY(
int fd) {
return isatty(fd); }
2008 inline int Stat(
const char* path, StatStruct* buf) {
2012 inline int StrCaseCmp(
const char* s1,
const char* s2) {
2013 return strcasecmp(s1, s2);
2015 inline char*
StrDup(
const char* src) {
return strdup(src); }
2016 inline int RmDir(
const char* dir) {
return rmdir(dir); }
2017 inline bool IsDir(
const StatStruct& st) {
return S_ISDIR(st.st_mode); }
2021 typedef struct stat StatStruct;
2023 inline int FileNo(FILE* file) {
return fileno(file); }
2025 inline int Stat(
const char* path, StatStruct* buf) {
return stat(path, buf); }
2027 return strcasecmp(s1, s2);
2029 inline char*
StrDup(
const char* src) {
return strdup(src); }
2030 inline int RmDir(
const char* dir) {
return rmdir(dir); }
2031 inline bool IsDir(
const StatStruct& st) {
return S_ISDIR(st.st_mode); }
2033 #endif // GTEST_OS_WINDOWS
2039 int savedErrno = errno;
2054 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
2055 inline int ChDir(
const char* dir) {
return chdir(dir); }
2057 inline FILE*
FOpen(
const char* path,
const char* mode) {
2058 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2059 struct wchar_codecvt :
public std::codecvt<wchar_t, char, std::mbstate_t> {};
2060 std::wstring_convert<wchar_codecvt> converter;
2061 std::wstring wide_path = converter.from_bytes(path);
2062 std::wstring wide_mode = converter.from_bytes(mode);
2063 return _wfopen(wide_path.c_str(), wide_mode.c_str());
2064 #else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2065 return fopen(path, mode);
2066 #endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2068 #if !GTEST_OS_WINDOWS_MOBILE
2069 inline FILE *
FReopen(
const char* path,
const char* mode, FILE* stream) {
2070 return freopen(path, mode, stream);
2072 inline FILE*
FDOpen(
int fd,
const char* mode) {
return fdopen(fd, mode); }
2074 inline int FClose(FILE* fp) {
return fclose(fp); }
2075 #if !GTEST_OS_WINDOWS_MOBILE
2077 return static_cast<int>(read(fd, buf, count));
2080 return static_cast<int>(write(fd, buf, count));
2082 inline int Close(
int fd) {
return close(fd); }
2083 inline const char*
StrError(
int errnum) {
return strerror(errnum); }
2085 inline const char*
GetEnv(
const char* name) {
2086 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \
2087 GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266
2089 static_cast<void>(name);
2091 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2094 const char*
const env = getenv(name);
2095 return (env !=
nullptr && env[0] !=
'\0') ? env :
nullptr;
2097 return getenv(name);
2103 #if GTEST_OS_WINDOWS_MOBILE
2107 [[noreturn]]
void Abort();
2109 [[noreturn]]
inline void Abort() { abort(); }
2110 #endif // GTEST_OS_WINDOWS_MOBILE
2119 #if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE
2121 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
2122 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2123 #elif defined(_MSC_VER)
2125 # define GTEST_SNPRINTF_ _snprintf
2127 # define GTEST_SNPRINTF_ snprintf
2156 template <
size_t size>
2186 #if !defined(GTEST_FLAG)
2187 # define GTEST_FLAG(name) FLAGS_gtest_##name
2188 #endif // !defined(GTEST_FLAG)
2190 #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2191 # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2192 #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2194 #if !defined(GTEST_DECLARE_bool_)
2195 # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2198 # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2199 # define GTEST_DECLARE_int32_(name) \
2200 GTEST_API_ extern std::int32_t GTEST_FLAG(name)
2201 # define GTEST_DECLARE_string_(name) \
2202 GTEST_API_ extern ::std::string GTEST_FLAG(name)
2205 # define GTEST_DEFINE_bool_(name, default_val, doc) \
2206 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2207 # define GTEST_DEFINE_int32_(name, default_val, doc) \
2208 GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val)
2209 # define GTEST_DEFINE_string_(name, default_val, doc) \
2210 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2212 #endif // !defined(GTEST_DECLARE_bool_)
2215 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2216 # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2217 # define GTEST_LOCK_EXCLUDED_(locks)
2218 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2236 #if !defined(GTEST_INTERNAL_DEPRECATED)
2246 #if defined(_MSC_VER)
2247 #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message))
2248 #elif defined(__GNUC__)
2249 #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message)))
2251 #define GTEST_INTERNAL_DEPRECATED(message)
2254 #endif // !defined(GTEST_INTERNAL_DEPRECATED)
2259 #define GTEST_INTERNAL_HAS_ANY 1
2260 #include "absl/types/any.h"
2262 namespace internal {
2263 using Any = ::absl::any;
2267 #ifdef __has_include
2268 #if __has_include(<any>) && __cplusplus >= 201703L
2271 #define GTEST_INTERNAL_HAS_ANY 1
2274 namespace internal {
2275 using Any = ::std::any;
2280 #endif // __has_include(<any>) && __cplusplus >= 201703L
2281 #endif // __has_include
2282 #endif // GTEST_HAS_ABSL
2287 #define GTEST_INTERNAL_HAS_OPTIONAL 1
2288 #include "absl/types/optional.h"
2290 namespace internal {
2291 template <
typename T>
2292 using Optional = ::absl::optional<T>;
2296 #ifdef __has_include
2297 #if __has_include(<optional>) && __cplusplus >= 201703L
2300 #define GTEST_INTERNAL_HAS_OPTIONAL 1
2303 namespace internal {
2304 template <
typename T>
2305 using Optional = ::std::optional<T>;
2310 #endif // __has_include(<optional>) && __cplusplus >= 201703L
2311 #endif // __has_include
2312 #endif // GTEST_HAS_ABSL
2317 # define GTEST_INTERNAL_HAS_STRING_VIEW 1
2318 #include "absl/strings/string_view.h"
2320 namespace internal {
2321 using StringView = ::absl::string_view;
2325 # ifdef __has_include
2326 # if __has_include(<string_view>) && __cplusplus >= 201703L
2329 # define GTEST_INTERNAL_HAS_STRING_VIEW 1
2330 #include <string_view>
2332 namespace internal {
2333 using StringView = ::std::string_view;
2338 # endif // __has_include(<string_view>) && __cplusplus >= 201703L
2339 # endif // __has_include
2340 #endif // GTEST_HAS_ABSL
2345 #define GTEST_INTERNAL_HAS_VARIANT 1
2346 #include "absl/types/variant.h"
2348 namespace internal {
2349 template <
typename...
T>
2350 using Variant = ::absl::variant<
T...>;
2354 #ifdef __has_include
2355 #if __has_include(<variant>) && __cplusplus >= 201703L
2358 #define GTEST_INTERNAL_HAS_VARIANT 1
2361 namespace internal {
2362 template <
typename...
T>
2363 using Variant = ::std::variant<
T...>;
2367 #endif // __has_include(<variant>) && __cplusplus >= 201703L
2368 #endif // __has_include
2369 #endif // GTEST_HAS_ABSL
2371 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
FILE * FReopen(const char *path, const char *mode, FILE *stream)
GTEST_API_ bool IsTrue(bool condition)
GTEST_API_ int32_t Int32FromGTestEnv(const char *flag, int32_t default_val)
GTEST_API_ std::string GetCapturedStdout()
GTEST_API_ std::string GetCapturedStderr()
static bool FullMatch(const ::std::string &str, const RE &re)
#define GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
const T * pointer() const
const char * StringFromGTestEnv(const char *flag, const char *default_val)
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
RE(const ::std::string ®ex)
constexpr BiggestInt kMaxBiggestInt
GTEST_API_ size_t GetThreadCount()
int Stat(const char *path, StatStruct *buf)
int ChDir(const char *dir)
const char * GetEnv(const char *name)
GTEST_API_ void CaptureStdout()
static bool PartialMatch(const ::std::string &str, const RE &re)
#define GTEST_DISABLE_MSC_DEPRECATED_POP_()
GTEST_API_ std::vector< std::string > GetArgvs()
GTEST_API_ std::string ReadEntireFile(FILE *file)
GTEST_API_ bool ParseInt32(const Message &src_text, const char *str, int32_t *value)
int Write(int fd, const void *buf, unsigned int count)
::std::ostream & GetStream()
const GTestLogSeverity severity_
int RmDir(const char *dir)
#define GTEST_CHECK_(condition)
GTEST_API_ size_t GetFileSize(FILE *file)
int Read(int fd, void *buf, unsigned int count)
ThreadLocal(const T &value)
int StrCaseCmp(const char *s1, const char *s2)
bool IsDir(const StatStruct &st)
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
const char * pattern() const
bool BoolFromGTestEnv(const char *flag, bool default_val)
FILE * FOpen(const char *path, const char *mode)
GTEST_API_ void CaptureStderr()
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
const T func(int n, T *x)
FILE * FDOpen(int fd, const char *mode)
char * StrDup(const char *src)
const char * StrError(int errnum)
Derived * CheckedDowncastToActualType(Base *base)
std::string OutputFlagAlsoCheckEnvVar()
std::string StripTrailingSpaces(std::string str)
#define GTEST_INTENTIONAL_CONST_COND_POP_()