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

Get rid of fmt::internal::Arg

parent 5f022ae0
...@@ -51,8 +51,6 @@ ...@@ -51,8 +51,6 @@
# endif # endif
#endif #endif
using fmt::internal::Arg;
#if FMT_EXCEPTIONS #if FMT_EXCEPTIONS
# define FMT_TRY try # define FMT_TRY try
# define FMT_CATCH(x) catch (x) # define FMT_CATCH(x) catch (x)
......
This diff is collapsed.
...@@ -83,11 +83,11 @@ struct is_same<T, T> { ...@@ -83,11 +83,11 @@ struct is_same<T, T> {
template <typename T> template <typename T>
class ArgConverter { class ArgConverter {
private: private:
internal::Arg &arg_; format_arg &arg_;
wchar_t type_; wchar_t type_;
public: public:
ArgConverter(internal::Arg &arg, wchar_t type) ArgConverter(format_arg &arg, wchar_t type)
: arg_(arg), type_(type) {} : arg_(arg), type_(type) {}
void operator()(bool value) { void operator()(bool value) {
...@@ -99,28 +99,27 @@ class ArgConverter { ...@@ -99,28 +99,27 @@ class ArgConverter {
typename std::enable_if<std::is_integral<U>::value>::type typename std::enable_if<std::is_integral<U>::value>::type
operator()(U value) { operator()(U value) {
bool is_signed = type_ == 'd' || type_ == 'i'; bool is_signed = type_ == 'd' || type_ == 'i';
using internal::Arg;
typedef typename internal::Conditional< typedef typename internal::Conditional<
is_same<T, void>::value, U, T>::type TargetType; is_same<T, void>::value, U, T>::type TargetType;
if (sizeof(TargetType) <= sizeof(int)) { if (sizeof(TargetType) <= sizeof(int)) {
// Extra casts are used to silence warnings. // Extra casts are used to silence warnings.
if (is_signed) { if (is_signed) {
arg_.type = Arg::INT; arg_.type = format_arg::INT;
arg_.int_value = static_cast<int>(static_cast<TargetType>(value)); arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
} else { } else {
arg_.type = Arg::UINT; arg_.type = format_arg::UINT;
typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned; typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value)); arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
} }
} else { } else {
if (is_signed) { 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: // glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254" // std::printf("%lld", -42); // prints "4294967254"
// but we don't have to do the same because it's a UB. // but we don't have to do the same because it's a UB.
arg_.long_long_value = static_cast<LongLong>(value); arg_.long_long_value = static_cast<LongLong>(value);
} else { } else {
arg_.type = Arg::ULONG_LONG; arg_.type = format_arg::ULONG_LONG;
arg_.ulong_long_value = arg_.ulong_long_value =
static_cast<typename internal::MakeUnsigned<U>::Type>(value); static_cast<typename internal::MakeUnsigned<U>::Type>(value);
} }
...@@ -146,17 +145,17 @@ void convert_arg(format_arg &arg, wchar_t type) { ...@@ -146,17 +145,17 @@ void convert_arg(format_arg &arg, wchar_t type) {
// Converts an integer argument to char for printf. // Converts an integer argument to char for printf.
class CharConverter { class CharConverter {
private: private:
internal::Arg &arg_; format_arg &arg_;
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
public: public:
explicit CharConverter(internal::Arg &arg) : arg_(arg) {} explicit CharConverter(format_arg &arg) : arg_(arg) {}
template <typename T> template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type typename std::enable_if<std::is_integral<T>::value>::type
operator()(T value) { operator()(T value) {
arg_.type = internal::Arg::CHAR; arg_.type = format_arg::CHAR;
arg_.int_value = static_cast<char>(value); arg_.int_value = static_cast<char>(value);
} }
...@@ -281,7 +280,7 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> { ...@@ -281,7 +280,7 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
} }
/** Formats an argument of a custom (user-defined) type. */ /** 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'}; const Char format_str[] = {'}', '\0'};
auto args = basic_format_args<basic_format_context<Char>>(); auto args = basic_format_args<basic_format_context<Char>>();
basic_format_context<Char> ctx(format_str, args); basic_format_context<Char> ctx(format_str, args);
...@@ -306,7 +305,7 @@ class printf_context : ...@@ -306,7 +305,7 @@ class printf_context :
// Returns the argument with specified index or, if arg_index is equal // Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument. // to the maximum unsigned value, the next argument.
internal::Arg get_arg( format_arg get_arg(
const Char *s, const Char *s,
unsigned arg_index = (std::numeric_limits<unsigned>::max)()); unsigned arg_index = (std::numeric_limits<unsigned>::max)());
...@@ -356,11 +355,11 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) { ...@@ -356,11 +355,11 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
} }
template <typename Char, typename AF> 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) { unsigned arg_index) {
(void)s; (void)s;
const char *error = 0; 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); this->next_arg(error) : Base::get_arg(arg_index - 1, error);
if (error) if (error)
FMT_THROW(format_error(!*s ? "invalid format string" : error)); FMT_THROW(format_error(!*s ? "invalid format string" : error));
...@@ -432,12 +431,11 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) { ...@@ -432,12 +431,11 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
} }
} }
using internal::Arg; format_arg arg = get_arg(s, arg_index);
Arg arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg)) if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG); spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') { if (spec.fill_ == '0') {
if (arg.type <= Arg::LAST_NUMERIC_TYPE) if (arg.type <= format_arg::LAST_NUMERIC_TYPE)
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
else else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
...@@ -480,7 +478,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) { ...@@ -480,7 +478,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
if (!*s) if (!*s)
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
spec.type_ = static_cast<char>(*s++); spec.type_ = static_cast<char>(*s++);
if (arg.type <= Arg::LAST_INTEGER_TYPE) { if (arg.type <= format_arg::LAST_INTEGER_TYPE) {
// Normalize type. // Normalize type.
switch (spec.type_) { switch (spec.type_) {
case 'i': case 'u': case 'i': case 'u':
......
...@@ -42,12 +42,12 @@ ...@@ -42,12 +42,12 @@
#undef max #undef max
TEST(FormatTest, ArgConverter) { TEST(FormatTest, ArgConverter) {
using fmt::internal::Arg; using fmt::format_arg;
Arg arg = Arg(); format_arg arg = format_arg();
arg.type = Arg::LONG_LONG; arg.type = format_arg::LONG_LONG;
arg.long_long_value = std::numeric_limits<fmt::LongLong>::max(); arg.long_long_value = std::numeric_limits<fmt::LongLong>::max();
visit(fmt::internal::ArgConverter<fmt::LongLong>(arg, 'd'), arg); 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) { TEST(FormatTest, FormatNegativeNaN) {
......
...@@ -51,9 +51,9 @@ ...@@ -51,9 +51,9 @@
#undef max #undef max
using fmt::StringRef; using fmt::format_arg;
using fmt::internal::Arg;
using fmt::Buffer; using fmt::Buffer;
using fmt::StringRef;
using fmt::internal::MemoryBuffer; using fmt::internal::MemoryBuffer;
using testing::Return; using testing::Return;
...@@ -70,9 +70,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test, ...@@ -70,9 +70,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test,
} }
template <typename Char, typename T> 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; 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>(); arg.type = fmt::internal::type<T>();
return arg; return arg;
} }
...@@ -406,13 +406,13 @@ TEST(UtilTest, Increment) { ...@@ -406,13 +406,13 @@ TEST(UtilTest, Increment) {
EXPECT_STREQ("200", s); EXPECT_STREQ("200", s);
} }
template <Arg::Type> template <format_arg::Type>
struct ArgInfo; struct ArgInfo;
#define ARG_INFO(type_code, Type, field) \ #define ARG_INFO(type_code, Type, field) \
template <> \ template <> \
struct ArgInfo<Arg::type_code> { \ struct ArgInfo<format_arg::type_code> { \
static Type get(const Arg &arg) { return arg.field; } \ static Type get(const format_arg &arg) { return arg.field; } \
} }
ARG_INFO(INT, int, int_value); ARG_INFO(INT, int, int_value);
...@@ -427,12 +427,12 @@ ARG_INFO(CSTRING, const char *, string.value); ...@@ -427,12 +427,12 @@ ARG_INFO(CSTRING, const char *, string.value);
ARG_INFO(STRING, const char *, string.value); ARG_INFO(STRING, const char *, string.value);
ARG_INFO(WSTRING, const wchar_t *, wstring.value); ARG_INFO(WSTRING, const wchar_t *, wstring.value);
ARG_INFO(POINTER, const void *, pointer); 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) { \ #define CHECK_ARG_INFO(Type, field, value) { \
Arg arg = Arg(); \ format_arg arg = format_arg(); \
arg.field = value; \ arg.field = value; \
EXPECT_EQ(value, ArgInfo<Arg::Type>::get(arg)); \ EXPECT_EQ(value, ArgInfo<format_arg::Type>::get(arg)); \
} }
TEST(ArgTest, ArgInfo) { TEST(ArgTest, ArgInfo) {
...@@ -449,17 +449,17 @@ TEST(ArgTest, ArgInfo) { ...@@ -449,17 +449,17 @@ TEST(ArgTest, ArgInfo) {
CHECK_ARG_INFO(WSTRING, wstring.value, WSTR); CHECK_ARG_INFO(WSTRING, wstring.value, WSTR);
int p = 0; int p = 0;
CHECK_ARG_INFO(POINTER, pointer, &p); CHECK_ARG_INFO(POINTER, pointer, &p);
Arg arg = Arg(); format_arg arg = format_arg();
arg.custom.value = &p; 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) { \ #define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \
MakeArgType input = static_cast<MakeArgType>(value); \ MakeArgType input = static_cast<MakeArgType>(value); \
Arg arg = make_arg<Char>(input); \ format_arg arg = make_arg<Char>(input); \
EXPECT_EQ(Arg::type_code, arg.type); \ EXPECT_EQ(format_arg::type_code, arg.type); \
ExpectedType expected_value = static_cast<ExpectedType>(value); \ 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) \ #define EXPECT_ARG(type_code, Type, value) \
...@@ -563,8 +563,8 @@ TEST(ArgTest, MakeArg) { ...@@ -563,8 +563,8 @@ TEST(ArgTest, MakeArg) {
EXPECT_ARG(POINTER, const void*, &n); EXPECT_ARG(POINTER, const void*, &n);
::Test t; ::Test t;
Arg arg = make_arg<char>(t); format_arg arg = make_arg<char>(t);
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type); EXPECT_EQ(format_arg::CUSTOM, arg.type);
EXPECT_EQ(&t, arg.custom.value); EXPECT_EQ(&t, arg.custom.value);
fmt::MemoryWriter w; fmt::MemoryWriter w;
fmt::format_context ctx("}", fmt::format_args()); fmt::format_context ctx("}", fmt::format_args());
...@@ -574,7 +574,7 @@ TEST(ArgTest, MakeArg) { ...@@ -574,7 +574,7 @@ TEST(ArgTest, MakeArg) {
TEST(UtilTest, FormatArgs) { TEST(UtilTest, FormatArgs) {
fmt::format_args args; fmt::format_args args;
EXPECT_EQ(Arg::NONE, args[1].type); EXPECT_EQ(format_arg::NONE, args[1].type);
} }
struct CustomFormatter { struct CustomFormatter {
...@@ -588,7 +588,7 @@ void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) { ...@@ -588,7 +588,7 @@ void format_value(fmt::Writer &, const Test &, CustomFormatter &ctx) {
TEST(UtilTest, MakeValueWithCustomFormatter) { TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t; ::Test t;
Arg arg = fmt::internal::MakeValue<CustomFormatter>(t); format_arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
CustomFormatter ctx = {false}; CustomFormatter ctx = {false};
fmt::MemoryWriter w; fmt::MemoryWriter w;
arg.custom.format(&w, &t, &ctx); arg.custom.format(&w, &t, &ctx);
...@@ -596,7 +596,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) { ...@@ -596,7 +596,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
} }
struct Result { struct Result {
Arg arg; format_arg arg;
Result() : arg(make_arg<char>(0xdeadbeef)) {} Result() : arg(make_arg<char>(0xdeadbeef)) {}
...@@ -627,10 +627,10 @@ struct TestVisitor { ...@@ -627,10 +627,10 @@ struct TestVisitor {
}; };
#define EXPECT_RESULT_(Char, type_code, value) { \ #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); \ Result result = fmt::visit(TestVisitor(), arg); \
EXPECT_EQ(Arg::type_code, result.arg.type); \ EXPECT_EQ(format_arg::type_code, result.arg.type); \
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \ EXPECT_EQ(value, ArgInfo<format_arg::type_code>::get(result.arg)); \
} }
#define EXPECT_RESULT(type_code, value) \ #define EXPECT_RESULT(type_code, value) \
...@@ -654,13 +654,13 @@ TEST(ArgVisitorTest, VisitAll) { ...@@ -654,13 +654,13 @@ TEST(ArgVisitorTest, VisitAll) {
EXPECT_RESULT(POINTER, p); EXPECT_RESULT(POINTER, p);
::Test t; ::Test t;
Result result = visit(TestVisitor(), make_arg<char>(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); EXPECT_EQ(&t, result.arg.custom.value);
} }
TEST(ArgVisitorTest, VisitInvalidArg) { TEST(ArgVisitorTest, VisitInvalidArg) {
Arg arg = Arg(); format_arg arg = format_arg();
arg.type = static_cast<Arg::Type>(Arg::NONE); arg.type = static_cast<format_arg::Type>(format_arg::NONE);
EXPECT_ASSERT(visit(TestVisitor(), arg), "invalid argument type"); 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