Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:55:09

0001 // Formatting library for C++ - range and tuple support
0002 //
0003 // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
0004 // All rights reserved.
0005 //
0006 // For the license information refer to format.h.
0007 
0008 #ifndef FMT_RANGES_H_
0009 #define FMT_RANGES_H_
0010 
0011 #include <initializer_list>
0012 #include <tuple>
0013 #include <type_traits>
0014 
0015 #include "format.h"
0016 
0017 FMT_BEGIN_NAMESPACE
0018 
0019 namespace detail {
0020 
0021 template <typename Range, typename OutputIt>
0022 auto copy(const Range& range, OutputIt out) -> OutputIt {
0023   for (auto it = range.begin(), end = range.end(); it != end; ++it)
0024     *out++ = *it;
0025   return out;
0026 }
0027 
0028 template <typename OutputIt>
0029 auto copy(const char* str, OutputIt out) -> OutputIt {
0030   while (*str) *out++ = *str++;
0031   return out;
0032 }
0033 
0034 template <typename OutputIt> auto copy(char ch, OutputIt out) -> OutputIt {
0035   *out++ = ch;
0036   return out;
0037 }
0038 
0039 template <typename OutputIt> auto copy(wchar_t ch, OutputIt out) -> OutputIt {
0040   *out++ = ch;
0041   return out;
0042 }
0043 
0044 // Returns true if T has a std::string-like interface, like std::string_view.
0045 template <typename T> class is_std_string_like {
0046   template <typename U>
0047   static auto check(U* p)
0048       -> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
0049   template <typename> static void check(...);
0050 
0051  public:
0052   static constexpr const bool value =
0053       is_string<T>::value ||
0054       std::is_convertible<T, std_string_view<char>>::value ||
0055       !std::is_void<decltype(check<T>(nullptr))>::value;
0056 };
0057 
0058 template <typename Char>
0059 struct is_std_string_like<fmt::basic_string_view<Char>> : std::true_type {};
0060 
0061 template <typename T> class is_map {
0062   template <typename U> static auto check(U*) -> typename U::mapped_type;
0063   template <typename> static void check(...);
0064 
0065  public:
0066 #ifdef FMT_FORMAT_MAP_AS_LIST  // DEPRECATED!
0067   static constexpr const bool value = false;
0068 #else
0069   static constexpr const bool value =
0070       !std::is_void<decltype(check<T>(nullptr))>::value;
0071 #endif
0072 };
0073 
0074 template <typename T> class is_set {
0075   template <typename U> static auto check(U*) -> typename U::key_type;
0076   template <typename> static void check(...);
0077 
0078  public:
0079 #ifdef FMT_FORMAT_SET_AS_LIST  // DEPRECATED!
0080   static constexpr const bool value = false;
0081 #else
0082   static constexpr const bool value =
0083       !std::is_void<decltype(check<T>(nullptr))>::value && !is_map<T>::value;
0084 #endif
0085 };
0086 
0087 template <typename... Ts> struct conditional_helper {};
0088 
0089 template <typename T, typename _ = void> struct is_range_ : std::false_type {};
0090 
0091 #if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1800
0092 
0093 #  define FMT_DECLTYPE_RETURN(val)  \
0094     ->decltype(val) { return val; } \
0095     static_assert(                  \
0096         true, "")  // This makes it so that a semicolon is required after the
0097                    // macro, which helps clang-format handle the formatting.
0098 
0099 // C array overload
0100 template <typename T, std::size_t N>
0101 auto range_begin(const T (&arr)[N]) -> const T* {
0102   return arr;
0103 }
0104 template <typename T, std::size_t N>
0105 auto range_end(const T (&arr)[N]) -> const T* {
0106   return arr + N;
0107 }
0108 
0109 template <typename T, typename Enable = void>
0110 struct has_member_fn_begin_end_t : std::false_type {};
0111 
0112 template <typename T>
0113 struct has_member_fn_begin_end_t<T, void_t<decltype(std::declval<T>().begin()),
0114                                            decltype(std::declval<T>().end())>>
0115     : std::true_type {};
0116 
0117 // Member function overload
0118 template <typename T>
0119 auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).begin());
0120 template <typename T>
0121 auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).end());
0122 
0123 // ADL overload. Only participates in overload resolution if member functions
0124 // are not found.
0125 template <typename T>
0126 auto range_begin(T&& rng)
0127     -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
0128                    decltype(begin(static_cast<T&&>(rng)))> {
0129   return begin(static_cast<T&&>(rng));
0130 }
0131 template <typename T>
0132 auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
0133                                        decltype(end(static_cast<T&&>(rng)))> {
0134   return end(static_cast<T&&>(rng));
0135 }
0136 
0137 template <typename T, typename Enable = void>
0138 struct has_const_begin_end : std::false_type {};
0139 template <typename T, typename Enable = void>
0140 struct has_mutable_begin_end : std::false_type {};
0141 
0142 template <typename T>
0143 struct has_const_begin_end<
0144     T,
0145     void_t<
0146         decltype(detail::range_begin(std::declval<const remove_cvref_t<T>&>())),
0147         decltype(detail::range_end(std::declval<const remove_cvref_t<T>&>()))>>
0148     : std::true_type {};
0149 
0150 template <typename T>
0151 struct has_mutable_begin_end<
0152     T, void_t<decltype(detail::range_begin(std::declval<T>())),
0153               decltype(detail::range_end(std::declval<T>())),
0154               // the extra int here is because older versions of MSVC don't
0155               // SFINAE properly unless there are distinct types
0156               int>> : std::true_type {};
0157 
0158 template <typename T>
0159 struct is_range_<T, void>
0160     : std::integral_constant<bool, (has_const_begin_end<T>::value ||
0161                                     has_mutable_begin_end<T>::value)> {};
0162 #  undef FMT_DECLTYPE_RETURN
0163 #endif
0164 
0165 // tuple_size and tuple_element check.
0166 template <typename T> class is_tuple_like_ {
0167   template <typename U>
0168   static auto check(U* p) -> decltype(std::tuple_size<U>::value, int());
0169   template <typename> static void check(...);
0170 
0171  public:
0172   static constexpr const bool value =
0173       !std::is_void<decltype(check<T>(nullptr))>::value;
0174 };
0175 
0176 // Check for integer_sequence
0177 #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VERSION >= 1900
0178 template <typename T, T... N>
0179 using integer_sequence = std::integer_sequence<T, N...>;
0180 template <size_t... N> using index_sequence = std::index_sequence<N...>;
0181 template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
0182 #else
0183 template <typename T, T... N> struct integer_sequence {
0184   using value_type = T;
0185 
0186   static FMT_CONSTEXPR auto size() -> size_t { return sizeof...(N); }
0187 };
0188 
0189 template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
0190 
0191 template <typename T, size_t N, T... Ns>
0192 struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
0193 template <typename T, T... Ns>
0194 struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
0195 
0196 template <size_t N>
0197 using make_index_sequence = make_integer_sequence<size_t, N>;
0198 #endif
0199 
0200 template <typename T>
0201 using tuple_index_sequence = make_index_sequence<std::tuple_size<T>::value>;
0202 
0203 template <typename T, typename C, bool = is_tuple_like_<T>::value>
0204 class is_tuple_formattable_ {
0205  public:
0206   static constexpr const bool value = false;
0207 };
0208 template <typename T, typename C> class is_tuple_formattable_<T, C, true> {
0209   template <std::size_t... Is>
0210   static auto check2(index_sequence<Is...>,
0211                      integer_sequence<bool, (Is == Is)...>) -> std::true_type;
0212   static auto check2(...) -> std::false_type;
0213   template <std::size_t... Is>
0214   static auto check(index_sequence<Is...>) -> decltype(check2(
0215       index_sequence<Is...>{},
0216       integer_sequence<bool,
0217                        (is_formattable<typename std::tuple_element<Is, T>::type,
0218                                        C>::value)...>{}));
0219 
0220  public:
0221   static constexpr const bool value =
0222       decltype(check(tuple_index_sequence<T>{}))::value;
0223 };
0224 
0225 template <typename Tuple, typename F, size_t... Is>
0226 FMT_CONSTEXPR void for_each(index_sequence<Is...>, Tuple&& t, F&& f) {
0227   using std::get;
0228   // Using a free function get<Is>(Tuple) now.
0229   const int unused[] = {0, ((void)f(get<Is>(t)), 0)...};
0230   ignore_unused(unused);
0231 }
0232 
0233 template <typename Tuple, typename F>
0234 FMT_CONSTEXPR void for_each(Tuple&& t, F&& f) {
0235   for_each(tuple_index_sequence<remove_cvref_t<Tuple>>(),
0236            std::forward<Tuple>(t), std::forward<F>(f));
0237 }
0238 
0239 template <typename Tuple1, typename Tuple2, typename F, size_t... Is>
0240 void for_each2(index_sequence<Is...>, Tuple1&& t1, Tuple2&& t2, F&& f) {
0241   using std::get;
0242   const int unused[] = {0, ((void)f(get<Is>(t1), get<Is>(t2)), 0)...};
0243   ignore_unused(unused);
0244 }
0245 
0246 template <typename Tuple1, typename Tuple2, typename F>
0247 void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) {
0248   for_each2(tuple_index_sequence<remove_cvref_t<Tuple1>>(),
0249             std::forward<Tuple1>(t1), std::forward<Tuple2>(t2),
0250             std::forward<F>(f));
0251 }
0252 
0253 namespace tuple {
0254 // Workaround a bug in MSVC 2019 (v140).
0255 template <typename Char, typename... T>
0256 using result_t = std::tuple<formatter<remove_cvref_t<T>, Char>...>;
0257 
0258 using std::get;
0259 template <typename Tuple, typename Char, std::size_t... Is>
0260 auto get_formatters(index_sequence<Is...>)
0261     -> result_t<Char, decltype(get<Is>(std::declval<Tuple>()))...>;
0262 }  // namespace tuple
0263 
0264 #if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920
0265 // Older MSVC doesn't get the reference type correctly for arrays.
0266 template <typename R> struct range_reference_type_impl {
0267   using type = decltype(*detail::range_begin(std::declval<R&>()));
0268 };
0269 
0270 template <typename T, std::size_t N> struct range_reference_type_impl<T[N]> {
0271   using type = T&;
0272 };
0273 
0274 template <typename T>
0275 using range_reference_type = typename range_reference_type_impl<T>::type;
0276 #else
0277 template <typename Range>
0278 using range_reference_type =
0279     decltype(*detail::range_begin(std::declval<Range&>()));
0280 #endif
0281 
0282 // We don't use the Range's value_type for anything, but we do need the Range's
0283 // reference type, with cv-ref stripped.
0284 template <typename Range>
0285 using uncvref_type = remove_cvref_t<range_reference_type<Range>>;
0286 
0287 template <typename Formatter>
0288 FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set)
0289     -> decltype(f.set_debug_format(set)) {
0290   f.set_debug_format(set);
0291 }
0292 template <typename Formatter>
0293 FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {}
0294 
0295 // These are not generic lambdas for compatibility with C++11.
0296 template <typename ParseContext> struct parse_empty_specs {
0297   template <typename Formatter> FMT_CONSTEXPR void operator()(Formatter& f) {
0298     f.parse(ctx);
0299     detail::maybe_set_debug_format(f, true);
0300   }
0301   ParseContext& ctx;
0302 };
0303 template <typename FormatContext> struct format_tuple_element {
0304   using char_type = typename FormatContext::char_type;
0305 
0306   template <typename T>
0307   void operator()(const formatter<T, char_type>& f, const T& v) {
0308     if (i > 0)
0309       ctx.advance_to(detail::copy_str<char_type>(separator, ctx.out()));
0310     ctx.advance_to(f.format(v, ctx));
0311     ++i;
0312   }
0313 
0314   int i;
0315   FormatContext& ctx;
0316   basic_string_view<char_type> separator;
0317 };
0318 
0319 }  // namespace detail
0320 
0321 template <typename T> struct is_tuple_like {
0322   static constexpr const bool value =
0323       detail::is_tuple_like_<T>::value && !detail::is_range_<T>::value;
0324 };
0325 
0326 template <typename T, typename C> struct is_tuple_formattable {
0327   static constexpr const bool value =
0328       detail::is_tuple_formattable_<T, C>::value;
0329 };
0330 
0331 template <typename Tuple, typename Char>
0332 struct formatter<Tuple, Char,
0333                  enable_if_t<fmt::is_tuple_like<Tuple>::value &&
0334                              fmt::is_tuple_formattable<Tuple, Char>::value>> {
0335  private:
0336   decltype(detail::tuple::get_formatters<Tuple, Char>(
0337       detail::tuple_index_sequence<Tuple>())) formatters_;
0338 
0339   basic_string_view<Char> separator_ = detail::string_literal<Char, ',', ' '>{};
0340   basic_string_view<Char> opening_bracket_ =
0341       detail::string_literal<Char, '('>{};
0342   basic_string_view<Char> closing_bracket_ =
0343       detail::string_literal<Char, ')'>{};
0344 
0345  public:
0346   FMT_CONSTEXPR formatter() {}
0347 
0348   FMT_CONSTEXPR void set_separator(basic_string_view<Char> sep) {
0349     separator_ = sep;
0350   }
0351 
0352   FMT_CONSTEXPR void set_brackets(basic_string_view<Char> open,
0353                                   basic_string_view<Char> close) {
0354     opening_bracket_ = open;
0355     closing_bracket_ = close;
0356   }
0357 
0358   template <typename ParseContext>
0359   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0360     auto it = ctx.begin();
0361     if (it != ctx.end() && *it != '}')
0362       FMT_THROW(format_error("invalid format specifier"));
0363     detail::for_each(formatters_, detail::parse_empty_specs<ParseContext>{ctx});
0364     return it;
0365   }
0366 
0367   template <typename FormatContext>
0368   auto format(const Tuple& value, FormatContext& ctx) const
0369       -> decltype(ctx.out()) {
0370     ctx.advance_to(detail::copy_str<Char>(opening_bracket_, ctx.out()));
0371     detail::for_each2(
0372         formatters_, value,
0373         detail::format_tuple_element<FormatContext>{0, ctx, separator_});
0374     return detail::copy_str<Char>(closing_bracket_, ctx.out());
0375   }
0376 };
0377 
0378 template <typename T, typename Char> struct is_range {
0379   static constexpr const bool value =
0380       detail::is_range_<T>::value && !detail::is_std_string_like<T>::value &&
0381       !std::is_convertible<T, std::basic_string<Char>>::value &&
0382       !std::is_convertible<T, detail::std_string_view<Char>>::value;
0383 };
0384 
0385 namespace detail {
0386 template <typename Context> struct range_mapper {
0387   using mapper = arg_mapper<Context>;
0388 
0389   template <typename T,
0390             FMT_ENABLE_IF(has_formatter<remove_cvref_t<T>, Context>::value)>
0391   static auto map(T&& value) -> T&& {
0392     return static_cast<T&&>(value);
0393   }
0394   template <typename T,
0395             FMT_ENABLE_IF(!has_formatter<remove_cvref_t<T>, Context>::value)>
0396   static auto map(T&& value)
0397       -> decltype(mapper().map(static_cast<T&&>(value))) {
0398     return mapper().map(static_cast<T&&>(value));
0399   }
0400 };
0401 
0402 template <typename Char, typename Element>
0403 using range_formatter_type =
0404     formatter<remove_cvref_t<decltype(range_mapper<buffer_context<Char>>{}.map(
0405                   std::declval<Element>()))>,
0406               Char>;
0407 
0408 template <typename R>
0409 using maybe_const_range =
0410     conditional_t<has_const_begin_end<R>::value, const R, R>;
0411 
0412 // Workaround a bug in MSVC 2015 and earlier.
0413 #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
0414 template <typename R, typename Char>
0415 struct is_formattable_delayed
0416     : is_formattable<uncvref_type<maybe_const_range<R>>, Char> {};
0417 #endif
0418 }  // namespace detail
0419 
0420 template <typename...> struct conjunction : std::true_type {};
0421 template <typename P> struct conjunction<P> : P {};
0422 template <typename P1, typename... Pn>
0423 struct conjunction<P1, Pn...>
0424     : conditional_t<bool(P1::value), conjunction<Pn...>, P1> {};
0425 
0426 template <typename T, typename Char, typename Enable = void>
0427 struct range_formatter;
0428 
0429 template <typename T, typename Char>
0430 struct range_formatter<
0431     T, Char,
0432     enable_if_t<conjunction<std::is_same<T, remove_cvref_t<T>>,
0433                             is_formattable<T, Char>>::value>> {
0434  private:
0435   detail::range_formatter_type<Char, T> underlying_;
0436   basic_string_view<Char> separator_ = detail::string_literal<Char, ',', ' '>{};
0437   basic_string_view<Char> opening_bracket_ =
0438       detail::string_literal<Char, '['>{};
0439   basic_string_view<Char> closing_bracket_ =
0440       detail::string_literal<Char, ']'>{};
0441 
0442  public:
0443   FMT_CONSTEXPR range_formatter() {}
0444 
0445   FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type<Char, T>& {
0446     return underlying_;
0447   }
0448 
0449   FMT_CONSTEXPR void set_separator(basic_string_view<Char> sep) {
0450     separator_ = sep;
0451   }
0452 
0453   FMT_CONSTEXPR void set_brackets(basic_string_view<Char> open,
0454                                   basic_string_view<Char> close) {
0455     opening_bracket_ = open;
0456     closing_bracket_ = close;
0457   }
0458 
0459   template <typename ParseContext>
0460   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0461     auto it = ctx.begin();
0462     auto end = ctx.end();
0463 
0464     if (it != end && *it == 'n') {
0465       set_brackets({}, {});
0466       ++it;
0467     }
0468 
0469     if (it != end && *it != '}') {
0470       if (*it != ':') FMT_THROW(format_error("invalid format specifier"));
0471       ++it;
0472     } else {
0473       detail::maybe_set_debug_format(underlying_, true);
0474     }
0475 
0476     ctx.advance_to(it);
0477     return underlying_.parse(ctx);
0478   }
0479 
0480   template <typename R, typename FormatContext>
0481   auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) {
0482     detail::range_mapper<buffer_context<Char>> mapper;
0483     auto out = ctx.out();
0484     out = detail::copy_str<Char>(opening_bracket_, out);
0485     int i = 0;
0486     auto it = detail::range_begin(range);
0487     auto end = detail::range_end(range);
0488     for (; it != end; ++it) {
0489       if (i > 0) out = detail::copy_str<Char>(separator_, out);
0490       ctx.advance_to(out);
0491       auto&& item = *it;
0492       out = underlying_.format(mapper.map(item), ctx);
0493       ++i;
0494     }
0495     out = detail::copy_str<Char>(closing_bracket_, out);
0496     return out;
0497   }
0498 };
0499 
0500 enum class range_format { disabled, map, set, sequence, string, debug_string };
0501 
0502 namespace detail {
0503 template <typename T>
0504 struct range_format_kind_
0505     : std::integral_constant<range_format,
0506                              std::is_same<uncvref_type<T>, T>::value
0507                                  ? range_format::disabled
0508                              : is_map<T>::value ? range_format::map
0509                              : is_set<T>::value ? range_format::set
0510                                                 : range_format::sequence> {};
0511 
0512 template <range_format K, typename R, typename Char, typename Enable = void>
0513 struct range_default_formatter;
0514 
0515 template <range_format K>
0516 using range_format_constant = std::integral_constant<range_format, K>;
0517 
0518 template <range_format K, typename R, typename Char>
0519 struct range_default_formatter<
0520     K, R, Char,
0521     enable_if_t<(K == range_format::sequence || K == range_format::map ||
0522                  K == range_format::set)>> {
0523   using range_type = detail::maybe_const_range<R>;
0524   range_formatter<detail::uncvref_type<range_type>, Char> underlying_;
0525 
0526   FMT_CONSTEXPR range_default_formatter() { init(range_format_constant<K>()); }
0527 
0528   FMT_CONSTEXPR void init(range_format_constant<range_format::set>) {
0529     underlying_.set_brackets(detail::string_literal<Char, '{'>{},
0530                              detail::string_literal<Char, '}'>{});
0531   }
0532 
0533   FMT_CONSTEXPR void init(range_format_constant<range_format::map>) {
0534     underlying_.set_brackets(detail::string_literal<Char, '{'>{},
0535                              detail::string_literal<Char, '}'>{});
0536     underlying_.underlying().set_brackets({}, {});
0537     underlying_.underlying().set_separator(
0538         detail::string_literal<Char, ':', ' '>{});
0539   }
0540 
0541   FMT_CONSTEXPR void init(range_format_constant<range_format::sequence>) {}
0542 
0543   template <typename ParseContext>
0544   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0545     return underlying_.parse(ctx);
0546   }
0547 
0548   template <typename FormatContext>
0549   auto format(range_type& range, FormatContext& ctx) const
0550       -> decltype(ctx.out()) {
0551     return underlying_.format(range, ctx);
0552   }
0553 };
0554 }  // namespace detail
0555 
0556 template <typename T, typename Char, typename Enable = void>
0557 struct range_format_kind
0558     : conditional_t<
0559           is_range<T, Char>::value, detail::range_format_kind_<T>,
0560           std::integral_constant<range_format, range_format::disabled>> {};
0561 
0562 template <typename R, typename Char>
0563 struct formatter<
0564     R, Char,
0565     enable_if_t<conjunction<bool_constant<range_format_kind<R, Char>::value !=
0566                                           range_format::disabled>
0567 // Workaround a bug in MSVC 2015 and earlier.
0568 #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
0569                             ,
0570                             detail::is_formattable_delayed<R, Char>
0571 #endif
0572                             >::value>>
0573     : detail::range_default_formatter<range_format_kind<R, Char>::value, R,
0574                                       Char> {
0575 };
0576 
0577 template <typename Char, typename... T> struct tuple_join_view : detail::view {
0578   const std::tuple<T...>& tuple;
0579   basic_string_view<Char> sep;
0580 
0581   tuple_join_view(const std::tuple<T...>& t, basic_string_view<Char> s)
0582       : tuple(t), sep{s} {}
0583 };
0584 
0585 // Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers
0586 // support in tuple_join. It is disabled by default because of issues with
0587 // the dynamic width and precision.
0588 #ifndef FMT_TUPLE_JOIN_SPECIFIERS
0589 #  define FMT_TUPLE_JOIN_SPECIFIERS 0
0590 #endif
0591 
0592 template <typename Char, typename... T>
0593 struct formatter<tuple_join_view<Char, T...>, Char> {
0594   template <typename ParseContext>
0595   FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
0596     return do_parse(ctx, std::integral_constant<size_t, sizeof...(T)>());
0597   }
0598 
0599   template <typename FormatContext>
0600   auto format(const tuple_join_view<Char, T...>& value,
0601               FormatContext& ctx) const -> typename FormatContext::iterator {
0602     return do_format(value, ctx,
0603                      std::integral_constant<size_t, sizeof...(T)>());
0604   }
0605 
0606  private:
0607   std::tuple<formatter<typename std::decay<T>::type, Char>...> formatters_;
0608 
0609   template <typename ParseContext>
0610   FMT_CONSTEXPR auto do_parse(ParseContext& ctx,
0611                               std::integral_constant<size_t, 0>)
0612       -> decltype(ctx.begin()) {
0613     return ctx.begin();
0614   }
0615 
0616   template <typename ParseContext, size_t N>
0617   FMT_CONSTEXPR auto do_parse(ParseContext& ctx,
0618                               std::integral_constant<size_t, N>)
0619       -> decltype(ctx.begin()) {
0620     auto end = ctx.begin();
0621 #if FMT_TUPLE_JOIN_SPECIFIERS
0622     end = std::get<sizeof...(T) - N>(formatters_).parse(ctx);
0623     if (N > 1) {
0624       auto end1 = do_parse(ctx, std::integral_constant<size_t, N - 1>());
0625       if (end != end1)
0626         FMT_THROW(format_error("incompatible format specs for tuple elements"));
0627     }
0628 #endif
0629     return end;
0630   }
0631 
0632   template <typename FormatContext>
0633   auto do_format(const tuple_join_view<Char, T...>&, FormatContext& ctx,
0634                  std::integral_constant<size_t, 0>) const ->
0635       typename FormatContext::iterator {
0636     return ctx.out();
0637   }
0638 
0639   template <typename FormatContext, size_t N>
0640   auto do_format(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
0641                  std::integral_constant<size_t, N>) const ->
0642       typename FormatContext::iterator {
0643     auto out = std::get<sizeof...(T) - N>(formatters_)
0644                    .format(std::get<sizeof...(T) - N>(value.tuple), ctx);
0645     if (N > 1) {
0646       out = std::copy(value.sep.begin(), value.sep.end(), out);
0647       ctx.advance_to(out);
0648       return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
0649     }
0650     return out;
0651   }
0652 };
0653 
0654 namespace detail {
0655 // Check if T has an interface like a container adaptor (e.g. std::stack,
0656 // std::queue, std::priority_queue).
0657 template <typename T> class is_container_adaptor_like {
0658   template <typename U> static auto check(U* p) -> typename U::container_type;
0659   template <typename> static void check(...);
0660 
0661  public:
0662   static constexpr const bool value =
0663       !std::is_void<decltype(check<T>(nullptr))>::value;
0664 };
0665 
0666 template <typename Container> struct all {
0667   const Container& c;
0668   auto begin() const -> typename Container::const_iterator { return c.begin(); }
0669   auto end() const -> typename Container::const_iterator { return c.end(); }
0670 };
0671 }  // namespace detail
0672 
0673 template <typename T, typename Char>
0674 struct formatter<
0675     T, Char,
0676     enable_if_t<conjunction<detail::is_container_adaptor_like<T>,
0677                             bool_constant<range_format_kind<T, Char>::value ==
0678                                           range_format::disabled>>::value>>
0679     : formatter<detail::all<typename T::container_type>, Char> {
0680   using all = detail::all<typename T::container_type>;
0681   template <typename FormatContext>
0682   auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) {
0683     struct getter : T {
0684       static auto get(const T& t) -> all {
0685         return {t.*(&getter::c)};  // Access c through the derived class.
0686       }
0687     };
0688     return formatter<all>::format(getter::get(t), ctx);
0689   }
0690 };
0691 
0692 FMT_BEGIN_EXPORT
0693 
0694 /**
0695   \rst
0696   Returns an object that formats `tuple` with elements separated by `sep`.
0697 
0698   **Example**::
0699 
0700     std::tuple<int, char> t = {1, 'a'};
0701     fmt::print("{}", fmt::join(t, ", "));
0702     // Output: "1, a"
0703   \endrst
0704  */
0705 template <typename... T>
0706 FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple, string_view sep)
0707     -> tuple_join_view<char, T...> {
0708   return {tuple, sep};
0709 }
0710 
0711 template <typename... T>
0712 FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple,
0713                         basic_string_view<wchar_t> sep)
0714     -> tuple_join_view<wchar_t, T...> {
0715   return {tuple, sep};
0716 }
0717 
0718 /**
0719   \rst
0720   Returns an object that formats `initializer_list` with elements separated by
0721   `sep`.
0722 
0723   **Example**::
0724 
0725     fmt::print("{}", fmt::join({1, 2, 3}, ", "));
0726     // Output: "1, 2, 3"
0727   \endrst
0728  */
0729 template <typename T>
0730 auto join(std::initializer_list<T> list, string_view sep)
0731     -> join_view<const T*, const T*> {
0732   return join(std::begin(list), std::end(list), sep);
0733 }
0734 
0735 FMT_END_EXPORT
0736 FMT_END_NAMESPACE
0737 
0738 #endif  // FMT_RANGES_H_