Skip to content

Commit

Permalink
de-templatize
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ballo committed Oct 18, 2017
1 parent 6f7a76c commit 21d08b6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 75 deletions.
42 changes: 14 additions & 28 deletions hpx/util/detail/pack_traversal_impl.hpp
Expand Up @@ -28,38 +28,26 @@ namespace util {
namespace detail {
/// Exposes useful facilities for dealing with 1:n mappings
namespace spreading {
/// A struct to mark a tuple to be unpacked into the parent context
template <typename... T>
class spread_box
{
tuple<T...> boxed_;

public:
explicit HPX_CONSTEXPR spread_box(tuple<T...> boxed)
: boxed_(std::move(boxed))
explicit HPX_CONSTEXPR spread_box() noexcept
{
}

tuple<T...> unbox()
HPX_CONSTEXPR tuple<> unbox() const noexcept
{
return std::move(boxed_);
return tuple<>{};
}
};

/// Returns an empty spread box which represents an empty
/// mapped object.
HPX_CONSTEXPR inline spread_box<> empty_spread() noexcept
{
return spread_box<>(util::make_tuple());
}

/// Deduces to a true_type if the given type is a spread marker
template <typename T>
struct is_spread : std::false_type
{
};
template <typename... T>
struct is_spread<spread_box<T...>> : std::true_type
template <>
struct is_spread<spread_box> : std::true_type
{
};

Expand All @@ -70,7 +58,7 @@ namespace util {
{
};
template <>
struct is_empty_spread<spread_box<>> : std::true_type
struct is_empty_spread<spread_box> : std::true_type
{
};

Expand All @@ -81,8 +69,7 @@ namespace util {
{
return std::forward<T>(type);
}
template <typename... T>
HPX_CONSTEXPR auto unpack(spread_box<T...> type)
HPX_CONSTEXPR auto unpack(spread_box type)
-> decltype(type.unbox())
{
return type.unbox();
Expand All @@ -102,7 +89,7 @@ namespace util {
{
return unpack(std::forward<T>(type));
}
inline void unpack_or_void(spread_box<>) noexcept
inline void unpack_or_void(spread_box) noexcept
{
}

Expand All @@ -113,8 +100,7 @@ namespace util {
{
return tuple<T>{std::forward<T>(type)};
}
template <typename... T>
HPX_CONSTEXPR auto undecorate(spread_box<T...> type)
HPX_CONSTEXPR auto undecorate(spread_box type)
-> decltype(type.unbox())
{
return type.unbox();
Expand Down Expand Up @@ -153,7 +139,7 @@ namespace util {
/// spread box is returned instead. This is useful to propagate
/// empty mappings back to the caller.
template <template <typename...> class Type = tuple>
using flat_tupelizer_of_t = tupelizer_base<spread_box<>, Type>;
using flat_tupelizer_of_t = tupelizer_base<spread_box, Type>;

/// A callable object which maps its content back to an
/// array like type.
Expand All @@ -179,9 +165,9 @@ namespace util {
}

HPX_CONSTEXPR auto operator()() const noexcept
-> decltype(spreading::empty_spread())
-> decltype(spreading::spread_box())
{
return spreading::empty_spread();
return spreading::spread_box();
}
};

Expand Down Expand Up @@ -557,7 +543,7 @@ namespace util {
/// Remap the container to zero arguments
template <typename M, typename T>
auto remap_container(container_mapping_tag<true, false>, M&& mapper,
T&& container) -> decltype(spreading::empty_spread())
T&& container) -> decltype(spreading::spread_box())
{
for (auto&& val :
container_accessor_of(std::forward<T>(container)))
Expand All @@ -567,7 +553,7 @@ namespace util {
std::forward<M>(mapper)(std::forward<decltype(val)>(val));
}
// Return one instance of an empty mapping for the container
return spreading::empty_spread();
return spreading::spread_box();
}

/// Remaps the content of the given container with type T,
Expand Down
12 changes: 6 additions & 6 deletions hpx/util/detail/unwrap_impl.hpp
Expand Up @@ -54,10 +54,10 @@ namespace util {
template <typename T,
typename std::enable_if<is_void_future<
typename std::decay<T>::type>::value>::type* = nullptr>
auto operator()(T&& future) const -> decltype(spreading::empty_spread())
auto operator()(T&& future) const -> decltype(spreading::spread_box())
{
std::forward<T>(future).get();
return spreading::empty_spread();
return spreading::spread_box();
}

template <typename T,
Expand All @@ -80,10 +80,10 @@ namespace util {
template <typename T,
typename std::enable_if<is_void_future<
typename std::decay<T>::type>::value>::type* = nullptr>
auto operator()(T&& future) const -> decltype(spreading::empty_spread())
auto operator()(T&& future) const -> decltype(spreading::spread_box())
{
std::forward<T>(future).get();
return spreading::empty_spread();
return spreading::spread_box();
}

template <typename T,
Expand All @@ -104,10 +104,10 @@ namespace util {
template <typename T,
typename std::enable_if<is_void_future<
typename std::decay<T>::type>::value>::type* = nullptr>
auto operator()(T&& future) const -> decltype(spreading::empty_spread())
auto operator()(T&& future) const -> decltype(spreading::spread_box())
{
std::forward<T>(future).get();
return spreading::empty_spread();
return spreading::spread_box();
}

template <typename T,
Expand Down
82 changes: 41 additions & 41 deletions tests/unit/util/pack_traversal.cpp
Expand Up @@ -22,7 +22,7 @@ using hpx::util::tuple;
using hpx::util::get;
using hpx::util::make_tuple;
using hpx::util::map_pack;
using hpx::util::spread_this;
//using hpx::util::spread_this;
using hpx::util::traverse_pack;
using hpx::traits::future_traits;
using hpx::traits::is_future;
Expand Down Expand Up @@ -747,36 +747,36 @@ static void test_strategic_tuple_like_traverse()
}
}

/// A mapper which duplicates the given element
struct duplicate_mapper
{
template <typename T>
auto operator()(T arg) -> decltype(hpx::util::spread_this(arg, arg))
{
return hpx::util::spread_this(arg, arg);
}
};
///// A mapper which duplicates the given element
//struct duplicate_mapper
//{
// template <typename T>
// auto operator()(T arg) -> decltype(hpx::util::spread_this(arg, arg))
// {
// return hpx::util::spread_this(arg, arg);
// }
//};

/// A mapper which removes the current element
struct zero_mapper
{
template <typename T>
auto operator()(T arg) -> decltype(hpx::util::spread_this())
auto operator()(T arg) -> decltype(hpx::util::spreading::spread_box())
{
return hpx::util::spread_this();
return hpx::util::spreading::spread_box();
}
};

static void test_spread_traverse()
{
// 1:2 mappings (multiple arguments)
{
tuple<int, int, int, int> res = map_pack(duplicate_mapper{}, 1, 2);
//// 1:2 mappings (multiple arguments)
//{
// tuple<int, int, int, int> res = map_pack(duplicate_mapper{}, 1, 2);

auto expected = make_tuple(1, 1, 2, 2);
// auto expected = make_tuple(1, 1, 2, 2);

HPX_TEST((res == expected));
}
// HPX_TEST((res == expected));
//}

// 1:0 mappings
{
Expand All @@ -787,16 +787,16 @@ static void test_spread_traverse()

static void test_spread_container_traverse()
{
// 1:2 mappings (multiple arguments)
{
std::vector<tuple<int, int>> res =
map_pack(duplicate_mapper{}, std::vector<int>{1});
//// 1:2 mappings (multiple arguments)
//{
// std::vector<tuple<int, int>> res =
// map_pack(duplicate_mapper{}, std::vector<int>{1});

std::vector<tuple<int, int>> expected;
expected.push_back(make_tuple(1, 1));
// std::vector<tuple<int, int>> expected;
// expected.push_back(make_tuple(1, 1));

HPX_TEST((res == expected));
}
// HPX_TEST((res == expected));
//}

// 1:0 mappings
{
Expand All @@ -807,16 +807,16 @@ static void test_spread_container_traverse()

static void test_spread_tuple_like_traverse()
{
// 1:2 mappings (multiple arguments)
{
tuple<tuple<int, int, int, int>> res =
map_pack(duplicate_mapper{}, make_tuple(make_tuple(1, 2)));
//// 1:2 mappings (multiple arguments)
//{
// tuple<tuple<int, int, int, int>> res =
// map_pack(duplicate_mapper{}, make_tuple(make_tuple(1, 2)));

tuple<tuple<int, int, int, int>> expected =
make_tuple(make_tuple(1, 1, 2, 2));
// tuple<tuple<int, int, int, int>> expected =
// make_tuple(make_tuple(1, 1, 2, 2));

HPX_TEST((res == expected));
}
// HPX_TEST((res == expected));
//}

// 1:0 mappings
{
Expand All @@ -825,15 +825,15 @@ static void test_spread_tuple_like_traverse()
static_assert(std::is_void<Result>::value, "Failed...");
}

// 1:2 mappings (multiple arguments)
{
std::array<int, 4> res =
map_pack(duplicate_mapper{}, std::array<int, 2>{{1, 2}});
//// 1:2 mappings (multiple arguments)
//{
// std::array<int, 4> res =
// map_pack(duplicate_mapper{}, std::array<int, 2>{{1, 2}});

std::array<int, 4> expected{{1, 1, 2, 2}};
// std::array<int, 4> expected{{1, 1, 2, 2}};

HPX_TEST((res == expected));
}
// HPX_TEST((res == expected));
//}

// 1:0 mappings
{
Expand Down

0 comments on commit 21d08b6

Please sign in to comment.