Skip to content

Commit

Permalink
Add an is_tuple_like trait for sequenceable type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Naios committed Jun 9, 2017
1 parent df67d48 commit aed3ff3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions hpx/include/traits.hpp
Expand Up @@ -53,6 +53,7 @@
#include <hpx/traits/is_placeholder.hpp>
#include <hpx/traits/is_range.hpp>
#include <hpx/traits/is_timed_executor.hpp>
#include <hpx/traits/is_tuple_like.hpp>
#include <hpx/traits/is_valid_action.hpp>
#include <hpx/traits/managed_component_policies.hpp>
#include <hpx/traits/needs_automatic_registration.hpp>
Expand Down
31 changes: 31 additions & 0 deletions hpx/traits/is_tuple_like.hpp
@@ -0,0 +1,31 @@
// Copyright (c) 2017 Denis Blank
//
// 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)

#if !defined(HPX_TRAITS_IS_TUPLE_LIKE_HPP)
#define HPX_TRAITS_IS_TUPLE_LIKE_HPP

#include <type_traits>

#include <hpx/util/always_void.hpp>
#include <hpx/util/tuple.hpp>

namespace hpx {
namespace traits {
/// Deduces to a true type if the given parameter T
/// has a specific tuple like size.
template <typename T, typename = void>
struct is_tuple_like : std::false_type
{
};
template <typename T>
struct is_tuple_like<T,
typename util::always_void<decltype(util::tuple_size<T>::value)>::type>
: std::true_type
{
};
} // namespace traits
} // namespace hpx

#endif
1 change: 1 addition & 0 deletions tests/unit/traits/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@

set(tests
is_callable
is_tuple_like
)

foreach(test ${tests})
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/traits/is_tuple_like.cpp
@@ -0,0 +1,47 @@
// Copyright (c) 2017 Denis Blank
//
// 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)

#include <utility>
#include <vector>

#if defined(HPX_HAVE_CXX11_STD_ARRAY)
#include <array>
#endif

#include <hpx/config.hpp>
#include <hpx/traits/is_tuple_like.hpp>
#include <hpx/util/lightweight_test.hpp>
#include <hpx/util/tuple.hpp>

void tuple_like_true()
{
using hpx::traits::is_tuple_like;

HPX_TEST_EQ((is_tuple_like<hpx::util::tuple<int, int, int>>::value), true);
HPX_TEST_EQ((is_tuple_like<std::pair<int, int>>::value), true);

#if defined(HPX_HAVE_CXX11_STD_ARRAY)
HPX_TEST_EQ((is_tuple_like<std::array<int, 4>>::value), true);
#endif
}

void tuple_like_false()
{
using hpx::traits::is_tuple_like;

HPX_TEST_EQ((is_tuple_like<int>::value), false);
HPX_TEST_EQ((is_tuple_like<std::vector<int>>::value), false);
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
{
tuple_like_true();
tuple_like_false();
}

return hpx::util::report_errors();
}

0 comments on commit aed3ff3

Please sign in to comment.