From ece7ae5f49897a8a92ac2598d26e966914474621 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 26 Aug 2016 15:09:40 -0700 Subject: [PATCH] Make format_arg_store convertible to format_args --- fmt/format.cc | 6 +-- fmt/format.h | 112 +++++++++++++++++++++++++------------------------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/fmt/format.cc b/fmt/format.cc index 3b46ded2..c4d67d56 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -410,7 +410,7 @@ void internal::ArgMap::init(const format_args &args) { typedef internal::NamedArg NamedArg; const NamedArg *named_arg = 0; bool use_values = - args.type(format_args::MAX_PACKED_ARGS - 1) == internal::Arg::NONE; + args.type(MAX_PACKED_ARGS - 1) == internal::Arg::NONE; if (use_values) { for (unsigned i = 0;/*nothing*/; ++i) { internal::Arg::Type arg_type = args.type(i); @@ -427,14 +427,14 @@ void internal::ArgMap::init(const format_args &args) { } return; } - for (unsigned i = 0; i != format_args::MAX_PACKED_ARGS; ++i) { + for (unsigned i = 0; i != MAX_PACKED_ARGS; ++i) { internal::Arg::Type arg_type = args.type(i); if (arg_type == internal::Arg::NAMED_ARG) { named_arg = static_cast(args.args_[i].pointer); map_.push_back(Pair(named_arg->name, *named_arg)); } } - for (unsigned i = format_args::MAX_PACKED_ARGS;/*nothing*/; ++i) { + for (unsigned i = MAX_PACKED_ARGS;/*nothing*/; ++i) { switch (args.args_[i].type) { case internal::Arg::NONE: return; diff --git a/fmt/format.h b/fmt/format.h index 5e8680c7..e05fa09f 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -1394,6 +1394,47 @@ class RuntimeError : public std::runtime_error { template class ArgMap; + +template +constexpr uint64_t make_type() { + return type() | (make_type() << 4); +} + +template <> +constexpr uint64_t make_type() { return 0; } + +// Maximum number of arguments with packed types. +enum { MAX_PACKED_ARGS = 16 }; + +template +class format_arg_store { + private: + static const size_t NUM_ARGS = sizeof...(Args); + static const bool PACKED = NUM_ARGS <= MAX_PACKED_ARGS; + + typedef typename std::conditional::type value_type; + + // If the arguments are not packed, add one more element to mark the end. + std::array data_; + + template + friend format_arg_store make_format_args(const A & ... args); + + public: + static const uint64_t TYPES = make_type(); + + template + format_arg_store(const Args &... args, Formatter *) + : data_{{MakeValue(args)...}} {} + + const value_type *data() const { return data_.data(); } +}; + +template +inline format_arg_store make_format_args(const Args & ... args) { + Formatter *f = nullptr; + return format_arg_store(args..., f); +} } // namespace internal /** Formatting arguments. */ @@ -1422,23 +1463,24 @@ class format_args { template friend class internal::ArgMap; - public: - // Maximum number of arguments with packed types. - enum { MAX_PACKED_ARGS = 16 }; + void set_data(const internal::Value *values) { values_ = values; } + void set_data(const internal::Arg *args) { args_ = args; } + public: format_args() : types_(0) {} - format_args(ULongLong types, const internal::Value *values) - : types_(types), values_(values) {} - format_args(ULongLong types, const internal::Arg *args) - : types_(types), args_(args) {} + template + format_args(const internal::format_arg_store &store) + : types_(store.TYPES) { + set_data(store.data()); + } /** Returns the argument at specified index. */ internal::Arg operator[](unsigned index) const { using internal::Arg; Arg arg; - bool use_values = type(MAX_PACKED_ARGS - 1) == Arg::NONE; - if (index < MAX_PACKED_ARGS) { + bool use_values = type(internal::MAX_PACKED_ARGS - 1) == Arg::NONE; + if (index < internal::MAX_PACKED_ARGS) { Arg::Type arg_type = type(index); internal::Value &val = arg; if (arg_type != Arg::NONE) @@ -1452,7 +1494,7 @@ class format_args { arg.type = Arg::NONE; return arg; } - for (unsigned i = MAX_PACKED_ARGS; i <= index; ++i) { + for (unsigned i = internal::MAX_PACKED_ARGS; i <= index; ++i) { if (args_[i].type == Arg::NONE) return args_[i]; } @@ -2130,47 +2172,6 @@ class BasicFormatter : private internal::FormatterBase { # define FMT_GEN14(f) FMT_GEN13(f), f(13) # define FMT_GEN15(f) FMT_GEN14(f), f(14) -namespace internal { - -template -constexpr uint64_t make_type() { - return type() | (make_type() << 4); -} - -template <> -constexpr uint64_t make_type() { return 0; } - -template -class format_arg_store { - private: - static const size_t NUM_ARGS = sizeof...(Args); - static const bool PACKED = NUM_ARGS <= format_args::MAX_PACKED_ARGS; - - typedef typename std::conditional::type value_type; - - // If the arguments are not packed, add one more element to mark the end. - std::array data_; - - template - friend format_arg_store make_format_args(const A & ... args); - - public: - static const uint64_t TYPES = make_type(); - - template - format_arg_store(const Args &... args, Formatter *) - : data_{{MakeValue(args)...}} {} - - const value_type *data() const { return data_.data(); } -}; - -template -inline format_arg_store make_format_args(const Args & ... args) { - Formatter *f = nullptr; - return format_arg_store(args..., f); -} -} // namespace internal - # define FMT_MAKE_TEMPLATE_ARG(n) typename T##n # define FMT_MAKE_ARG_TYPE(n) T##n # define FMT_MAKE_ARG(n) const T##n &v##n @@ -2184,7 +2185,7 @@ inline format_arg_store make_format_args(const Args & ... args) { template \ void func(arg_type arg0, const Args & ... args) { \ auto store = fmt::internal::make_format_args< fmt::BasicFormatter >(args...); \ - func(arg0, fmt::format_args(store.TYPES, store.data())); \ + func(arg0, fmt::format_args(store)); \ } // Defines a variadic constructor. @@ -2192,7 +2193,7 @@ inline format_arg_store make_format_args(const Args & ... args) { template \ ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \ auto store = internal::make_format_args< fmt::BasicFormatter >(args...); \ - func(arg0, arg1, fmt::format_args(store.TYPES, store.data())); \ + func(arg0, arg1, fmt::format_args(store)); \ } // Generates a comma-separated list with results of applying f to pairs @@ -3141,7 +3142,7 @@ inline std::string vformat(CStringRef format_str, format_args args) { template inline std::string format(CStringRef format_str, const Args & ... args) { auto vargs = internal::make_format_args>(args...); - vformat(format_str, format_args(vargs.TYPES, vargs.data())); + return vformat(format_str, vargs); } inline std::wstring format(WCStringRef format_str, format_args args) { @@ -3336,8 +3337,7 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ const Args & ... args) { \ auto store = fmt::internal::make_format_args< fmt::BasicFormatter >(args...); \ - call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), \ - fmt::format_args(store.TYPES, store.data())); \ + call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::format_args(store)); \ } /**