get_type -> mapped_type_constant

This commit is contained in:
Victor Zverovich 2019-06-10 21:21:45 -07:00
parent a48daa60e5
commit 87fbc6f756
4 changed files with 20 additions and 14 deletions

View File

@ -827,6 +827,12 @@ template <typename Context> struct arg_mapper {
} }
}; };
// A type constant after applying arg_mapper<Context>.
template <typename T, typename Context>
using mapped_type_constant =
type_constant<decltype(arg_mapper<Context>().map(std::declval<T>())),
typename Context::char_type>;
// Maximum number of arguments with packed types. // Maximum number of arguments with packed types.
enum { max_packed_args = 15 }; enum { max_packed_args = 15 };
enum : unsigned long long { is_unpacked_bit = 1ull << 63 }; enum : unsigned long long { is_unpacked_bit = 1ull << 63 };
@ -983,22 +989,18 @@ class locale_ref {
template <typename Locale> Locale get() const; template <typename Locale> Locale get() const;
}; };
template <typename Context, typename T>
using get_type =
type_constant<decltype(arg_mapper<Context>().map(std::declval<T>())),
typename Context::char_type>;
template <typename> constexpr unsigned long long get_types() { return 0; } template <typename> constexpr unsigned long long get_types() { return 0; }
template <typename Context, typename Arg, typename... Args> template <typename Context, typename Arg, typename... Args>
constexpr unsigned long long get_types() { constexpr unsigned long long get_types() {
return get_type<Context, Arg>::value | (get_types<Context, Args...>() << 4); return mapped_type_constant<Arg, Context>::value |
(get_types<Context, Args...>() << 4);
} }
template <typename Context, typename T> template <typename Context, typename T>
FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T& value) { FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T& value) {
basic_format_arg<Context> arg; basic_format_arg<Context> arg;
arg.type_ = get_type<Context, T>::value; arg.type_ = mapped_type_constant<T, Context>::value;
arg.value_ = arg_mapper<Context>().map(value); arg.value_ = arg_mapper<Context>().map(value);
return arg; return arg;
} }

View File

@ -3165,7 +3165,8 @@ template <typename Char = char> class dynamic_formatter {
auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out()) { auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out()) {
handle_specs(ctx); handle_specs(ctx);
internal::specs_checker<null_handler> checker( internal::specs_checker<null_handler> checker(
null_handler(), internal::get_type<FormatContext, T>::value); null_handler(),
internal::mapped_type_constant<T, FormatContext>::value);
checker.on_align(specs_.align()); checker.on_align(specs_.align());
if (specs_.flags == 0) if (specs_.flags == 0)
; // Do nothing. ; // Do nothing.

View File

@ -567,15 +567,18 @@ TEST(CoreTest, ToStringViewForeignStrings) {
EXPECT_EQ(to_string_view(my_string<wchar_t>(L"42")), L"42"); EXPECT_EQ(to_string_view(my_string<wchar_t>(L"42")), L"42");
EXPECT_EQ(to_string_view(QString(L"42")), L"42"); EXPECT_EQ(to_string_view(QString(L"42")), L"42");
fmt::internal::type type = fmt::internal::type type =
fmt::internal::get_type<fmt::format_context, my_string<char>>::value; fmt::internal::mapped_type_constant<my_string<char>,
fmt::format_context>::value;
EXPECT_EQ(type, fmt::internal::string_type);
type = fmt::internal::mapped_type_constant<my_string<wchar_t>,
fmt::wformat_context>::value;
EXPECT_EQ(type, fmt::internal::string_type); EXPECT_EQ(type, fmt::internal::string_type);
type = type =
fmt::internal::get_type<fmt::wformat_context, my_string<wchar_t>>::value; fmt::internal::mapped_type_constant<QString, fmt::wformat_context>::value;
EXPECT_EQ(type, fmt::internal::string_type);
type = fmt::internal::get_type<fmt::wformat_context, QString>::value;
EXPECT_EQ(type, fmt::internal::string_type); EXPECT_EQ(type, fmt::internal::string_type);
// Does not compile: only wide format contexts are compatible with QString! // Does not compile: only wide format contexts are compatible with QString!
// type = fmt::internal::get_type<fmt::format_context, QString>::value; // type = fmt::internal::mapped_type_constant<QString,
// fmt::format_context>::value;
} }
TEST(CoreTest, FormatForeignStrings) { TEST(CoreTest, FormatForeignStrings) {

View File

@ -643,7 +643,7 @@ struct formatter {
FMT_CONSTEXPR typename ParseContext::iterator parse(ParseContext& ctx) { FMT_CONSTEXPR typename ParseContext::iterator parse(ParseContext& ctx) {
namespace internal = fmt::internal; namespace internal = fmt::internal;
typedef internal::dynamic_specs_handler<ParseContext> handler_type; typedef internal::dynamic_specs_handler<ParseContext> handler_type;
auto type = internal::get_type<fmt::buffer_context<Char>, T>::value; auto type = internal::mapped_type_constant<T, fmt::buffer_context<Char>>::value;
internal::specs_checker<handler_type> handler(handler_type(specs_, ctx), internal::specs_checker<handler_type> handler(handler_type(specs_, ctx),
type); type);
auto it = parse_format_specs(ctx.begin(), ctx.end(), handler); auto it = parse_format_specs(ctx.begin(), ctx.end(), handler);