Demacrify

This commit is contained in:
Victor Zverovich
2019-05-30 20:50:07 -07:00
parent 637bf3c6d9
commit 49f78a427b
2 changed files with 43 additions and 36 deletions

View File

@ -187,25 +187,34 @@
# define FMT_ASSERT(condition, message) assert((condition) && message) # define FMT_ASSERT(condition, message) assert((condition) && message)
#endif #endif
// libc++ supports string_view in pre-c++17.
#if (FMT_HAS_INCLUDE(<string_view>) && \
(__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
# include <string_view>
# define FMT_STRING_VIEW std::basic_string_view
#elif FMT_HAS_INCLUDE("experimental/string_view") && __cplusplus >= 201402L
# include <experimental/string_view>
# define FMT_STRING_VIEW std::experimental::basic_string_view
#endif
// An enable_if helper to be used in template parameters. enable_if in template // An enable_if helper to be used in template parameters. enable_if in template
// parameters results in much shorter symbols: https://godbolt.org/z/sWw4vP. // parameters results in much shorter symbols: https://godbolt.org/z/sWw4vP.
#define FMT_ENABLE_IF_T(...) typename std::enable_if<(__VA_ARGS__), int>::type #define FMT_ENABLE_IF_T(...) typename std::enable_if<(__VA_ARGS__), int>::type
#define FMT_ENABLE_IF(...) FMT_ENABLE_IF_T(__VA_ARGS__) = 0 #define FMT_ENABLE_IF(...) FMT_ENABLE_IF_T(__VA_ARGS__) = 0
// libc++ supports string_view in pre-c++17.
#if (FMT_HAS_INCLUDE(<string_view>) && \
(__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
# include <string_view>
# define FMT_USE_STRING_VIEW
#elif FMT_HAS_INCLUDE("experimental/string_view") && __cplusplus >= 201402L
# include <experimental/string_view>
# define FMT_USE_EXPERIMENTAL_STRING_VIEW
#endif
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace internal { namespace internal {
#if defined(FMT_USE_STRING_VIEW)
template <typename Char> using std_string_view = std::basic_string_view<Char>;
#elif defined(FMT_USE_EXPERIMENTAL_STRING_VIEW)
template <typename Char>
using std_string_view = std::experimental::basic_string_view<Char>;
#else
template <typename T> struct std_string_view {};
#endif
template <typename> struct result_of; template <typename> struct result_of;
#if (__cplusplus >= 201703L || \ #if (__cplusplus >= 201703L || \
@ -396,11 +405,11 @@ template <typename Char> class basic_string_view {
FMT_NOEXCEPT : data_(s.data()), FMT_NOEXCEPT : data_(s.data()),
size_(s.size()) {} size_(s.size()) {}
#ifdef FMT_STRING_VIEW template <
FMT_CONSTEXPR basic_string_view(FMT_STRING_VIEW<Char> s) FMT_NOEXCEPT typename S,
: data_(s.data()), FMT_ENABLE_IF(std::is_same<S, internal::std_string_view<Char>>::value)>
size_(s.size()) {} FMT_CONSTEXPR basic_string_view(S s) FMT_NOEXCEPT : data_(s.data()),
#endif size_(s.size()) {}
/** Returns a pointer to the string data. */ /** Returns a pointer to the string data. */
FMT_CONSTEXPR const Char* data() const { return data_; } FMT_CONSTEXPR const Char* data() const { return data_; }
@ -486,12 +495,12 @@ inline basic_string_view<Char> to_string_view(const Char* s) {
return s; return s;
} }
#ifdef FMT_STRING_VIEW template <typename Char,
template <typename Char> FMT_ENABLE_IF(!std::is_empty<internal::std_string_view<Char>>::value)>
inline basic_string_view<Char> to_string_view(FMT_STRING_VIEW<Char> s) { inline basic_string_view<Char> to_string_view(
internal::std_string_view<Char> s) {
return s; return s;
} }
#endif
// A base class for compile-time strings. It is defined in the fmt namespace to // A base class for compile-time strings. It is defined in the fmt namespace to
// make formatting functions visible via ADL, e.g. format(fmt("{}"), 42). // make formatting functions visible via ADL, e.g. format(fmt("{}"), 42).

View File

@ -475,9 +475,7 @@ class QString {
QString(const wchar_t* s) : s_(std::make_shared<std::wstring>(s)) {} QString(const wchar_t* s) : s_(std::make_shared<std::wstring>(s)) {}
const wchar_t* utf16() const FMT_NOEXCEPT { return s_->data(); } const wchar_t* utf16() const FMT_NOEXCEPT { return s_->data(); }
int size() const FMT_NOEXCEPT { return static_cast<int>(s_->size()); } int size() const FMT_NOEXCEPT { return static_cast<int>(s_->size()); }
#ifdef FMT_STRING_VIEW
operator FMT_STRING_VIEW<wchar_t>() const FMT_NOEXCEPT { return *s_; }
#endif
private: private:
std::shared_ptr<std::wstring> s_; std::shared_ptr<std::wstring> s_;
}; };
@ -499,21 +497,21 @@ struct derived_from_string_view : fmt::basic_string_view<Char> {};
} // namespace } // namespace
TYPED_TEST(IsStringTest, IsString) { TYPED_TEST(IsStringTest, IsString) {
EXPECT_TRUE((fmt::internal::is_string<TypeParam*>::value)); EXPECT_TRUE(fmt::internal::is_string<TypeParam*>::value);
EXPECT_TRUE((fmt::internal::is_string<const TypeParam*>::value)); EXPECT_TRUE(fmt::internal::is_string<const TypeParam*>::value);
EXPECT_TRUE((fmt::internal::is_string<TypeParam[2]>::value)); EXPECT_TRUE(fmt::internal::is_string<TypeParam[2]>::value);
EXPECT_TRUE((fmt::internal::is_string<const TypeParam[2]>::value)); EXPECT_TRUE(fmt::internal::is_string<const TypeParam[2]>::value);
EXPECT_TRUE((fmt::internal::is_string<std::basic_string<TypeParam>>::value)); EXPECT_TRUE(fmt::internal::is_string<std::basic_string<TypeParam>>::value);
EXPECT_TRUE( EXPECT_TRUE(
(fmt::internal::is_string<fmt::basic_string_view<TypeParam>>::value)); fmt::internal::is_string<fmt::basic_string_view<TypeParam>>::value);
EXPECT_TRUE( EXPECT_TRUE(
(fmt::internal::is_string<derived_from_string_view<TypeParam>>::value)); fmt::internal::is_string<derived_from_string_view<TypeParam>>::value);
#ifdef FMT_STRING_VIEW using string_view = fmt::internal::std_string_view<TypeParam>;
EXPECT_TRUE((fmt::internal::is_string<FMT_STRING_VIEW<TypeParam>>::value)); EXPECT_TRUE(std::is_empty<string_view>::value !=
#endif fmt::internal::is_string<string_view>::value);
EXPECT_TRUE((fmt::internal::is_string<my_ns::my_string<TypeParam>>::value)); EXPECT_TRUE(fmt::internal::is_string<my_ns::my_string<TypeParam>>::value);
EXPECT_FALSE((fmt::internal::is_string<my_ns::non_string>::value)); EXPECT_FALSE(fmt::internal::is_string<my_ns::non_string>::value);
EXPECT_TRUE((fmt::internal::is_string<FakeQt::QString>::value)); EXPECT_TRUE(fmt::internal::is_string<FakeQt::QString>::value);
} }
TEST(CoreTest, Format) { TEST(CoreTest, Format) {