/* * 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 */ #ifndef __UTIL_TUPLE_HPP #define __UTIL_TUPLE_HPP #include "types.hpp" #include "variadic.hpp" #include #include #include namespace util::tuple { /////////////////////////////////////////////////////////////////////////// /// call a provided object with type_tag for each type in a tuple template < typename T, typename F, size_t S = 0 > typename std::enable_if< S == std::tuple_size::value, void >::type for_type (F) { } //------------------------------------------------------------------------- template< typename T, typename F, size_t S = 0 > typename std::enable_if< S < std::tuple_size::value, void >::type for_type (F f) { using E = typename std::tuple_element::type; f (type_tag {}); for_type (f); } //------------------------------------------------------------------------- template auto for_type (F f, T t) { return for_type (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&) { } //------------------------------------------------------------------------- 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) { f (std::get (t)); for_each (f, t); } /////////////////////////////////////////////////////////////////////////////// /// 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, typename I = std::make_index_sequence< std::tuple_size::value > > struct map; //----------------------------------------------------------------------------- template < typename T, template < typename > class F, size_t ...I > struct map< T, F, std::index_sequence > { typedef std::tuple< typename F< typename std::tuple_element::type >::type... > type; }; /////////////////////////////////////////////////////////////////////////// namespace detail { template auto convert (std::index_sequence, FuncT &&func, ArgT &&arg) { return std::tuple (std::invoke (func, std::get (arg))...); } }; template < typename FuncT, typename ArgT, typename IndicesV = std::make_index_sequence< std::tuple_size_v> > > auto convert (FuncT &&func, ArgT &&arg) { return detail::convert (IndicesV{}, func, arg); } /////////////////////////////////////////////////////////////////////////// /// do nothing with a set of parameters. /// /// useful for temporarily silencing unused argument warnings in parameter /// packs, or for avoiding assignment of [[gnu::warn_unused_result]] to a /// temporary value we'd just cast to void anyway (GCC#66425). /// /// 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. inline void ignore (void) noexcept { ; } //------------------------------------------------------------------------- template void ignore (T, const Args&...) noexcept { ; } /////////////////////////////////////////////////////////////////////////// /// 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 struct index; //------------------------------------------------------------------------- template struct index> { static constexpr std::size_t value = 0; }; //------------------------------------------------------------------------- template struct index> { static constexpr std::size_t value = 1 + index>::value; }; /////////////////////////////////////////////////////////////////////////// template auto nth (Args &&...args) { return std::get (std::tuple {std::forward (args)...}); } }; #endif