libcruft-util/tuple/type.hpp

163 lines
4.5 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;
};
};
#endif