Skip to content

Commit

Permalink
Replace enable_if with require clauses (#843)
Browse files Browse the repository at this point in the history
Refactor std::enable_if statements as requires clauses. Corresponding concepts will be developed in a later step
  • Loading branch information
niermann999 authored Oct 7, 2024
1 parent 48801b8 commit 11ce95d
Show file tree
Hide file tree
Showing 51 changed files with 578 additions and 697 deletions.
15 changes: 7 additions & 8 deletions core/include/detray/builders/detail/bin_association.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ namespace detray::detail {
/// taken absolute or relative
template <typename context_t, typename surface_container_t,
typename transform_container_t, typename mask_container_t,
typename grid_t, std::enable_if_t<grid_t::dim == 2, bool> = true>
static inline void bin_association(const context_t & /*context*/,
const surface_container_t &surfaces,
const transform_container_t &transforms,
const mask_container_t &surface_masks,
grid_t &grid,
const std::array<scalar, 2> &bin_tolerance,
bool absolute_tolerance = true) {
typename grid_t>
requires(grid_t::dim == 2) static inline void bin_association(
const context_t & /*context*/, const surface_container_t &surfaces,
const transform_container_t &transforms,
const mask_container_t &surface_masks, grid_t &grid,
const std::array<scalar, 2> &bin_tolerance,
bool absolute_tolerance = true) {

using algebra_t = typename grid_t::local_frame_type::algebra_type;
using point2_t = dpoint2D<algebra_t>;
Expand Down
147 changes: 65 additions & 82 deletions core/include/detray/builders/grid_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ class grid_factory {
typename r_bounds = axis::closed<axis::label::e_r>,
typename phi_bounds = axis::circular<>,
typename r_binning = axis::regular<host_container_types, scalar_type>,
typename phi_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(r_bounds::label)>, bool> =
true>
auto new_grid(const mask<annulus2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<annulus2D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {
typename phi_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(r_bounds::label)> auto new_grid(
const mask<annulus2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<annulus2D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {

static_assert(
std::is_same_v<phi_bounds, axis::circular<>>,
Expand Down Expand Up @@ -132,17 +131,16 @@ class grid_factory {
typename z_bounds = axis::closed<axis::label::e_z>,
typename x_binning = axis::regular<host_container_types, scalar_type>,
typename y_binning = axis::regular<host_container_types, scalar_type>,
typename z_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(x_bounds::label)>, bool> =
true>
auto new_grid(const mask<cuboid3D> &grid_bounds,
const std::array<std::size_t, 3UL> n_bins,
const std::vector<std::pair<loc_bin_index<cuboid3D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 3UL> &bin_edges =
std::array<std::vector<scalar_type>, 3UL>(),
const std::array<std::vector<scalar_type>, 3UL> &axis_spans =
std::array<std::vector<scalar_type>, 3UL>()) const {
typename z_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(x_bounds::label)> auto new_grid(
const mask<cuboid3D> &grid_bounds,
const std::array<std::size_t, 3UL> n_bins,
const std::vector<std::pair<loc_bin_index<cuboid3D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 3UL> &bin_edges =
std::array<std::vector<scalar_type>, 3UL>(),
const std::array<std::vector<scalar_type>, 3UL> &axis_spans =
std::array<std::vector<scalar_type>, 3UL>()) const {
// Axes boundaries and local indices
using boundary = cuboid3D::boundaries;
using axes_t = axes<cuboid3D>::template type<algebra_t>;
Expand Down Expand Up @@ -189,10 +187,8 @@ class grid_factory {
typename z_bounds = axis::closed<axis::label::e_cyl_z>,
typename rphi_binning =
axis::regular<host_container_types, scalar_type>,
typename z_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(rphi_bounds::label)>, bool> =
true>
auto new_grid(
typename z_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(rphi_bounds::label)> auto new_grid(
const mask<cylinder2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<cylinder2D>, dindex>>
Expand Down Expand Up @@ -237,18 +233,16 @@ class grid_factory {
typename z_bounds = axis::closed<axis::label::e_cyl_z>,
typename rphi_binning =
axis::regular<host_container_types, scalar_type>,
typename z_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(rphi_bounds::label)>, bool> =
true>
auto new_grid(const mask<concentric_cylinder2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<
std::pair<loc_bin_index<concentric_cylinder2D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {
typename z_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(rphi_bounds::label)> auto new_grid(
const mask<concentric_cylinder2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<concentric_cylinder2D>,
dindex>> &bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {

static_assert(
std::is_same_v<rphi_bounds, axis::circular<axis::label::e_rphi>>,
Expand Down Expand Up @@ -287,10 +281,8 @@ class grid_factory {
typename z_bounds = axis::closed<axis::label::e_z>,
typename r_binning = axis::regular<host_container_types, scalar_type>,
typename phi_binning = axis::regular<host_container_types, scalar_type>,
typename z_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(r_bounds::label)>, bool> =
true>
auto new_grid(
typename z_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(r_bounds::label)> auto new_grid(
const mask<cylinder3D> &grid_bounds,
const std::array<std::size_t, 3UL> n_bins,
const std::vector<std::pair<loc_bin_index<cylinder3D>, dindex>>
Expand Down Expand Up @@ -352,17 +344,16 @@ class grid_factory {
typename r_bounds = axis::closed<axis::label::e_r>,
typename phi_bounds = axis::circular<>,
typename r_binning = axis::regular<host_container_types, scalar_type>,
typename phi_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(r_bounds::label)>, bool> =
true>
auto new_grid(const mask<ring2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<ring2D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {
typename phi_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(r_bounds::label)> auto new_grid(
const mask<ring2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<ring2D>, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, 2UL> &bin_edges =
std::array<std::vector<scalar_type>, 2UL>(),
const std::array<std::vector<scalar_type>, 2UL> &axis_spans =
std::array<std::vector<scalar_type>, 2UL>()) const {

static_assert(std::is_same_v<phi_bounds, axis::circular<>>,
"Phi axis bounds need to be circular for ring shape");
Expand Down Expand Up @@ -399,10 +390,8 @@ class grid_factory {
typename x_bounds = axis::closed<axis::label::e_x>,
typename y_bounds = axis::closed<axis::label::e_y>,
typename x_binning = axis::regular<host_container_types, scalar_type>,
typename y_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(x_bounds::label)>, bool> =
true>
auto new_grid(
typename y_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(x_bounds::label)> auto new_grid(
const mask<rectangle2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<rectangle2D>, dindex>>
Expand Down Expand Up @@ -446,10 +435,8 @@ class grid_factory {
typename x_bounds = axis::closed<axis::label::e_x>,
typename y_bounds = axis::closed<axis::label::e_y>,
typename x_binning = axis::regular<host_container_types, scalar_type>,
typename y_binning = axis::regular<host_container_types, scalar_type>,
std::enable_if_t<std::is_enum_v<decltype(x_bounds::label)>, bool> =
true>
auto new_grid(
typename y_binning = axis::regular<host_container_types, scalar_type>>
requires std::is_enum_v<decltype(x_bounds::label)> auto new_grid(
const mask<trapezoid2D> &grid_bounds,
const std::array<std::size_t, 2UL> n_bins,
const std::vector<std::pair<loc_bin_index<trapezoid2D>, dindex>>
Expand Down Expand Up @@ -503,11 +490,9 @@ class grid_factory {
/// @param ax_bin_edges the explicit bin edges for irregular axes
/// (lower bin edges + the the upper edge of the
/// last bin), otherwise ignored.
template <
typename grid_frame_t, typename... bound_ts, typename... binning_ts,
std::enable_if_t<std::is_object_v<typename grid_frame_t::loc_point>,
bool> = true>
auto new_grid(
template <typename grid_frame_t, typename... bound_ts,
typename... binning_ts>
requires std::is_object_v<typename grid_frame_t::loc_point> auto new_grid(
const std::vector<scalar_type> &spans,
const std::vector<std::size_t> &n_bins,
const std::vector<std::pair<axis::multi_bin<sizeof...(bound_ts)>,
Expand All @@ -529,11 +514,9 @@ class grid_factory {
}

/// Helper to build grid from shape plus binning and bounds types
template <
typename grid_shape_t, typename... bound_ts, typename... binning_ts,
std::enable_if_t<std::is_enum_v<typename grid_shape_t::boundaries>,
bool> = true>
auto new_grid(
template <typename grid_shape_t, typename... bound_ts,
typename... binning_ts>
requires std::is_enum_v<typename grid_shape_t::boundaries> auto new_grid(
const std::vector<scalar_type> &spans,
const std::vector<std::size_t> &n_bins,
const std::vector<std::pair<axis::multi_bin<sizeof...(bound_ts)>,
Expand All @@ -549,28 +532,29 @@ class grid_factory {

/// Helper overload for grid builder: Build from mask and resolve bounds
/// and binnings from concrete grid type
template <typename grid_t, typename grid_shape_t,
std::enable_if_t<detail::is_grid_v<grid_t>, bool> = true>
auto new_grid(
const mask<grid_shape_t> &spans,
template <typename grid_t, typename grid_shape_t>
requires detail::is_grid_v<grid_t> auto new_grid(
const mask<grid_shape_t> &m,
const std::array<std::size_t, grid_t::dim> &n_bins,
const std::vector<std::pair<typename grid_t::loc_bin_index, dindex>>
&bin_capacities = {},
const std::array<std::vector<scalar_type>, grid_t::dim> &ax_bin_edges =
{}) const {

return new_grid(spans, n_bins, bin_capacities, ax_bin_edges,
return new_grid(m, n_bins, bin_capacities, ax_bin_edges,
typename grid_t::axes_type::bounds{},
typename grid_t::axes_type::binnings{});
}

/// Helper overload for grid builder: Build from mask and resolve bounds
/// and binnings
// TODO: Remove enable_if once clang can resolve the corresponding
// 'requires' clause
template <
typename grid_shape_t, typename... bound_ts, typename... binning_ts,
std::enable_if_t<std::is_enum_v<typename grid_shape_t::boundaries>,
bool> = true>
auto new_grid(const mask<grid_shape_t> &spans,
auto new_grid(const mask<grid_shape_t> &m,
const std::array<std::size_t, grid_shape_t::dim> &n_bins,
const std::vector<
std::pair<axis::multi_bin<sizeof...(bound_ts)>, dindex>>
Expand All @@ -580,8 +564,8 @@ class grid_factory {
const types::list<bound_ts...> & = {},
const types::list<binning_ts...> & = {}) const {

return new_grid<bound_ts..., binning_ts...>(
spans, n_bins, bin_capacities, ax_bin_edges);
return new_grid<bound_ts..., binning_ts...>(m, n_bins, bin_capacities,
ax_bin_edges);
}

/// @brief Create and empty grid with fully initialized axes.
Expand All @@ -594,9 +578,8 @@ class grid_factory {
/// @param ax_bin_edges the explicit bin edges for irregular axes
/// (lower bin edges + the the upper edge of the
/// last bin), otherwise ignored.
template <typename grid_t,
std::enable_if_t<detail::is_grid_v<grid_t>, bool> = true>
auto new_grid(
template <typename grid_t>
requires detail::is_grid_v<grid_t> auto new_grid(
const std::vector<scalar_type> spans,
const std::vector<std::size_t> n_bins,
[[maybe_unused]] const std::vector<
Expand Down
14 changes: 6 additions & 8 deletions core/include/detray/core/detail/indexing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,16 @@ DETRAY_HOST_DEVICE constexpr decltype(auto) get(
}

/// Custom get function for the typed_index struct. Get the type.
template <std::size_t ID, typename id_type, typename index_type,
std::enable_if_t<ID == 0, bool> = true>
DETRAY_HOST_DEVICE constexpr decltype(auto) get(
const typed_index<id_type, index_type>& index) noexcept {
template <std::size_t ID, typename id_type, typename index_type>
requires(ID == 0) DETRAY_HOST_DEVICE constexpr decltype(auto)
get(const typed_index<id_type, index_type>& index) noexcept {
return index.id();
}

/// Custom get function for the typed_index struct. Get the index.
template <std::size_t ID, typename id_type, typename index_type,
std::enable_if_t<ID == 1, bool> = true>
DETRAY_HOST_DEVICE constexpr decltype(auto) get(
const typed_index<id_type, index_type>& index) noexcept {
template <std::size_t ID, typename id_type, typename index_type>
requires(ID == 1) DETRAY_HOST_DEVICE constexpr decltype(auto)
get(const typed_index<id_type, index_type>& index) noexcept {
return index.index();
}

Expand Down
24 changes: 11 additions & 13 deletions core/include/detray/core/detail/multi_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,24 @@ class multi_store {

/// Construct with a specific vecmem memory resource @param resource
/// (host-side only)
template <
typename allocator_t = vecmem::memory_resource,
std::enable_if_t<!detail::is_device_view_v<allocator_t>, bool> = true>
DETRAY_HOST explicit multi_store(allocator_t &resource)
template <typename allocator_t = vecmem::memory_resource>
requires(!detail::is_device_view_v<allocator_t>) DETRAY_HOST
explicit multi_store(allocator_t &resource)
: m_tuple_container(resource) {}

/// Copy Construct with a specific (vecmem) memory resource @param resource
/// (host-side only)
template <
typename allocator_t = vecmem::memory_resource,
typename T = tuple_t<Ts...>,
std::enable_if_t<std::is_same_v<T, std::tuple<Ts...>>, bool> = true>
DETRAY_HOST explicit multi_store(allocator_t &resource, const Ts &... args)
template <typename allocator_t = vecmem::memory_resource,
typename T = tuple_t<Ts...>>
requires std::is_same_v<T, detray::tuple<Ts...>>
DETRAY_HOST explicit multi_store(allocator_t &resource,
const Ts &... args)
: m_tuple_container(resource, args...) {}

/// Construct from the container @param view . Mainly used device-side.
template <
typename tuple_view_t,
std::enable_if_t<detail::is_device_view_v<tuple_view_t>, bool> = true>
DETRAY_HOST_DEVICE explicit multi_store(tuple_view_t &view)
template <typename tuple_view_t>
requires detail::is_device_view_v<tuple_view_t>
DETRAY_HOST_DEVICE explicit multi_store(tuple_view_t &view)
: m_tuple_container(view) {}

/// Move assignment operator
Expand Down
20 changes: 9 additions & 11 deletions core/include/detray/core/detail/single_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,23 @@ class single_store {

/// Construct with a specific memory resource @param resource
/// (host-side only)
template <
typename allocator_t = vecmem::memory_resource,
std::enable_if_t<!detail::is_device_view_v<allocator_t>, bool> = true>
DETRAY_HOST explicit single_store(allocator_t &resource)
template <typename allocator_t = vecmem::memory_resource>
requires(!detail::is_device_view_v<allocator_t>) DETRAY_HOST
explicit single_store(allocator_t &resource)
: m_container(&resource) {}

/// Copy Construct with a specific memory resource @param resource
/// (host-side only)
template <typename allocator_t = vecmem::memory_resource,
typename C = container_t<T>,
std::enable_if_t<std::is_same_v<C, std::vector<T>>, bool> = true>
DETRAY_HOST single_store(allocator_t &resource, const T &arg)
typename C = container_t<T>>
requires std::is_same_v<C, std::vector<T>> DETRAY_HOST
single_store(allocator_t &resource, const T &arg)
: m_container(&resource, arg) {}

/// Construct from the container @param view . Mainly used device-side.
template <typename container_view_t,
std::enable_if_t<detail::is_device_view_v<container_view_t>,
bool> = true>
DETRAY_HOST_DEVICE explicit single_store(container_view_t &view)
template <typename container_view_t>
requires detail::is_device_view_v<container_view_t>
DETRAY_HOST_DEVICE explicit single_store(container_view_t &view)
: m_container(view) {}

/// @returns a pointer to the underlying container - const
Expand Down
Loading

0 comments on commit 11ce95d

Please sign in to comment.