Clang-format applied.

This commit is contained in:
vsol 2020-03-10 21:14:55 +03:00
parent 448fb74633
commit 5930040c99
3 changed files with 65 additions and 87 deletions

View File

@ -259,7 +259,8 @@ namespace internal {
// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
template <typename... Ts> 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 <typename Context> struct arg_mapper {
}
FMT_CONSTEXPR const named_arg_base<char_type>& map(
const named_arg_base<char_type>& val){
const named_arg_base<char_type>& val) {
return val;
}
@ -1366,8 +1367,8 @@ template <typename Context> 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 <typename... Args>
basic_format_args(const dynamic_format_arg_store<Context, Args...>& store)

View File

@ -4,9 +4,9 @@
#ifndef FMT_DYN_ARGS_H_
#define FMT_DYN_ARGS_H_
#include <vector>
#include <forward_list>
#include <functional>
#include <vector>
#include "core.h"
@ -21,53 +21,46 @@ FMT_BEGIN_NAMESPACE
namespace internal {
template<typename T, typename Char>
struct is_string_view : std::false_type{};
template <typename T, typename Char> struct is_string_view : std::false_type {};
template<typename Char>
struct is_string_view<basic_string_view<Char>, Char>
: std::true_type{};
template <typename Char>
struct is_string_view<basic_string_view<Char>, Char> : std::true_type {};
#ifdef FMT_USE_STRING_VIEW
template<typename Traits, typename Char>
template <typename Traits, typename Char>
struct is_string_view<std::basic_string_view<Char, Traits>, Char>
: std::true_type{};
: std::true_type {};
#endif
#ifdef FMT_USE_EXPERIMENTAL_STRING_VIEW
template<typename Traits, typename Char>
template <typename Traits, typename Char>
struct is_string_view<std::experimental::basic_string_view<Char, Traits>, Char>
: std::true_type{};
: std::true_type {};
#endif
template<typename T>
struct is_ref_wrapper : std::false_type{};
template <typename T> struct is_ref_wrapper : std::false_type {};
template<typename T>
struct is_ref_wrapper<std::reference_wrapper<T>> : std::true_type{};
template <typename T>
struct is_ref_wrapper<std::reference_wrapper<T>> : std::true_type {};
template<typename T, typename Context>
struct need_dyn_copy{
template <typename T, typename Context> struct need_dyn_copy {
using mapped_type = mapped_type_constant<T, Context>;
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<bool,!(
is_ref_wrapper<T>::value ||
is_string_view<T, typename Context::char_type>::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<T>::value ||
is_string_view<T, typename Context::char_type>::value ||
(mapped_type::value != internal::type::cstring_type &&
mapped_type::value != internal::type::custom_type &&
mapped_type::value != internal::type::string_type))>;
};
template<typename T, typename Context> using need_dyn_copy_t =
typename need_dyn_copy<T, Context>::type;
template <typename T, typename Context>
using need_dyn_copy_t = typename need_dyn_copy<T, Context>::type;
template<typename T, typename StorageValue>
const T& get(const StorageValue& v){
template <typename T, typename StorageValue>
const T& get(const StorageValue& v) {
return v;
}
@ -98,18 +91,18 @@ class dynamic_format_arg_store
using string_type = std::basic_string<char_type>;
#ifdef FMT_HAS_VARIANT
using storage_item_type =
conditional_t<has_custom_args,
std::variant<string_type, Args...>,
conditional_t<has_custom_args, std::variant<string_type, Args...>,
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<Context>;
using named_value_type = internal::named_arg_base<char_type>;
template<typename T>
template <typename T>
using storaged_type =
conditional_t<internal::is_string<T>::value, string_type, T>;
@ -128,8 +121,7 @@ class dynamic_format_arg_store
friend class basic_format_args<Context>;
template<typename T>
const T& get_last_pushed() const{
template <typename T> const T& get_last_pushed() const {
using internal::get;
return get<T>(storage_.front());
}
@ -138,23 +130,23 @@ class dynamic_format_arg_store
return internal::is_unpacked_bit | data_.size();
}
template<typename T> const T& stored_value(const T& arg, std::false_type) {
template <typename T> const T& stored_value(const T& arg, std::false_type) {
return arg;
}
template<typename T> const T& stored_value(
const std::reference_wrapper<T>& arg, std::false_type) {
template <typename T>
const T& stored_value(const std::reference_wrapper<T>& arg, std::false_type) {
return arg.get();
}
template<typename T> const storaged_type<T>& stored_value(const T& arg,
std::true_type) {
template <typename T>
const storaged_type<T>& stored_value(const T& arg, std::true_type) {
using type = storaged_type<T>;
storage_.emplace_front(type{arg});
return get_last_pushed<type>();
}
template<typename T> void emplace_arg(const T& arg) {
template <typename T> void emplace_arg(const T& arg) {
data_.emplace_back(internal::make_arg<Context>(arg));
}
@ -176,23 +168,23 @@ class dynamic_format_arg_store
emplace_arg(arg.get());
}
template <typename T> void push_back(
const internal::named_arg<T, char_type>& arg) {
template <typename T>
void push_back(const internal::named_arg<T, char_type>& 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<char_type> name = get_last_pushed<string_type>();
const auto& val = stored_value(
arg.value, internal::need_dyn_copy_t<T, Context>{});
const auto& val =
stored_value(arg.value, internal::need_dyn_copy_t<T, Context>{});
auto named_with_stored_parts = fmt::arg(name, val);
// Serialize value into base
internal::arg_mapper<Context>().map(named_with_stored_parts);
named_args_.push_front(named_with_stored_parts);
data_.emplace_back(internal::make_arg<Context>(named_args_.front()));
// data_.emplace_back(internal::make_arg_from_serialized_named<Context>(
// named_args_.front()));
// data_.emplace_back(internal::make_arg_from_serialized_named<Context>(
// named_args_.front()));
}
};

View File

@ -5,16 +5,13 @@
#include "gtest-extra.h"
TEST(FormatDynArgsTest, Basic) {
fmt::dynamic_format_arg_store<fmt::format_context> 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<Custom> {
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
{
template <> struct formatter<Custom> {
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const Custom& p, FormatContext& ctx) ->
decltype(format_to(ctx.out(),
std::declval<typename FormatContext::char_type const*>())) {
return format_to(
ctx.out(),
"cust={}", p.i);
}
auto format(const Custom& p, FormatContext& ctx) -> decltype(format_to(
ctx.out(), std::declval<typename FormatContext::char_type const*>())) {
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<fmt::format_context> 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