Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / abseil-cpp / absl / strings / internal / str_format / output.h
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Output extension hooks for the Format library.
16 // `internal::InvokeFlush` calls the appropriate flush function for the
17 // specified output argument.
18 // `BufferRawSink` is a simple output sink for a char buffer. Used by SnprintF.
19 // `FILERawSink` is a std::FILE* based sink. Used by PrintF and FprintF.
20
21 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
22 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
23
24 #include <cstdio>
25 #include <ostream>
26 #include <string>
27
28 #include "absl/base/port.h"
29 #include "absl/strings/string_view.h"
30
31 class Cord;
32
33 namespace absl {
34 namespace str_format_internal {
35
36 // RawSink implementation that writes into a char* buffer.
37 // It will not overflow the buffer, but will keep the total count of chars
38 // that would have been written.
39 class BufferRawSink {
40  public:
41   BufferRawSink(char* buffer, size_t size) : buffer_(buffer), size_(size) {}
42
43   size_t total_written() const { return total_written_; }
44   void Write(string_view v);
45
46  private:
47   char* buffer_;
48   size_t size_;
49   size_t total_written_ = 0;
50 };
51
52 // RawSink implementation that writes into a FILE*.
53 // It keeps track of the total number of bytes written and any error encountered
54 // during the writes.
55 class FILERawSink {
56  public:
57   explicit FILERawSink(std::FILE* output) : output_(output) {}
58
59   void Write(string_view v);
60
61   size_t count() const { return count_; }
62   int error() const { return error_; }
63
64  private:
65   std::FILE* output_;
66   int error_ = 0;
67   size_t count_ = 0;
68 };
69
70 // Provide RawSink integration with common types from the STL.
71 inline void AbslFormatFlush(std::string* out, string_view s) {
72   out->append(s.data(), s.size());
73 }
74 inline void AbslFormatFlush(std::ostream* out, string_view s) {
75   out->write(s.data(), s.size());
76 }
77
78 template <class AbslCord, typename = typename std::enable_if<
79                               std::is_same<AbslCord, ::Cord>::value>::type>
80 inline void AbslFormatFlush(AbslCord* out, string_view s) {
81   out->Append(s);
82 }
83
84 inline void AbslFormatFlush(FILERawSink* sink, string_view v) {
85   sink->Write(v);
86 }
87
88 inline void AbslFormatFlush(BufferRawSink* sink, string_view v) {
89   sink->Write(v);
90 }
91
92 template <typename T>
93 auto InvokeFlush(T* out, string_view s)
94     -> decltype(str_format_internal::AbslFormatFlush(out, s)) {
95   str_format_internal::AbslFormatFlush(out, s);
96 }
97
98 }  // namespace str_format_internal
99 }  // namespace absl
100
101 #endif  // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_