📄 gtest-port.h
字号:
#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))#else#define GTEST_MUST_USE_RESULT_#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICCnamespace testing {class Message;namespace internal {class String;// std::strstream is deprecated. However, we have to use it on// Windows as std::stringstream won't compile on Windows when// exceptions are disabled. We use std::stringstream on other// platforms to avoid compiler warnings there.#if GTEST_HAS_STD_STRINGtypedef ::std::stringstream StrStream;#elsetypedef ::std::strstream StrStream;#endif // GTEST_HAS_STD_STRING// Defines scoped_ptr.// This implementation of scoped_ptr is PARTIAL - it only contains// enough stuff to satisfy Google Test's need.template <typename T>class scoped_ptr { public: explicit scoped_ptr(T* p = NULL) : ptr_(p) {} ~scoped_ptr() { reset(); } T& operator*() const { return *ptr_; } T* operator->() const { return ptr_; } T* get() const { return ptr_; } T* release() { T* const ptr = ptr_; ptr_ = NULL; return ptr; } void reset(T* p = NULL) { if (p != ptr_) { if (sizeof(T) > 0) { // Makes sure T is a complete type. delete ptr_; } ptr_ = p; } } private: T* ptr_; GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);};#ifdef GTEST_HAS_DEATH_TEST// Defines RE.// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended// Regular Expression syntax.class RE { public: // Constructs an RE from a string.#if GTEST_HAS_STD_STRING RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT#endif // GTEST_HAS_STD_STRING#if GTEST_HAS_GLOBAL_STRING RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT#endif // GTEST_HAS_GLOBAL_STRING RE(const char* regex) { Init(regex); } // NOLINT ~RE(); // Returns the string representation of the regex. const char* pattern() const { return pattern_; } // FullMatch(str, re) returns true iff regular expression re matches // the entire str. // PartialMatch(str, re) returns true iff regular expression re // matches a substring of str (including str itself). // // TODO(wan@google.com): make FullMatch() and PartialMatch() work // when str contains NUL characters.#if GTEST_HAS_STD_STRING static bool FullMatch(const ::std::string& str, const RE& re) { return FullMatch(str.c_str(), re); } static bool PartialMatch(const ::std::string& str, const RE& re) { return PartialMatch(str.c_str(), re); }#endif // GTEST_HAS_STD_STRING#if GTEST_HAS_GLOBAL_STRING static bool FullMatch(const ::string& str, const RE& re) { return FullMatch(str.c_str(), re); } static bool PartialMatch(const ::string& str, const RE& re) { return PartialMatch(str.c_str(), re); }#endif // GTEST_HAS_GLOBAL_STRING static bool FullMatch(const char* str, const RE& re); static bool PartialMatch(const char* str, const RE& re); private: void Init(const char* regex); // We use a const char* instead of a string, as Google Test may be used // where string is not available. We also do not use Google Test's own // String type here, in order to simplify dependencies between the // files. const char* pattern_; regex_t full_regex_; // For FullMatch(). regex_t partial_regex_; // For PartialMatch(). bool is_valid_;};#endif // GTEST_HAS_DEATH_TEST// Defines logging utilities:// GTEST_LOG_() - logs messages at the specified severity level.// LogToStderr() - directs all log messages to stderr.// FlushInfoLog() - flushes informational log messages.enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL};void GTestLog(GTestLogSeverity severity, const char* file, int line, const char* msg);#define GTEST_LOG_(severity, msg)\ ::testing::internal::GTestLog(\ ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \ (::testing::Message() << (msg)).GetString().c_str())inline void LogToStderr() {}inline void FlushInfoLog() { fflush(NULL); }// Defines the stderr capturer:// CaptureStderr - starts capturing stderr.// GetCapturedStderr - stops capturing stderr and returns the captured string.#ifdef GTEST_HAS_DEATH_TEST// A copy of all command line arguments. Set by InitGoogleTest().extern ::std::vector<String> g_argvs;void CaptureStderr();// GTEST_HAS_DEATH_TEST implies we have ::std::string.::std::string GetCapturedStderr();const ::std::vector<String>& GetArgvs();#endif // GTEST_HAS_DEATH_TEST// Defines synchronization primitives.// A dummy implementation of synchronization primitives (mutex, lock,// and thread-local variable). Necessary for compiling Google Test where// mutex is not supported - using Google Test in multiple threads is not// supported on such platforms.class Mutex { public: Mutex() {} explicit Mutex(int /*unused*/) {} void AssertHeld() const {} enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 };};// We cannot call it MutexLock directly as the ctor declaration would// conflict with a macro named MutexLock, which is defined on some// platforms. Hence the typedef trick below.class GTestMutexLock { public: explicit GTestMutexLock(Mutex*) {} // NOLINT};typedef GTestMutexLock MutexLock;template <typename T>class ThreadLocal { public: ThreadLocal() : value_() {} explicit ThreadLocal(const T& value) : value_(value) {} T* pointer() { return &value_; } const T* pointer() const { return &value_; } const T& get() const { return value_; } void set(const T& value) { value_ = value; } private: T value_;};// There's no portable way to detect the number of threads, so we just// return 0 to indicate that we cannot detect it.inline size_t GetThreadCount() { return 0; }// The above synchronization primitives have dummy implementations.// Therefore Google Test is not thread-safe.#define GTEST_IS_THREADSAFE 0#if defined(__SYMBIAN32__) || defined(__IBMCPP__)// Passing non-POD classes through ellipsis (...) crashes the ARM// compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to// instantiate a copy constructor for objects passed through ellipsis// (...), failing for uncopyable objects. We define this to indicate// the fact.#define GTEST_ELLIPSIS_NEEDS_COPY_ 1// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between// const T& and const T* in a function template. These compilers// _can_ decide between class template specializations for T and T*,// so a tr1::type_traits-like is_pointer works.#define GTEST_NEEDS_IS_POINTER_ 1#endif // defined(__SYMBIAN32__) || defined(__IBMCPP__)template <bool bool_value>struct bool_constant { typedef bool_constant<bool_value> type; static const bool value = bool_value;};template <bool bool_value> const bool bool_constant<bool_value>::value;typedef bool_constant<false> false_type;typedef bool_constant<true> true_type;template <typename T>struct is_pointer : public false_type {};template <typename T>struct is_pointer<T*> : public true_type {};// Defines BiggestInt as the biggest signed integer type the compiler// supports.#ifdef GTEST_OS_WINDOWStypedef __int64 BiggestInt;#elsetypedef long long BiggestInt; // NOLINT#endif // GTEST_OS_WINDOWS// The maximum number a BiggestInt can represent. This definition// works no matter BiggestInt is represented in one's complement or// two's complement.//// We cannot rely on numeric_limits in STL, as __int64 and long long// are not part of standard C++ and numeric_limits doesn't need to be// defined for them.const BiggestInt kMaxBiggestInt = ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));// This template class serves as a compile-time function from size to// type. It maps a size in bytes to a primitive type with that// size. e.g.//// TypeWithSize<4>::UInt//// is typedef-ed to be unsigned int (unsigned integer made up of 4// bytes).//// Such functionality should belong to STL, but I cannot find it// there.//// Google Test uses this class in the implementation of floating-point// comparison.//// For now it only handles UInt (unsigned int) as that's all Google Test// needs. Other types can be easily added in the future if need// arises.template <size_t size>class TypeWithSize { public: // This prevents the user from using TypeWithSize<N> with incorrect // values of N. typedef void UInt;};// The specialization for size 4.template <>class TypeWithSize<4> { public: // unsigned int has size 4 in both gcc and MSVC. // // As base/basictypes.h doesn't compile on Windows, we cannot use // uint32, uint64, and etc here. typedef int Int; typedef unsigned int UInt;};// The specialization for size 8.template <>class TypeWithSize<8> { public:#ifdef GTEST_OS_WINDOWS typedef __int64 Int; typedef unsigned __int64 UInt;#else typedef long long Int; // NOLINT typedef unsigned long long UInt; // NOLINT#endif // GTEST_OS_WINDOWS};// Integer types of known sizes.typedef TypeWithSize<4>::Int Int32;typedef TypeWithSize<4>::UInt UInt32;typedef TypeWithSize<8>::Int Int64;typedef TypeWithSize<8>::UInt UInt64;typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.// Utilities for command line flags and environment variables.// A wrapper for getenv() that works on Linux, Windows, and Mac OS.inline const char* GetEnv(const char* name) {#ifdef _WIN32_WCE // We are on Windows CE. // CE has no environment variables. return NULL;#elif defined(GTEST_OS_WINDOWS) // We are on Windows proper. // MSVC 8 deprecates getenv(), so we want to suppress warning 4996 // (deprecated function) there.#pragma warning(push) // Saves the current warning state.#pragma warning(disable:4996) // Temporarily disables warning 4996. return getenv(name);#pragma warning(pop) // Restores the warning state.#else // We are on Linux or Mac OS. return getenv(name);#endif}#ifdef _WIN32_WCE// Windows CE has no C library. The abort() function is used in// several places in Google Test. This implementation provides a reasonable// imitation of standard behaviour.void abort();#elseinline void abort() { ::abort(); }#endif // _WIN32_WCE// INTERNAL IMPLEMENTATION - DO NOT USE.//// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition// is not satisfied.// Synopsys:// GTEST_CHECK_(boolean_condition);// or// GTEST_CHECK_(boolean_condition) << "Additional message";//// This checks the condition and if the condition is not satisfied// it prints message about the condition violation, including the// condition itself, plus additional message streamed into it, if any,// and then it aborts the program. It aborts the program irrespective of// whether it is built in the debug mode or not.class GTestCheckProvider { public: GTestCheckProvider(const char* condition, const char* file, int line) { FormatFileLocation(file, line); ::std::cerr << " ERROR: Condition " << condition << " failed. "; } ~GTestCheckProvider() { ::std::cerr << ::std::endl; abort(); } void FormatFileLocation(const char* file, int line) { if (file == NULL) file = "unknown file"; if (line < 0) { ::std::cerr << file << ":"; } else {#if _MSC_VER ::std::cerr << file << "(" << line << "):";#else ::std::cerr << file << ":" << line << ":";#endif } } ::std::ostream& GetStream() { return ::std::cerr; }};#define GTEST_CHECK_(condition) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ if (condition) \ ; \ else \ ::testing::internal::GTestCheckProvider(\ #condition, __FILE__, __LINE__).GetStream()// Macro for referencing flags.#define GTEST_FLAG(name) FLAGS_gtest_##name// Macros for declaring flags.#define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name)#define GTEST_DECLARE_int32_(name) \ extern ::testing::internal::Int32 GTEST_FLAG(name)#define GTEST_DECLARE_string_(name) \ extern ::testing::internal::String GTEST_FLAG(name)// Macros for defining flags.#define GTEST_DEFINE_bool_(name, default_val, doc) \ bool GTEST_FLAG(name) = (default_val)#define GTEST_DEFINE_int32_(name, default_val, doc) \ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)#define GTEST_DEFINE_string_(name, default_val, doc) \ ::testing::internal::String GTEST_FLAG(name) = (default_val)// Parses 'str' for a 32-bit signed integer. If successful, writes the result// to *value and returns true; otherwise leaves *value unchanged and returns// false.// TODO(chandlerc): Find a better way to refactor flag and environment parsing// out of both gtest-port.cc and gtest.cc to avoid exporting this utility// function.bool ParseInt32(const Message& src_text, const char* str, Int32* value);// Parses a bool/Int32/string from the environment variable// corresponding to the given Google Test flag.bool BoolFromGTestEnv(const char* flag, bool default_val);Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);const char* StringFromGTestEnv(const char* flag, const char* default_val);} // namespace internal} // namespace testing#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -