Skip to content

Commit

Permalink
Merge #6264
Browse files Browse the repository at this point in the history
6264: hpx::is_trivially_relocatable trait implementation r=hkaiser a=isidorostsa

This PR deals with the implementation of the `is_trivially_relocatable` trait. It offers the user an interface to declare their custom class as `trivially_relocatable` using the macro:

`HPX_DECLARE_TRIVIALLY_RELOCATABLE(<class name>)`

This assumes that trivially_copyable types are also trivially relocatable, following Folly's convention: https://github.com/facebook/folly/blob/ec297b748575e8ab86333899295715e6e85f909d/folly/Traits.h#L542

Co-authored-by: isidorostsa <tsa.isidoros@gmail.com>
Co-authored-by: Isidoros <tsa.isidoros@gmail.com>
  • Loading branch information
StellarBot and isidorostsa committed Jul 10, 2023
2 parents 5b7fbc8 + 3b626c4 commit 8b4f11d
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 31 deletions.
1 change: 0 additions & 1 deletion libs/core/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

set(algorithms_headers
hpx/algorithms/traits/is_pair.hpp
hpx/algorithms/traits/is_relocatable.hpp
hpx/algorithms/traits/is_value_proxy.hpp
hpx/algorithms/traits/pointer_category.hpp
hpx/algorithms/traits/projected.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#pragma once

#include <hpx/algorithms/traits/is_relocatable.hpp>
#include <hpx/iterator_support/traits/is_iterator.hpp>
#include <hpx/type_support/is_relocatable.hpp>

#include <type_traits>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# 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)

set(tests test_is_relocatable test_low_level test_merge_four test_merge_vector
test_nbits test_range
set(tests test_low_level test_merge_four test_merge_vector test_nbits
test_range
)

foreach(test ${tests})
Expand Down
2 changes: 2 additions & 0 deletions libs/core/type_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(type_support_headers
hpx/type_support/equality.hpp
hpx/type_support/extra_data.hpp
hpx/type_support/identity.hpp
hpx/type_support/is_relocatable.hpp
hpx/type_support/is_trivially_relocatable.hpp
hpx/type_support/lazy_conditional.hpp
hpx/type_support/lazy_enable_if.hpp
hpx/type_support/pack.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
namespace hpx {

template <typename T>
struct is_relocatable
struct is_relocatable : std::is_move_constructible<T>
{
static constexpr bool value =
std::is_move_constructible_v<T> && std::is_destructible_v<T>;
};

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2023 Isidoros Tsaousis-Seiras
//
// SPDX-License-Identifier: BSL-1.0
// 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)

#pragma once

#include <type_traits>

// Macro to specialize template for given type
#define HPX_DECLARE_TRIVIALLY_RELOCATABLE(T) \
namespace hpx { \
template <> \
struct is_trivially_relocatable<T> : std::true_type \
{ \
}; \
}

#define HPX_DECLARE_TRIVIALLY_RELOCATABLE_TEMPLATE(T) \
namespace hpx { \
template <typename... K> \
struct is_trivially_relocatable<T<K...>> : std::true_type \
{ \
}; \
}

#define HPX_DECLARE_TRIVIALLY_RELOCATABLE_TEMPLATE_IF(T, Condition) \
namespace hpx { \
template <typename... K> \
struct is_trivially_relocatable<T<K...>> : Condition<K...> \
{ \
}; \
}

namespace hpx {

template <typename T>
// All trivially copyable types are trivially relocatable
// Other types should default to false.
struct is_trivially_relocatable : std::is_trivially_copyable<T>
{
};

template <typename T>
inline constexpr bool is_trivially_relocatable_v =
is_trivially_relocatable<T>::value;
} // namespace hpx
2 changes: 1 addition & 1 deletion libs/core/type_support/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 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)

set(tests)
set(tests is_relocatable is_trivially_relocatable)

if(HPX_WITH_CXX20_COROUTINES)
set(tests ${tests} generator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,65 @@
// 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 <hpx/algorithms/traits/is_relocatable.hpp>
#include <hpx/algorithms/traits/pointer_category.hpp>
#include <hpx/type_support/is_relocatable.hpp>

#include <cassert>

#include <mutex>

// Integral types are relocatable
static_assert(hpx::is_relocatable_v<int>);
static_assert(hpx::is_relocatable_v<const int>);
static_assert(hpx::is_relocatable_v<int const>);
static_assert(hpx::is_relocatable_v<int*>);
static_assert(hpx::is_relocatable_v<int (*)()>);

// Array types are not move-constructible and thus not relocatable
static_assert(!hpx::is_relocatable_v<int[]>);
static_assert(!hpx::is_relocatable_v<const int[]>);
static_assert(!hpx::is_relocatable_v<int const[]>);
static_assert(!hpx::is_relocatable_v<int[4]>);
static_assert(!hpx::is_relocatable_v<const int[4]>);
static_assert(!hpx::is_relocatable_v<int const[4]>);

// Function types are not move-constructible and thus not relocatable
static_assert(!hpx::is_relocatable_v<int()>);

// Void types are not move-constructible and thus not relocatable
static_assert(!hpx::is_relocatable_v<void>);
static_assert(!hpx::is_relocatable_v<const void>);
static_assert(!hpx::is_relocatable_v<void const>);

// std::mutex is not relocatable
static_assert(!hpx::is_relocatable_v<std::mutex>);

struct NotDestructible
struct not_destructible
{
NotDestructible(const NotDestructible&);
NotDestructible(NotDestructible&&);
~NotDestructible() = delete;
not_destructible(not_destructible const&);
not_destructible(not_destructible&&);
~not_destructible() = delete;
};

static_assert(!hpx::is_relocatable_v<NotDestructible>);

struct NotMoveConstructible
static_assert(!hpx::is_relocatable_v<not_destructible>);
struct not_move_constructible
{
NotMoveConstructible(const NotMoveConstructible&);
NotMoveConstructible(NotMoveConstructible&&) = delete;
not_move_constructible(not_move_constructible const&);
not_move_constructible(not_move_constructible&&) = delete;
};

static_assert(!hpx::is_relocatable_v<NotMoveConstructible>);
static_assert(!hpx::is_relocatable_v<not_move_constructible>);

struct NotCopyConstructible
struct not_copy_constructible
{
NotCopyConstructible(const NotCopyConstructible&) = delete;
NotCopyConstructible(NotCopyConstructible&&);
not_copy_constructible(not_copy_constructible const&) = delete;
not_copy_constructible(not_copy_constructible&&);
};

static_assert(hpx::is_relocatable_v<NotCopyConstructible>);
static_assert(hpx::is_relocatable_v<not_copy_constructible>);

// reference types are relocatable
static_assert(hpx::is_relocatable_v<int&>);
static_assert(hpx::is_relocatable_v<int&&>);
static_assert(hpx::is_relocatable_v<int (&)()>);
static_assert(hpx::is_relocatable_v<std::mutex&>);
static_assert(hpx::is_relocatable_v<NotMoveConstructible&>);
static_assert(hpx::is_relocatable_v<NotCopyConstructible&>);
static_assert(hpx::is_relocatable_v<NotDestructible&>);
static_assert(hpx::is_relocatable_v<not_move_constructible&>);
static_assert(hpx::is_relocatable_v<not_copy_constructible&>);
static_assert(hpx::is_relocatable_v<not_destructible&>);

int main(int, char*[]) {}
Loading

0 comments on commit 8b4f11d

Please sign in to comment.