//          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_STRONG_TYPEDEF_MAP_HPP_INCLUDED
#define FCPPT_STRONG_TYPEDEF_MAP_HPP_INCLUDED

#include <fcppt/is_strong_typedef.hpp>
#include <fcppt/move_if_rvalue.hpp>
#include <fcppt/strong_typedef_impl.hpp>
#include <fcppt/strong_typedef_tag.hpp>
#include <fcppt/config/external_begin.hpp>
#include <type_traits>
#include <fcppt/config/external_end.hpp>

namespace fcppt
{
/**
\brief Maps over a strong typedef.

\ingroup fcpptstrongtypedef

Returns a strong typedef with the same tag type and value <code>_function(_input.get())</code>.

\tparam StrongTypedef Must be an #fcppt::strong_typedef.

\tparam Function Must be a function callable as <code>R (StrongTypedef::value_type)</code>,
where <code>R</code> is the result type.
*/
template <typename StrongTypedef, typename Function>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
inline auto strong_typedef_map(StrongTypedef &&_input, Function const &_function)
    -> fcppt::strong_typedef<
        decltype(_function(fcppt::move_if_rvalue<StrongTypedef>(_input.get()))),
        fcppt::strong_typedef_tag<std::remove_cvref_t<StrongTypedef>>>
  requires fcppt::is_strong_typedef<std::remove_cvref_t<StrongTypedef>>::value
{
  return fcppt::strong_typedef<
      decltype(_function(fcppt::move_if_rvalue<StrongTypedef>(_input.get()))),
      fcppt::strong_typedef_tag<std::remove_cvref_t<StrongTypedef>>>(
      _function(fcppt::move_if_rvalue<StrongTypedef>(_input.get())));
}

}

#endif
