mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-15 20:42:27 +00:00
Parameterize Value on context
This commit is contained in:
parent
a4d6cb32d1
commit
42a319074c
21
fmt/format.h
21
fmt/format.h
@ -1098,7 +1098,7 @@ struct CustomValue {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// A formatting argument value.
|
// A formatting argument value.
|
||||||
template <typename Char>
|
template <typename Context>
|
||||||
struct Value {
|
struct Value {
|
||||||
union {
|
union {
|
||||||
int int_value;
|
int int_value;
|
||||||
@ -1111,8 +1111,8 @@ struct Value {
|
|||||||
StringValue<char> string;
|
StringValue<char> string;
|
||||||
StringValue<signed char> sstring;
|
StringValue<signed char> sstring;
|
||||||
StringValue<unsigned char> ustring;
|
StringValue<unsigned char> ustring;
|
||||||
StringValue<Char> tstring;
|
StringValue<typename Context::char_type> tstring;
|
||||||
CustomValue<Char> custom;
|
CustomValue<typename Context::char_type> custom;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1177,7 +1177,7 @@ constexpr Type type() { return gettype<typename std::decay<T>::type>(); }
|
|||||||
|
|
||||||
// Makes a format_arg object from any type.
|
// Makes a format_arg object from any type.
|
||||||
template <typename Context>
|
template <typename Context>
|
||||||
class MakeValue : public Value<typename Context::char_type> {
|
class MakeValue : public Value<Context> {
|
||||||
public:
|
public:
|
||||||
typedef typename Context::char_type Char;
|
typedef typename Context::char_type Char;
|
||||||
|
|
||||||
@ -1347,7 +1347,7 @@ class basic_format_args;
|
|||||||
template <typename Context>
|
template <typename Context>
|
||||||
class basic_format_arg {
|
class basic_format_arg {
|
||||||
private:
|
private:
|
||||||
internal::Value<typename Context::char_type> value_;
|
internal::Value<Context> value_;
|
||||||
internal::Type type_;
|
internal::Type type_;
|
||||||
|
|
||||||
template <typename ContextType, typename T>
|
template <typename ContextType, typename T>
|
||||||
@ -1514,8 +1514,7 @@ constexpr uint64_t make_type<void>() { return 0; }
|
|||||||
enum { MAX_PACKED_ARGS = 16 };
|
enum { MAX_PACKED_ARGS = 16 };
|
||||||
|
|
||||||
template <bool IS_PACKED, typename Context, typename T>
|
template <bool IS_PACKED, typename Context, typename T>
|
||||||
inline typename std::enable_if<
|
inline typename std::enable_if<IS_PACKED, Value<Context>>::type
|
||||||
IS_PACKED, Value<typename Context::char_type>>::type
|
|
||||||
make_arg(const T& value) {
|
make_arg(const T& value) {
|
||||||
return MakeValue<Context>(value);
|
return MakeValue<Context>(value);
|
||||||
}
|
}
|
||||||
@ -1538,7 +1537,7 @@ class format_arg_store {
|
|||||||
typedef typename Context::char_type char_type;
|
typedef typename Context::char_type char_type;
|
||||||
|
|
||||||
typedef typename std::conditional<IS_PACKED,
|
typedef typename std::conditional<IS_PACKED,
|
||||||
internal::Value<char_type>, basic_format_arg<Context>>::type value_type;
|
internal::Value<Context>, basic_format_arg<Context>>::type value_type;
|
||||||
|
|
||||||
// If the arguments are not packed, add one more element to mark the end.
|
// If the arguments are not packed, add one more element to mark the end.
|
||||||
typedef std::array<value_type, NUM_ARGS + (IS_PACKED ? 0 : 1)> Array;
|
typedef std::array<value_type, NUM_ARGS + (IS_PACKED ? 0 : 1)> Array;
|
||||||
@ -1582,7 +1581,7 @@ class basic_format_args {
|
|||||||
// This is done to reduce compiled code size as storing larger objects
|
// This is done to reduce compiled code size as storing larger objects
|
||||||
// may require more code (at least on x86-64) even if the same amount of
|
// may require more code (at least on x86-64) even if the same amount of
|
||||||
// data is actually copied to stack. It saves ~10% on the bloat test.
|
// data is actually copied to stack. It saves ~10% on the bloat test.
|
||||||
const internal::Value<Char> *values_;
|
const internal::Value<Context> *values_;
|
||||||
const format_arg *args_;
|
const format_arg *args_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1595,7 +1594,7 @@ class basic_format_args {
|
|||||||
|
|
||||||
friend class internal::ArgMap<Context>;
|
friend class internal::ArgMap<Context>;
|
||||||
|
|
||||||
void set_data(const internal::Value<Char> *values) { values_ = values; }
|
void set_data(const internal::Value<Context> *values) { values_ = values; }
|
||||||
void set_data(const format_arg *args) { args_ = args; }
|
void set_data(const format_arg *args) { args_ = args; }
|
||||||
|
|
||||||
format_arg get(size_type index) const {
|
format_arg get(size_type index) const {
|
||||||
@ -1603,7 +1602,7 @@ class basic_format_args {
|
|||||||
bool use_values = type(internal::MAX_PACKED_ARGS - 1) == internal::NONE;
|
bool use_values = type(internal::MAX_PACKED_ARGS - 1) == internal::NONE;
|
||||||
if (index < internal::MAX_PACKED_ARGS) {
|
if (index < internal::MAX_PACKED_ARGS) {
|
||||||
typename internal::Type arg_type = type(index);
|
typename internal::Type arg_type = type(index);
|
||||||
internal::Value<Char> &val = arg.value_;
|
internal::Value<Context> &val = arg.value_;
|
||||||
if (arg_type != internal::NONE)
|
if (arg_type != internal::NONE)
|
||||||
val = use_values ? values_[index] : args_[index].value_;
|
val = use_values ? values_[index] : args_[index].value_;
|
||||||
arg.type_ = arg_type;
|
arg.type_ = arg_type;
|
||||||
|
@ -412,19 +412,20 @@ TEST(UtilTest, FormatArgs) {
|
|||||||
EXPECT_FALSE(args[1]);
|
EXPECT_FALSE(args[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CustomFormatter {
|
struct CustomContext {
|
||||||
typedef char char_type;
|
typedef char char_type;
|
||||||
bool called;
|
bool called;
|
||||||
};
|
};
|
||||||
|
|
||||||
void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) {
|
void format_value(fmt::Writer &, const Test &, CustomContext &ctx) {
|
||||||
ctx.called = true;
|
ctx.called = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, MakeValueWithCustomFormatter) {
|
TEST(UtilTest, MakeValueWithCustomFormatter) {
|
||||||
::Test t;
|
::Test t;
|
||||||
fmt::internal::Value<char> arg = fmt::internal::MakeValue<CustomFormatter>(t);
|
fmt::internal::Value<CustomContext> arg =
|
||||||
CustomFormatter ctx = {false};
|
fmt::internal::MakeValue<CustomContext>(t);
|
||||||
|
CustomContext ctx = {false};
|
||||||
fmt::MemoryWriter w;
|
fmt::MemoryWriter w;
|
||||||
arg.custom.format(w, &t, &ctx);
|
arg.custom.format(w, &t, &ctx);
|
||||||
EXPECT_TRUE(ctx.called);
|
EXPECT_TRUE(ctx.called);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user