• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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...)`.
AbslFormatFlush(StringifySink * sink,absl::string_view v)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>
ExtractStringification(StringifySink & sink,const T & v)47 string_view ExtractStringification(StringifySink& sink, const T& v) {
48   AbslStringify(sink, v);
49   return sink.buffer_;
50 }
51 
52 template <typename T, typename = void>
53 struct HasAbslStringify : std::false_type {};
54 
55 template <typename T>
56 struct HasAbslStringify<T, std::enable_if_t<std::is_void<decltype(AbslStringify(
57                                std::declval<strings_internal::StringifySink&>(),
58                                std::declval<const T&>()))>::value>>
59     : std::true_type {};
60 
61 }  // namespace strings_internal
62 
63 ABSL_NAMESPACE_END
64 }  // namespace absl
65 
66 #endif  // ABSL_STRINGS_INTERNAL_STRINGIFY_SINK_H_
67