xref: /freebsd/contrib/llvm-project/libcxx/include/__format/concepts.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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_parse_context.h>
17*0fca6ea1SDimitry Andric #include <__fwd/format.h>
18*0fca6ea1SDimitry Andric #include <__fwd/tuple.h>
19*0fca6ea1SDimitry Andric #include <__tuple/tuple_size.h>
20bdd1243dSDimitry Andric #include <__type_traits/is_specialization.h>
2106c3fb27SDimitry Andric #include <__type_traits/remove_const.h>
22*0fca6ea1SDimitry Andric #include <__type_traits/remove_reference.h>
23bdd1243dSDimitry Andric #include <__utility/pair.h>
2481ad6265SDimitry Andric 
2581ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2681ad6265SDimitry Andric #  pragma GCC system_header
2781ad6265SDimitry Andric #endif
2881ad6265SDimitry Andric 
2981ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3081ad6265SDimitry Andric 
3106c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3281ad6265SDimitry Andric 
33bdd1243dSDimitry Andric /// The character type specializations of \ref formatter.
34bdd1243dSDimitry Andric template <class _CharT>
35bdd1243dSDimitry Andric concept __fmt_char_type =
36bdd1243dSDimitry Andric     same_as<_CharT, char>
37bdd1243dSDimitry Andric #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
38bdd1243dSDimitry Andric     || same_as<_CharT, wchar_t>
39bdd1243dSDimitry Andric #  endif
40bdd1243dSDimitry Andric     ;
41bdd1243dSDimitry Andric 
4281ad6265SDimitry Andric // The output iterator isn't specified. A formatter should accept any
4381ad6265SDimitry Andric // output_iterator. This iterator is a minimal iterator to test the concept.
4481ad6265SDimitry Andric // (Note testing for (w)format_context would be a valid choice, but requires
4581ad6265SDimitry Andric // selecting the proper one depending on the type of _CharT.)
4681ad6265SDimitry Andric template <class _CharT>
4781ad6265SDimitry Andric using __fmt_iter_for = _CharT*;
4881ad6265SDimitry Andric 
4906c3fb27SDimitry Andric template <class _Tp, class _Context, class _Formatter = typename _Context::template formatter_type<remove_const_t<_Tp>>>
5006c3fb27SDimitry Andric concept __formattable_with =
5106c3fb27SDimitry Andric     semiregular<_Formatter> &&
requires(_Formatter & __f,const _Formatter & __cf,_Tp && __t,_Context __fc,basic_format_parse_context<typename _Context::char_type> __pc)5206c3fb27SDimitry Andric     requires(_Formatter& __f,
5306c3fb27SDimitry Andric              const _Formatter& __cf,
5406c3fb27SDimitry Andric              _Tp&& __t,
5506c3fb27SDimitry Andric              _Context __fc,
5606c3fb27SDimitry Andric              basic_format_parse_context<typename _Context::char_type> __pc) {
5706c3fb27SDimitry Andric       { __f.parse(__pc) } -> same_as<typename decltype(__pc)::iterator>;
5806c3fb27SDimitry Andric       { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
5981ad6265SDimitry Andric     };
6081ad6265SDimitry Andric 
6106c3fb27SDimitry Andric template <class _Tp, class _CharT>
6206c3fb27SDimitry Andric concept __formattable =
6306c3fb27SDimitry Andric     __formattable_with<remove_reference_t<_Tp>, basic_format_context<__fmt_iter_for<_CharT>, _CharT>>;
6406c3fb27SDimitry Andric 
6506c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
66bdd1243dSDimitry Andric template <class _Tp, class _CharT>
67bdd1243dSDimitry Andric concept formattable = __formattable<_Tp, _CharT>;
68bdd1243dSDimitry Andric 
69bdd1243dSDimitry Andric // [tuple.like] defines a tuple-like exposition only concept. This concept is
70bdd1243dSDimitry Andric // not related to that. Therefore it uses a different name for the concept.
71bdd1243dSDimitry Andric //
72bdd1243dSDimitry Andric // TODO FMT Add a test to validate we fail when using that concept after P2165
73bdd1243dSDimitry Andric // has been implemented.
74bdd1243dSDimitry Andric template <class _Tp>
751ac55f4cSDimitry Andric concept __fmt_pair_like =
761ac55f4cSDimitry Andric     __is_specialization_v<_Tp, pair> || (__is_specialization_v<_Tp, tuple> && tuple_size_v<_Tp> == 2);
77bdd1243dSDimitry Andric 
7806c3fb27SDimitry Andric #  endif //_LIBCPP_STD_VER >= 23
7906c3fb27SDimitry Andric #endif   //_LIBCPP_STD_VER >= 20
8081ad6265SDimitry Andric 
8181ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
8281ad6265SDimitry Andric 
8381ad6265SDimitry Andric #endif // _LIBCPP___FORMAT_CONCEPTS_H
84