79 lines
2.2 KiB
C++
79 lines
2.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_VALUE_HPP
|
|
#define CRUFT_UTIL_TUPLE_VALUE_HPP
|
|
|
|
#include "../types.hpp"
|
|
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <tuple>
|
|
#include <utility>
|
|
|
|
|
|
namespace util::tuple::value {
|
|
///////////////////////////////////////////////////////////////////////////
|
|
/// Call a provided functor of type FunctionT with each value in a
|
|
/// provided tuple-like object TupleT
|
|
template<
|
|
typename FunctionT,
|
|
typename TupleT,
|
|
std::size_t S = 0
|
|
>
|
|
void
|
|
each (FunctionT &&func, TupleT &&value)
|
|
{
|
|
using tuple_t = std::decay_t<TupleT>;
|
|
static_assert (S < std::tuple_size_v<tuple_t>);
|
|
|
|
func (std::get<S> (value));
|
|
|
|
if constexpr (S + 1 < std::tuple_size_v<tuple_t>) {
|
|
each<FunctionT,TupleT,S+1> (std::forward<FunctionT> (func), std::forward<TupleT> (value));
|
|
}
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
namespace detail {
|
|
template <typename FuncT, typename ArgT, std::size_t ...Indices>
|
|
auto
|
|
map (std::index_sequence<Indices...>, FuncT &&func, ArgT &&arg)
|
|
{
|
|
return std::tuple (std::invoke (func, std::get<Indices> (arg))...);
|
|
}
|
|
};
|
|
|
|
|
|
/// returns a tuple of the result of applying the provided function to
|
|
/// each value of the supplied tuple.
|
|
template <
|
|
typename FuncT,
|
|
typename ArgT,
|
|
typename IndicesV = std::make_index_sequence<
|
|
std::tuple_size_v<std::decay_t<ArgT>>
|
|
>
|
|
>
|
|
auto map (FuncT &&func, ArgT &&arg)
|
|
{
|
|
return detail::map (IndicesV{}, func, arg);
|
|
}
|
|
};
|
|
|
|
|
|
#endif
|