Skip to content

Commit

Permalink
rewrite meta::ForEach using boost::mp11
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Oct 1, 2021
1 parent 049f2d5 commit 76ecdbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 75 deletions.
2 changes: 1 addition & 1 deletion include/pmacc/PMaccConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ endif(MPI_CXX_FOUND)
# Find Boost
################################################################################

find_package(Boost 1.65.1 REQUIRED COMPONENTS filesystem system math_tr1)
find_package(Boost 1.66 REQUIRED COMPONENTS filesystem system math_tr1)
if(TARGET Boost::filesystem)
set(PMacc_LIBRARIES ${PMacc_LIBRARIES} Boost::boost Boost::filesystem
Boost::system Boost::math_tr1)
Expand Down
84 changes: 10 additions & 74 deletions include/pmacc/meta/ForEach.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,21 @@

#include "pmacc/meta/accessors/Identity.hpp"

#include <boost/mp11.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/transform.hpp>

#include <type_traits>
#include <utility>


namespace pmacc
{
namespace meta
{
namespace detail
{
/** call the functor were itBegin points to
*
* @tparam itBegin iterator to an element in a mpl sequence
* @tparam itEnd iterator to the end of a mpl sequence
* @tparam isEnd true if itBegin == itEnd, else false
*/
template<typename itBegin, typename itEnd, bool isEnd = std::is_same<itBegin, itEnd>::value>
struct CallFunctorOfIterator
{
using nextIt = typename boost::mpl::next<itBegin>::type;
using Functor = typename boost::mpl::deref<itBegin>::type;
using NextCall = CallFunctorOfIterator<nextIt, itEnd>;

PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&... ts) const
{
Functor()(std::forward<T_Types>(ts)...);
NextCall()(ts...);
}

PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&... ts)
{
Functor()(std::forward<T_Types>(ts)...);
NextCall()(ts...);
}
};

/** Recursion end of ForEach */
template<typename itBegin, typename itEnd>
struct CallFunctorOfIterator<itBegin, itEnd, true>
{
PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&...) const
{
}

PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&...)
{
}
};

template<typename Seq>
using SeqToList = typename boost::mpl::copy<Seq, boost::mpl::back_inserter<boost::mp11::mp_list<>>>::type;
} // namespace detail

/** Compile-Time for each for Boost::MPL Type Lists
Expand All @@ -110,36 +63,19 @@ namespace pmacc
template<typename T_MPLSeq, typename T_Functor, typename T_Accessor = meta::accessors::Identity<>>
struct ForEach
{
template<typename X>
struct ReplacePlaceholder : bmpl::apply1<T_Functor, typename bmpl::apply1<T_Accessor, X>::type>
{
};
using List = detail::SeqToList<T_MPLSeq>;

using SolvedFunctors = typename bmpl::transform<T_MPLSeq, ReplacePlaceholder<bmpl::_1>>::type;

using begin = typename boost::mpl::begin<SolvedFunctors>::type;
using end = typename boost::mpl::end<SolvedFunctors>::type;


using NextCall = detail::CallFunctorOfIterator<begin, end>;
template<typename X>
using ReplacePlaceholder =
typename bmpl::apply1<T_Functor, typename bmpl::apply1<T_Accessor, X>::type>::type;

/* this functor does nothing */
using Functor = detail::CallFunctorOfIterator<end, end>;
using SolvedFunctors = boost::mp11::mp_transform<ReplacePlaceholder, List>;

PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&... ts) const
{
Functor()(std::forward<T_Types>(ts)...);
NextCall()(ts...);
}

PMACC_NO_NVCC_HDWARNING
template<typename... T_Types>
HDINLINE void operator()(T_Types&&... ts)
{
Functor()(std::forward<T_Types>(ts)...);
NextCall()(ts...);
boost::mp11::mp_for_each<SolvedFunctors>([&](auto functor) { functor(std::forward<T_Types>(ts)...); });
}
};

Expand Down

0 comments on commit 76ecdbe

Please sign in to comment.