Loading...
Searching...
No Matches
type_pack/traits.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <type_traits>
#include <utility>
#include <seqan3/utility/type_list/type_list.hpp>
// ----------------------------------------------------------------------------
// seqan3::pack_traits::detail
// ----------------------------------------------------------------------------
namespace seqan3::pack_traits::detail
{
template <typename query_t, typename... pack_t>
constexpr ptrdiff_t find()
{
ptrdiff_t i = 0;
return ((std::is_same_v<query_t, pack_t> ? false : ++i) && ...) ? -1 : i;
}
template <template <typename> typename pred_t, typename... pack_t>
constexpr ptrdiff_t find_if()
{
ptrdiff_t i = 0;
return ((pred_t<pack_t>::value ? false : ++i) && ...) ? -1 : i;
}
template <ptrdiff_t idx, typename head_t, typename... tail_t>
auto at()
{
if constexpr (idx == 0)
return std::type_identity<head_t>{};
else if constexpr (idx > 0)
#ifdef __clang__
return std::type_identity<__type_pack_element<idx - 1, tail_t...>>{};
#else
return at<idx - 1, tail_t...>();
#endif // __clang__
else
return at<sizeof...(tail_t) + 1 + idx, head_t, tail_t...>();
}
template <typename head_t, typename... tail_t>
std::type_identity<head_t> front();
template <typename head_t, typename... tail_t>
type_list<tail_t...> drop_front();
template <ptrdiff_t idx, typename head_t, typename... pack2_t, typename... pack1_t>
auto split_after(type_list<pack1_t...>)
{
if constexpr (idx == sizeof...(pack2_t) + 1)
return std::pair<type_list<pack1_t..., head_t, pack2_t...>, type_list<>>{};
else if constexpr (idx == 0)
return std::pair<type_list<pack1_t...>, type_list<head_t, pack2_t...>>{};
else
return split_after<idx - 1, pack2_t...>(type_list<pack1_t..., head_t>{});
}
template <typename replace_t, ptrdiff_t idx, typename... pack_t, size_t... i>
auto replace_at(std::index_sequence<i...>) -> type_list<std::conditional_t<i == idx, replace_t, pack_t>...>;
} // namespace seqan3::pack_traits::detail
// ----------------------------------------------------------------------------
// seqan3::pack_traits
// ----------------------------------------------------------------------------
namespace seqan3::pack_traits
{
template <typename... pack_t>
inline constexpr size_t size = sizeof...(pack_t);
template <typename query_t, typename... pack_t>
inline constexpr ptrdiff_t count = (std::is_same_v<query_t, pack_t> + ... + 0);
template <typename query_t, typename... pack_t>
inline constexpr ptrdiff_t find = seqan3::pack_traits::detail::find<query_t, pack_t...>();
template <template <typename> typename pred_t, typename... pack_t>
inline constexpr ptrdiff_t find_if = seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
template <typename query_t, typename... pack_t>
inline constexpr bool contains = (find<query_t, pack_t...> != -1);
template <ptrdiff_t idx, typename... pack_t>
requires (idx >= 0 && idx < sizeof...(pack_t)) || (-idx <= sizeof...(pack_t))
using at = typename decltype(detail::at<idx, pack_t...>())::type;
template <typename... pack_t>
requires (sizeof...(pack_t) > 0)
using front = typename decltype(detail::front<pack_t...>())::type;
template <typename... pack_t>
requires (sizeof...(pack_t) > 0)
using back = typename decltype((std::type_identity<pack_t>{}, ...))::type; // use comma operator
template <typename... pack_t>
requires (sizeof...(pack_t) > 0)
using drop_front = typename decltype(detail::drop_front<pack_t...>())::type;
template <template <typename> typename trait_t, typename... pack_t>
using transform = seqan3::type_list<trait_t<pack_t>...>;
template <ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i <= size<pack_t...>)
using take = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::first_type;
template <ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i <= size<pack_t...>)
using drop = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::second_type;
template <ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i <= size<pack_t...>)
using take_last = drop<size<pack_t...> - i, pack_t...>;
template <ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i <= size<pack_t...>)
using drop_last = take<size<pack_t...> - i, pack_t...>;
template <ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i <= size<pack_t...>)
using split_after = decltype(detail::split_after<i, pack_t...>(type_list<>{}));
template <typename replace_t, std::ptrdiff_t i, typename... pack_t>
requires (i >= 0 && i < size<pack_t...>)
using replace_at = decltype(detail::replace_at<replace_t, i, pack_t...>(std::make_index_sequence<size<pack_t...>>{}));
} // namespace seqan3::pack_traits
seqan3::pack_traits::take_last
drop< size< pack_t... > - i, pack_t... > take_last
Return a seqan3::type_list of the last n types in the type pack.
Definition type_pack/traits.hpp:385
seqan3::pack_traits::detail::replace_at
auto replace_at(std::index_sequence< i... >) -> type_list< std::conditional_t< i==idx, replace_t, pack_t >... >
Implementation for seqan3::pack_traits::replace_at.
seqan3::pack_traits::detail::split_after
auto split_after(type_list< pack1_t... >)
Implementation for seqan3::pack_traits::split_after.
Definition type_pack/traits.hpp:95
seqan3::pack_traits::at
typename decltype(detail::at< idx, pack_t... >())::type at
Return the type at given index from the type pack.
Definition type_pack/traits.hpp:245
seqan3::pack_traits::detail::drop_front
type_list< tail_t... > drop_front()
Implementation for seqan3::pack_traits::drop_front.
seqan3::pack_traits::find
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition type_pack/traits.hpp:179
seqan3::pack_traits::count
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition type_pack/traits.hpp:161
seqan3::pack_traits::drop_front
typename decltype(detail::drop_front< pack_t... >())::type drop_front
Return a seqan3::type_list of all the types in the type pack, except the first.
Definition type_pack/traits.hpp:305
seqan3::pack_traits::front
typename decltype(detail::front< pack_t... >())::type front
Return the first type from the type pack.
Definition type_pack/traits.hpp:262
seqan3::pack_traits::detail::find_if
constexpr ptrdiff_t find_if()
Implementation for seqan3::pack_traits::find_if.
Definition type_pack/traits.hpp:44
seqan3::pack_traits::split_after
decltype(detail::split_after< i, pack_t... >(type_list<>{})) split_after
Split a type pack into two parts returned as a pair of seqan3::type_list.
Definition type_pack/traits.hpp:421
seqan3::pack_traits::detail::find
constexpr ptrdiff_t find()
Implementation for seqan3::pack_traits::find.
Definition type_pack/traits.hpp:31
seqan3::pack_traits::detail::at
auto at()
Implementation for seqan3::pack_traits::at.
Definition type_pack/traits.hpp:57
seqan3::pack_traits::replace_at
decltype(detail::replace_at< replace_t, i, pack_t... >(std::make_index_sequence< size< pack_t... > >{})) replace_at
Replace the type at the given index with the given type.
Definition type_pack/traits.hpp:440
seqan3::pack_traits::back
typename decltype((std::type_identity< pack_t >{},...))::type back
Return the last type from the type pack.
Definition type_pack/traits.hpp:282
seqan3::pack_traits::transform
seqan3::type_list< trait_t< pack_t >... > transform
Apply a transformation trait to every type in the pack and return a seqan3::type_list of the results.
Definition type_pack/traits.hpp:325
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition type_pack/traits.hpp:143
seqan3::pack_traits::take
typename decltype(detail::split_after< i, pack_t... >(type_list<>{}))::first_type take
Return a seqan3::type_list of the first n types in the type pack.
Definition type_pack/traits.hpp:349
seqan3::pack_traits::detail::front
std::type_identity< head_t > front()
Implementation for seqan3::pack_traits::front.
seqan3::pack_traits::drop
typename decltype(detail::split_after< i, pack_t... >(type_list<>{}))::second_type drop
Return a seqan3::type_list of the types in the type pack, except the first n.
Definition type_pack/traits.hpp:367
seqan3::pack_traits::drop_last
take< size< pack_t... > - i, pack_t... > drop_last
Return a seqan3::type_list of the types the type pack, except the last n.
Definition type_pack/traits.hpp:403
seqan3::pack_traits::find_if
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
seqan3::pack_traits::contains
constexpr bool contains
Whether a type occurs in a pack or not.
Definition type_pack/traits.hpp:220
std::index_sequence
std::is_same_v
T is_same_v
seqan3::pack_traits
Namespace containing traits for working on type packs.
std::pair
std::ptrdiff_t
seqan3::type_list
Type that contains multiple types.
Definition type_list.hpp:26
type_list.hpp
Provides seqan3::type_list.
type_traits
utility
Hide me