Commit 9cf6c8fd authored by Victor Zverovich's avatar Victor Zverovich

Get rid of fmt::internal::Arg

parent 5f022ae0
......@@ -51,8 +51,6 @@
# endif
#endif
using fmt::internal::Arg;
#if FMT_EXCEPTIONS
# define FMT_TRY try
# define FMT_CATCH(x) catch (x)
......
This diff is collapsed.
......@@ -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,
unsigned arg_index) {
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':
......
......@@ -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) {
......
......@@ -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");
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment