stringify_sink.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifndef ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_
  15. #define ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_
  16. #include <string>
  17. #include <type_traits>
  18. #include <utility>
  19. #include "absl/strings/string_view.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace strings_internal {
  23. class StringifySink {
  24. public:
  25. void Append(size_t count, char ch);
  26. void Append(string_view v);
  27. // Support `absl::Format(&sink, format, args...)`.
  28. friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
  29. sink->Append(v);
  30. }
  31. private:
  32. template <typename T>
  33. friend string_view ExtractStringification(StringifySink& sink, const T& v);
  34. std::string buffer_;
  35. };
  36. template <typename T>
  37. string_view ExtractStringification(StringifySink& sink, const T& v) {
  38. AbslStringify(sink, v);
  39. return sink.buffer_;
  40. }
  41. } // namespace strings_internal
  42. ABSL_NAMESPACE_END
  43. } // namespace absl
  44. #endif // ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_