181ad6265SDimitry Andric // -*- C++ -*-
281ad6265SDimitry Andric //===----------------------------------------------------------------------===//
381ad6265SDimitry Andric //
481ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
581ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
681ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
781ad6265SDimitry Andric //
881ad6265SDimitry Andric //===----------------------------------------------------------------------===//
981ad6265SDimitry Andric 
1081ad6265SDimitry Andric #ifndef _LIBCPP___FORMAT_CONCEPTS_H
1181ad6265SDimitry Andric #define _LIBCPP___FORMAT_CONCEPTS_H
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #include <__concepts/same_as.h>
1481ad6265SDimitry Andric #include <__concepts/semiregular.h>
1581ad6265SDimitry Andric #include <__config>
1681ad6265SDimitry Andric #include <__format/format_fwd.h>
1781ad6265SDimitry Andric #include <__format/format_parse_context.h>
18bdd1243dSDimitry Andric #include <__type_traits/is_specialization.h>
19*06c3fb27SDimitry Andric #include <__type_traits/remove_const.h>
20bdd1243dSDimitry Andric #include <__utility/pair.h>
21bdd1243dSDimitry Andric #include <tuple>
2281ad6265SDimitry Andric 
2381ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2481ad6265SDimitry Andric #  pragma GCC system_header
2581ad6265SDimitry Andric #endif
2681ad6265SDimitry Andric 
2781ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2881ad6265SDimitry Andric 
29*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3081ad6265SDimitry Andric 
31bdd1243dSDimitry Andric /// The character type specializations of \ref formatter.
32bdd1243dSDimitry Andric template <class _CharT>
33bdd1243dSDimitry Andric concept __fmt_char_type =
34bdd1243dSDimitry Andric     same_as<_CharT, char>
35bdd1243dSDimitry Andric #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
36bdd1243dSDimitry Andric     || same_as<_CharT, wchar_t>
37bdd1243dSDimitry Andric #  endif
38bdd1243dSDimitry Andric     ;
39bdd1243dSDimitry Andric 
4081ad6265SDimitry Andric // The output iterator isn't specified. A formatter should accept any
4181ad6265SDimitry Andric // output_iterator. This iterator is a minimal iterator to test the concept.
4281ad6265SDimitry Andric // (Note testing for (w)format_context would be a valid choice, but requires
4381ad6265SDimitry Andric // selecting the proper one depending on the type of _CharT.)
4481ad6265SDimitry Andric template <class _CharT>
4581ad6265SDimitry Andric using __fmt_iter_for = _CharT*;
4681ad6265SDimitry Andric 
47*06c3fb27SDimitry Andric template <class _Tp, class _Context, class _Formatter = typename _Context::template formatter_type<remove_const_t<_Tp>>>
48*06c3fb27SDimitry Andric concept __formattable_with =
49*06c3fb27SDimitry Andric     semiregular<_Formatter> &&
requires(_Formatter & __f,const _Formatter & __cf,_Tp && __t,_Context __fc,basic_format_parse_context<typename _Context::char_type> __pc)50*06c3fb27SDimitry Andric     requires(_Formatter& __f,
51*06c3fb27SDimitry Andric              const _Formatter& __cf,
52*06c3fb27SDimitry Andric              _Tp&& __t,
53*06c3fb27SDimitry Andric              _Context __fc,
54*06c3fb27SDimitry Andric              basic_format_parse_context<typename _Context::char_type> __pc) {
55*06c3fb27SDimitry Andric       { __f.parse(__pc) } -> same_as<typename decltype(__pc)::iterator>;
56*06c3fb27SDimitry Andric       { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
5781ad6265SDimitry Andric     };
5881ad6265SDimitry Andric 
59*06c3fb27SDimitry Andric template <class _Tp, class _CharT>
60*06c3fb27SDimitry Andric concept __formattable =
61*06c3fb27SDimitry Andric     __formattable_with<remove_reference_t<_Tp>, basic_format_context<__fmt_iter_for<_CharT>, _CharT>>;
62*06c3fb27SDimitry Andric 
63*06c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
64bdd1243dSDimitry Andric template <class _Tp, class _CharT>
65bdd1243dSDimitry Andric concept formattable = __formattable<_Tp, _CharT>;
66bdd1243dSDimitry Andric 
67bdd1243dSDimitry Andric // [tuple.like] defines a tuple-like exposition only concept. This concept is
68bdd1243dSDimitry Andric // not related to that. Therefore it uses a different name for the concept.
69bdd1243dSDimitry Andric //
70bdd1243dSDimitry Andric // TODO FMT Add a test to validate we fail when using that concept after P2165
71bdd1243dSDimitry Andric // has been implemented.
72bdd1243dSDimitry Andric template <class _Tp>
731ac55f4cSDimitry Andric concept __fmt_pair_like =
741ac55f4cSDimitry Andric     __is_specialization_v<_Tp, pair> || (__is_specialization_v<_Tp, tuple> && tuple_size_v<_Tp> == 2);
75bdd1243dSDimitry Andric 
76*06c3fb27SDimitry Andric #  endif //_LIBCPP_STD_VER >= 23
77*06c3fb27SDimitry Andric #endif   //_LIBCPP_STD_VER >= 20
7881ad6265SDimitry Andric 
7981ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
8081ad6265SDimitry Andric 
8181ad6265SDimitry Andric #endif // _LIBCPP___FORMAT_CONCEPTS_H
82