basic_arg -> basic_format_arg, arg_store -> format_arg_store

This commit is contained in:
Victor Zverovich 2018-04-04 07:38:21 -07:00
parent 4975297eb0
commit 23759b2688
5 changed files with 69 additions and 65 deletions

View File

@ -289,7 +289,7 @@ typedef basic_string_view<char> string_view;
typedef basic_string_view<wchar_t> wstring_view; typedef basic_string_view<wchar_t> wstring_view;
template <typename Context> template <typename Context>
class basic_arg; class basic_format_arg;
template <typename Context> template <typename Context>
class basic_format_args; class basic_format_args;
@ -546,7 +546,7 @@ struct typed_value : value<Context> {
}; };
template <typename Context, typename T> template <typename Context, typename T>
FMT_CONSTEXPR basic_arg<Context> make_arg(const T &value); FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T &value);
#define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \ #define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
template <typename C> \ template <typename C> \
@ -641,7 +641,7 @@ inline typename std::enable_if<
template <typename C, typename T> template <typename C, typename T>
typed_value<C, name_arg_type> typed_value<C, name_arg_type>
make_value(const named_arg<T, typename C::char_type> &val) { make_value(const named_arg<T, typename C::char_type> &val) {
basic_arg<C> arg = make_arg<C>(val.value); basic_format_arg<C> arg = make_arg<C>(val.value);
std::memcpy(val.data, &arg, sizeof(arg)); std::memcpy(val.data, &arg, sizeof(arg));
return static_cast<const void*>(&val); return static_cast<const void*>(&val);
} }
@ -666,17 +666,18 @@ struct result_of<F(Args...)> {
// A formatting argument. It is a trivially copyable/constructible type to // A formatting argument. It is a trivially copyable/constructible type to
// allow storage in basic_memory_buffer. // allow storage in basic_memory_buffer.
template <typename Context> template <typename Context>
class basic_arg { class basic_format_arg {
private: private:
internal::value<Context> value_; internal::value<Context> value_;
internal::type type_; internal::type type_;
template <typename ContextType, typename T> template <typename ContextType, typename T>
friend FMT_CONSTEXPR basic_arg<ContextType> internal::make_arg(const T &value); friend FMT_CONSTEXPR basic_format_arg<ContextType>
internal::make_arg(const T &value);
template <typename Visitor, typename Ctx> template <typename Visitor, typename Ctx>
friend FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type friend FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
visit(Visitor &&vis, basic_arg<Ctx> arg); visit(Visitor &&vis, basic_format_arg<Ctx> arg);
friend class basic_format_args<Context>; friend class basic_format_args<Context>;
friend class internal::arg_map<Context>; friend class internal::arg_map<Context>;
@ -694,7 +695,7 @@ class basic_arg {
internal::custom_value<Context> custom_; internal::custom_value<Context> custom_;
}; };
FMT_CONSTEXPR basic_arg() : type_(internal::none_type) {} FMT_CONSTEXPR basic_format_arg() : type_(internal::none_type) {}
FMT_EXPLICIT operator bool() const FMT_NOEXCEPT { FMT_EXPLICIT operator bool() const FMT_NOEXCEPT {
return type_ != internal::none_type; return type_ != internal::none_type;
@ -771,7 +772,7 @@ class arg_map {
struct entry { struct entry {
basic_string_view<char_type> name; basic_string_view<char_type> name;
basic_arg<Context> arg; basic_format_arg<Context> arg;
}; };
entry *map_; entry *map_;
@ -788,13 +789,13 @@ class arg_map {
void init(const basic_format_args<Context> &args); void init(const basic_format_args<Context> &args);
~arg_map() { delete [] map_; } ~arg_map() { delete [] map_; }
basic_arg<Context> find(basic_string_view<char_type> name) const { basic_format_arg<Context> find(basic_string_view<char_type> name) const {
// The list is unsorted, so just return the first matching name. // The list is unsorted, so just return the first matching name.
for (auto it = map_, end = map_ + size_; it != end; ++it) { for (auto it = map_, end = map_ + size_; it != end; ++it) {
if (it->name == name) if (it->name == name)
return it->arg; return it->arg;
} }
return basic_arg<Context>(); return basic_format_arg<Context>();
} }
}; };
@ -810,7 +811,7 @@ class context_base {
protected: protected:
typedef Char char_type; typedef Char char_type;
typedef basic_arg<Context> format_arg; typedef basic_format_arg<Context> format_arg;
context_base(OutputIt out, basic_string_view<char_type> format_str, context_base(OutputIt out, basic_string_view<char_type> format_str,
basic_format_args<Context> args) basic_format_args<Context> args)
@ -932,8 +933,8 @@ FMT_CONSTEXPR uint64_t get_types() {
} }
template <typename Context, typename T> template <typename Context, typename T>
FMT_CONSTEXPR basic_arg<Context> make_arg(const T &value) { FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T &value) {
basic_arg<Context> arg; basic_format_arg<Context> arg;
arg.type_ = get_type<Context, T>::value; arg.type_ = get_type<Context, T>::value;
arg.value_ = make_value<Context>(value); arg.value_ = make_value<Context>(value);
return arg; return arg;
@ -946,7 +947,7 @@ inline typename std::enable_if<IS_PACKED, value<Context>>::type
} }
template <bool IS_PACKED, typename Context, typename T> template <bool IS_PACKED, typename Context, typename T>
inline typename std::enable_if<!IS_PACKED, basic_arg<Context>>::type inline typename std::enable_if<!IS_PACKED, basic_format_arg<Context>>::type
make_arg(const T &value) { make_arg(const T &value) {
return make_arg<Context>(value); return make_arg<Context>(value);
} }
@ -960,15 +961,15 @@ inline typename std::enable_if<!IS_PACKED, basic_arg<Context>>::type
\endrst \endrst
*/ */
template <typename Context, typename ...Args> template <typename Context, typename ...Args>
class arg_store { class format_arg_store {
private: private:
static const size_t NUM_ARGS = sizeof...(Args); static const size_t NUM_ARGS = sizeof...(Args);
// Packed is a macro on MinGW so use IS_PACKED instead. // Packed is a macro on MinGW so use IS_PACKED instead.
static const bool IS_PACKED = NUM_ARGS < internal::max_packed_args; static const bool IS_PACKED = NUM_ARGS < internal::max_packed_args;
typedef typename std::conditional< typedef typename std::conditional<IS_PACKED,
IS_PACKED, internal::value<Context>, basic_arg<Context>>::type value_type; internal::value<Context>, basic_format_arg<Context>>::type value_type;
// If the arguments are not packed, add one more element to mark the end. // If the arguments are not packed, add one more element to mark the end.
value_type data_[NUM_ARGS + (IS_PACKED && NUM_ARGS != 0 ? 0 : 1)]; value_type data_[NUM_ARGS + (IS_PACKED && NUM_ARGS != 0 ? 0 : 1)];
@ -980,11 +981,11 @@ class arg_store {
#if FMT_GCC_VERSION && FMT_GCC_VERSION <= 405 #if FMT_GCC_VERSION && FMT_GCC_VERSION <= 405
// Workaround an array initialization bug in gcc 4.5 and earlier. // Workaround an array initialization bug in gcc 4.5 and earlier.
arg_store(const Args &... args) { format_arg_store(const Args &... args) {
data_ = {internal::make_arg<IS_PACKED, Context>(args)...}; data_ = {internal::make_arg<IS_PACKED, Context>(args)...};
} }
#else #else
arg_store(const Args &... args) format_arg_store(const Args &... args)
: data_{internal::make_arg<IS_PACKED, Context>(args)...} {} : data_{internal::make_arg<IS_PACKED, Context>(args)...} {}
#endif #endif
@ -992,25 +993,25 @@ class arg_store {
}; };
template <typename Context, typename ...Args> template <typename Context, typename ...Args>
const uint64_t arg_store<Context, Args...>::TYPES = IS_PACKED ? const uint64_t format_arg_store<Context, Args...>::TYPES = IS_PACKED ?
internal::get_types<Context, Args...>() : internal::get_types<Context, Args...>() :
-static_cast<int64_t>(NUM_ARGS); -static_cast<int64_t>(NUM_ARGS);
/** /**
\rst \rst
Constructs an `~fmt::arg_store` object that contains references to arguments Constructs an `~fmt::format_arg_store` object that contains references to
and can be implicitly converted to `~fmt::format_args`. `Context` can be arguments and can be implicitly converted to `~fmt::format_args`. `Context` can
omitted in which case it defaults to `~fmt::context`. be omitted in which case it defaults to `~fmt::context`.
\endrst \endrst
*/ */
template <typename Context, typename ...Args> template <typename Context, typename ...Args>
inline arg_store<Context, Args...> make_args(const Args & ... args) { inline format_arg_store<Context, Args...> make_args(const Args & ... args) {
return arg_store<Context, Args...>(args...); return format_arg_store<Context, Args...>(args...);
} }
template <typename ...Args> template <typename ...Args>
inline arg_store<context, Args...> make_args(const Args & ... args) { inline format_arg_store<context, Args...> make_args(const Args & ... args) {
return arg_store<context, Args...>(args...); return format_arg_store<context, Args...>(args...);
} }
/** Formatting arguments. */ /** Formatting arguments. */
@ -1018,7 +1019,7 @@ template <typename Context>
class basic_format_args { class basic_format_args {
public: public:
typedef unsigned size_type; typedef unsigned size_type;
typedef basic_arg<Context> format_arg; typedef basic_format_arg<Context> format_arg;
private: private:
// To reduce compiled code size per formatting function call, types of first // To reduce compiled code size per formatting function call, types of first
@ -1068,11 +1069,11 @@ class basic_format_args {
/** /**
\rst \rst
Constructs a `basic_format_args` object from `~fmt::arg_store`. Constructs a `basic_format_args` object from `~fmt::format_arg_store`.
\endrst \endrst
*/ */
template <typename... Args> template <typename... Args>
basic_format_args(const arg_store<Context, Args...> &store) basic_format_args(const format_arg_store<Context, Args...> &store)
: types_(store.TYPES) { : types_(store.TYPES) {
set_data(store.data_); set_data(store.data_);
} }
@ -1107,14 +1108,14 @@ struct named_arg_base {
basic_string_view<Char> name; basic_string_view<Char> name;
// Serialized value<context>. // Serialized value<context>.
mutable char data[sizeof(basic_arg<context>)]; mutable char data[sizeof(basic_format_arg<context>)];
named_arg_base(basic_string_view<Char> nm) : name(nm) {} named_arg_base(basic_string_view<Char> nm) : name(nm) {}
template <typename Context> template <typename Context>
basic_arg<Context> deserialize() const { basic_format_arg<Context> deserialize() const {
basic_arg<Context> arg; basic_format_arg<Context> arg;
std::memcpy(&arg, data, sizeof(basic_arg<Context>)); std::memcpy(&arg, data, sizeof(basic_format_arg<Context>));
return arg; return arg;
} }
}; };
@ -1212,12 +1213,12 @@ inline std::string format(string_view format_str, const Args & ... args) {
// This should be just // This should be just
// return vformat(format_str, make_args(args...)); // return vformat(format_str, make_args(args...));
// but gcc has trouble optimizing the latter, so break it down. // but gcc has trouble optimizing the latter, so break it down.
arg_store<context, Args...> as(args...); format_arg_store<context, Args...> as(args...);
return vformat(format_str, as); return vformat(format_str, as);
} }
template <typename... Args> template <typename... Args>
inline std::wstring format(wstring_view format_str, const Args & ... args) { inline std::wstring format(wstring_view format_str, const Args & ... args) {
arg_store<wcontext, Args...> as(args...); format_arg_store<wcontext, Args...> as(args...);
return vformat(format_str, as); return vformat(format_str, as);
} }
@ -1234,7 +1235,7 @@ FMT_API void vprint(std::FILE *f, string_view format_str, format_args args);
*/ */
template <typename... Args> template <typename... Args>
inline void print(std::FILE *f, string_view format_str, const Args & ... args) { inline void print(std::FILE *f, string_view format_str, const Args & ... args) {
arg_store<context, Args...> as(args...); format_arg_store<context, Args...> as(args...);
vprint(f, format_str, as); vprint(f, format_str, as);
} }
@ -1251,7 +1252,7 @@ FMT_API void vprint(string_view format_str, format_args args);
*/ */
template <typename... Args> template <typename... Args>
inline void print(string_view format_str, const Args & ... args) { inline void print(string_view format_str, const Args & ... args) {
arg_store<context, Args...> as(args...); format_arg_store<context, Args...> as(args...);
vprint(format_str, as); vprint(format_str, as);
} }
} // namespace fmt } // namespace fmt

View File

@ -1068,7 +1068,7 @@ struct monostate {};
*/ */
template <typename Visitor, typename Context> template <typename Visitor, typename Context>
FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
visit(Visitor &&vis, basic_arg<Context> arg) { visit(Visitor &&vis, basic_format_arg<Context> arg) {
typedef typename Context::char_type char_type; typedef typename Context::char_type char_type;
switch (arg.type_) { switch (arg.type_) {
case internal::none_type: case internal::none_type:
@ -1100,7 +1100,7 @@ FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
case internal::pointer_type: case internal::pointer_type:
return vis(arg.value_.pointer); return vis(arg.value_.pointer);
case internal::custom_type: case internal::custom_type:
return vis(typename basic_arg<Context>::handle(arg.value_.custom)); return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
} }
return vis(monostate()); return vis(monostate());
} }
@ -1560,7 +1560,7 @@ class custom_formatter: public function<bool> {
public: public:
explicit custom_formatter(Context &ctx): ctx_(ctx) {} explicit custom_formatter(Context &ctx): ctx_(ctx) {}
bool operator()(typename basic_arg<Context>::handle h) const { bool operator()(typename basic_format_arg<Context>::handle h) const {
h.format(ctx_); h.format(ctx_);
return true; return true;
} }
@ -1726,7 +1726,7 @@ class specs_checker : public Handler {
template <template <typename> class Handler, typename T, template <template <typename> class Handler, typename T,
typename Context, typename ErrorHandler> typename Context, typename ErrorHandler>
FMT_CONSTEXPR void set_dynamic_spec( FMT_CONSTEXPR void set_dynamic_spec(
T &value, basic_arg<Context> arg, ErrorHandler eh) { T &value, basic_format_arg<Context> arg, ErrorHandler eh) {
unsigned long long big_value = visit(Handler<ErrorHandler>(eh), arg); unsigned long long big_value = visit(Handler<ErrorHandler>(eh), arg);
if (big_value > (std::numeric_limits<int>::max)()) if (big_value > (std::numeric_limits<int>::max)())
eh.on_error("number is too big"); eh.on_error("number is too big");
@ -1761,12 +1761,12 @@ class specs_handler: public specs_setter<typename Context::char_type> {
} }
private: private:
FMT_CONSTEXPR basic_arg<Context> get_arg(auto_id) { FMT_CONSTEXPR basic_format_arg<Context> get_arg(auto_id) {
return context_.next_arg(); return context_.next_arg();
} }
template <typename Id> template <typename Id>
FMT_CONSTEXPR basic_arg<Context> get_arg(Id arg_id) { FMT_CONSTEXPR basic_format_arg<Context> get_arg(Id arg_id) {
context_.parse_context().check_arg_id(arg_id); context_.parse_context().check_arg_id(arg_id);
return context_.get_arg(arg_id); return context_.get_arg(arg_id);
} }
@ -2212,7 +2212,7 @@ class arg_formatter:
using base::operator(); using base::operator();
/** Formats an argument of a user-defined type. */ /** Formats an argument of a user-defined type. */
iterator operator()(typename basic_arg<context_type>::handle handle) { iterator operator()(typename basic_format_arg<context_type>::handle handle) {
handle.format(ctx_); handle.format(ctx_);
return this->out(); return this->out();
} }
@ -3256,7 +3256,7 @@ struct format_handler : internal::error_handler {
} }
Context context; Context context;
basic_arg<Context> arg; basic_format_arg<Context> arg;
}; };
/** Formats arguments and writes the output to the range. */ /** Formats arguments and writes the output to the range. */
@ -3473,7 +3473,8 @@ inline typename std::enable_if<internal::is_format_string<String>::value>::type
// Counts the number of characters in the output of format(format_str, args...). // Counts the number of characters in the output of format(format_str, args...).
template <typename... Args> template <typename... Args>
inline std::size_t count(string_view format_str, const Args & ... args) { inline std::size_t formatted_size(string_view format_str,
const Args & ... args) {
auto it = format_to(internal::counting_iterator<char>(), format_str, args...); auto it = format_to(internal::counting_iterator<char>(), format_str, args...);
return it.count(); return it.count();
} }

View File

@ -81,11 +81,11 @@ class arg_converter: public function<void> {
private: private:
typedef typename Context::char_type Char; typedef typename Context::char_type Char;
basic_arg<Context> &arg_; basic_format_arg<Context> &arg_;
typename Context::char_type type_; typename Context::char_type type_;
public: public:
arg_converter(basic_arg<Context> &arg, Char type) arg_converter(basic_format_arg<Context> &arg, Char type)
: arg_(arg), type_(type) {} : arg_(arg), type_(type) {}
void operator()(bool value) { void operator()(bool value) {
@ -133,7 +133,7 @@ class arg_converter: public function<void> {
// 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 Context, typename Char> template <typename T, typename Context, typename Char>
void convert_arg(basic_arg<Context> &arg, Char type) { void convert_arg(basic_format_arg<Context> &arg, Char type) {
visit(arg_converter<T, Context>(arg, type), arg); visit(arg_converter<T, Context>(arg, type), arg);
} }
@ -141,12 +141,12 @@ void convert_arg(basic_arg<Context> &arg, Char type) {
template <typename Context> template <typename Context>
class char_converter: public function<void> { class char_converter: public function<void> {
private: private:
basic_arg<Context> &arg_; basic_format_arg<Context> &arg_;
FMT_DISALLOW_COPY_AND_ASSIGN(char_converter); FMT_DISALLOW_COPY_AND_ASSIGN(char_converter);
public: public:
explicit char_converter(basic_arg<Context> &arg) : arg_(arg) {} explicit char_converter(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
@ -286,7 +286,7 @@ class printf_arg_formatter:
} }
/** Formats an argument of a custom (user-defined) type. */ /** Formats an argument of a custom (user-defined) type. */
iterator operator()(typename basic_arg<context_type>::handle handle) { iterator operator()(typename basic_format_arg<context_type>::handle handle) {
handle.format(context_); handle.format(context_);
return this->out(); return this->out();
} }
@ -545,8 +545,10 @@ struct printf_context {
}; };
template <typename ...Args> template <typename ...Args>
inline arg_store<printf_context<internal::buffer>::type, Args...> make_printf_args(const Args & ... args) { inline format_arg_store<printf_context<internal::buffer>::type, Args...>
return arg_store<printf_context<internal::buffer>::type, Args...>(args...); make_printf_args(const Args & ... args) {
return format_arg_store<printf_context<internal::buffer>::type, Args...>(
args...);
} }
typedef basic_format_args<printf_context<internal::buffer>::type> printf_args; typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;

View File

@ -1421,7 +1421,7 @@ class mock_arg_formatter:
return base::operator()(value); return base::operator()(value);
} }
iterator operator()(fmt::basic_arg<fmt::context>::handle) { iterator operator()(fmt::basic_format_arg<fmt::context>::handle) {
return base::operator()(fmt::monostate()); return base::operator()(fmt::monostate());
} }
}; };
@ -1506,8 +1506,8 @@ TEST(FormatTest, OutputIterators) {
EXPECT_EQ("42", s.str()); EXPECT_EQ("42", s.str());
} }
TEST(FormatTest, OutputSize) { TEST(FormatTest, FormattedSize) {
EXPECT_EQ(2u, fmt::count("{}", 42)); EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
} }
TEST(FormatTest, FormatToN) { TEST(FormatTest, FormatToN) {
@ -1633,12 +1633,12 @@ TEST(FormatTest, ConstexprParseFormatSpecs) {
struct test_context { struct test_context {
typedef char char_type; typedef char char_type;
FMT_CONSTEXPR fmt::basic_arg<test_context> next_arg() { FMT_CONSTEXPR fmt::basic_format_arg<test_context> next_arg() {
return fmt::internal::make_arg<test_context>(11); return fmt::internal::make_arg<test_context>(11);
} }
template <typename Id> template <typename Id>
FMT_CONSTEXPR fmt::basic_arg<test_context> get_arg(Id) { FMT_CONSTEXPR fmt::basic_format_arg<test_context> get_arg(Id) {
return fmt::internal::make_arg<test_context>(22); return fmt::internal::make_arg<test_context>(22);
} }

View File

@ -33,7 +33,7 @@
#undef min #undef min
#undef max #undef max
using fmt::basic_arg; using fmt::basic_format_arg;
using fmt::internal::basic_buffer; using fmt::internal::basic_buffer;
using fmt::basic_memory_buffer; using fmt::basic_memory_buffer;
using fmt::string_view; using fmt::string_view;
@ -48,7 +48,7 @@ namespace {
struct Test {}; struct Test {};
template <typename Context, typename T> template <typename Context, typename T>
basic_arg<Context> make_arg(const T &value) { basic_format_arg<Context> make_arg(const T &value) {
return fmt::internal::make_arg<Context>(value); return fmt::internal::make_arg<Context>(value);
} }
} // namespace } // namespace
@ -579,7 +579,7 @@ TEST(UtilTest, PointerArg) {
} }
struct check_custom { struct check_custom {
Result operator()(fmt::basic_arg<fmt::context>::handle h) const { Result operator()(fmt::basic_format_arg<fmt::context>::handle h) const {
fmt::memory_buffer buffer; fmt::memory_buffer buffer;
fmt::internal::basic_buffer<char> &base = buffer; fmt::internal::basic_buffer<char> &base = buffer;
fmt::context ctx(std::back_inserter(base), "", fmt::format_args()); fmt::context ctx(std::back_inserter(base), "", fmt::format_args());
@ -591,7 +591,7 @@ struct check_custom {
TEST(UtilTest, CustomArg) { TEST(UtilTest, CustomArg) {
::Test test; ::Test test;
typedef MockVisitor<fmt::basic_arg<fmt::context>::handle> visitor; typedef MockVisitor<fmt::basic_format_arg<fmt::context>::handle> visitor;
testing::StrictMock<visitor> v; testing::StrictMock<visitor> v;
EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom())); EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke(check_custom()));
fmt::visit(v, make_arg<fmt::context>(test)); fmt::visit(v, make_arg<fmt::context>(test));
@ -601,7 +601,7 @@ TEST(ArgVisitorTest, VisitInvalidArg) {
typedef MockVisitor<fmt::monostate> Visitor; typedef MockVisitor<fmt::monostate> Visitor;
testing::StrictMock<Visitor> visitor; testing::StrictMock<Visitor> visitor;
EXPECT_CALL(visitor, visit(_)); EXPECT_CALL(visitor, visit(_));
fmt::basic_arg<fmt::context> arg; fmt::basic_format_arg<fmt::context> arg;
visit(visitor, arg); visit(visitor, arg);
} }