1 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
2 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
9 #include "absl/base/port.h"
10 #include "absl/strings/internal/str_format/arg.h"
11 #include "absl/strings/internal/str_format/checker.h"
12 #include "absl/strings/internal/str_format/parser.h"
13 #include "absl/types/span.h"
17 class UntypedFormatSpec;
19 namespace str_format_internal {
21 class BoundConversion : public ConversionSpec {
23 const FormatArgImpl* arg() const { return arg_; }
24 void set_arg(const FormatArgImpl* a) { arg_ = a; }
27 const FormatArgImpl* arg_;
30 // This is the type-erased class that the implementation uses.
31 class UntypedFormatSpecImpl {
33 UntypedFormatSpecImpl() = delete;
35 explicit UntypedFormatSpecImpl(string_view s)
36 : data_(s.data()), size_(s.size()) {}
37 explicit UntypedFormatSpecImpl(
38 const str_format_internal::ParsedFormatBase* pc)
39 : data_(pc), size_(~size_t{}) {}
41 bool has_parsed_conversion() const { return size_ == ~size_t{}; }
43 string_view str() const {
44 assert(!has_parsed_conversion());
45 return string_view(static_cast<const char*>(data_), size_);
47 const str_format_internal::ParsedFormatBase* parsed_conversion() const {
48 assert(has_parsed_conversion());
49 return static_cast<const str_format_internal::ParsedFormatBase*>(data_);
53 static const UntypedFormatSpecImpl& Extract(const T& s) {
62 template <typename T, typename...>
63 struct MakeDependent {
67 // Implicitly convertible from `const char*`, `string_view`, and the
68 // `ExtendedParsedFormat` type. This abstraction allows all format functions to
69 // operate on any without providing too many overloads.
70 template <typename... Args>
71 class FormatSpecTemplate
72 : public MakeDependent<UntypedFormatSpec, Args...>::type {
73 using Base = typename MakeDependent<UntypedFormatSpec, Args...>::type;
76 #if ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
78 // Honeypot overload for when the std::string is not constexpr.
79 // We use the 'unavailable' attribute to give a better compiler error than
80 // just 'method is deleted'.
81 FormatSpecTemplate(...) // NOLINT
82 __attribute__((unavailable("Format std::string is not constexpr.")));
84 // Honeypot overload for when the format is constexpr and invalid.
85 // We use the 'unavailable' attribute to give a better compiler error than
86 // just 'method is deleted'.
87 // To avoid checking the format twice, we just check that the format is
88 // constexpr. If is it valid, then the overload below will kick in.
89 // We add the template here to make this overload have lower priority.
90 template <typename = void>
91 FormatSpecTemplate(const char* s) // NOLINT
93 enable_if(str_format_internal::EnsureConstexpr(s), "constexpr trap"),
95 "Format specified does not match the arguments passed.")));
97 template <typename T = void>
98 FormatSpecTemplate(string_view s) // NOLINT
99 __attribute__((enable_if(str_format_internal::EnsureConstexpr(s),
100 "constexpr trap"))) {
101 static_assert(sizeof(T*) == 0,
102 "Format specified does not match the arguments passed.");
105 // Good format overload.
106 FormatSpecTemplate(const char* s) // NOLINT
107 __attribute__((enable_if(ValidFormatImpl<ArgumentToConv<Args>()...>(s),
111 FormatSpecTemplate(string_view s) // NOLINT
112 __attribute__((enable_if(ValidFormatImpl<ArgumentToConv<Args>()...>(s),
116 #else // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
118 FormatSpecTemplate(const char* s) : Base(s) {} // NOLINT
119 FormatSpecTemplate(string_view s) : Base(s) {} // NOLINT
121 #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
123 template <Conv... C, typename = typename std::enable_if<
124 sizeof...(C) == sizeof...(Args) &&
125 AllOf(Contains(ArgumentToConv<Args>(),
127 FormatSpecTemplate(const ExtendedParsedFormat<C...>& pc) // NOLINT
131 template <typename... Args>
132 struct FormatSpecDeductionBarrier {
133 using type = FormatSpecTemplate<Args...>;
138 Streamable(const UntypedFormatSpecImpl& format,
139 absl::Span<const FormatArgImpl> args)
141 if (args.size() <= ABSL_ARRAYSIZE(few_args_)) {
142 for (size_t i = 0; i < args.size(); ++i) {
143 few_args_[i] = args[i];
145 args_ = absl::MakeSpan(few_args_, args.size());
147 many_args_.assign(args.begin(), args.end());
152 std::ostream& Print(std::ostream& os) const;
154 friend std::ostream& operator<<(std::ostream& os, const Streamable& l) {
159 const UntypedFormatSpecImpl& format_;
160 absl::Span<const FormatArgImpl> args_;
161 // if args_.size() is 4 or less:
162 FormatArgImpl few_args_[4] = {FormatArgImpl(0), FormatArgImpl(0),
163 FormatArgImpl(0), FormatArgImpl(0)};
164 // if args_.size() is more than 4:
165 std::vector<FormatArgImpl> many_args_;
169 std::string Summarize(UntypedFormatSpecImpl format,
170 absl::Span<const FormatArgImpl> args);
171 bool BindWithPack(const UnboundConversion* props,
172 absl::Span<const FormatArgImpl> pack, BoundConversion* bound);
174 bool FormatUntyped(FormatRawSinkImpl raw_sink,
175 UntypedFormatSpecImpl format,
176 absl::Span<const FormatArgImpl> args);
178 std::string& AppendPack(std::string* out, UntypedFormatSpecImpl format,
179 absl::Span<const FormatArgImpl> args);
181 inline std::string FormatPack(const UntypedFormatSpecImpl format,
182 absl::Span<const FormatArgImpl> args) {
184 AppendPack(&out, format, args);
188 int FprintF(std::FILE* output, UntypedFormatSpecImpl format,
189 absl::Span<const FormatArgImpl> args);
190 int SnprintF(char* output, size_t size, UntypedFormatSpecImpl format,
191 absl::Span<const FormatArgImpl> args);
193 // Returned by Streamed(v). Converts via '%s' to the std::string created
194 // by std::ostream << v.
195 template <typename T>
196 class StreamedWrapper {
198 explicit StreamedWrapper(const T& v) : v_(v) { }
201 template <typename S>
202 friend ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<S>& v,
204 FormatSinkImpl* out);
208 } // namespace str_format_internal
211 #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_