diff --git a/include/fmt/core.h b/include/fmt/core.h index 50159808..6aa45877 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -31,10 +31,10 @@ #include #include #include +#include #include #include #include -#include #ifdef __has_feature # define FMT_HAS_FEATURE(x) __has_feature(x) @@ -223,8 +223,8 @@ class basic_string_view { } }; -typedef basic_string_view string_view; -typedef basic_string_view wstring_view; +using string_view = basic_string_view; +using wstring_view = basic_string_view; namespace internal { @@ -255,8 +255,8 @@ inline void require_wchar() { "formatting of wide characters into a narrow output is disallowed"); } -typedef char yes[1]; -typedef char no[2]; +using yes = char[1]; +using no = char[2]; yes &convert(unsigned long long); no &convert(...); @@ -389,7 +389,7 @@ struct string_value { template struct custom_value { - typedef void (*format_func)( + using format_func = void (*)( basic_buffer &buffer, const void *arg, Context &ctx); @@ -672,15 +672,28 @@ inline typename std::enable_if>::type return make_arg(value); } +template +struct named_arg : basic_arg { + typedef typename Context::char_type Char; + + basic_string_view name; + + template + named_arg(basic_string_view argname, const T &value) + : basic_arg(make_arg(value)), name(argname) {} +}; + template class arg_map { private: - typedef typename Context::char_type Char; - typedef std::vector< - std::pair, basic_arg > > MapType; - typedef typename MapType::value_type Pair; + using Char = typename Context::char_type; - MapType map_; + struct arg { + fmt::basic_string_view name; + basic_arg value; + }; + + std::vector map_; public: void init(const basic_format_args &args); @@ -688,10 +701,9 @@ class arg_map { const basic_arg *find(const fmt::basic_string_view &name) const { // The list is unsorted, so just return the first matching name. - for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); - it != end; ++it) { - if (it->first == name) - return &it->second; + for (auto it = map_.begin(), end = map_.end(); it != end; ++it) { + if (it->name == name) + return &it->value; } return 0; } @@ -703,7 +715,7 @@ class context_base : public basic_parse_context{ basic_format_args args_; protected: - typedef basic_arg format_arg; + using format_arg = basic_arg; context_base(basic_string_view format_str, basic_format_args args) @@ -747,9 +759,9 @@ class basic_context : FMT_DISALLOW_COPY_AND_ASSIGN(basic_context); - typedef internal::context_base> Base; + using Base = internal::context_base>; - typedef typename Base::format_arg format_arg; + using format_arg = typename Base::format_arg; using Base::get_arg; public: @@ -779,13 +791,11 @@ class arg_store { // Packed is a macro on MinGW so use IS_PACKED instead. static const bool IS_PACKED = NUM_ARGS < internal::MAX_PACKED_ARGS; - typedef typename Context::char_type char_type; - - typedef typename std::conditional, basic_arg>::type value_type; + using value_type = typename std::conditional, basic_arg>::type; // If the arguments are not packed, add one more element to mark the end. - typedef std::array Array; + using Array = std::array; Array data_; public: @@ -812,8 +822,8 @@ inline arg_store make_args(const Args & ... args) { template class basic_format_args { public: - typedef unsigned size_type; - typedef basic_arg format_arg; + using size_type = unsigned; + using format_arg = basic_arg ; private: // To reduce compiled code size per formatting function call, types of first @@ -875,8 +885,37 @@ class basic_format_args { } }; -typedef basic_format_args format_args; -typedef basic_format_args wformat_args; +using format_args = basic_format_args; +using wformat_args = basic_format_args; + +/** + \rst + Returns a named argument for formatting functions. + + **Example**:: + + print("Elapsed time: {s:.2f} seconds", arg("s", 1.23)); + + \endrst + */ +template +inline internal::named_arg arg(string_view name, const T &arg) { + return internal::named_arg(name, arg); +} + +template +inline internal::named_arg arg(wstring_view name, const T &arg) { + return internal::named_arg(name, arg); +} + +// The following two functions are deleted intentionally to disable +// nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. +template +void arg(string_view, const internal::named_arg&) + FMT_DELETED_OR_UNDEFINED; +template +void arg(wstring_view, const internal::named_arg&) + FMT_DELETED_OR_UNDEFINED; enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; @@ -942,8 +981,7 @@ FMT_API void vprint(std::FILE *f, string_view format_str, format_args args); \endrst */ template -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) { vprint(f, format_str, make_args(args...)); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 41ffffb0..eddb580d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -919,20 +919,6 @@ constexpr typename std::result_of::type return typename std::result_of::type(); } -namespace internal { - -template -struct named_arg : basic_arg { - typedef typename Context::char_type Char; - - basic_string_view name; - - template - named_arg(basic_string_view argname, const T &value) - : basic_arg(make_arg(value)), name(argname) {} -}; -} // namespace internal - enum alignment { ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC }; @@ -1195,7 +1181,7 @@ void arg_map::init(const basic_format_args &args) { return; case internal::NAMED_ARG: named_arg = static_cast(args.values_[i].pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); + map_.push_back(arg{named_arg->name, *named_arg}); break; default: break; // Do nothing. @@ -1207,7 +1193,7 @@ void arg_map::init(const basic_format_args &args) { internal::type arg_type = args.type(i); if (arg_type == internal::NAMED_ARG) { named_arg = static_cast(args.args_[i].value_.pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); + map_.push_back(arg{named_arg->name, *named_arg}); } } for (unsigned i = MAX_PACKED_ARGS; ; ++i) { @@ -1216,7 +1202,7 @@ void arg_map::init(const basic_format_args &args) { return; case internal::NAMED_ARG: named_arg = static_cast(args.args_[i].value_.pointer); - map_.push_back(Pair(named_arg->name, *named_arg)); + map_.push_back(arg{named_arg->name, *named_arg}); break; default: break; // Do nothing. @@ -2869,35 +2855,6 @@ inline void format_decimal(char *&buffer, T value) { buffer += num_digits; } -/** - \rst - Returns a named argument for formatting functions. - - **Example**:: - - print("Elapsed time: {s:.2f} seconds", arg("s", 1.23)); - - \endrst - */ -template -inline internal::named_arg arg(string_view name, const T &arg) { - return internal::named_arg(name, arg); -} - -template -inline internal::named_arg arg(wstring_view name, const T &arg) { - return internal::named_arg(name, arg); -} - -// The following two functions are deleted intentionally to disable -// nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. -template -void arg(string_view, const internal::named_arg&) - FMT_DELETED_OR_UNDEFINED; -template -void arg(wstring_view, const internal::named_arg&) - FMT_DELETED_OR_UNDEFINED; - // Formatter of objects of type T. template struct formatter<