//          Copyright Carl Philipp Reh 2009 - 2021.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#ifndef FCPPT_TUPLE_MAP_HPP_INCLUDED
#define FCPPT_TUPLE_MAP_HPP_INCLUDED

#include <fcppt/move_if_rvalue.hpp>
#include <fcppt/tuple/get.hpp>
#include <fcppt/tuple/init.hpp>
#include <fcppt/tuple/is_object.hpp>
#include <fcppt/tuple/map_result.hpp>
#include <fcppt/tuple/size.hpp>
#include <fcppt/config/external_begin.hpp>
#include <cstddef>
#include <type_traits>
#include <fcppt/config/external_end.hpp>

namespace fcppt::tuple
{
/**
\brief Maps over a tuple.

\ingroup fcppttuple

Let \a _tuple be <code>(v_1,...,v_n)</code>.
The result of the function is the tuple <code>(_function(v_1),...,_function(v_n))</code>.

\tparam Tuple Must be an <code>fcppt::tuple::object</code>.

\tparam Function Must be callable with every type in \a Tuple.
*/
template <typename Tuple, typename Function>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
fcppt::tuple::map_result<Tuple, Function> map(Tuple &&_tuple, Function const &_function)
  requires(
      fcppt::tuple::is_object<std::remove_cvref_t<Tuple>>::value &&
      fcppt::tuple::size<std::remove_cvref_t<Tuple>>::value ==
          fcppt::tuple::size<fcppt::tuple::map_result<Tuple, Function>>::value)
{
  return fcppt::tuple::init<fcppt::tuple::map_result<Tuple, Function>>(
      [&_function, &_tuple]<std::size_t Index>(std::integral_constant<std::size_t, Index>)
      { return _function(fcppt::move_if_rvalue<Tuple>(fcppt::tuple::get<Index>(_tuple))); });
}
}

#endif
