Commit d705d516 authored by Victor Zverovich's avatar Victor Zverovich

Parameterize basic_format_arg on context (#442)

parent 422236af
...@@ -458,7 +458,7 @@ template struct internal::BasicData<void>; ...@@ -458,7 +458,7 @@ template struct internal::BasicData<void>;
template void internal::FixedBuffer<char>::grow(std::size_t); template void internal::FixedBuffer<char>::grow(std::size_t);
template void internal::ArgMap<char>::init(const format_args &args); template void internal::ArgMap<format_context>::init(const format_args &args);
template void printf_context<char>::format(Writer &writer); template void printf_context<char>::format(Writer &writer);
...@@ -472,9 +472,11 @@ template int internal::CharTraits<char>::format_float( ...@@ -472,9 +472,11 @@ template int internal::CharTraits<char>::format_float(
// Explicit instantiations for wchar_t. // Explicit instantiations for wchar_t.
template class basic_format_context<wchar_t>;
template void internal::FixedBuffer<wchar_t>::grow(std::size_t); template void internal::FixedBuffer<wchar_t>::grow(std::size_t);
template void internal::ArgMap<wchar_t>::init(const wformat_args &args); template void internal::ArgMap<wformat_context>::init(const wformat_args &args);
template void printf_context<wchar_t>::format(WWriter &writer); template void printf_context<wchar_t>::format(WWriter &writer);
......
This diff is collapsed.
...@@ -80,14 +80,16 @@ struct is_same<T, T> { ...@@ -80,14 +80,16 @@ struct is_same<T, T> {
enum { value = 1 }; enum { value = 1 };
}; };
template <typename T, typename Char> template <typename T, typename Context>
class ArgConverter { class ArgConverter {
private: private:
basic_format_arg<Char> &arg_; typedef typename Context::char_type Char;
Char type_;
basic_format_arg<Context> &arg_;
typename Context::char_type type_;
public: public:
ArgConverter(basic_format_arg<Char> &arg, Char type) ArgConverter(basic_format_arg<Context> &arg, Char type)
: arg_(arg), type_(type) {} : arg_(arg), type_(type) {}
void operator()(bool value) { void operator()(bool value) {
...@@ -105,11 +107,11 @@ class ArgConverter { ...@@ -105,11 +107,11 @@ class ArgConverter {
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_ = internal::make_arg<format_context>( arg_ = internal::make_arg<Context>(
static_cast<int>(static_cast<TargetType>(value))); static_cast<int>(static_cast<TargetType>(value)));
} else { } else {
typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned; typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
arg_ = internal::make_arg<format_context>( arg_ = internal::make_arg<Context>(
static_cast<unsigned>(static_cast<Unsigned>(value))); static_cast<unsigned>(static_cast<Unsigned>(value)));
} }
} else { } else {
...@@ -117,10 +119,9 @@ class ArgConverter { ...@@ -117,10 +119,9 @@ class ArgConverter {
// 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_ = internal::make_arg<format_context>( arg_ = internal::make_arg<Context>(static_cast<LongLong>(value));
static_cast<LongLong>(value));
} else { } else {
arg_ = internal::make_arg<format_context>( arg_ = internal::make_arg<Context>(
static_cast<typename internal::MakeUnsigned<U>::Type>(value)); static_cast<typename internal::MakeUnsigned<U>::Type>(value));
} }
} }
...@@ -137,32 +138,30 @@ class ArgConverter { ...@@ -137,32 +138,30 @@ class ArgConverter {
// If T is void, the argument is converted to corresponding signed or unsigned // If T is void, the argument is converted to corresponding signed or unsigned
// type depending on the type specifier: 'd' and 'i' - signed, other - // type depending on the type specifier: 'd' and 'i' - signed, other -
// unsigned). // unsigned).
template <typename T, typename Char> template <typename T, typename Context, typename Char>
void convert_arg(basic_format_arg<Char> &arg, Char type) { void convert_arg(basic_format_arg<Context> &arg, Char type) {
visit(ArgConverter<T, Char>(arg, type), arg); visit(ArgConverter<T, Context>(arg, type), arg);
} }
// Converts an integer argument to char for printf. // Converts an integer argument to char for printf.
template <typename Char> template <typename Context>
class CharConverter { class CharConverter {
private: private:
basic_format_arg<Char> &arg_; basic_format_arg<Context> &arg_;
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
public: public:
explicit CharConverter(basic_format_arg<Char> &arg) : arg_(arg) {} explicit CharConverter(basic_format_arg<Context> &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_ = arg_ = internal::make_arg<Context>(static_cast<char>(value));
internal::make_arg<basic_format_context<Char>>(static_cast<char>(value));
} }
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) {
operator()(T value) {
// No coversion needed for non-integral types. // No coversion needed for non-integral types.
} }
}; };
...@@ -301,12 +300,13 @@ class printf_context : ...@@ -301,12 +300,13 @@ class printf_context :
private: private:
typedef internal::format_context_base<Char, printf_context> Base; typedef internal::format_context_base<Char, printf_context> Base;
typedef typename Base::format_arg format_arg;
void parse_flags(FormatSpec &spec, const Char *&s); void parse_flags(FormatSpec &spec, const Char *&s);
// 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.
basic_format_arg<Char> 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,12 +356,11 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) { ...@@ -356,12 +356,11 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
} }
template <typename Char, typename AF> template <typename Char, typename AF>
basic_format_arg<Char> printf_context<Char, AF>::get_arg( typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
const Char *s, unsigned arg_index) { const Char *s, unsigned arg_index) {
(void)s; (void)s;
const char *error = 0; const char *error = 0;
basic_format_arg<Char> arg = format_arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
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));
...@@ -433,7 +432,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) { ...@@ -433,7 +432,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
} }
} }
basic_format_arg<Char> arg = get_arg(s, arg_index); format_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') {
...@@ -488,7 +487,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) { ...@@ -488,7 +487,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
break; break;
case 'c': case 'c':
// TODO: handle wchar_t // TODO: handle wchar_t
visit(internal::CharConverter<Char>(arg), arg); visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
break; break;
} }
} }
......
...@@ -58,7 +58,8 @@ TEST(FormatTest, ArgConverter) { ...@@ -58,7 +58,8 @@ TEST(FormatTest, ArgConverter) {
using fmt::format_arg; using fmt::format_arg;
fmt::LongLong value = std::numeric_limits<fmt::LongLong>::max(); fmt::LongLong value = std::numeric_limits<fmt::LongLong>::max();
format_arg arg = fmt::internal::make_arg<fmt::format_context>(value); format_arg arg = fmt::internal::make_arg<fmt::format_context>(value);
visit(fmt::internal::ArgConverter<fmt::LongLong, char>(arg, 'd'), arg); visit(fmt::internal::ArgConverter<
fmt::LongLong, fmt::format_context>(arg, 'd'), arg);
EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg)); EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg));
} }
......
...@@ -73,9 +73,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test, ...@@ -73,9 +73,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test,
w << "test"; w << "test";
} }
template <typename Char, typename T> template <typename Context, typename T>
basic_format_arg<Char> make_arg(const T &value) { basic_format_arg<Context> make_arg(const T &value) {
return fmt::internal::make_arg< fmt::basic_format_context<Char> >(value); return fmt::internal::make_arg<Context>(value);
} }
} // namespace } // namespace
...@@ -487,7 +487,7 @@ VISIT_TYPE(float, double); ...@@ -487,7 +487,7 @@ VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) { \ #define CHECK_ARG_(Char, expected, value) { \
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \ testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \ EXPECT_CALL(visitor, visit(expected)); \
fmt::visit(visitor, make_arg<Char>(value)); \ fmt::visit(visitor, make_arg<fmt::basic_format_context<Char>>(value)); \
} }
#define CHECK_ARG(value) { \ #define CHECK_ARG(value) { \
...@@ -575,7 +575,7 @@ TEST(UtilTest, CustomArg) { ...@@ -575,7 +575,7 @@ TEST(UtilTest, CustomArg) {
EXPECT_EQ("test", w.str()); EXPECT_EQ("test", w.str());
return Visitor::Result(); return Visitor::Result();
})); }));
fmt::visit(visitor, make_arg<char>(test)); fmt::visit(visitor, make_arg<fmt::format_context>(test));
} }
TEST(ArgVisitorTest, VisitInvalidArg) { TEST(ArgVisitorTest, VisitInvalidArg) {
......
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