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_RANGE_FORMAT_H
11#define _LIBCPP___FORMAT_RANGE_FORMAT_H
12
13#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
14# pragma GCC system_header
15#endif
16
17#include <__concepts/same_as.h>
18#include <__config>
19#include <__format/fmt_pair_like.h>
20#include <__ranges/concepts.h>
21#include <__type_traits/remove_cvref.h>
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25#if _LIBCPP_STD_VER >= 23
26
27_LIBCPP_DIAGNOSTIC_PUSH
28_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wshadow")
29_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wshadow")
30// This shadows map, set, and string.
31enum class range_format { disabled, map, set, sequence, string, debug_string };
32_LIBCPP_DIAGNOSTIC_POP
33
34template <class _Rp>
35constexpr range_format format_kind = [] {
36 // [format.range.fmtkind]/1
37 // A program that instantiates the primary template of format_kind is ill-formed.
38 static_assert(sizeof(_Rp) != sizeof(_Rp), "create a template specialization of format_kind for your type");
39 return range_format::disabled;
40}();
41
42template <ranges::input_range _Rp>
43 requires same_as<_Rp, remove_cvref_t<_Rp>>
44inline constexpr range_format format_kind<_Rp> = [] {
45 // [format.range.fmtkind]/2
46
47 // 2.1 If same_as<remove_cvref_t<ranges::range_reference_t<R>>, R> is true,
48 // Otherwise format_kind<R> is range_format::disabled.
49 if constexpr (same_as<remove_cvref_t<ranges::range_reference_t<_Rp>>, _Rp>)
50 return range_format::disabled;
51 // 2.2 Otherwise, if the qualified-id R::key_type is valid and denotes a type:
52 else if constexpr (requires { typename _Rp::key_type; }) {
53 // 2.2.1 If the qualified-id R::mapped_type is valid and denotes a type ...
54 if constexpr (requires { typename _Rp::mapped_type; } &&
55 // 2.2.1 ... If either U is a specialization of pair or U is a specialization
56 // of tuple and tuple_size_v<U> == 2
57 __fmt_pair_like<remove_cvref_t<ranges::range_reference_t<_Rp>>>)
58 return range_format::map;
59 else
60 // 2.2.2 Otherwise format_kind<R> is range_format::set.
61 return range_format::set;
62 } else
63 // 2.3 Otherwise, format_kind<R> is range_format::sequence.
64 return range_format::sequence;
65}();
66
67#endif // _LIBCPP_STD_VER >= 23
68
69_LIBCPP_END_NAMESPACE_STD
70
71#endif
72