Addressed minor issues, removed named_arg support.
named_args will be added in a separate PR. Pending: avoid Args types from dynamic_format_arg_store template parameters, replace std::forward_list<std::variant> with a custom list.
This commit is contained in:
parent
7433fd5ae2
commit
f0984c43bc
@ -971,11 +971,6 @@ template <typename Context> struct arg_mapper {
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR const named_arg_base<char_type>& map(
|
|
||||||
const named_arg_base<char_type>& val) {
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
int map(...) {
|
int map(...) {
|
||||||
constexpr bool formattable = sizeof(Context) == 0;
|
constexpr bool formattable = sizeof(Context) == 0;
|
||||||
static_assert(
|
static_assert(
|
||||||
@ -1368,8 +1363,9 @@ template <typename Context> class basic_format_args {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Constructs a `dynamic_basic_format_args` object from
|
Constructs a `basic_format_args` object from
|
||||||
`~fmt::format_arg_store`. \endrst
|
`~fmt::dynamic_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)
|
||||||
|
|||||||
@ -85,7 +85,6 @@ class dynamic_format_arg_store
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
using char_type = typename Context::char_type;
|
using char_type = typename Context::char_type;
|
||||||
static const bool is_packed = false;
|
|
||||||
|
|
||||||
static const bool has_custom_args = (sizeof...(Args) > 0);
|
static const bool has_custom_args = (sizeof...(Args) > 0);
|
||||||
using string_type = std::basic_string<char_type>;
|
using string_type = std::basic_string<char_type>;
|
||||||
@ -115,10 +114,6 @@ class dynamic_format_arg_store
|
|||||||
|
|
||||||
std::forward_list<storage_item_type> storage_;
|
std::forward_list<storage_item_type> storage_;
|
||||||
|
|
||||||
// Storage of serialized name_args. Must grow without relocation
|
|
||||||
// because items in data_ refer to it.
|
|
||||||
std::forward_list<named_value_type> named_args_;
|
|
||||||
|
|
||||||
friend class basic_format_args<Context>;
|
friend class basic_format_args<Context>;
|
||||||
|
|
||||||
template <typename T> const T& get_last_pushed() const {
|
template <typename T> const T& get_last_pushed() const {
|
||||||
@ -167,25 +162,6 @@ class dynamic_format_arg_store
|
|||||||
template <typename T> void push_back(std::reference_wrapper<T> arg) {
|
template <typename T> void push_back(std::reference_wrapper<T> arg) {
|
||||||
emplace_arg(arg.get());
|
emplace_arg(arg.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
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>{});
|
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|||||||
@ -89,41 +89,3 @@ TEST(FormatDynArgsTest, NamedArgByRef) {
|
|||||||
|
|
||||||
EXPECT_EQ("42", result);
|
EXPECT_EQ("42", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatDynArgsTest, NamedInt) {
|
|
||||||
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
|
||||||
store.push_back(fmt::arg("a1", 42));
|
|
||||||
std::string result = fmt::vformat("{a1}", store);
|
|
||||||
EXPECT_EQ("42", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(FormatDynArgsTest, NamedStrings) {
|
|
||||||
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
|
||||||
char str[]{"1234567890"};
|
|
||||||
store.push_back(fmt::arg("a1", str));
|
|
||||||
store.push_back(fmt::arg("a2", std::cref(str)));
|
|
||||||
str[0] = 'X';
|
|
||||||
|
|
||||||
std::string result = fmt::vformat("{a1} and {a2}", store);
|
|
||||||
|
|
||||||
EXPECT_EQ("1234567890 and X234567890", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef FMT_HAS_VARIANT
|
|
||||||
|
|
||||||
TEST(FormatDynArgsTest, NamedCustomFormat) {
|
|
||||||
fmt::dynamic_format_arg_store<fmt::format_context, Custom> store;
|
|
||||||
Custom c{};
|
|
||||||
store.push_back(fmt::arg("a1", c));
|
|
||||||
++c.i;
|
|
||||||
store.push_back(fmt::arg("a2", c));
|
|
||||||
++c.i;
|
|
||||||
store.push_back(fmt::arg("a3", std::cref(c)));
|
|
||||||
++c.i;
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user