concepts.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___FORMAT_CONCEPTS_H
  10. #define _LIBCPP___FORMAT_CONCEPTS_H
  11. #include <__concepts/same_as.h>
  12. #include <__concepts/semiregular.h>
  13. #include <__config>
  14. #include <__format/format_fwd.h>
  15. #include <__format/format_parse_context.h>
  16. #include <__type_traits/is_specialization.h>
  17. #include <__type_traits/remove_const.h>
  18. #include <__utility/pair.h>
  19. #include <tuple>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. #if _LIBCPP_STD_VER >= 20
  25. /// The character type specializations of \ref formatter.
  26. template <class _CharT>
  27. concept __fmt_char_type =
  28. same_as<_CharT, char>
  29. # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  30. || same_as<_CharT, wchar_t>
  31. # endif
  32. ;
  33. // The output iterator isn't specified. A formatter should accept any
  34. // output_iterator. This iterator is a minimal iterator to test the concept.
  35. // (Note testing for (w)format_context would be a valid choice, but requires
  36. // selecting the proper one depending on the type of _CharT.)
  37. template <class _CharT>
  38. using __fmt_iter_for = _CharT*;
  39. template <class _Tp, class _Context, class _Formatter = typename _Context::template formatter_type<remove_const_t<_Tp>>>
  40. concept __formattable_with =
  41. semiregular<_Formatter> &&
  42. requires(_Formatter& __f,
  43. const _Formatter& __cf,
  44. _Tp&& __t,
  45. _Context __fc,
  46. basic_format_parse_context<typename _Context::char_type> __pc) {
  47. { __f.parse(__pc) } -> same_as<typename decltype(__pc)::iterator>;
  48. { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
  49. };
  50. template <class _Tp, class _CharT>
  51. concept __formattable =
  52. __formattable_with<remove_reference_t<_Tp>, basic_format_context<__fmt_iter_for<_CharT>, _CharT>>;
  53. # if _LIBCPP_STD_VER >= 23
  54. template <class _Tp, class _CharT>
  55. concept formattable = __formattable<_Tp, _CharT>;
  56. // [tuple.like] defines a tuple-like exposition only concept. This concept is
  57. // not related to that. Therefore it uses a different name for the concept.
  58. //
  59. // TODO FMT Add a test to validate we fail when using that concept after P2165
  60. // has been implemented.
  61. template <class _Tp>
  62. concept __fmt_pair_like =
  63. __is_specialization_v<_Tp, pair> || (__is_specialization_v<_Tp, tuple> && tuple_size_v<_Tp> == 2);
  64. # endif //_LIBCPP_STD_VER >= 23
  65. #endif //_LIBCPP_STD_VER >= 20
  66. _LIBCPP_END_NAMESPACE_STD
  67. #endif // _LIBCPP___FORMAT_CONCEPTS_H