SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
configuration.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
20
21namespace seqan3
22{
23
24// ----------------------------------------------------------------------------
25// configuration
26// ----------------------------------------------------------------------------
27
40template <detail::config_element... configs_t>
41class configuration : public std::tuple<configs_t...>
42{
44 template <detail::config_element... _configs_t>
45 friend class configuration;
46
47public:
50 using base_type = std::tuple<configs_t...>;
51
53
56 constexpr configuration() = default;
57 constexpr configuration(configuration const &) = default;
58 constexpr configuration(configuration &&) = default;
59 constexpr configuration & operator=(configuration const &) = default;
60 constexpr configuration & operator=(configuration &&) = default;
61 ~configuration() = default;
62
68 template <typename config_element_t>
69 requires (!std::same_as<std::remove_cvref_t<config_element_t>, configuration>)
70 && detail::config_element<std::remove_cvref_t<config_element_t>>
71 constexpr configuration(config_element_t && config_element) :
72 base_type{std::forward<config_element_t>(config_element)}
73 {}
75
81 constexpr size_t size() const noexcept
82 {
83 return std::tuple_size_v<base_type>;
84 }
85
116 template <typename alternative_t>
117 constexpr decltype(auto) get_or(alternative_t && alternative) & noexcept
118 {
119 return get_or_impl(*this, alternative, std::forward<alternative_t>(alternative));
120 }
121
123 template <typename alternative_t>
124 constexpr decltype(auto) get_or(alternative_t && alternative) const & noexcept
125 {
126 return get_or_impl(*this, alternative, std::forward<alternative_t>(alternative));
127 }
128
130 template <typename alternative_t>
131 constexpr decltype(auto) get_or(alternative_t && alternative) && noexcept
132 {
133 return get_or_impl(std::move(*this), alternative, std::forward<alternative_t>(alternative));
134 }
135
137 template <typename alternative_t>
138 constexpr decltype(auto) get_or(alternative_t && alternative) const && noexcept
139 {
140 return get_or_impl(std::move(*this), alternative, std::forward<alternative_t>(alternative));
141 }
142
144 template <typename query_t>
145 static constexpr bool exists() noexcept
146 {
147 return pack_traits::contains<query_t, configs_t...>;
148 }
150 template <template <typename...> typename query_t>
151 static constexpr bool exists() noexcept
152 {
153 return (pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...> > -1);
154 }
156
176 template <typename other_configuration_t>
177 requires (is_config_element_combineable_v<configs_t, std::remove_cvref_t<other_configuration_t>> && ...)
178 constexpr auto append(other_configuration_t && other_config) const
179 {
180 if constexpr (detail::config_element<std::remove_cvref_t<other_configuration_t>>)
181 {
183 std::tuple_cat(static_cast<base_type>(*this),
184 std::tuple{std::forward<other_configuration_t>(other_config)})};
185 }
186 else
187 {
188 // The following type aliases are needed to extract the correct reference and const qualifiers for the
189 // given `other_configuration_t` type (input parameter).
190 // Note the alternative would be to repeat multiple interfaces with `other_config_t &`,
191 // `other_config_t const &`, `other_config_t &&` and `other_config_t const &&`.
192
193 // Get the actual base tuple type from the other configuration.
194 using other_base_t = typename std::remove_cvref_t<other_configuration_t>::base_type;
195
196 // The other base tuple type matching the reference type and the const qualifier of the input parameter.
197 using other_base_same_modifier_t =
198 detail::transfer_type_modifier_onto_t<other_configuration_t, other_base_t>;
199
200 // Form a new seqan3::configuration type with the concatenated configuration element types of this and the
201 // other configuration.
202 using other_configs_list_t = detail::transfer_template_args_onto_t<other_base_t, type_list>;
203 using appended_configuration_t = detail::transfer_template_args_onto_t<
204 list_traits::concat<type_list<configs_t...>, other_configs_list_t>,
206
207 // Concatenate the two configurations using their base tuple types.
208 return appended_configuration_t{
209 std::tuple_cat(static_cast<base_type>(*this), std::forward<other_base_same_modifier_t>(other_config))};
210 }
211 }
212
217 template <typename query_t>
218 [[nodiscard]] constexpr auto remove() const
219 requires (exists<query_t>())
220 {
221 constexpr int index = pack_traits::find<query_t, configs_t...>;
222 return remove_at<index>();
223 }
224
226 template <template <typename...> typename query_t>
227 [[nodiscard]] constexpr auto remove() const
228 requires (exists<query_t>())
229 {
230 constexpr int index =
231 pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...>;
232 return remove_at<index>();
233 }
235
236private:
241 template <typename... _configs_t>
242 explicit constexpr configuration(std::tuple<_configs_t...> const & cfg) : base_type{cfg}
243 {}
244
246 template <typename... _configs_t>
247 explicit constexpr configuration(std::tuple<_configs_t...> && cfg) : base_type{std::move(cfg)}
248 {}
250
260 template <int index>
261 [[nodiscard]] constexpr auto remove_at() const
262 {
263 static_assert((index >= 0) && (index < sizeof...(configs_t)), "Index to remove from config is out of bounds.");
264
265 auto [head, middle] = tuple_split<index>(static_cast<base_type>(*this));
266 auto tail = tuple_pop_front(middle);
267
268 using head_list_t = detail::transfer_template_args_onto_t<decltype(head), type_list>;
269 using tail_list_t = detail::transfer_template_args_onto_t<decltype(tail), type_list>;
270 using concat_list_t = list_traits::concat<head_list_t, tail_list_t>;
271 using new_configuration_t = detail::transfer_template_args_onto_t<concat_list_t, configuration>;
272
273 return new_configuration_t{std::tuple_cat(std::move(head), std::move(tail))};
274 }
276
294 template <typename this_t, typename query_t, typename alternative_t>
295 static constexpr decltype(auto)
296 get_or_impl(this_t && me, query_t const & SEQAN3_DOXYGEN_ONLY(query), alternative_t && alternative) noexcept
297 {
298 if constexpr (exists<query_t>())
299 {
300 return get<query_t>(std::forward<this_t>(me));
301 }
302 else
303 {
304 using ret_type = remove_rvalue_reference_t<decltype(alternative)>;
305 return static_cast<ret_type>(alternative);
306 }
307 }
308
310 template <typename this_t,
311 template <typename...>
312 typename query_template_t,
313 typename... parameters_t,
314 typename alternative_t>
315 static constexpr decltype(auto)
316 get_or_impl(this_t && me, query_template_t<parameters_t...> const &, alternative_t && alternative) noexcept
317 {
318 if constexpr (exists<query_template_t>())
319 {
320 return get<query_template_t>(std::forward<this_t>(me));
321 }
322 else
323 {
324 using ret_type = remove_rvalue_reference_t<decltype(alternative)>;
325 return static_cast<ret_type>(alternative);
326 }
327 }
328};
329
337template <detail::config_element config_t>
340
360template <typename lhs_config_t, typename rhs_config_t>
361 requires (is_config_element_combineable_v<std::remove_cvref_t<lhs_config_t>, std::remove_cvref_t<rhs_config_t>>)
362constexpr auto operator|(lhs_config_t && lhs, rhs_config_t && rhs)
363{
364 if constexpr (detail::config_element<std::remove_cvref_t<lhs_config_t>>)
365 return configuration{std::forward<lhs_config_t>(lhs)}.append(std::forward<rhs_config_t>(rhs));
366 else
367 return std::forward<lhs_config_t>(lhs).append(std::forward<rhs_config_t>(rhs));
368}
369
401template <template <typename...> class query_t, typename... configs_t>
402constexpr auto & get(configuration<configs_t...> & config) noexcept
403{
404 constexpr auto pos = pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...>;
405 static_assert(pos > -1, "Access error: The requested type is not contained.");
406
407 return get<pos>(config);
408}
409
411template <template <typename...> class query_t, typename... configs_t>
412constexpr auto const & get(configuration<configs_t...> const & config) noexcept
413{
414 constexpr auto pos = pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...>;
415 static_assert(pos > -1, "Access error: The requested type is not contained.");
416
417 return get<pos>(config);
418}
419
421template <template <typename...> class query_t, typename... configs_t>
422constexpr auto && get(configuration<configs_t...> && config) noexcept
423{
424 constexpr auto pos = pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...>;
425 static_assert(pos > -1, "Access error: The requested type is not contained.");
426
427 return get<pos>(std::move(config));
428}
429
431template <template <typename...> class query_t, typename... configs_t>
432constexpr auto const && get(configuration<configs_t...> const && config) noexcept
433{
434 constexpr auto pos = pack_traits::find_if<detail::is_same_configuration_f<query_t>::template invoke, configs_t...>;
435 static_assert(pos > -1, "Access error: The requested type is not contained.");
436
437 return get<pos>(std::move(config));
438}
440
441} // namespace seqan3
442
443namespace std
444{
446
452template <seqan3::detail::config_element... configs_t>
453struct tuple_size<seqan3::configuration<configs_t...>>
454{
456 static constexpr size_t value = std::tuple_size_v<typename seqan3::configuration<configs_t...>::base_type>;
457};
458
464template <size_t pos, seqan3::detail::config_element... configs_t>
465struct tuple_element<pos, seqan3::configuration<configs_t...>>
466{
468 using type = std::tuple_element_t<pos, typename seqan3::configuration<configs_t...>::base_type>;
469};
471} //namespace std
Collection of elements to configure an algorithm.
Definition configuration.hpp:42
constexpr decltype(auto) get_or(alternative_t &&alternative) const &&noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:138
constexpr size_t size() const noexcept
Returns the number of contained config elements.
Definition configuration.hpp:81
configuration(config_t) -> configuration< config_t >
Deduces the correct configuration element type from the passed seqan3::pipeable_config_element.
constexpr configuration(config_element_t &&config_element)
Constructs a configuration from a single configuration element.
Definition configuration.hpp:71
constexpr decltype(auto) get_or(alternative_t &&alternative) &&noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:131
constexpr configuration(configuration &&)=default
Defaulted.
constexpr auto remove() const
Remove a config element from the configuration.
Definition configuration.hpp:218
constexpr decltype(auto) get_or(alternative_t &&alternative) &noexcept
Returns the stored configuration element if present otherwise the given alternative.
Definition configuration.hpp:117
constexpr configuration & operator=(configuration const &)=default
Defaulted.
constexpr configuration & operator=(configuration &&)=default
Defaulted.
~configuration()=default
Defaulted.
constexpr auto append(other_configuration_t &&other_config) const
Returns a new configuration by appending the given configuration to the current one.
Definition configuration.hpp:178
constexpr decltype(auto) get_or(alternative_t &&alternative) const &noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:124
constexpr configuration(configuration const &)=default
Defaulted.
constexpr configuration()=default
Defaulted.
static constexpr bool exists() noexcept
Checks if the given type exists in the tuple.
Definition configuration.hpp:145
friend class configuration
Friend declaration for other instances of the configuration.
Definition configuration.hpp:45
Provides various auxiliary functions with which parts of the configurations can be checked.
Provides concepts for the configuration classes.
constexpr auto & get(configuration< configs_t... > &config) noexcept
Returns the stored element.
Definition configuration.hpp:402
constexpr auto tuple_pop_front(tuple_t &&t)
Removes the first element of a tuple.
Definition pop_front.hpp:39
decltype(detail::concat(lists_t{}...)) concat
Join two seqan3::type_list s into one.
Definition type_list/traits.hpp:339
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition type_pack/traits.hpp:179
constexpr ptrdiff_t find_if
Get the index of the first type in a pack that satisfies the given predicate.
Definition type_pack/traits.hpp:202
constexpr bool contains
Whether a type occurs in a pack or not.
Definition type_pack/traits.hpp:220
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:412
SeqAn specific customisations in the standard namespace.
Provides seqan3::tuple_pop_front.
Type that contains multiple types.
Definition type_list.hpp:26
Provides type traits for working with templates.
Provides type traits seqan3::detail::transfer_type_modifier_onto.
T tuple_cat(T... args)
T tuple_size_v
Provides seqan3::type_list.
Hide me