Clang-format applied.
This commit is contained in:
parent
448fb74633
commit
5930040c99
@ -259,7 +259,8 @@ namespace internal {
|
|||||||
// A workaround for gcc 4.8 to make void_t work in a SFINAE context.
|
// 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; };
|
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
|
#ifndef FMT_ASSERT
|
||||||
# ifdef NDEBUG
|
# ifdef NDEBUG
|
||||||
@ -970,7 +971,7 @@ template <typename Context> struct arg_mapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR const named_arg_base<char_type>& map(
|
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;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1366,8 +1367,8 @@ template <typename Context> class basic_format_args {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Constructs a `dynamic_basic_format_args` object from `~fmt::format_arg_store`.
|
Constructs a `dynamic_basic_format_args` object from
|
||||||
\endrst
|
`~fmt::format_arg_store`. \endrst
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
basic_format_args(const dynamic_format_arg_store<Context, Args...>& store)
|
basic_format_args(const dynamic_format_arg_store<Context, Args...>& store)
|
||||||
|
|||||||
@ -4,9 +4,9 @@
|
|||||||
#ifndef FMT_DYN_ARGS_H_
|
#ifndef FMT_DYN_ARGS_H_
|
||||||
#define FMT_DYN_ARGS_H_
|
#define FMT_DYN_ARGS_H_
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
|
|
||||||
@ -21,53 +21,46 @@ FMT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
template<typename T, typename Char>
|
template <typename T, typename Char> struct is_string_view : std::false_type {};
|
||||||
struct is_string_view : std::false_type{};
|
|
||||||
|
|
||||||
template<typename Char>
|
template <typename Char>
|
||||||
struct is_string_view<basic_string_view<Char>, Char>
|
struct is_string_view<basic_string_view<Char>, Char> : std::true_type {};
|
||||||
: std::true_type{};
|
|
||||||
|
|
||||||
#ifdef FMT_USE_STRING_VIEW
|
#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>
|
struct is_string_view<std::basic_string_view<Char, Traits>, Char>
|
||||||
: std::true_type{};
|
: std::true_type {};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef FMT_USE_EXPERIMENTAL_STRING_VIEW
|
#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>
|
struct is_string_view<std::experimental::basic_string_view<Char, Traits>, Char>
|
||||||
: std::true_type{};
|
: std::true_type {};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> struct is_ref_wrapper : std::false_type {};
|
||||||
struct is_ref_wrapper : std::false_type{};
|
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
struct is_ref_wrapper<std::reference_wrapper<T>> : std::true_type{};
|
struct is_ref_wrapper<std::reference_wrapper<T>> : std::true_type {};
|
||||||
|
|
||||||
template<typename T, typename Context>
|
template <typename T, typename Context> struct need_dyn_copy {
|
||||||
struct need_dyn_copy{
|
|
||||||
using mapped_type = mapped_type_constant<T, Context>;
|
using mapped_type = mapped_type_constant<T, Context>;
|
||||||
static_assert(mapped_type::value != internal::type::named_arg_type,
|
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,!(
|
using type = std::integral_constant<
|
||||||
is_ref_wrapper<T>::value ||
|
bool, !(is_ref_wrapper<T>::value ||
|
||||||
is_string_view<T, typename Context::char_type>::value ||
|
is_string_view<T, typename Context::char_type>::value ||
|
||||||
(
|
(mapped_type::value != internal::type::cstring_type &&
|
||||||
mapped_type::value != internal::type::cstring_type &&
|
mapped_type::value != internal::type::custom_type &&
|
||||||
mapped_type::value != internal::type::custom_type &&
|
mapped_type::value != internal::type::string_type))>;
|
||||||
mapped_type::value != internal::type::string_type
|
|
||||||
)
|
|
||||||
)>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename Context> using need_dyn_copy_t =
|
template <typename T, typename Context>
|
||||||
typename need_dyn_copy<T, Context>::type;
|
using need_dyn_copy_t = typename need_dyn_copy<T, Context>::type;
|
||||||
|
|
||||||
template<typename T, typename StorageValue>
|
template <typename T, typename StorageValue>
|
||||||
const T& get(const StorageValue& v){
|
const T& get(const StorageValue& v) {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,18 +91,18 @@ class dynamic_format_arg_store
|
|||||||
using string_type = std::basic_string<char_type>;
|
using string_type = std::basic_string<char_type>;
|
||||||
#ifdef FMT_HAS_VARIANT
|
#ifdef FMT_HAS_VARIANT
|
||||||
using storage_item_type =
|
using storage_item_type =
|
||||||
conditional_t<has_custom_args,
|
conditional_t<has_custom_args, std::variant<string_type, Args...>,
|
||||||
std::variant<string_type, Args...>,
|
|
||||||
string_type>;
|
string_type>;
|
||||||
#else
|
#else
|
||||||
static_assert(!has_custom_args, "std::variant<> is required to support "
|
static_assert(!has_custom_args,
|
||||||
"custom types in dynamic_format_arg_store");
|
"std::variant<> is required to support "
|
||||||
|
"custom types in dynamic_format_arg_store");
|
||||||
using storage_item_type = string_type;
|
using storage_item_type = string_type;
|
||||||
#endif
|
#endif
|
||||||
using value_type = basic_format_arg<Context>;
|
using value_type = basic_format_arg<Context>;
|
||||||
using named_value_type = internal::named_arg_base<char_type>;
|
using named_value_type = internal::named_arg_base<char_type>;
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
using storaged_type =
|
using storaged_type =
|
||||||
conditional_t<internal::is_string<T>::value, string_type, T>;
|
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>;
|
friend class basic_format_args<Context>;
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> const T& get_last_pushed() const {
|
||||||
const T& get_last_pushed() const{
|
|
||||||
using internal::get;
|
using internal::get;
|
||||||
return get<T>(storage_.front());
|
return get<T>(storage_.front());
|
||||||
}
|
}
|
||||||
@ -138,23 +130,23 @@ class dynamic_format_arg_store
|
|||||||
return internal::is_unpacked_bit | data_.size();
|
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;
|
return arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> const T& stored_value(
|
template <typename T>
|
||||||
const std::reference_wrapper<T>& arg, std::false_type) {
|
const T& stored_value(const std::reference_wrapper<T>& arg, std::false_type) {
|
||||||
return arg.get();
|
return arg.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> const storaged_type<T>& stored_value(const T& arg,
|
template <typename T>
|
||||||
std::true_type) {
|
const storaged_type<T>& stored_value(const T& arg, std::true_type) {
|
||||||
using type = storaged_type<T>;
|
using type = storaged_type<T>;
|
||||||
storage_.emplace_front(type{arg});
|
storage_.emplace_front(type{arg});
|
||||||
return get_last_pushed<type>();
|
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));
|
data_.emplace_back(internal::make_arg<Context>(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,23 +168,23 @@ class dynamic_format_arg_store
|
|||||||
emplace_arg(arg.get());
|
emplace_arg(arg.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void push_back(
|
template <typename T>
|
||||||
const internal::named_arg<T, char_type>& arg) {
|
void push_back(const internal::named_arg<T, char_type>& arg) {
|
||||||
// Named argument is tricky. It's returned by value from fmt::arg()
|
// Named argument is tricky. It's returned by value from fmt::arg()
|
||||||
// and then pointer to it is stored in basic_format_arg<>.
|
// and then pointer to it is stored in basic_format_arg<>.
|
||||||
// So after end of expression the pointer becomes dangling.
|
// So after end of expression the pointer becomes dangling.
|
||||||
storage_.emplace_front(string_type{arg.name.data(), arg.name.size()});
|
storage_.emplace_front(string_type{arg.name.data(), arg.name.size()});
|
||||||
basic_string_view<char_type> name = get_last_pushed<string_type>();
|
basic_string_view<char_type> name = get_last_pushed<string_type>();
|
||||||
const auto& val = stored_value(
|
const auto& val =
|
||||||
arg.value, internal::need_dyn_copy_t<T, Context>{});
|
stored_value(arg.value, internal::need_dyn_copy_t<T, Context>{});
|
||||||
|
|
||||||
auto named_with_stored_parts = fmt::arg(name, val);
|
auto named_with_stored_parts = fmt::arg(name, val);
|
||||||
// Serialize value into base
|
// Serialize value into base
|
||||||
internal::arg_mapper<Context>().map(named_with_stored_parts);
|
internal::arg_mapper<Context>().map(named_with_stored_parts);
|
||||||
named_args_.push_front(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<Context>(named_args_.front()));
|
||||||
// data_.emplace_back(internal::make_arg_from_serialized_named<Context>(
|
// data_.emplace_back(internal::make_arg_from_serialized_named<Context>(
|
||||||
// named_args_.front()));
|
// named_args_.front()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -5,16 +5,13 @@
|
|||||||
|
|
||||||
#include "gtest-extra.h"
|
#include "gtest-extra.h"
|
||||||
|
|
||||||
|
|
||||||
TEST(FormatDynArgsTest, Basic) {
|
TEST(FormatDynArgsTest, Basic) {
|
||||||
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
||||||
store.push_back(42);
|
store.push_back(42);
|
||||||
store.push_back("abc1");
|
store.push_back("abc1");
|
||||||
store.push_back(1.5f);
|
store.push_back(1.5f);
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{} and {} and {}", store);
|
||||||
"{} and {} and {}",
|
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("42 and abc1 and 1.5", result);
|
EXPECT_EQ("42 and abc1 and 1.5", result);
|
||||||
}
|
}
|
||||||
@ -29,31 +26,26 @@ TEST(FormatDynArgsTest, StringsAndRefs) {
|
|||||||
store.push_back(fmt::string_view{str});
|
store.push_back(fmt::string_view{str});
|
||||||
str[0] = 'X';
|
str[0] = 'X';
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{} and {} and {}", store);
|
||||||
"{} and {} and {}",
|
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("1234567890 and X234567890 and X234567890", result);
|
EXPECT_EQ("1234567890 and X234567890 and X234567890", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Custom{ int i{0}; };
|
struct Custom {
|
||||||
|
int i{0};
|
||||||
|
};
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template <>
|
template <> struct formatter<Custom> {
|
||||||
struct formatter<Custom> {
|
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin()) {
|
||||||
auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
|
|
||||||
{
|
|
||||||
return ctx.begin();
|
return ctx.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const Custom& p, FormatContext& ctx) ->
|
auto format(const Custom& p, FormatContext& ctx) -> decltype(format_to(
|
||||||
decltype(format_to(ctx.out(),
|
ctx.out(), std::declval<typename FormatContext::char_type const*>())) {
|
||||||
std::declval<typename FormatContext::char_type const*>())) {
|
return format_to(ctx.out(), "cust={}", p.i);
|
||||||
return format_to(
|
}
|
||||||
ctx.out(),
|
|
||||||
"cust={}", p.i);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
@ -69,14 +61,12 @@ TEST(FormatDynArgsTest, CustomFormat) {
|
|||||||
store.push_back(std::cref(c));
|
store.push_back(std::cref(c));
|
||||||
++c.i;
|
++c.i;
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{} and {} and {}", store);
|
||||||
"{} and {} and {}",
|
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
|
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // FMT_HAS_VARIANT
|
#endif // FMT_HAS_VARIANT
|
||||||
|
|
||||||
TEST(FormatDynArgsTest, NamedArgByRef) {
|
TEST(FormatDynArgsTest, NamedArgByRef) {
|
||||||
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
||||||
@ -88,15 +78,14 @@ TEST(FormatDynArgsTest, NamedArgByRef) {
|
|||||||
// complains about this.
|
// complains about this.
|
||||||
//
|
//
|
||||||
// A real life usecase is when you have both name and value alive
|
// 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.
|
// storages.
|
||||||
int a1_val{42};
|
int a1_val{42};
|
||||||
auto a1 = fmt::arg("a1_", a1_val);
|
auto a1 = fmt::arg("a1_", a1_val);
|
||||||
store.push_back(std::cref(a1));
|
store.push_back(std::cref(a1));
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{a1_}", // and {} and {}",
|
||||||
"{a1_}", // and {} and {}",
|
store);
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("42", result);
|
EXPECT_EQ("42", result);
|
||||||
}
|
}
|
||||||
@ -115,9 +104,7 @@ TEST(FormatDynArgsTest, NamedStrings) {
|
|||||||
store.push_back(fmt::arg("a2", std::cref(str)));
|
store.push_back(fmt::arg("a2", std::cref(str)));
|
||||||
str[0] = 'X';
|
str[0] = 'X';
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{a1} and {a2}", store);
|
||||||
"{a1} and {a2}",
|
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("1234567890 and X234567890", result);
|
EXPECT_EQ("1234567890 and X234567890", result);
|
||||||
}
|
}
|
||||||
@ -134,11 +121,9 @@ TEST(FormatDynArgsTest, NamedCustomFormat) {
|
|||||||
store.push_back(fmt::arg("a3", std::cref(c)));
|
store.push_back(fmt::arg("a3", std::cref(c)));
|
||||||
++c.i;
|
++c.i;
|
||||||
|
|
||||||
std::string result = fmt::vformat(
|
std::string result = fmt::vformat("{a1} and {a2} and {a3}", store);
|
||||||
"{a1} and {a2} and {a3}",
|
|
||||||
store);
|
|
||||||
|
|
||||||
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
|
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // FMT_HAS_VARIANT
|
#endif // FMT_HAS_VARIANT
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user