• 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_HAS_ABSL_STRINGIFY_H_
16 #define ABSL_STRINGS_HAS_ABSL_STRINGIFY_H_
17 
18 #include <type_traits>
19 #include <utility>
20 
21 #include "third_party/abseil-cpp/absl/base/config.h"
22 #include "third_party/abseil-cpp/absl/strings/string_view.h"
23 
24 namespace absl {
25     ABSL_NAMESPACE_BEGIN
26 
27     namespace strings_internal {
28 
29 // This is an empty class not intended to be used. It exists so that
30 // `HasAbslStringify` can reference a universal class rather than needing to be
31 // copied for each new sink.
32         class UnimplementedSink {
33         public:
34             void Append(size_t count, char ch);
35 
36             void Append(string_view v);
37 
38             // Support `absl::Format(&sink, format, args...)`.
39             friend void AbslFormatFlush(UnimplementedSink* sink, absl::string_view v);
40         };
41 
42     }  // namespace strings_internal
43 
44 // `HasAbslStringify<T>` detects if type `T` supports the `AbslStringify()`
45 // customization point (see
46 // https://abseil.io/docs/cpp/guides/format#abslstringify for the
47 // documentation).
48 // absl:google3-begin(internal comment)
49 // See go/abslstringify and go/totw/215.
50 // absl:google3-end
51 //
52 // Note that there are types that can be `StrCat`-ed that do not use the
53 // `AbslStringify` customization point (for example, `int`).
54 
55     template <typename T, typename = void>
56     struct HasAbslStringify : std::false_type {};
57 
58     template <typename T>
59     struct HasAbslStringify<
60             T, std::enable_if_t<std::is_void<decltype(AbslStringify(
61                     std::declval<strings_internal::UnimplementedSink&>(),
62                     std::declval<const T&>()))>::value>> : std::true_type {};
63 
64 ABSL_NAMESPACE_END
65 }  // namespace absl
66 
67 #endif  // THIRD_PARTY_ABSL_STRINGS_HAS_ABSL_STRINGIFY_H_