/src/abseil-cpp/absl/strings/internal/stringify_sink.h
Line | Count | Source |
1 | | // Copyright 2022 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 | | #ifndef ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_ |
16 | | #define ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_ |
17 | | |
18 | | #include <array> |
19 | | #include <string> |
20 | | #include <type_traits> |
21 | | #include <utility> |
22 | | |
23 | | #include "absl/strings/numbers.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/types/source_location.h" |
26 | | |
27 | | namespace absl { |
28 | | ABSL_NAMESPACE_BEGIN |
29 | | |
30 | | namespace strings_internal { |
31 | | class StringifySink { |
32 | | public: |
33 | | void Append(size_t count, char ch); |
34 | | |
35 | | void Append(string_view v); |
36 | | |
37 | | // Support `absl::Format(&sink, format, args...)`. |
38 | 0 | friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) { |
39 | 0 | sink->Append(v); |
40 | 0 | } |
41 | | |
42 | | private: |
43 | | template <typename T> |
44 | | friend string_view ExtractStringification(StringifySink& sink, const T& v); |
45 | | |
46 | | std::string buffer_; |
47 | | }; |
48 | | |
49 | | template <typename T> |
50 | | string_view ExtractStringification(StringifySink& sink, const T& v) { |
51 | | AbslStringify(sink, v); |
52 | | return sink.buffer_; |
53 | | } |
54 | | |
55 | | } // namespace strings_internal |
56 | | |
57 | | template <typename Sink> |
58 | | void AbslStringify(Sink& sink, SourceLocation l) { |
59 | | sink.Append(l.file_name()); |
60 | | sink.Append(":"); |
61 | | std::array<char, numbers_internal::kFastToBufferSize> buffer; |
62 | | numbers_internal::FastIntToBuffer(l.line(), buffer.data()); |
63 | | sink.Append(buffer.data()); |
64 | | } |
65 | | |
66 | | ABSL_NAMESPACE_END |
67 | | } // namespace absl |
68 | | |
69 | | #endif // ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_ |