stringify_sink.h (1526B)
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 <string> 19 #include <type_traits> 20 #include <utility> 21 22 #include "absl/strings/string_view.h" 23 24 namespace absl { 25 ABSL_NAMESPACE_BEGIN 26 27 namespace strings_internal { 28 class StringifySink { 29 public: 30 void Append(size_t count, char ch); 31 32 void Append(string_view v); 33 34 // Support `absl::Format(&sink, format, args...)`. 35 friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) { 36 sink->Append(v); 37 } 38 39 private: 40 template <typename T> 41 friend string_view ExtractStringification(StringifySink& sink, const T& v); 42 43 std::string buffer_; 44 }; 45 46 template <typename T> 47 string_view ExtractStringification(StringifySink& sink, const T& v) { 48 AbslStringify(sink, v); 49 return sink.buffer_; 50 } 51 52 } // namespace strings_internal 53 54 ABSL_NAMESPACE_END 55 } // namespace absl 56 57 #endif // ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_