SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
utility/tuple/concept.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <concepts>
13#include <tuple>
14#include <type_traits>
15
20
21namespace seqan3::detail
22{
23
30template <typename tuple_t>
31concept tuple_size = requires (tuple_t v) {
32 { std::tuple_size<tuple_t>::value } -> std::convertible_to<size_t>;
33};
35
42template <typename tuple_t>
43concept tuple_get = requires (tuple_t & v, tuple_t const & v_c) {
44 requires std::tuple_size_v<tuple_t> > 0;
45
46 typename std::tuple_element<0, tuple_t>::type;
47
48 { get<0>(v) } -> std::convertible_to<typename std::tuple_element<0, tuple_t>::type>;
49 // requires weakly_assignable_from<decltype(get<0>(v)), typename std::tuple_element<0, tuple_t>::type>;
50 //TODO check that the previous returns something that can be assigned to
51 // unfortunately std::assignable_from requires lvalue-reference, but we want to accept xvalues too (returned
52 // proxies)
53 { get<0>(v_c) } -> std::convertible_to<typename std::tuple_element<0, tuple_t>::type>;
54 { get<0>(std::move(v)) } -> std::convertible_to<typename std::tuple_element<0, tuple_t>::type>;
55 { get<0>(std::move(v_c)) } -> std::convertible_to<typename std::tuple_element<0, tuple_t>::type const &&>;
56};
58
66template <detail::tuple_size tuple_t>
67struct tuple_type_list
68{
69protected:
71 template <size_t... Is>
72 static constexpr auto invoke_to_type_list(std::index_sequence<Is...>)
73 {
74 return type_list<std::tuple_element_t<Is, tuple_t>...>{};
75 }
76
77public:
79 using type = decltype(invoke_to_type_list(std::make_index_sequence<std::tuple_size<tuple_t>::value>{}));
80};
81
87template <detail::tuple_size tuple_t>
88using tuple_type_list_t = typename tuple_type_list<tuple_t>::type;
89
93template <typename... elements_t>
94inline constexpr auto all_elements_model_totally_ordered(seqan3::type_list<elements_t...>)
95 -> std::bool_constant<(std::totally_ordered<elements_t> && ... && true)>;
96
101template <typename tuple_t>
102 requires requires () {
103 { detail::all_elements_model_totally_ordered(tuple_type_list_t<tuple_t>{}) };
104 }
105static constexpr bool all_elements_model_totally_ordered_v =
106 decltype(detail::all_elements_model_totally_ordered(tuple_type_list_t<tuple_t>{}))::value;
107} // namespace seqan3::detail
108
109namespace seqan3
110{
111
112// ----------------------------------------------------------------------------
113// tuple_like
114// ----------------------------------------------------------------------------
115
165template <typename t>
166concept tuple_like = detail::tuple_size<std::remove_reference_t<t>> && requires (t v) {
167 typename detail::tuple_type_list<std::remove_cvref_t<t>>::type;
168
169 // NOTE(rrahn): To check the full tuple_concept including the get interface and the std::totally_ordered
170 // we need to make some assumptions. In general these checks can only be executed if the tuple is not
171 // empty. Furthermore, the std::totally_ordered can only be checked if all elements in the
172 // tuple are strict_totally_ordered. This is done, by the fold expression in the second part.
173 requires (std::tuple_size<std::remove_reference_t<t>>::value == 0)
174 || (detail::tuple_get<std::remove_cvref_t<t>>
175 && (!detail::all_elements_model_totally_ordered_v<std::remove_cvref_t<t>>
176 || std::totally_ordered<std::remove_cvref_t<t>>));
177};
179
191template <typename t>
192concept pair_like = tuple_like<t> && std::tuple_size_v<std::remove_reference_t<t>> == 2;
194
195} // namespace seqan3
Provides various type traits on generic types.
Whether a type behaves like a tuple with exactly two elements.
Whether a type behaves like a tuple.
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides seqan3::pod_tuple.
Type that contains multiple types.
Definition type_list.hpp:26
Provides type traits for working with templates.
Provides seqan3::type_list.
Hide me