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
10#ifndef _LIBCPP___FORMAT_CONCEPTS_H
11#define _LIBCPP___FORMAT_CONCEPTS_H
12
13#include <__concepts/same_as.h>
14#include <__concepts/semiregular.h>
15#include <__config>
16#include <__format/format_parse_context.h>
17#include <__fwd/format.h>
18#include <__type_traits/remove_const.h>
19#include <__type_traits/remove_reference.h>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27#if _LIBCPP_STD_VER >= 20
28
29/// The character type specializations of \ref formatter.
30template <class _CharT>
31concept __fmt_char_type =
32 same_as<_CharT, char>
33# if _LIBCPP_HAS_WIDE_CHARACTERS
34 || same_as<_CharT, wchar_t>
35# endif
36 ;
37
38// The output iterator isn't specified. A formatter should accept any
39// output_iterator. This iterator is a minimal iterator to test the concept.
40// (Note testing for (w)format_context would be a valid choice, but requires
41// selecting the proper one depending on the type of _CharT.)
42template <class _CharT>
43using __fmt_iter_for _LIBCPP_NODEBUG = _CharT*;
44
45template <class _Tp, class _Context, class _Formatter = typename _Context::template formatter_type<remove_const_t<_Tp>>>
46concept __formattable_with =
47 semiregular<_Formatter> &&
48 requires(_Formatter& __f,
49 const _Formatter& __cf,
50 _Tp&& __t,
51 _Context __fc,
52 basic_format_parse_context<typename _Context::char_type> __pc) {
53 { __f.parse(__pc) } -> same_as<typename decltype(__pc)::iterator>;
54 { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
55 };
56
57template <class _Tp, class _CharT>
58concept __formattable =
59 __formattable_with<remove_reference_t<_Tp>, basic_format_context<__fmt_iter_for<_CharT>, _CharT>>;
60
61# if _LIBCPP_STD_VER >= 23
62template <class _Tp, class _CharT>
63concept formattable = __formattable<_Tp, _CharT>;
64# endif // _LIBCPP_STD_VER >= 23
65#endif // _LIBCPP_STD_VER >= 20
66
67_LIBCPP_END_NAMESPACE_STD
68
69#endif // _LIBCPP___FORMAT_CONCEPTS_H
70