BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
fmt.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
17
18#if __has_include(<fmt/format.h>)
19
20# include <fmt/format.h>
21# include <fmt/ranges.h>
22
25
26template <typename alph_t>
27 requires((!std::integral<alph_t>) && bio::alphabet::alphabet<alph_t>)
28struct fmt::formatter<alph_t> : fmt::formatter<bio::alphabet::char_t<alph_t>, bio::alphabet::char_t<alph_t>>
29{
30 constexpr auto format(alph_t const a, auto & ctx) const
31 {
32 return fmt::formatter<bio::alphabet::char_t<alph_t>>::format(bio::alphabet::to_char(a), ctx);
33 }
34};
35
36/* *** Tuple overload ***
37 *
38 * We need to remove our alphabet_tuples from the default overload first, because
39 * there is no concept refinement (onyl enable_if).
40 */
41
42template <bio::alphabet::detail::alphabet_tuple_like alph_t>
43struct fmt::is_tuple_like<alph_t> : std::false_type
44{};
45
46/* *** Ranges overload ***
47 *
48 * We need to remove our ranges from the default overload first, because
49 * there is no concept refinement (onyl enable_if).
50 */
51
52//TODO: get rid of the following once formatting ranges as strings works in FMT via {:s}
53
54template <typename rng_t>
55concept bio_range =
56 std::ranges::forward_range<rng_t> &&
57 (!std::integral<std::ranges::range_value_t<rng_t>>)&&bio::alphabet::alphabet<std::ranges::range_reference_t<rng_t>>;
58
59template <bio_range rng_t>
60struct fmt::is_range<rng_t, bio::alphabet::char_t<std::ranges::range_reference_t<rng_t>>> : std::false_type
61{};
62
63template <bio_range rng_t, typename _char_t>
64struct fmt::formatter<rng_t, _char_t> :
65 fmt::formatter<
66 fmt::join_view<std::ranges::iterator_t<decltype(std::declval<rng_t const &>() | bio::ranges::views::to_char)>,
67 std::ranges::sentinel_t<decltype(std::declval<rng_t const &>() | bio::ranges::views::to_char)>,
68 bio::alphabet::char_t<std::ranges::range_reference_t<rng_t const>>>,
69 _char_t>
70{
71 // TODO const & is not ideal here, but some fmt-bug breaks other solutions
72 // all our formattable ranges are also const-formattable, so it should be OK
73 auto format(rng_t const & r, auto & ctx) const
74 {
75 using trans_t = decltype(std::declval<rng_t const &>() | bio::ranges::views::to_char);
76 using formatter_t =
77 fmt::formatter<fmt::join_view<std::ranges::iterator_t<trans_t>,
78 std::ranges::sentinel_t<trans_t>,
80 return formatter_t::format(fmt::join(r | bio::ranges::views::to_char, ""), ctx);
81 }
82};
83#else
84# error "You included bio/alphabet/fmt.hpp which only works if the {fmt}-library is present."
85#endif
Provides implementation detail for bio::alphabet::variant and bio::alphabet::tuple_base.
Core alphabet concept and free function/type trait wrappers.
The generic alphabet concept that covers most data types used in ranges.
Definition: concept.hpp:643
T format(T... args)
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: concept.hpp:192
decltype(bio::alphabet::to_char(std::declval< alphabet_type const >())) char_t
The char_type of the alphabet; defined as the return type of bio::alphabet::to_char.
Definition: concept.hpp:212
auto const to_char
A view that calls bio::alphabet::to_char() on each element in the input range.
Definition: to_char.hpp:100
The main BioC++ namespace.
Definition: aa10li.hpp:23
Provides bio::views::to_char.