has_absl_stringify.h 2.0 KB

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