2015-04-20 17:16:18 +10:00
|
|
|
/*
|
|
|
|
* 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 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_TUPLE_HPP
|
|
|
|
#define __UTIL_TUPLE_HPP
|
|
|
|
|
|
|
|
#include "types.hpp"
|
2015-04-21 14:19:56 +10:00
|
|
|
#include "variadic.hpp"
|
2015-04-20 17:16:18 +10:00
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2018-03-13 14:38:51 +11:00
|
|
|
#include <functional>
|
2015-04-20 17:16:18 +10:00
|
|
|
|
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::tuple {
|
2015-04-20 17:16:18 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// call a provided object with type_tag<T> for each type in a tuple
|
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
typename F,
|
|
|
|
size_t S = 0
|
|
|
|
>
|
|
|
|
typename std::enable_if<
|
|
|
|
S == std::tuple_size<T>::value,
|
|
|
|
void
|
|
|
|
>::type
|
|
|
|
for_type (F)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template<
|
|
|
|
typename T,
|
|
|
|
typename F,
|
|
|
|
size_t S = 0
|
|
|
|
>
|
|
|
|
typename std::enable_if<
|
|
|
|
S < std::tuple_size<T>::value,
|
|
|
|
void
|
|
|
|
>::type
|
|
|
|
for_type (F f)
|
|
|
|
{
|
|
|
|
using E = typename std::tuple_element<S,T>::type;
|
|
|
|
|
|
|
|
f (type_tag<E> {});
|
|
|
|
for_type<T,F,S+1> (f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T, typename F>
|
|
|
|
auto for_type (F f, T t)
|
|
|
|
{ return for_type<decltype(t)> (f); }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// call a provided object with each value in a tuple
|
|
|
|
template <
|
|
|
|
size_t S = 0,
|
|
|
|
typename F,
|
|
|
|
typename ...T
|
|
|
|
>
|
|
|
|
typename std::enable_if<
|
|
|
|
S == sizeof...(T),
|
|
|
|
void
|
|
|
|
>::type
|
|
|
|
for_each (F, const std::tuple<T...>&)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template<
|
|
|
|
size_t S = 0,
|
|
|
|
typename F,
|
|
|
|
typename ...T
|
|
|
|
>
|
|
|
|
typename std::enable_if<
|
|
|
|
S < sizeof...(T),
|
|
|
|
void
|
|
|
|
>::type
|
|
|
|
for_each (F f, const std::tuple<T...> &t)
|
|
|
|
{
|
|
|
|
f (std::get<S> (t));
|
|
|
|
for_each<S+1,F,T...> (f, t);
|
|
|
|
}
|
2015-04-21 14:19:56 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Statically map the member types of a tuple via F<>::type
|
|
|
|
///
|
|
|
|
/// T: tuple type
|
|
|
|
/// F: type mapping object, conversion uses F<>::type
|
|
|
|
/// I: tuple indexing helper
|
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
template <
|
|
|
|
typename
|
|
|
|
> class F,
|
2017-06-07 16:31:55 +10:00
|
|
|
typename I = std::make_index_sequence<
|
2015-04-21 14:19:56 +10:00
|
|
|
std::tuple_size<T>::value
|
2017-06-07 16:31:55 +10:00
|
|
|
>
|
2015-04-21 14:19:56 +10:00
|
|
|
>
|
|
|
|
struct map;
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
typename T,
|
|
|
|
template <
|
|
|
|
typename
|
|
|
|
> class F,
|
|
|
|
size_t ...I
|
|
|
|
>
|
|
|
|
struct map<
|
|
|
|
T,
|
|
|
|
F,
|
2017-06-07 16:31:55 +10:00
|
|
|
std::index_sequence<I...>
|
2015-04-21 14:19:56 +10:00
|
|
|
> {
|
|
|
|
typedef std::tuple<
|
|
|
|
typename F<
|
|
|
|
typename std::tuple_element<I, T>::type
|
|
|
|
>::type...
|
|
|
|
> type;
|
|
|
|
};
|
2017-05-18 18:21:25 +10:00
|
|
|
|
|
|
|
|
2018-03-13 14:38:51 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <typename FuncT, typename ArgT, std::size_t ...Indices>
|
|
|
|
auto
|
|
|
|
convert (std::index_sequence<Indices...>, FuncT &&func, ArgT &&arg)
|
|
|
|
{
|
|
|
|
return std::tuple (std::invoke (func, std::get<Indices> (arg))...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename FuncT,
|
|
|
|
typename ArgT,
|
|
|
|
typename IndicesV = std::make_index_sequence<
|
|
|
|
std::tuple_size_v<std::decay_t<ArgT>>
|
|
|
|
>
|
|
|
|
>
|
|
|
|
auto convert (FuncT &&func, ArgT &&arg)
|
|
|
|
{
|
|
|
|
return detail::convert (IndicesV{}, func, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-18 18:21:25 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2017-06-13 17:00:17 +10:00
|
|
|
/// do nothing with a set of parameters.
|
|
|
|
///
|
|
|
|
/// useful for temporarily silencing unused argument warnings in parameter
|
2017-08-16 17:25:29 +10:00
|
|
|
/// packs, or for avoiding assignment of [[gnu::warn_unused_result]] to a
|
|
|
|
/// temporary value we'd just cast to void anyway (GCC#66425).
|
2017-06-13 17:00:17 +10:00
|
|
|
///
|
|
|
|
/// it is guaranteed that this function will never be defined out in
|
|
|
|
/// debug/release/whatever builds. so it is safe to use to guarantee
|
|
|
|
/// parameter evaluation.
|
2017-07-11 11:06:59 +10:00
|
|
|
inline void
|
2017-09-08 17:03:22 +10:00
|
|
|
ignore (void) noexcept
|
2017-05-18 18:21:25 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
void
|
2017-09-08 17:03:22 +10:00
|
|
|
ignore (T, const Args&...) noexcept
|
|
|
|
{ ; }
|
2017-07-11 11:07:48 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// query the index of the first occurrence of type `T' in the tuple type
|
|
|
|
/// `TupleT'.
|
|
|
|
///
|
|
|
|
/// if the query type does not occur in the tuple type a compiler error
|
|
|
|
/// should be generated.
|
|
|
|
template <class T, class TupleT>
|
|
|
|
struct index;
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <class T, class ...Types>
|
|
|
|
struct index<T, std::tuple<T, Types...>> {
|
|
|
|
static constexpr std::size_t value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <class T, class U, class ...Types>
|
|
|
|
struct index<T,std::tuple<U, Types...>> {
|
|
|
|
static constexpr std::size_t value = 1 + index<T, std::tuple<Types...>>::value;
|
|
|
|
};
|
2018-01-23 17:25:43 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <std::size_t Idx, typename ...Args>
|
|
|
|
auto
|
|
|
|
nth (Args &&...args)
|
|
|
|
{
|
|
|
|
return std::get<Idx> (std::tuple {std::forward<Args> (args)...});
|
|
|
|
}
|
2017-07-11 11:07:48 +10:00
|
|
|
};
|
2015-04-20 17:16:18 +10:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|