diff --git a/include/fmt/core.h b/include/fmt/core.h index 48de108a..691aa635 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -259,7 +259,8 @@ namespace internal { // A workaround for gcc 4.8 to make void_t work in a SFINAE context. template struct void_t_impl { using type = void; }; -FMT_NORETURN FMT_API void assert_fail(const char* file, int line, const char* message); +FMT_NORETURN FMT_API void assert_fail(const char* file, int line, + const char* message); #ifndef FMT_ASSERT # ifdef NDEBUG @@ -970,7 +971,7 @@ template struct arg_mapper { } FMT_CONSTEXPR const named_arg_base& map( - const named_arg_base& val){ + const named_arg_base& val) { return val; } @@ -1366,8 +1367,8 @@ template class basic_format_args { /** \rst - Constructs a `dynamic_basic_format_args` object from `~fmt::format_arg_store`. - \endrst + Constructs a `dynamic_basic_format_args` object from + `~fmt::format_arg_store`. \endrst */ template basic_format_args(const dynamic_format_arg_store& store) diff --git a/include/fmt/dyn-args.h b/include/fmt/dyn-args.h index 6b3c5ee1..a305b7bc 100644 --- a/include/fmt/dyn-args.h +++ b/include/fmt/dyn-args.h @@ -4,9 +4,9 @@ #ifndef FMT_DYN_ARGS_H_ #define FMT_DYN_ARGS_H_ -#include #include #include +#include #include "core.h" @@ -21,53 +21,46 @@ FMT_BEGIN_NAMESPACE namespace internal { -template -struct is_string_view : std::false_type{}; +template struct is_string_view : std::false_type {}; -template -struct is_string_view, Char> -: std::true_type{}; +template +struct is_string_view, Char> : std::true_type {}; #ifdef FMT_USE_STRING_VIEW -template +template struct is_string_view, Char> -: std::true_type{}; + : std::true_type {}; #endif #ifdef FMT_USE_EXPERIMENTAL_STRING_VIEW -template +template struct is_string_view, Char> -: std::true_type{}; + : std::true_type {}; #endif -template -struct is_ref_wrapper : std::false_type{}; +template struct is_ref_wrapper : std::false_type {}; -template -struct is_ref_wrapper> : std::true_type{}; +template +struct is_ref_wrapper> : std::true_type {}; -template -struct need_dyn_copy{ +template struct need_dyn_copy { using mapped_type = mapped_type_constant; static_assert(mapped_type::value != internal::type::named_arg_type, - "Bug indicator. Named arguments must be processed separately"); + "Bug indicator. Named arguments must be processed separately"); - using type = std::integral_constant::value || - is_string_view::value || - ( - mapped_type::value != internal::type::cstring_type && - mapped_type::value != internal::type::custom_type && - mapped_type::value != internal::type::string_type - ) - )>; + using type = std::integral_constant< + bool, !(is_ref_wrapper::value || + is_string_view::value || + (mapped_type::value != internal::type::cstring_type && + mapped_type::value != internal::type::custom_type && + mapped_type::value != internal::type::string_type))>; }; -template using need_dyn_copy_t = - typename need_dyn_copy::type; +template +using need_dyn_copy_t = typename need_dyn_copy::type; -template -const T& get(const StorageValue& v){ +template +const T& get(const StorageValue& v) { return v; } @@ -98,18 +91,18 @@ class dynamic_format_arg_store using string_type = std::basic_string; #ifdef FMT_HAS_VARIANT using storage_item_type = - conditional_t, + conditional_t, string_type>; #else - static_assert(!has_custom_args, "std::variant<> is required to support " - "custom types in dynamic_format_arg_store"); + static_assert(!has_custom_args, + "std::variant<> is required to support " + "custom types in dynamic_format_arg_store"); using storage_item_type = string_type; #endif using value_type = basic_format_arg; using named_value_type = internal::named_arg_base; - template + template using storaged_type = conditional_t::value, string_type, T>; @@ -128,8 +121,7 @@ class dynamic_format_arg_store friend class basic_format_args; - template - const T& get_last_pushed() const{ + template const T& get_last_pushed() const { using internal::get; return get(storage_.front()); } @@ -138,23 +130,23 @@ class dynamic_format_arg_store return internal::is_unpacked_bit | data_.size(); } - template const T& stored_value(const T& arg, std::false_type) { + template const T& stored_value(const T& arg, std::false_type) { return arg; } - template const T& stored_value( - const std::reference_wrapper& arg, std::false_type) { + template + const T& stored_value(const std::reference_wrapper& arg, std::false_type) { return arg.get(); } - template const storaged_type& stored_value(const T& arg, - std::true_type) { + template + const storaged_type& stored_value(const T& arg, std::true_type) { using type = storaged_type; storage_.emplace_front(type{arg}); return get_last_pushed(); } - template void emplace_arg(const T& arg) { + template void emplace_arg(const T& arg) { data_.emplace_back(internal::make_arg(arg)); } @@ -176,23 +168,23 @@ class dynamic_format_arg_store emplace_arg(arg.get()); } - template void push_back( - const internal::named_arg& arg) { + template + void push_back(const internal::named_arg& arg) { // Named argument is tricky. It's returned by value from fmt::arg() // and then pointer to it is stored in basic_format_arg<>. // So after end of expression the pointer becomes dangling. storage_.emplace_front(string_type{arg.name.data(), arg.name.size()}); basic_string_view name = get_last_pushed(); - const auto& val = stored_value( - arg.value, internal::need_dyn_copy_t{}); + const auto& val = + stored_value(arg.value, internal::need_dyn_copy_t{}); auto named_with_stored_parts = fmt::arg(name, val); // Serialize value into base internal::arg_mapper().map(named_with_stored_parts); named_args_.push_front(named_with_stored_parts); data_.emplace_back(internal::make_arg(named_args_.front())); -// data_.emplace_back(internal::make_arg_from_serialized_named( -// named_args_.front())); + // data_.emplace_back(internal::make_arg_from_serialized_named( + // named_args_.front())); } }; diff --git a/test/format-dyn-args-test.cc b/test/format-dyn-args-test.cc index ee486374..fcd9815d 100644 --- a/test/format-dyn-args-test.cc +++ b/test/format-dyn-args-test.cc @@ -5,16 +5,13 @@ #include "gtest-extra.h" - TEST(FormatDynArgsTest, Basic) { fmt::dynamic_format_arg_store store; store.push_back(42); store.push_back("abc1"); store.push_back(1.5f); - std::string result = fmt::vformat( - "{} and {} and {}", - store); + std::string result = fmt::vformat("{} and {} and {}", store); EXPECT_EQ("42 and abc1 and 1.5", result); } @@ -29,31 +26,26 @@ TEST(FormatDynArgsTest, StringsAndRefs) { store.push_back(fmt::string_view{str}); str[0] = 'X'; - std::string result = fmt::vformat( - "{} and {} and {}", - store); + std::string result = fmt::vformat("{} and {} and {}", store); EXPECT_EQ("1234567890 and X234567890 and X234567890", result); } -struct Custom{ int i{0}; }; +struct Custom { + int i{0}; +}; FMT_BEGIN_NAMESPACE -template <> -struct formatter { - auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) - { +template <> struct formatter { + auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) { return ctx.begin(); } template - auto format(const Custom& p, FormatContext& ctx) -> - decltype(format_to(ctx.out(), - std::declval())) { - return format_to( - ctx.out(), - "cust={}", p.i); - } + auto format(const Custom& p, FormatContext& ctx) -> decltype(format_to( + ctx.out(), std::declval())) { + return format_to(ctx.out(), "cust={}", p.i); + } }; FMT_END_NAMESPACE @@ -69,14 +61,12 @@ TEST(FormatDynArgsTest, CustomFormat) { store.push_back(std::cref(c)); ++c.i; - std::string result = fmt::vformat( - "{} and {} and {}", - store); + std::string result = fmt::vformat("{} and {} and {}", store); EXPECT_EQ("cust=0 and cust=1 and cust=3", result); } -#endif // FMT_HAS_VARIANT +#endif // FMT_HAS_VARIANT TEST(FormatDynArgsTest, NamedArgByRef) { fmt::dynamic_format_arg_store store; @@ -88,15 +78,14 @@ TEST(FormatDynArgsTest, NamedArgByRef) { // complains about this. // // A real life usecase is when you have both name and value alive - // guarantee their lifetime and thus don't want them to be copied into + // guarantee their lifetime and thus don't want them to be copied into // storages. int a1_val{42}; auto a1 = fmt::arg("a1_", a1_val); store.push_back(std::cref(a1)); - std::string result = fmt::vformat( - "{a1_}", // and {} and {}", - store); + std::string result = fmt::vformat("{a1_}", // and {} and {}", + store); EXPECT_EQ("42", result); } @@ -115,9 +104,7 @@ TEST(FormatDynArgsTest, NamedStrings) { store.push_back(fmt::arg("a2", std::cref(str))); str[0] = 'X'; - std::string result = fmt::vformat( - "{a1} and {a2}", - store); + std::string result = fmt::vformat("{a1} and {a2}", store); EXPECT_EQ("1234567890 and X234567890", result); } @@ -134,11 +121,9 @@ TEST(FormatDynArgsTest, NamedCustomFormat) { store.push_back(fmt::arg("a3", std::cref(c))); ++c.i; - std::string result = fmt::vformat( - "{a1} and {a2} and {a3}", - store); + std::string result = fmt::vformat("{a1} and {a2} and {a3}", store); EXPECT_EQ("cust=0 and cust=1 and cust=3", result); } -#endif // FMT_HAS_VARIANT +#endif // FMT_HAS_VARIANT