Simplify API

This commit is contained in:
Victor Zverovich 2017-02-05 06:41:39 -08:00
parent 7ae8bd7073
commit 624c58682d
11 changed files with 82 additions and 85 deletions

View File

@ -465,7 +465,7 @@ template struct internal::BasicData<void>;
template void internal::FixedBuffer<char>::grow(std::size_t);
template void internal::ArgMap<format_context>::init(const format_args &args);
template void internal::ArgMap<context>::init(const format_args &args);
template void printf_context<char>::format(writer &writer);
@ -479,11 +479,11 @@ template int internal::CharTraits<char>::format_float(
// Explicit instantiations for wchar_t.
template class basic_format_context<wchar_t>;
template class basic_context<wchar_t>;
template void internal::FixedBuffer<wchar_t>::grow(std::size_t);
template void internal::ArgMap<wformat_context>::init(const wformat_args &args);
template void internal::ArgMap<wcontext>::init(const wformat_args &args);
template void printf_context<wchar_t>::format(wwriter &writer);

View File

@ -380,10 +380,10 @@ template <typename Char>
class PrintfArgFormatter;
template <typename Char>
class basic_format_context;
class basic_context;
typedef basic_format_context<char> format_context;
typedef basic_format_context<wchar_t> wformat_context;
typedef basic_context<char> context;
typedef basic_context<wchar_t> wcontext;
/**
\rst
@ -1369,8 +1369,8 @@ class basic_arg {
}
};
typedef basic_arg<format_context> format_arg;
typedef basic_arg<wformat_context> wformat_arg;
typedef basic_arg<context> format_arg;
typedef basic_arg<wcontext> wformat_arg;
/**
\rst
@ -1516,7 +1516,7 @@ inline typename std::enable_if<!IS_PACKED, basic_arg<Context>>::type
} // namespace internal
template <typename Context, typename ...Args>
class format_arg_store {
class arg_store {
private:
static const size_t NUM_ARGS = sizeof...(Args);
@ -1535,22 +1535,20 @@ class format_arg_store {
public:
static const uint64_t TYPES = internal::make_type<Args..., void>();
format_arg_store(const Args &... args)
arg_store(const Args &... args)
: data_(Array{{internal::make_arg<IS_PACKED, Context>(args)...}}) {}
const value_type *data() const { return data_.data(); }
};
template <typename Context, typename ...Args>
inline format_arg_store<Context, Args...>
make_xformat_args(const Args & ... args) {
return format_arg_store<Context, Args...>(args...);
inline arg_store<Context, Args...> make_args(const Args & ... args) {
return arg_store<Context, Args...>(args...);
}
template <typename ...Args>
inline format_arg_store<format_context, Args...>
make_format_args(const Args & ... args) {
return format_arg_store<format_context, Args...>(args...);
inline arg_store<context, Args...> make_args(const Args & ... args) {
return arg_store<context, Args...>(args...);
}
/** Formatting arguments. */
@ -1614,7 +1612,7 @@ class basic_args {
basic_args() : types_(0) {}
template <typename... Args>
basic_args(const format_arg_store<Context, Args...> &store)
basic_args(const arg_store<Context, Args...> &store)
: types_(store.TYPES) {
set_data(store.data());
}
@ -1627,8 +1625,8 @@ class basic_args {
}
};
typedef basic_args<format_context> format_args;
typedef basic_args<wformat_context> wformat_args;
typedef basic_args<context> format_args;
typedef basic_args<wcontext> wformat_args;
enum Alignment {
ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
@ -1985,7 +1983,7 @@ inline void write(basic_writer<Char> &w, const Char *start, const Char *end) {
}
template <typename Char, typename Context>
class format_context_base {
class context_base {
private:
const Char *ptr_;
basic_args<Context> args_;
@ -1994,9 +1992,9 @@ class format_context_base {
protected:
typedef basic_arg<Context> format_arg;
format_context_base(const Char *format_str, basic_args<Context> args)
context_base(const Char *format_str, basic_args<Context> args)
: ptr_(format_str), args_(args), next_arg_index_(0) {}
~format_context_base() {}
~context_base() {}
basic_args<Context> args() const { return args_; }
@ -2042,7 +2040,7 @@ class format_context_base {
template <typename Char>
class ArgFormatter : public internal::ArgFormatterBase<Char> {
private:
basic_format_context<Char> &ctx_;
basic_context<Char> &ctx_;
typedef internal::ArgFormatterBase<Char> Base;
@ -2057,7 +2055,7 @@ class ArgFormatter : public internal::ArgFormatterBase<Char> {
format specifier information for standard argument types.
\endrst
*/
ArgFormatter(basic_writer<Char> &writer, basic_format_context<Char> &ctx,
ArgFormatter(basic_writer<Char> &writer, basic_context<Char> &ctx,
format_specs &spec)
: internal::ArgFormatterBase<Char>(writer, spec), ctx_(ctx) {}
@ -2070,18 +2068,18 @@ class ArgFormatter : public internal::ArgFormatterBase<Char> {
};
template <typename Char>
class basic_format_context :
public internal::format_context_base<Char, basic_format_context<Char>> {
class basic_context :
public internal::context_base<Char, basic_context<Char>> {
public:
/** The character type for the output. */
typedef Char char_type;
private:
internal::ArgMap<basic_format_context<Char>> map_;
internal::ArgMap<basic_context<Char>> map_;
FMT_DISALLOW_COPY_AND_ASSIGN(basic_format_context);
FMT_DISALLOW_COPY_AND_ASSIGN(basic_context);
typedef internal::format_context_base<Char, basic_format_context<Char>> Base;
typedef internal::context_base<Char, basic_context<Char>> Base;
typedef typename Base::format_arg format_arg;
using Base::get_arg;
@ -2093,12 +2091,12 @@ class basic_format_context :
public:
/**
\rst
Constructs a ``basic_format_context`` object. References to the arguments are
Constructs a ``basic_context`` object. References to the arguments are
stored in the object so make sure they have appropriate lifetimes.
\endrst
*/
basic_format_context(const Char *format_str,
basic_args<basic_format_context> args)
basic_context(const Char *format_str,
basic_args<basic_context> args)
: Base(format_str, args) {}
// Parses argument id and returns corresponding argument.
@ -2141,7 +2139,7 @@ class SystemError : public internal::RuntimeError {
*/
template <typename... Args>
SystemError(int error_code, CStringRef message, const Args & ... args) {
init(error_code, message, make_format_args(args...));
init(error_code, message, make_args(args...));
}
~SystemError() throw();
@ -2349,7 +2347,7 @@ class basic_writer {
}
void vformat(BasicCStringRef<Char> format,
basic_args<basic_format_context<Char>> args);
basic_args<basic_context<Char>> args);
/**
\rst
Writes formatted data.
@ -2377,7 +2375,7 @@ class basic_writer {
*/
template <typename... Args>
void format(BasicCStringRef<Char> format, const Args & ... args) {
vformat(format, make_xformat_args<basic_format_context<Char>>(args...));
vformat(format, make_args<basic_context<Char>>(args...));
}
void write(int value) {
@ -2978,7 +2976,7 @@ class WindowsError : public SystemError {
*/
template <typename... Args>
WindowsError(int error_code, CStringRef message, const Args & ... args) {
init(error_code, message, make_format_args(args...));
init(error_code, message, make_args(args...));
}
};
@ -3002,7 +3000,7 @@ FMT_API void vprint_colored(Color c, CStringRef format, format_args args);
template <typename... Args>
inline void print_colored(Color c, CStringRef format_str,
const Args & ... args) {
vprint_colored(c, format_str, make_format_args(args...));
vprint_colored(c, format_str, make_args(args...));
}
inline std::string vformat(CStringRef format_str, format_args args) {
@ -3022,7 +3020,7 @@ inline std::string vformat(CStringRef format_str, format_args args) {
*/
template <typename... Args>
inline std::string format(CStringRef format_str, const Args & ... args) {
return vformat(format_str, make_format_args(args...));
return vformat(format_str, make_args(args...));
}
inline std::wstring vformat(WCStringRef format_str, wformat_args args) {
@ -3033,7 +3031,7 @@ inline std::wstring vformat(WCStringRef format_str, wformat_args args) {
template <typename... Args>
inline std::wstring format(WCStringRef format_str, const Args & ... args) {
auto vargs = make_xformat_args<wformat_context>(args...);
auto vargs = make_args<wcontext>(args...);
return vformat(format_str, vargs);
}
@ -3050,7 +3048,7 @@ FMT_API void vprint(std::FILE *f, CStringRef format_str, format_args args);
*/
template <typename... Args>
inline void print(std::FILE *f, CStringRef format_str, const Args & ... args) {
vprint(f, format_str, make_format_args(args...));
vprint(f, format_str, make_args(args...));
}
FMT_API void vprint(CStringRef format_str, format_args args);
@ -3066,7 +3064,7 @@ FMT_API void vprint(CStringRef format_str, format_args args);
*/
template <typename... Args>
inline void print(CStringRef format_str, const Args & ... args) {
vprint(format_str, make_format_args(args...));
vprint(format_str, make_args(args...));
}
/**
@ -3185,13 +3183,13 @@ inline void format_decimal(char *&buffer, T value) {
\endrst
*/
template <typename T>
inline internal::NamedArg<format_context> arg(StringRef name, const T &arg) {
return internal::NamedArg<format_context>(name, arg);
inline internal::NamedArg<context> arg(StringRef name, const T &arg) {
return internal::NamedArg<context>(name, arg);
}
template <typename T>
inline internal::NamedArg<wformat_context> arg(WStringRef name, const T &arg) {
return internal::NamedArg<wformat_context>(name, arg);
inline internal::NamedArg<wcontext> arg(WStringRef name, const T &arg) {
return internal::NamedArg<wcontext>(name, arg);
}
// The following two functions are deleted intentionally to disable
@ -3338,8 +3336,8 @@ struct PrecisionHandler {
} // namespace internal
template <typename Char>
inline typename basic_format_context<Char>::format_arg
basic_format_context<Char>::get_arg(
inline typename basic_context<Char>::format_arg
basic_context<Char>::get_arg(
BasicStringRef<Char> name, const char *&error) {
if (this->check_no_auto_index(error)) {
map_.init(this->args());
@ -3351,8 +3349,8 @@ inline typename basic_format_context<Char>::format_arg
}
template <typename Char>
inline typename basic_format_context<Char>::format_arg
basic_format_context<Char>::parse_arg_id() {
inline typename basic_context<Char>::format_arg
basic_context<Char>::parse_arg_id() {
const Char *&s = this->ptr();
if (!internal::is_name_start(*s)) {
const char *error = 0;
@ -3507,7 +3505,7 @@ void do_format_arg(basic_writer<Char> &writer, basic_arg<Context> arg,
template <typename ArgFormatter, typename Char, typename Context>
void vwrite(basic_writer<Char> &writer, BasicCStringRef<Char> format_str,
basic_args<Context> args) {
basic_format_context<Char> ctx(format_str.c_str(), args);
basic_context<Char> ctx(format_str.c_str(), args);
const Char *&s = ctx.ptr();
const Char *start = s;
while (*s) {
@ -3532,7 +3530,7 @@ void vwrite(basic_writer<Char> &writer, BasicCStringRef<Char> format_str,
template <typename Char>
inline void basic_writer<Char>::vformat(
BasicCStringRef<Char> format,
basic_args<basic_format_context<Char>> args) {
basic_args<basic_context<Char>> args) {
vwrite<ArgFormatter<Char>>(*this, format, args);
}
} // namespace fmt
@ -3557,7 +3555,7 @@ struct UdlArg {
const Char *str;
template <typename T>
NamedArg<basic_format_context<Char>> operator=(T &&value) const {
NamedArg<basic_context<Char>> operator=(T &&value) const {
return {str, std::forward<T>(value)};
}
};

View File

@ -84,11 +84,11 @@ BasicStringRef<Char> format_value(
// Formats a value.
template <typename Char, typename T>
void format_value(basic_writer<Char> &w, const T &value,
basic_format_context<Char> &ctx) {
basic_context<Char> &ctx) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
auto str = internal::format_value(buffer, value);
do_format_arg< ArgFormatter<Char> >(
w, internal::make_arg< basic_format_context<Char> >(str), ctx);
w, internal::make_arg< basic_context<Char> >(str), ctx);
}
FMT_API void vprint(std::ostream &os, CStringRef format_str, format_args args);
@ -105,7 +105,7 @@ FMT_API void vprint(std::ostream &os, CStringRef format_str, format_args args);
template <typename... Args>
inline void print(std::ostream &os, CStringRef format_str,
const Args & ... args) {
vprint(os, format_str, make_format_args(args...));
vprint(os, format_str, make_args(args...));
}
} // namespace fmt

View File

@ -172,7 +172,7 @@ public:
template <typename... Args>
inline void print(CStringRef format_str, const Args & ... args) {
vprint(format_str, make_format_args(args...));
vprint(format_str, make_args(args...));
}
};

View File

@ -103,7 +103,7 @@ class ArgConverter {
bool is_signed = type_ == 'd' || type_ == 'i';
typedef typename internal::Conditional<
is_same<T, void>::value, U, T>::type TargetType;
typedef basic_format_context<Char> format_context;
typedef basic_context<Char> context;
if (sizeof(TargetType) <= sizeof(int)) {
// Extra casts are used to silence warnings.
if (is_signed) {
@ -287,8 +287,8 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
/** Formats an argument of a custom (user-defined) type. */
void operator()(internal::CustomValue<Char> c) {
const Char format_str[] = {'}', '\0'};
auto args = basic_args<basic_format_context<Char>>();
basic_format_context<Char> ctx(format_str, args);
auto args = basic_args<basic_context<Char>>();
basic_context<Char> ctx(format_str, args);
c.format(this->writer(), c.value, &ctx);
}
};
@ -297,14 +297,14 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
template <typename Char,
typename ArgFormatter = PrintfArgFormatter<Char> >
class printf_context :
private internal::format_context_base<
private internal::context_base<
Char, printf_context<Char, ArgFormatter>> {
public:
/** The character type for the output. */
typedef Char char_type;
private:
typedef internal::format_context_base<Char, printf_context> Base;
typedef internal::context_base<Char, printf_context> Base;
typedef typename Base::format_arg format_arg;
typedef basic_format_specs<Char> format_specs;
@ -539,7 +539,7 @@ inline std::string vsprintf(CStringRef format, printf_args args) {
*/
template <typename... Args>
inline std::string sprintf(CStringRef format_str, const Args & ... args) {
return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
return vsprintf(format_str, make_args<printf_context<char>>(args...));
}
inline std::wstring vsprintf(
@ -551,7 +551,7 @@ inline std::wstring vsprintf(
template <typename... Args>
inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
auto vargs = make_xformat_args<printf_context<wchar_t>>(args...);
auto vargs = make_args<printf_context<wchar_t>>(args...);
return vsprintf(format_str, vargs);
}
@ -568,7 +568,7 @@ FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
*/
template <typename... Args>
inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
auto vargs = make_xformat_args<printf_context<char>>(args...);
auto vargs = make_args<printf_context<char>>(args...);
return vfprintf(f, format_str, vargs);
}
@ -587,7 +587,7 @@ inline int vprintf(CStringRef format, printf_args args) {
*/
template <typename... Args>
inline int printf(CStringRef format_str, const Args & ... args) {
return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
return vprintf(format_str, make_args<printf_context<char>>(args...));
}
inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
@ -609,7 +609,7 @@ inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
template <typename... Args>
inline int fprintf(std::ostream &os, CStringRef format_str,
const Args & ... args) {
auto vargs = make_xformat_args<printf_context<char>>(args...);
auto vargs = make_args<printf_context<char>>(args...);
return vfprintf(os, format_str, vargs);
}
} // namespace fmt

View File

@ -15,7 +15,7 @@
namespace fmt {
void format_value(writer &w, const std::tm &tm, format_context &ctx) {
void format_value(writer &w, const std::tm &tm, context &ctx) {
const char *&s = ctx.ptr();
if (*s == ':')
++s;

View File

@ -16,7 +16,7 @@ using fmt::PrintfArgFormatter;
// rounded to 0.
class CustomArgFormatter : public fmt::ArgFormatter<char> {
public:
CustomArgFormatter(fmt::writer &w, fmt::basic_format_context<char> &ctx,
CustomArgFormatter(fmt::writer &w, fmt::basic_context<char> &ctx,
fmt::format_specs &s)
: fmt::ArgFormatter<char>(w, ctx, s) {}
@ -54,7 +54,7 @@ std::string custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
template <typename... Args>
std::string custom_format(const char *format_str, const Args & ... args) {
auto va = fmt::make_format_args(args...);
auto va = fmt::make_args(args...);
return custom_vformat(format_str, va);
}
@ -72,7 +72,7 @@ std::string custom_vsprintf(
template <typename... Args>
std::string custom_sprintf(const char *format_str, const Args & ... args) {
auto va = fmt::make_xformat_args<CustomPrintfFormatter>(args...);
auto va = fmt::make_args<CustomPrintfFormatter>(args...);
return custom_vsprintf(format_str, va);
}

View File

@ -57,9 +57,9 @@ struct ValueExtractor {
TEST(FormatTest, ArgConverter) {
using fmt::format_arg;
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::context>(value);
visit(fmt::internal::ArgConverter<
fmt::LongLong, fmt::format_context>(arg, 'd'), arg);
fmt::LongLong, fmt::context>(arg, 'd'), arg);
EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg));
}

View File

@ -1366,7 +1366,7 @@ TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ("test", format("{0}", CStringRef("test")));
}
void format_value(fmt::writer &w, const Date &d, fmt::format_context &) {
void format_value(fmt::writer &w, const Date &d, fmt::context &) {
w.write(d.year());
w.write('-');
w.write(d.month());
@ -1383,7 +1383,7 @@ TEST(FormatterTest, FormatCustom) {
class Answer {};
template <typename Char>
void format_value(basic_writer<Char> &w, Answer, fmt::format_context &) {
void format_value(basic_writer<Char> &w, Answer, fmt::context &) {
w.write("42");
}
@ -1575,7 +1575,7 @@ std::string vformat_message(int id, const char *format, fmt::format_args args) {
template <typename... Args>
std::string format_message(int id, const char *format, const Args & ... args) {
auto va = fmt::make_format_args(args...);
auto va = fmt::make_args(args...);
return vformat_message(id, format, va);
}
@ -1643,7 +1643,7 @@ class MockArgFormatter : public fmt::internal::ArgFormatterBase<char> {
public:
typedef fmt::internal::ArgFormatterBase<char> Base;
MockArgFormatter(fmt::writer &w, fmt::format_context &ctx,
MockArgFormatter(fmt::writer &w, fmt::context &ctx,
fmt::format_specs &s)
: fmt::internal::ArgFormatterBase<char>(w, s) {
EXPECT_CALL(*this, call(42));
@ -1663,7 +1663,7 @@ void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
template <typename... Args>
void custom_format(const char *format_str, const Args & ... args) {
auto va = fmt::make_format_args(args...);
auto va = fmt::make_args(args...);
return custom_vformat(format_str, va);
}

View File

@ -59,17 +59,16 @@ TEST(OStreamTest, Enum) {
}
struct TestArgFormatter : fmt::ArgFormatter<char> {
TestArgFormatter(fmt::writer &w, fmt::format_context &ctx,
fmt::format_specs &s)
TestArgFormatter(fmt::writer &w, fmt::context &ctx, fmt::format_specs &s)
: fmt::ArgFormatter<char>(w, ctx, s) {}
};
TEST(OStreamTest, CustomArg) {
fmt::MemoryWriter writer;
fmt::format_context ctx("}", fmt::format_args());
fmt::context ctx("}", fmt::format_args());
fmt::format_specs spec;
TestArgFormatter af(writer, ctx, spec);
visit(af, fmt::internal::make_arg<fmt::format_context>(TestEnum()));
visit(af, fmt::internal::make_arg<fmt::context>(TestEnum()));
EXPECT_EQ("TestEnum", writer.str());
}

View File

@ -69,7 +69,7 @@ struct Test {};
template <typename Char>
void format_value(fmt::basic_writer<Char> &w, Test,
fmt::basic_format_context<Char> &) {
fmt::basic_context<Char> &) {
w.write("test");
}
@ -487,7 +487,7 @@ VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) { \
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
fmt::visit(visitor, make_arg<fmt::basic_format_context<Char>>(value)); \
fmt::visit(visitor, make_arg<fmt::basic_context<Char>>(value)); \
}
#define CHECK_ARG(value) { \
@ -570,12 +570,12 @@ TEST(UtilTest, CustomArg) {
testing::Invoke([&](fmt::internal::CustomValue<char> custom) {
EXPECT_EQ(&test, custom.value);
fmt::MemoryWriter w;
fmt::format_context ctx("}", fmt::format_args());
fmt::context ctx("}", fmt::format_args());
custom.format(w, &test, &ctx);
EXPECT_EQ("test", w.str());
return Visitor::Result();
}));
fmt::visit(visitor, make_arg<fmt::format_context>(test));
fmt::visit(visitor, make_arg<fmt::context>(test));
}
TEST(ArgVisitorTest, VisitInvalidArg) {