Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / abseil-cpp / absl / strings / internal / str_format / bind.h
1 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
2 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_
3
4 #include <array>
5 #include <cstdio>
6 #include <sstream>
7 #include <string>
8
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"
14
15 namespace absl {
16
17 class UntypedFormatSpec;
18
19 namespace str_format_internal {
20
21 class BoundConversion : public ConversionSpec {
22  public:
23   const FormatArgImpl* arg() const { return arg_; }
24   void set_arg(const FormatArgImpl* a) { arg_ = a; }
25
26  private:
27   const FormatArgImpl* arg_;
28 };
29
30 // This is the type-erased class that the implementation uses.
31 class UntypedFormatSpecImpl {
32  public:
33   UntypedFormatSpecImpl() = delete;
34
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{}) {}
40
41   bool has_parsed_conversion() const { return size_ == ~size_t{}; }
42
43   string_view str() const {
44     assert(!has_parsed_conversion());
45     return string_view(static_cast<const char*>(data_), size_);
46   }
47   const str_format_internal::ParsedFormatBase* parsed_conversion() const {
48     assert(has_parsed_conversion());
49     return static_cast<const str_format_internal::ParsedFormatBase*>(data_);
50   }
51
52   template <typename T>
53   static const UntypedFormatSpecImpl& Extract(const T& s) {
54     return s.spec_;
55   }
56
57  private:
58   const void* data_;
59   size_t size_;
60 };
61
62 template <typename T, typename...>
63 struct MakeDependent {
64   using type = T;
65 };
66
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;
74
75  public:
76 #if ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
77
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.")));
83
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
92       __attribute__((
93           enable_if(str_format_internal::EnsureConstexpr(s), "constexpr trap"),
94           unavailable(
95               "Format specified does not match the arguments passed.")));
96
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.");
103   }
104
105   // Good format overload.
106   FormatSpecTemplate(const char* s)  // NOLINT
107       __attribute__((enable_if(ValidFormatImpl<ArgumentToConv<Args>()...>(s),
108                                "bad format trap")))
109       : Base(s) {}
110
111   FormatSpecTemplate(string_view s)  // NOLINT
112       __attribute__((enable_if(ValidFormatImpl<ArgumentToConv<Args>()...>(s),
113                                "bad format trap")))
114       : Base(s) {}
115
116 #else  // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
117
118   FormatSpecTemplate(const char* s) : Base(s) {}  // NOLINT
119   FormatSpecTemplate(string_view s) : Base(s) {}  // NOLINT
120
121 #endif  // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
122
123   template <Conv... C, typename = typename std::enable_if<
124                            sizeof...(C) == sizeof...(Args) &&
125                            AllOf(Contains(ArgumentToConv<Args>(),
126                                           C)...)>::type>
127   FormatSpecTemplate(const ExtendedParsedFormat<C...>& pc)  // NOLINT
128       : Base(&pc) {}
129 };
130
131 template <typename... Args>
132 struct FormatSpecDeductionBarrier {
133   using type = FormatSpecTemplate<Args...>;
134 };
135
136 class Streamable {
137  public:
138   Streamable(const UntypedFormatSpecImpl& format,
139              absl::Span<const FormatArgImpl> args)
140       : format_(format) {
141     if (args.size() <= ABSL_ARRAYSIZE(few_args_)) {
142       for (size_t i = 0; i < args.size(); ++i) {
143         few_args_[i] = args[i];
144       }
145       args_ = absl::MakeSpan(few_args_, args.size());
146     } else {
147       many_args_.assign(args.begin(), args.end());
148       args_ = many_args_;
149     }
150   }
151
152   std::ostream& Print(std::ostream& os) const;
153
154   friend std::ostream& operator<<(std::ostream& os, const Streamable& l) {
155     return l.Print(os);
156   }
157
158  private:
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_;
166 };
167
168 // for testing
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);
173
174 bool FormatUntyped(FormatRawSinkImpl raw_sink,
175                    UntypedFormatSpecImpl format,
176                    absl::Span<const FormatArgImpl> args);
177
178 std::string& AppendPack(std::string* out, UntypedFormatSpecImpl format,
179                         absl::Span<const FormatArgImpl> args);
180
181 inline std::string FormatPack(const UntypedFormatSpecImpl format,
182                               absl::Span<const FormatArgImpl> args) {
183   std::string out;
184   AppendPack(&out, format, args);
185   return out;
186 }
187
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);
192
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 {
197  public:
198   explicit StreamedWrapper(const T& v) : v_(v) { }
199
200  private:
201   template <typename S>
202   friend ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<S>& v,
203                                                   ConversionSpec conv,
204                                                   FormatSinkImpl* out);
205   const T& v_;
206 };
207
208 }  // namespace str_format_internal
209 }  // namespace absl
210
211 #endif  // ABSL_STRINGS_INTERNAL_STR_FORMAT_BIND_H_