Get rid of fmt::internal::Arg

This commit is contained in:
Victor Zverovich 2016-11-26 10:21:31 -08:00
parent 5f022ae081
commit 9cf6c8fdc6
5 changed files with 152 additions and 137 deletions

View File

@ -51,8 +51,6 @@
# endif
#endif
using fmt::internal::Arg;
#if FMT_EXCEPTIONS
# define FMT_TRY try
# define FMT_CATCH(x) catch (x)

View File

@ -1039,8 +1039,6 @@ struct format_arg : internal::Value {
namespace internal {
typedef format_arg Arg;
template <typename Char>
struct NamedArg;
@ -1180,61 +1178,80 @@ typedef Value::Type Type;
template <typename T>
constexpr Type gettype() {
typedef format_arg Arg;
return IsNamedArg<T>::value ?
Arg::NAMED_ARG : (ConvertToInt<T>::value ? Arg::INT : Arg::CUSTOM);
}
template <> constexpr Type gettype<bool>() { return Arg::BOOL; }
template <> constexpr Type gettype<short>() { return Arg::INT; }
template <> constexpr Type gettype<unsigned short>() { return Arg::UINT; }
template <> constexpr Type gettype<int>() { return Arg::INT; }
template <> constexpr Type gettype<unsigned>() { return Arg::UINT; }
template <> constexpr Type gettype<bool>() { return format_arg::BOOL; }
template <> constexpr Type gettype<short>() { return format_arg::INT; }
template <> constexpr Type gettype<unsigned short>() {
return format_arg::UINT;
}
template <> constexpr Type gettype<int>() { return format_arg::INT; }
template <> constexpr Type gettype<unsigned>() { return format_arg::UINT; }
template <> constexpr Type gettype<long>() {
return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG;
return sizeof(long) == sizeof(int) ? format_arg::INT : format_arg::LONG_LONG;
}
template <> constexpr Type gettype<unsigned long>() {
return sizeof(unsigned long) == sizeof(unsigned) ?
Arg::UINT : Arg::ULONG_LONG;
format_arg::UINT : format_arg::ULONG_LONG;
}
template <> constexpr Type gettype<LongLong>() { return Arg::LONG_LONG; }
template <> constexpr Type gettype<ULongLong>() { return Arg::ULONG_LONG; }
template <> constexpr Type gettype<float>() { return Arg::DOUBLE; }
template <> constexpr Type gettype<double>() { return Arg::DOUBLE; }
template <> constexpr Type gettype<long double>() { return Arg::LONG_DOUBLE; }
template <> constexpr Type gettype<signed char>() { return Arg::INT; }
template <> constexpr Type gettype<unsigned char>() { return Arg::UINT; }
template <> constexpr Type gettype<char>() { return Arg::CHAR; }
template <> constexpr Type gettype<LongLong>() { return format_arg::LONG_LONG; }
template <> constexpr Type gettype<ULongLong>() {
return format_arg::ULONG_LONG;
}
template <> constexpr Type gettype<float>() { return format_arg::DOUBLE; }
template <> constexpr Type gettype<double>() { return format_arg::DOUBLE; }
template <> constexpr Type gettype<long double>() {
return format_arg::LONG_DOUBLE;
}
template <> constexpr Type gettype<signed char>() { return format_arg::INT; }
template <> constexpr Type gettype<unsigned char>() { return format_arg::UINT; }
template <> constexpr Type gettype<char>() { return format_arg::CHAR; }
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
template <> constexpr Type gettype<wchar_t>() { return Arg::CHAR; }
template <> constexpr Type gettype<wchar_t>() { return format_arg::CHAR; }
#endif
template <> constexpr Type gettype<char *>() { return Arg::CSTRING; }
template <> constexpr Type gettype<const char *>() { return Arg::CSTRING; }
template <> constexpr Type gettype<signed char *>() { return Arg::CSTRING; }
template <> constexpr Type gettype<char *>() { return format_arg::CSTRING; }
template <> constexpr Type gettype<const char *>() {
return format_arg::CSTRING;
}
template <> constexpr Type gettype<signed char *>() {
return format_arg::CSTRING;
}
template <> constexpr Type gettype<const signed char *>() {
return Arg::CSTRING;
return format_arg::CSTRING;
}
template <> constexpr Type gettype<unsigned char *>() {
return format_arg::CSTRING;
}
template <> constexpr Type gettype<unsigned char *>() { return Arg::CSTRING; }
template <> constexpr Type gettype<const unsigned char *>() {
return Arg::CSTRING;
return format_arg::CSTRING;
}
template <> constexpr Type gettype<std::string>() { return format_arg::STRING; }
template <> constexpr Type gettype<StringRef>() { return format_arg::STRING; }
template <> constexpr Type gettype<CStringRef>() { return format_arg::CSTRING; }
template <> constexpr Type gettype<wchar_t *>() { return format_arg::WSTRING; }
template <> constexpr Type gettype<const wchar_t *>() {
return format_arg::WSTRING;
}
template <> constexpr Type gettype<std::wstring>() {
return format_arg::WSTRING;
}
template <> constexpr Type gettype<WStringRef>() { return format_arg::WSTRING; }
template <> constexpr Type gettype<void *>() { return format_arg::POINTER; }
template <> constexpr Type gettype<const void *>() {
return format_arg::POINTER;
}
template <> constexpr Type gettype<std::string>() { return Arg::STRING; }
template <> constexpr Type gettype<StringRef>() { return Arg::STRING; }
template <> constexpr Type gettype<CStringRef>() { return Arg::CSTRING; }
template <> constexpr Type gettype<wchar_t *>() { return Arg::WSTRING; }
template <> constexpr Type gettype<const wchar_t *>() { return Arg::WSTRING; }
template <> constexpr Type gettype<std::wstring>() { return Arg::WSTRING; }
template <> constexpr Type gettype<WStringRef>() { return Arg::WSTRING; }
template <> constexpr Type gettype<void *>() { return Arg::POINTER; }
template <> constexpr Type gettype<const void *>() { return Arg::POINTER; }
template <typename T>
constexpr Type type() { return gettype<typename std::decay<T>::type>(); }
// Makes an Arg object from any type.
// Makes a format_arg object from any type.
template <typename Context>
class MakeValue : public Arg {
class MakeValue : public format_arg {
public:
typedef typename Context::char_type Char;
@ -1387,27 +1404,27 @@ class MakeValue : public Arg {
}
};
template <typename Formatter>
class MakeArg : public Arg {
template <typename Context>
class MakeArg : public format_arg {
public:
MakeArg() {
type = Arg::NONE;
type = format_arg::NONE;
}
template <typename T>
MakeArg(const T &value)
: Arg(MakeValue<Formatter>(value)) {
: format_arg(MakeValue<Context>(value)) {
type = internal::type<T>();
}
};
template <typename Char>
struct NamedArg : Arg {
struct NamedArg : format_arg {
BasicStringRef<Char> name;
template <typename T>
NamedArg(BasicStringRef<Char> argname, const T &value)
: Arg(MakeArg< basic_format_context<Char> >(value)), name(argname) {}
: format_arg(MakeArg< basic_format_context<Char> >(value)), name(argname) {}
};
class RuntimeError : public std::runtime_error {
@ -1438,7 +1455,7 @@ class format_arg_store {
static const bool IS_PACKED = NUM_ARGS <= internal::MAX_PACKED_ARGS;
typedef typename std::conditional<
IS_PACKED, internal::Value, internal::Arg>::type value_type;
IS_PACKED, internal::Value, format_arg>::type value_type;
// If the arguments are not packed, add one more element to mark the end.
std::array<value_type, NUM_ARGS + (IS_PACKED ? 0 : 1)> data_;
@ -1805,7 +1822,7 @@ template <typename Char>
class ArgMap {
private:
typedef std::vector<
std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
std::pair<fmt::BasicStringRef<Char>, format_arg> > MapType;
typedef typename MapType::value_type Pair;
MapType map_;
@ -1814,7 +1831,7 @@ class ArgMap {
template <typename Formatter>
void init(const basic_format_args<Formatter> &args);
const internal::Arg* find(const fmt::BasicStringRef<Char> &name) const {
const format_arg *find(const fmt::BasicStringRef<Char> &name) const {
// The list is unsorted, so just return the first matching name.
for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
it != end; ++it) {
@ -1833,14 +1850,14 @@ void ArgMap<Char>::init(const basic_format_args<Formatter> &args) {
typedef internal::NamedArg<Char> NamedArg;
const NamedArg *named_arg = 0;
bool use_values =
args.type(MAX_PACKED_ARGS - 1) == internal::Arg::NONE;
args.type(MAX_PACKED_ARGS - 1) == format_arg::NONE;
if (use_values) {
for (unsigned i = 0;/*nothing*/; ++i) {
internal::Arg::Type arg_type = args.type(i);
format_arg::Type arg_type = args.type(i);
switch (arg_type) {
case internal::Arg::NONE:
case format_arg::NONE:
return;
case internal::Arg::NAMED_ARG:
case format_arg::NAMED_ARG:
named_arg = static_cast<const NamedArg*>(args.values_[i].pointer);
map_.push_back(Pair(named_arg->name, *named_arg));
break;
@ -1851,17 +1868,17 @@ void ArgMap<Char>::init(const basic_format_args<Formatter> &args) {
return;
}
for (unsigned i = 0; i != MAX_PACKED_ARGS; ++i) {
internal::Arg::Type arg_type = args.type(i);
if (arg_type == internal::Arg::NAMED_ARG) {
format_arg::Type arg_type = args.type(i);
if (arg_type == format_arg::NAMED_ARG) {
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.push_back(Pair(named_arg->name, *named_arg));
}
}
for (unsigned i = MAX_PACKED_ARGS;/*nothing*/; ++i) {
switch (args.args_[i].type) {
case internal::Arg::NONE:
case format_arg::NONE:
return;
case internal::Arg::NAMED_ARG:
case format_arg::NAMED_ARG:
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.push_back(Pair(named_arg->name, *named_arg));
break;
@ -1886,7 +1903,7 @@ class ArgFormatterBase {
}
template <typename StrChar>
void write_str(Arg::StringValue<StrChar> value,
void write_str(format_arg::StringValue<StrChar> value,
typename EnableIf<
std::is_same<Char, wchar_t>::value &&
std::is_same<StrChar, wchar_t>::value, int>::type = 0) {
@ -1894,7 +1911,7 @@ class ArgFormatterBase {
}
template <typename StrChar>
void write_str(Arg::StringValue<StrChar> value,
void write_str(format_arg::StringValue<StrChar> value,
typename EnableIf<
!std::is_same<Char, wchar_t>::value ||
!std::is_same<StrChar, wchar_t>::value, int>::type = 0) {
@ -1907,12 +1924,14 @@ class ArgFormatterBase {
void write(bool value) {
const char *str_value = value ? "true" : "false";
Arg::StringValue<char> str = { str_value, std::strlen(str_value) };
format_arg::StringValue<char> str = { str_value, std::strlen(str_value) };
writer_.write_str(str, spec_);
}
void write(const char *value) {
Arg::StringValue<char> str = {value, value != 0 ? std::strlen(value) : 0};
format_arg::StringValue<char> str = {
value, value != 0 ? std::strlen(value) : 0
};
writer_.write_str(str, spec_);
}
@ -1972,11 +1991,11 @@ class ArgFormatterBase {
write(value);
}
void operator()(Arg::StringValue<char> value) {
void operator()(format_arg::StringValue<char> value) {
writer_.write_str(value, spec_);
}
void operator()(Arg::StringValue<wchar_t> value) {
void operator()(format_arg::StringValue<wchar_t> value) {
write_str(value);
}
@ -2273,7 +2292,7 @@ class BasicWriter {
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
template <typename StrChar>
void write_str(const internal::Arg::StringValue<StrChar> &str,
void write_str(const format_arg::StringValue<StrChar> &str,
const FormatSpec &spec);
// This following methods are private to disallow writing wide characters
@ -2497,7 +2516,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
template <typename Char>
template <typename StrChar>
void BasicWriter<Char>::write_str(
const internal::Arg::StringValue<StrChar> &s, const FormatSpec &spec) {
const format_arg::StringValue<StrChar> &s, const FormatSpec &spec) {
// Check if StrChar is convertible to Char.
internal::CharTraits<Char>::convert(StrChar());
if (spec.type_ && spec.type_ != 's')
@ -3262,8 +3281,8 @@ unsigned parse_nonnegative_int(const Char *&s) {
return value;
}
inline void require_numeric_argument(const Arg &arg, char spec) {
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
inline void require_numeric_argument(const format_arg &arg, char spec) {
if (arg.type > format_arg::LAST_NUMERIC_TYPE) {
std::string message =
fmt::format("format specifier '{}' requires numeric argument", spec);
FMT_THROW(fmt::format_error(message));
@ -3271,10 +3290,10 @@ inline void require_numeric_argument(const Arg &arg, char spec) {
}
template <typename Char>
void check_sign(const Char *&s, const Arg &arg) {
void check_sign(const Char *&s, const format_arg &arg) {
char sign = static_cast<char>(*s);
require_numeric_argument(arg, sign);
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
if (arg.type == format_arg::UINT || arg.type == format_arg::ULONG_LONG) {
FMT_THROW(format_error(fmt::format(
"format specifier '{}' requires signed argument", sign)));
}
@ -3287,7 +3306,7 @@ inline format_arg basic_format_context<Char>::get_arg(
BasicStringRef<Char> name, const char *&error) {
if (this->check_no_auto_index(error)) {
map_.init(this->args());
const internal::Arg *arg = map_.find(name);
const format_arg *arg = map_.find(name);
if (arg)
return *arg;
error = "argument not found";
@ -3322,13 +3341,12 @@ inline format_arg basic_format_context<Char>::parse_arg_id() {
// Formats a single argument.
template <typename ArgFormatter, typename Char, typename Context>
void do_format_arg(BasicWriter<Char> &writer, const internal::Arg &arg,
void do_format_arg(BasicWriter<Char> &writer, const format_arg &arg,
Context &ctx) {
using internal::Arg;
const Char *&s = ctx.ptr();
FormatSpec spec;
if (*s == ':') {
if (arg.type == Arg::CUSTOM) {
if (arg.type == format_arg::CUSTOM) {
arg.custom.format(&writer, arg.custom.value, &ctx);
return;
}
@ -3402,25 +3420,25 @@ void do_format_arg(BasicWriter<Char> &writer, const internal::Arg &arg,
spec.width_ = internal::parse_nonnegative_int(s);
} else if (*s == '{') {
++s;
Arg width_arg = ctx.parse_arg_id();
format_arg width_arg = ctx.parse_arg_id();
if (*s++ != '}')
FMT_THROW(format_error("invalid format string"));
ULongLong value = 0;
switch (width_arg.type) {
case Arg::INT:
case format_arg::INT:
if (width_arg.int_value < 0)
FMT_THROW(format_error("negative width"));
value = width_arg.int_value;
break;
case Arg::UINT:
case format_arg::UINT:
value = width_arg.uint_value;
break;
case Arg::LONG_LONG:
case format_arg::LONG_LONG:
if (width_arg.long_long_value < 0)
FMT_THROW(format_error("negative width"));
value = width_arg.long_long_value;
break;
case Arg::ULONG_LONG:
case format_arg::ULONG_LONG:
value = width_arg.ulong_long_value;
break;
default:
@ -3439,25 +3457,25 @@ void do_format_arg(BasicWriter<Char> &writer, const internal::Arg &arg,
spec.precision_ = internal::parse_nonnegative_int(s);
} else if (*s == '{') {
++s;
Arg precision_arg = ctx.parse_arg_id();
format_arg precision_arg = ctx.parse_arg_id();
if (*s++ != '}')
FMT_THROW(format_error("invalid format string"));
ULongLong value = 0;
switch (precision_arg.type) {
case Arg::INT:
case format_arg::INT:
if (precision_arg.int_value < 0)
FMT_THROW(format_error("negative precision"));
value = precision_arg.int_value;
break;
case Arg::UINT:
case format_arg::UINT:
value = precision_arg.uint_value;
break;
case Arg::LONG_LONG:
case format_arg::LONG_LONG:
if (precision_arg.long_long_value < 0)
FMT_THROW(format_error("negative precision"));
value = precision_arg.long_long_value;
break;
case Arg::ULONG_LONG:
case format_arg::ULONG_LONG:
value = precision_arg.ulong_long_value;
break;
default:
@ -3469,10 +3487,11 @@ void do_format_arg(BasicWriter<Char> &writer, const internal::Arg &arg,
} else {
FMT_THROW(format_error("missing precision specifier"));
}
if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
if (arg.type <= format_arg::LAST_INTEGER_TYPE ||
arg.type == format_arg::POINTER) {
FMT_THROW(format_error(
fmt::format("precision not allowed in {} format specifier",
arg.type == Arg::POINTER ? "pointer" : "integer")));
arg.type == format_arg::POINTER ? "pointer" : "integer")));
}
}

View File

@ -83,11 +83,11 @@ struct is_same<T, T> {
template <typename T>
class ArgConverter {
private:
internal::Arg &arg_;
format_arg &arg_;
wchar_t type_;
public:
ArgConverter(internal::Arg &arg, wchar_t type)
ArgConverter(format_arg &arg, wchar_t type)
: arg_(arg), type_(type) {}
void operator()(bool value) {
@ -99,28 +99,27 @@ class ArgConverter {
typename std::enable_if<std::is_integral<U>::value>::type
operator()(U value) {
bool is_signed = type_ == 'd' || type_ == 'i';
using internal::Arg;
typedef typename internal::Conditional<
is_same<T, void>::value, U, T>::type TargetType;
if (sizeof(TargetType) <= sizeof(int)) {
// Extra casts are used to silence warnings.
if (is_signed) {
arg_.type = Arg::INT;
arg_.type = format_arg::INT;
arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
} else {
arg_.type = Arg::UINT;
arg_.type = format_arg::UINT;
typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
}
} else {
if (is_signed) {
arg_.type = Arg::LONG_LONG;
arg_.type = format_arg::LONG_LONG;
// glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254"
// but we don't have to do the same because it's a UB.
arg_.long_long_value = static_cast<LongLong>(value);
} else {
arg_.type = Arg::ULONG_LONG;
arg_.type = format_arg::ULONG_LONG;
arg_.ulong_long_value =
static_cast<typename internal::MakeUnsigned<U>::Type>(value);
}
@ -146,17 +145,17 @@ void convert_arg(format_arg &arg, wchar_t type) {
// Converts an integer argument to char for printf.
class CharConverter {
private:
internal::Arg &arg_;
format_arg &arg_;
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
public:
explicit CharConverter(internal::Arg &arg) : arg_(arg) {}
explicit CharConverter(format_arg &arg) : arg_(arg) {}
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
operator()(T value) {
arg_.type = internal::Arg::CHAR;
arg_.type = format_arg::CHAR;
arg_.int_value = static_cast<char>(value);
}
@ -281,7 +280,7 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
}
/** Formats an argument of a custom (user-defined) type. */
void operator()(internal::Arg::CustomValue c) {
void operator()(format_arg::CustomValue c) {
const Char format_str[] = {'}', '\0'};
auto args = basic_format_args<basic_format_context<Char>>();
basic_format_context<Char> ctx(format_str, args);
@ -306,7 +305,7 @@ class printf_context :
// Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument.
internal::Arg get_arg(
format_arg get_arg(
const Char *s,
unsigned arg_index = (std::numeric_limits<unsigned>::max)());
@ -356,11 +355,11 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
}
template <typename Char, typename AF>
internal::Arg printf_context<Char, AF>::get_arg(const Char *s,
format_arg printf_context<Char, AF>::get_arg(const Char *s,
unsigned arg_index) {
(void)s;
const char *error = 0;
internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
format_arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
this->next_arg(error) : Base::get_arg(arg_index - 1, error);
if (error)
FMT_THROW(format_error(!*s ? "invalid format string" : error));
@ -432,12 +431,11 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
}
}
using internal::Arg;
Arg arg = get_arg(s, arg_index);
format_arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') {
if (arg.type <= Arg::LAST_NUMERIC_TYPE)
if (arg.type <= format_arg::LAST_NUMERIC_TYPE)
spec.align_ = ALIGN_NUMERIC;
else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
@ -480,7 +478,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
if (!*s)
FMT_THROW(format_error("invalid format string"));
spec.type_ = static_cast<char>(*s++);
if (arg.type <= Arg::LAST_INTEGER_TYPE) {
if (arg.type <= format_arg::LAST_INTEGER_TYPE) {
// Normalize type.
switch (spec.type_) {
case 'i': case 'u':

View File

@ -42,12 +42,12 @@
#undef max
TEST(FormatTest, ArgConverter) {
using fmt::internal::Arg;
Arg arg = Arg();
arg.type = Arg::LONG_LONG;
using fmt::format_arg;
format_arg arg = format_arg();
arg.type = format_arg::LONG_LONG;
arg.long_long_value = std::numeric_limits<fmt::LongLong>::max();
visit(fmt::internal::ArgConverter<fmt::LongLong>(arg, 'd'), arg);
EXPECT_EQ(Arg::LONG_LONG, arg.type);
EXPECT_EQ(format_arg::LONG_LONG, arg.type);
}
TEST(FormatTest, FormatNegativeNaN) {

View File

@ -51,9 +51,9 @@
#undef max
using fmt::StringRef;
using fmt::internal::Arg;
using fmt::format_arg;
using fmt::Buffer;
using fmt::StringRef;
using fmt::internal::MemoryBuffer;
using testing::Return;
@ -70,9 +70,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test,
}
template <typename Char, typename T>
Arg make_arg(const T &value) {
format_arg make_arg(const T &value) {
typedef fmt::internal::MakeValue< fmt::basic_format_context<Char> > MakeValue;
Arg arg = MakeValue(value);
format_arg arg = MakeValue(value);
arg.type = fmt::internal::type<T>();
return arg;
}
@ -406,13 +406,13 @@ TEST(UtilTest, Increment) {
EXPECT_STREQ("200", s);
}
template <Arg::Type>
template <format_arg::Type>
struct ArgInfo;
#define ARG_INFO(type_code, Type, field) \
template <> \
struct ArgInfo<Arg::type_code> { \
static Type get(const Arg &arg) { return arg.field; } \
struct ArgInfo<format_arg::type_code> { \
static Type get(const format_arg &arg) { return arg.field; } \
}
ARG_INFO(INT, int, int_value);
@ -427,12 +427,12 @@ ARG_INFO(CSTRING, const char *, string.value);
ARG_INFO(STRING, const char *, string.value);
ARG_INFO(WSTRING, const wchar_t *, wstring.value);
ARG_INFO(POINTER, const void *, pointer);
ARG_INFO(CUSTOM, Arg::CustomValue, custom);
ARG_INFO(CUSTOM, format_arg::CustomValue, custom);
#define CHECK_ARG_INFO(Type, field, value) { \
Arg arg = Arg(); \
format_arg arg = format_arg(); \
arg.field = value; \
EXPECT_EQ(value, ArgInfo<Arg::Type>::get(arg)); \
EXPECT_EQ(value, ArgInfo<format_arg::Type>::get(arg)); \
}
TEST(ArgTest, ArgInfo) {
@ -449,17 +449,17 @@ TEST(ArgTest, ArgInfo) {
CHECK_ARG_INFO(WSTRING, wstring.value, WSTR);
int p = 0;
CHECK_ARG_INFO(POINTER, pointer, &p);
Arg arg = Arg();
format_arg arg = format_arg();
arg.custom.value = &p;
EXPECT_EQ(&p, ArgInfo<Arg::CUSTOM>::get(arg).value);
EXPECT_EQ(&p, ArgInfo<format_arg::CUSTOM>::get(arg).value);
}
#define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \
MakeArgType input = static_cast<MakeArgType>(value); \
Arg arg = make_arg<Char>(input); \
EXPECT_EQ(Arg::type_code, arg.type); \
format_arg arg = make_arg<Char>(input); \
EXPECT_EQ(format_arg::type_code, arg.type); \
ExpectedType expected_value = static_cast<ExpectedType>(value); \
EXPECT_EQ(expected_value, ArgInfo<Arg::type_code>::get(arg)); \
EXPECT_EQ(expected_value, ArgInfo<format_arg::type_code>::get(arg)); \
}
#define EXPECT_ARG(type_code, Type, value) \
@ -563,8 +563,8 @@ TEST(ArgTest, MakeArg) {
EXPECT_ARG(POINTER, const void*, &n);
::Test t;
Arg arg = make_arg<char>(t);
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
format_arg arg = make_arg<char>(t);
EXPECT_EQ(format_arg::CUSTOM, arg.type);
EXPECT_EQ(&t, arg.custom.value);
fmt::MemoryWriter w;
fmt::format_context ctx("}", fmt::format_args());
@ -574,7 +574,7 @@ TEST(ArgTest, MakeArg) {
TEST(UtilTest, FormatArgs) {
fmt::format_args args;
EXPECT_EQ(Arg::NONE, args[1].type);
EXPECT_EQ(format_arg::NONE, args[1].type);
}
struct CustomFormatter {
@ -588,7 +588,7 @@ void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) {
TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t;
Arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
format_arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
CustomFormatter ctx = {false};
fmt::MemoryWriter w;
arg.custom.format(&w, &t, &ctx);
@ -596,7 +596,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
}
struct Result {
Arg arg;
format_arg arg;
Result() : arg(make_arg<char>(0xdeadbeef)) {}
@ -627,10 +627,10 @@ struct TestVisitor {
};
#define EXPECT_RESULT_(Char, type_code, value) { \
Arg arg = make_arg<Char>(value); \
format_arg arg = make_arg<Char>(value); \
Result result = fmt::visit(TestVisitor(), arg); \
EXPECT_EQ(Arg::type_code, result.arg.type); \
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \
EXPECT_EQ(format_arg::type_code, result.arg.type); \
EXPECT_EQ(value, ArgInfo<format_arg::type_code>::get(result.arg)); \
}
#define EXPECT_RESULT(type_code, value) \
@ -654,13 +654,13 @@ TEST(ArgVisitorTest, VisitAll) {
EXPECT_RESULT(POINTER, p);
::Test t;
Result result = visit(TestVisitor(), make_arg<char>(t));
EXPECT_EQ(Arg::CUSTOM, result.arg.type);
EXPECT_EQ(format_arg::CUSTOM, result.arg.type);
EXPECT_EQ(&t, result.arg.custom.value);
}
TEST(ArgVisitorTest, VisitInvalidArg) {
Arg arg = Arg();
arg.type = static_cast<Arg::Type>(Arg::NONE);
format_arg arg = format_arg();
arg.type = static_cast<format_arg::Type>(format_arg::NONE);
EXPECT_ASSERT(visit(TestVisitor(), arg), "invalid argument type");
}