libcruft-util/tuple/type.hpp

302 lines
8.2 KiB
C++

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_TUPLE_TYPE_HPP
#define CRUFT_UTIL_TUPLE_TYPE_HPP
#include "../types.hpp"
#include "../types/traits.hpp"
#include <cstddef>
#include <tuple>
namespace util::tuple::type {
///////////////////////////////////////////////////////////////////////////
/// call a provided FunctorT with type_tag<T> for each type in the
/// tuple-like type TupleT.
template<
typename TupleT,
typename FunctorT,
size_t S = 0
>
void
each (FunctorT &&func)
{
static_assert (S < std::tuple_size_v<TupleT>);
using value_type = typename std::tuple_element<S,TupleT>::type;
func (util::type_tag<value_type> {});
if constexpr (S + 1 < std::tuple_size_v<TupleT>) {
each<TupleT,FunctorT,S+1> (std::forward<FunctorT> (func));
}
}
///////////////////////////////////////////////////////////////////////////////
/// Statically map the member types of a tuple via F<>::type
///
/// TupleT: tuple type
/// FieldT: type mapping object, conversion uses FieldT<>::type
/// Indices: tuple indexing helper
template <
typename TupleT,
template <
typename
> class FieldT,
typename Indices = std::make_index_sequence<
std::tuple_size<TupleT>::value
>
>
struct map;
//-----------------------------------------------------------------------------
template <
typename TupleT,
template <
typename
> class FieldT,
size_t ...Indices
>
struct map<
TupleT,
FieldT,
std::index_sequence<Indices...>
> {
typedef std::tuple<
typename FieldT<
typename std::tuple_element<Indices, TupleT>::type
>::type...
> type;
};
///////////////////////////////////////////////////////////////////////////
/// query the index of the first occurrence of type `T' in the tuple-like
/// type `TupleT'.
///
/// if the query type does not occur in the tuple type a compiler error
/// should be generated.
template<
typename TupleT,
typename ValueT,
typename = std::make_index_sequence<
std::tuple_size_v<TupleT>
>
>
struct index { };
//-------------------------------------------------------------------------
// specialisation for tuples with matching first elements. the index is 0.
template<
typename ValueT,
typename ...TailT,
size_t ...Indices
>
struct index<
std::tuple<ValueT, TailT...>,
ValueT,
std::index_sequence<Indices...>
> {
static constexpr std::size_t value = 0;
};
//-------------------------------------------------------------------------
// specialisation for tuples with non-matching first elements.
// increment and recurse.
template<
typename ValueT,
typename HeadT,
typename ...TailT,
size_t ...Indices
>
struct index<
std::tuple<HeadT, TailT...>,
ValueT,
std::index_sequence<Indices...>
> {
static constexpr std::size_t value = 1u + index<std::tuple<TailT...>,ValueT>::value;
};
//-------------------------------------------------------------------------
// convert the tuple type (which is not a tuple or it would have matched
// the tuple specialisation, into the equivalent std::tuple and requery
// against that.
template <
typename TupleT,
typename ValueT,
size_t ...Indices
> struct index<
TupleT,
ValueT,
std::index_sequence<Indices...>
> {
static constexpr std::size_t value = index<
std::tuple<std::tuple_element_t<Indices,TupleT>...>,
ValueT,
std::index_sequence<Indices...>
>::value;
};
///////////////////////////////////////////////////////////////////////////
// transform a tuple-like type (ie, something that responds to
// tuple_element) into a tuple
template <
typename TupleT,
typename Indices = std::make_index_sequence<
std::tuple_size_v<TupleT>
>
> struct entuple;
//-------------------------------------------------------------------------
template <
typename TupleT,
size_t ...Indices
> struct entuple<
TupleT,
std::index_sequence<Indices...>
> {
using type = std::tuple<
std::tuple_element_t<Indices,TupleT>...
>;
};
//-------------------------------------------------------------------------
template <typename T>
using entuple_t = typename entuple<T>::type;
///////////////////////////////////////////////////////////////////////////
// concatenate a variadic collection of tuple-like types
template <typename ...Args>
struct cat;
//-------------------------------------------------------------------------
template <>
struct cat<> { using type = std::tuple<>; };
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT>
struct cat<HeadT,TailT...> {
using type = decltype(
std::tuple_cat (
std::declval<
entuple_t<HeadT>
> (),
std::declval<
typename cat<TailT...>::type
> ()
)
);
};
//-------------------------------------------------------------------------
template <typename ...TupleT>
using cat_t = typename cat<TupleT...>::type;
///////////////////////////////////////////////////////////////////////////
// remove a type from a tuple-like type
template <
typename DoomedT,
typename TupleT
>
struct remove;
//-------------------------------------------------------------------------
template <
typename DoomedT
> struct remove<
DoomedT,
std::tuple<>
> {
using type = std::tuple<>;
};
//-------------------------------------------------------------------------
template <
typename DoomedT,
typename HeadT,
typename ...TailT
>
struct remove<
DoomedT,
std::tuple<HeadT,TailT...>
> {
using type = cat_t<
std::conditional_t<
std::is_same_v<DoomedT,HeadT>,
std::tuple< >,
std::tuple<HeadT>
>,
typename remove<DoomedT,std::tuple<TailT...>>::type
>;
};
//-------------------------------------------------------------------------
template <typename DoomedT, typename TupleT>
using remove_t = typename remove<DoomedT,TupleT>::type;
///////////////////////////////////////////////////////////////////////////
template <typename TupleT>
struct unique;
//-------------------------------------------------------------------------
template <>
struct unique<std::tuple<>> { using type = std::tuple<>; };
//-------------------------------------------------------------------------
template <typename HeadT, typename ...TailT>
struct unique<
std::tuple<HeadT, TailT...>
> {
using type = cat_t<
std::tuple<HeadT>,
typename unique<
typename remove<
HeadT,
std::tuple<TailT...>
>::type
>::type
>;
};
//-------------------------------------------------------------------------
template <typename T>
using unique_t = typename unique<T>::type;
}
#endif