Parameterize Value on context

This commit is contained in:
Victor Zverovich 2016-12-29 18:07:05 -08:00
parent a4d6cb32d1
commit 42a319074c
2 changed files with 15 additions and 15 deletions

View File

@ -1098,7 +1098,7 @@ struct CustomValue {
};
// A formatting argument value.
template <typename Char>
template <typename Context>
struct Value {
union {
int int_value;
@ -1111,8 +1111,8 @@ struct Value {
StringValue<char> string;
StringValue<signed char> sstring;
StringValue<unsigned char> ustring;
StringValue<Char> tstring;
CustomValue<Char> custom;
StringValue<typename Context::char_type> tstring;
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.
template <typename Context>
class MakeValue : public Value<typename Context::char_type> {
class MakeValue : public Value<Context> {
public:
typedef typename Context::char_type Char;
@ -1347,7 +1347,7 @@ class basic_format_args;
template <typename Context>
class basic_format_arg {
private:
internal::Value<typename Context::char_type> value_;
internal::Value<Context> value_;
internal::Type type_;
template <typename ContextType, typename T>
@ -1514,8 +1514,7 @@ constexpr uint64_t make_type<void>() { return 0; }
enum { MAX_PACKED_ARGS = 16 };
template <bool IS_PACKED, typename Context, typename T>
inline typename std::enable_if<
IS_PACKED, Value<typename Context::char_type>>::type
inline typename std::enable_if<IS_PACKED, Value<Context>>::type
make_arg(const T& value) {
return MakeValue<Context>(value);
}
@ -1538,7 +1537,7 @@ class format_arg_store {
typedef typename Context::char_type char_type;
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.
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
// 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.
const internal::Value<Char> *values_;
const internal::Value<Context> *values_;
const format_arg *args_;
};
@ -1595,7 +1594,7 @@ class basic_format_args {
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; }
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;
if (index < internal::MAX_PACKED_ARGS) {
typename internal::Type arg_type = type(index);
internal::Value<Char> &val = arg.value_;
internal::Value<Context> &val = arg.value_;
if (arg_type != internal::NONE)
val = use_values ? values_[index] : args_[index].value_;
arg.type_ = arg_type;

View File

@ -412,19 +412,20 @@ TEST(UtilTest, FormatArgs) {
EXPECT_FALSE(args[1]);
}
struct CustomFormatter {
struct CustomContext {
typedef char char_type;
bool called;
};
void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) {
void format_value(fmt::Writer &, const Test &, CustomContext &ctx) {
ctx.called = true;
}
TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t;
fmt::internal::Value<char> arg = fmt::internal::MakeValue<CustomFormatter>(t);
CustomFormatter ctx = {false};
fmt::internal::Value<CustomContext> arg =
fmt::internal::MakeValue<CustomContext>(t);
CustomContext ctx = {false};
fmt::MemoryWriter w;
arg.custom.format(w, &t, &ctx);
EXPECT_TRUE(ctx.called);