#ifndef LLVM_SUPPORT_FORMATVARIADIC_H
#define LLVM_SUPPORT_FORMATVARIADIC_H
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
class formatv_object_base {
protected:
- // The parameters are stored in a std::tuple, which does not provide runtime
- // indexing capabilities. In order to enable runtime indexing, we use this
- // structure to put the parameters into a std::vector. Since the parameters
- // are not all the same type, we use some type-erasure by wrapping the
- // parameters in a template class that derives from a non-template superclass.
- // Essentially, we are converting a std::tuple<Derived<Ts...>> to a
- // std::vector<Base*>.
- struct create_adapters {
- template <typename... Ts>
- std::vector<detail::format_adapter *> operator()(Ts &... Items) {
- return std::vector<detail::format_adapter *>{&Items...};
- }
- };
-
StringRef Fmt;
- std::vector<detail::format_adapter *> Adapters;
- std::vector<ReplacementItem> Replacements;
+ ArrayRef<detail::format_adapter *> Adapters;
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
size_t &Align, char &Pad);
static std::pair<ReplacementItem, StringRef>
splitLiteralAndReplacement(StringRef Fmt);
-public:
- formatv_object_base(StringRef Fmt, std::size_t ParamCount)
- : Fmt(Fmt), Replacements(parseFormatString(Fmt)) {
- Adapters.reserve(ParamCount);
- }
+ formatv_object_base(StringRef Fmt,
+ ArrayRef<detail::format_adapter *> Adapters)
+ : Fmt(Fmt), Adapters(Adapters) {}
formatv_object_base(formatv_object_base const &rhs) = delete;
+ formatv_object_base(formatv_object_base &&rhs) = default;
- formatv_object_base(formatv_object_base &&rhs)
- : Fmt(std::move(rhs.Fmt)),
- Adapters(), // Adapters are initialized by formatv_object
- Replacements(std::move(rhs.Replacements)) {
- Adapters.reserve(rhs.Adapters.size());
- };
-
+public:
void format(raw_ostream &S) const {
- for (auto &R : Replacements) {
+ for (auto &R : parseFormatString(Fmt)) {
if (R.Type == ReplacementType::Empty)
continue;
if (R.Type == ReplacementType::Literal) {
Align.format(S, R.Options);
}
}
- static std::vector<ReplacementItem> parseFormatString(StringRef Fmt);
+ static SmallVector<ReplacementItem, 2> parseFormatString(StringRef Fmt);
static Optional<ReplacementItem> parseReplacementItem(StringRef Spec);
// of the parameters, we have to own the storage for the parameters here, and
// have the base class store type-erased pointers into this tuple.
Tuple Parameters;
+ std::array<detail::format_adapter *, std::tuple_size<Tuple>::value>
+ ParameterPointers;
+
+ // The parameters are stored in a std::tuple, which does not provide runtime
+ // indexing capabilities. In order to enable runtime indexing, we use this
+ // structure to put the parameters into a std::array. Since the parameters
+ // are not all the same type, we use some type-erasure by wrapping the
+ // parameters in a template class that derives from a non-template superclass.
+ // Essentially, we are converting a std::tuple<Derived<Ts...>> to a
+ // std::array<Base*>.
+ struct create_adapters {
+ template <typename... Ts>
+ std::array<detail::format_adapter *, std::tuple_size<Tuple>::value>
+ operator()(Ts &... Items) {
+ return {&Items...};
+ }
+ };
public:
formatv_object(StringRef Fmt, Tuple &&Params)
- : formatv_object_base(Fmt, std::tuple_size<Tuple>::value),
+ : formatv_object_base(Fmt, ParameterPointers),
Parameters(std::move(Params)) {
- Adapters = apply_tuple(create_adapters(), Parameters);
+ ParameterPointers = apply_tuple(create_adapters(), Parameters);
}
formatv_object(formatv_object const &rhs) = delete;
formatv_object(formatv_object &&rhs)
: formatv_object_base(std::move(rhs)),
Parameters(std::move(rhs.Parameters)) {
- Adapters = apply_tuple(create_adapters(), Parameters);
+ ParameterPointers = apply_tuple(create_adapters(), Parameters);
+ Adapters = ParameterPointers;
}
};
// assertion. Otherwise, it will try to do something reasonable, but in general
// the details of what that is are undefined.
//
-template <typename... Ts>
-inline auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object<decltype(
- std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...))> {
+template <typename... Ts> inline auto formatv(const char *Fmt, Ts &&... Vals) {
using ParamTuple = decltype(
std::make_tuple(detail::build_format_adapter(std::forward<Ts>(Vals))...));
return formatv_object<ParamTuple>(