Skip to content

Commit

Permalink
refactor: Remove uses of std::enable_if
Browse files Browse the repository at this point in the history
With the move to C++20, we no longer need `std::enable_if` as we can use
the much more elegant `requires` keyword. This commit swaps virtually
all uses of `std::enable_if`, except those in the existing pre-C++20
concepts library.
  • Loading branch information
stephenswat committed Aug 6, 2024
1 parent 0c37673 commit 019f2db
Show file tree
Hide file tree
Showing 26 changed files with 299 additions and 313 deletions.
10 changes: 4 additions & 6 deletions Core/include/Acts/EventData/ProxyAccessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ struct ProxyAccessorBase {
/// @tparam proxy_t the type of the proxy
/// @param proxy the proxy object to access
/// @return mutable reference to the column behind the key
template <detail::MutableProxyType proxy_t, bool RO = ReadOnly,
typename = std::enable_if_t<!RO>>
T& operator()(proxy_t proxy) const {
template <detail::MutableProxyType proxy_t, bool RO = ReadOnly>
requires(!RO) T& operator()(proxy_t proxy) const {
static_assert(!proxy_t::ReadOnly,
"Cannot get mutable ref for const track proxy");
return proxy.template component<T>(key);
Expand All @@ -92,9 +91,8 @@ struct ProxyAccessorBase {
/// @tparam proxy_t the type of the track proxy
/// @param proxy the proxy to access
/// @return const reference to the column behind the key
template <detail::ProxyType proxy_t, bool RO = ReadOnly,
typename = std::enable_if_t<RO>>
const T& operator()(proxy_t proxy) const {
template <detail::ProxyType proxy_t, bool RO = ReadOnly>
requires(RO) const T& operator()(proxy_t proxy) const {
if constexpr (proxy_t::ReadOnly) {
return proxy.template component<T>(key);

Expand Down
7 changes: 4 additions & 3 deletions Core/include/Acts/EventData/SourceLink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "Acts/Utilities/TypeTraits.hpp"

#include <cassert>
#include <concepts>
#include <iostream>
#include <type_traits>
#include <utility>
Expand All @@ -37,9 +38,9 @@ class SourceLink final {
/// Constructor from concrete sourcelink
/// @tparam T The source link type
/// @param upstream The upstream source link to store
template <typename T, typename = std::enable_if_t<
!std::is_same_v<std::decay_t<T>, SourceLink>>>
explicit SourceLink(T&& upstream) {
template <typename T>
requires(!std::same_as<std::decay_t<T>, SourceLink>) explicit SourceLink(
T&& upstream) {
static_assert(!std::is_same_v<std::decay_t<T>, SourceLink>,
"Cannot wrap SourceLink in SourceLink");

Expand Down
8 changes: 4 additions & 4 deletions Core/include/Acts/EventData/TrackStateProxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ class TrackStateProxy {
/// This overloaded is only enabled if not read-only, and returns a mutable
/// reference.
/// @return Mutable reference to the pathlength.
template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
double& pathLength() {
template <bool RO = ReadOnly>
requires(!RO) double& pathLength() {
return component<double, hashString("pathLength")>();
}

Expand Down Expand Up @@ -440,8 +440,8 @@ class TrackStateProxy {
component<IndexType, hashString("predicted")>());
}

template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
Parameters predicted() {
template <bool RO = ReadOnly>
requires(!RO) Parameters predicted() {
assert(has<hashString("predicted")>());
return m_traj->self().parameters(
component<IndexType, hashString("predicted")>());
Expand Down
10 changes: 6 additions & 4 deletions Core/include/Acts/Surfaces/SurfaceArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,18 @@ class SurfaceArray {
/// interface stays the same, since we don't care what happens
/// here on the callers end
/// This is the version for DIM>1
template <std::size_t D = DIM, std::enable_if_t<D != 1, int> = 0>
Vector3 getBinCenterImpl(std::size_t bin) const {
template <std::size_t D = DIM>
requires(D != 1) Vector3 getBinCenterImpl(std::size_t bin)
const {
return m_localToGlobal(ActsVector<DIM>(
m_grid.binCenter(m_grid.localBinsFromGlobalBin(bin)).data()));
}

/// Internal method, see above.
/// This is the version for DIM==1
template <std::size_t D = DIM, std::enable_if_t<D == 1, int> = 0>
Vector3 getBinCenterImpl(std::size_t bin) const {
template <std::size_t D = DIM>
requires(D == 1) Vector3 getBinCenterImpl(std::size_t bin)
const {
point_t pos = m_grid.binCenter(m_grid.localBinsFromGlobalBin(bin));
return m_localToGlobal(pos);
}
Expand Down
14 changes: 7 additions & 7 deletions Core/include/Acts/TrackFitting/GlobalChiSquareFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,13 @@ class Gx2Fitter {
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
auto fit(source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const Gx2FitterOptions<traj_t>& gx2fOptions,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer)
const -> std::enable_if_t<
!_isdn, Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>> {
requires(!_isdn) auto fit(
source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const Gx2FitterOptions<traj_t>& gx2fOptions,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer) const
-> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy> {
// Preprocess Measurements (SourceLinks -> map)
// To be able to find measurements later, we put them into a map
// We need to copy input SourceLinks anyway, so the map can own them.
Expand Down
30 changes: 15 additions & 15 deletions Core/include/Acts/TrackFitting/KalmanFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1084,13 +1084,13 @@ class KalmanFitter {
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
auto fit(source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const KalmanFitterOptions<traj_t>& kfOptions,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer)
const -> std::enable_if_t<
!_isdn, Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>> {
requires(!_isdn) auto fit(
source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const KalmanFitterOptions<traj_t>& kfOptions,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer) const
-> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy> {
// To be able to find measurements later, we put them into a map
// We need to copy input SourceLinks anyway, so the map can own them.
ACTS_VERBOSE("Preparing " << std::distance(it, end)
Expand Down Expand Up @@ -1174,14 +1174,14 @@ class KalmanFitter {
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
auto fit(source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const KalmanFitterOptions<traj_t>& kfOptions,
const std::vector<const Surface*>& sSequence,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer)
const -> std::enable_if_t<
_isdn, Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>> {
requires(_isdn) auto fit(
source_link_iterator_t it, source_link_iterator_t end,
const start_parameters_t& sParameters,
const KalmanFitterOptions<traj_t>& kfOptions,
const std::vector<const Surface*>& sSequence,
TrackContainer<track_container_t, traj_t, holder_t>& trackContainer) const
-> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy> {
// To be able to find measurements later, we put them into a map
// We need to copy input SourceLinks anyway, so the map can own them.
ACTS_VERBOSE("Preparing " << std::distance(it, end)
Expand Down
6 changes: 3 additions & 3 deletions Core/include/Acts/Utilities/Any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ class AnyBase : public AnyBaseAll {
AnyBase() = default;
#endif

template <typename T, typename = std::enable_if_t<
!std::is_same_v<std::decay_t<T>, AnyBase<SIZE>>>>
explicit AnyBase(T&& value)
template <typename T>
requires(!std::same_as<std::decay_t<T>, AnyBase<SIZE>>) explicit AnyBase(
T&& value)
: AnyBase{std::in_place_type<T>, std::forward<T>(value)} {}

template <typename T>
Expand Down
98 changes: 42 additions & 56 deletions Core/include/Acts/Utilities/Axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,10 @@ class Axis<AxisType::Equidistant, bdt> final : public IAxis {
/// @note Open varies given bin and allows 0 and NBins+1 (underflow,
/// overflow)
/// as neighbors
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Open, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Open) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
constexpr int min = 0;
const int max = getNBins() + 1;
const int itmin = std::clamp(static_cast<int>(idx + sizes.first), min, max);
Expand All @@ -196,11 +195,10 @@ class Axis<AxisType::Equidistant, bdt> final : public IAxis {
/// @return Set of neighboring bin indices (global)
/// @note Bound varies given bin and allows 1 and NBins (regular bins)
/// as neighbors
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Bound, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Bound) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
}
Expand All @@ -221,11 +219,10 @@ class Axis<AxisType::Equidistant, bdt> final : public IAxis {
/// @return Set of neighboring bin indices (global)
/// @note Closed varies given bin and allows bins on the opposite
/// side of the axis as neighbors. (excludes underflow / overflow)
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Closed, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Closed) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
// Handle invalid indices
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
Expand Down Expand Up @@ -266,33 +263,29 @@ class Axis<AxisType::Equidistant, bdt> final : public IAxis {
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Open, int> = 0>
std::size_t wrapBin(int bin) const {
return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0);
}
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Open) std::size_t wrapBin(int bin)
const { return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0); }

/// @brief Converts bin index into a valid one for this axis.
///
/// @note Bound: bin index is clamped to [1, nBins]
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Bound, int> = 0>
std::size_t wrapBin(int bin) const {
return std::max(std::min(bin, static_cast<int>(getNBins())), 1);
}
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Bound) std::size_t wrapBin(int bin)
const { return std::max(std::min(bin, static_cast<int>(getNBins())), 1); }

/// @brief Converts bin index into a valid one for this axis.
///
/// @note Closed: bin index wraps around to other side
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Closed, int> = 0>
std::size_t wrapBin(int bin) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Closed) std::size_t wrapBin(int bin)
const {
const int w = getNBins();
return 1 + (w + ((bin - 1) % w)) % w;
// return int(bin<1)*w - int(bin>w)*w + bin;
Expand Down Expand Up @@ -477,11 +470,10 @@ class Axis<AxisType::Variable, bdt> final : public IAxis {
/// @note Open varies given bin and allows 0 and NBins+1 (underflow,
/// overflow)
/// as neighbors
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Open, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Open) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
constexpr int min = 0;
const int max = getNBins() + 1;
const int itmin = std::max(min, static_cast<int>(idx) + sizes.first);
Expand All @@ -498,11 +490,10 @@ class Axis<AxisType::Variable, bdt> final : public IAxis {
/// @return Set of neighboring bin indices (global)
/// @note Bound varies given bin and allows 1 and NBins (regular bins)
/// as neighbors
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Bound, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Bound) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
}
Expand All @@ -522,11 +513,10 @@ class Axis<AxisType::Variable, bdt> final : public IAxis {
/// @return Set of neighboring bin indices (global)
/// @note Closed varies given bin and allows bins on the opposite
/// side of the axis as neighbors. (excludes underflow / overflow)
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Closed, int> = 0>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Closed) NeighborHoodIndices
neighborHoodIndices(std::size_t idx, std::pair<int, int> sizes = {-1, 1})
const {
// Handle invalid indices
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
Expand Down Expand Up @@ -567,33 +557,29 @@ class Axis<AxisType::Variable, bdt> final : public IAxis {
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Open, int> = 0>
std::size_t wrapBin(int bin) const {
return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0);
}
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Open) std::size_t wrapBin(int bin)
const { return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0); }

/// @brief Converts bin index into a valid one for this axis.
///
/// @note Bound: bin index is clamped to [1, nBins]
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Bound, int> = 0>
std::size_t wrapBin(int bin) const {
return std::max(std::min(bin, static_cast<int>(getNBins())), 1);
}
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Bound) std::size_t wrapBin(int bin)
const { return std::max(std::min(bin, static_cast<int>(getNBins())), 1); }

/// @brief Converts bin index into a valid one for this axis.
///
/// @note Closed: bin index wraps around to other side
///
/// @param [in] bin The bin to wrap
/// @return valid bin index
template <AxisBoundaryType T = bdt,
std::enable_if_t<T == AxisBoundaryType::Closed, int> = 0>
std::size_t wrapBin(int bin) const {
template <AxisBoundaryType T = bdt>
requires(T == AxisBoundaryType::Closed) std::size_t wrapBin(int bin)
const {
const int w = getNBins();
return 1 + (w + ((bin - 1) % w)) % w;
// return int(bin<1)*w - int(bin>w)*w + bin;
Expand Down
34 changes: 19 additions & 15 deletions Core/include/Acts/Utilities/BoundingBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ class AxisAlignedBoundingBox {
* @param color The color to use for drawing
* @param trf An optional transform to apply first.
*/
template <std::size_t D = DIM, std::enable_if_t<D == 3, int> = 0>
void draw(IVisualization3D& helper,
std::array<int, 3> color = {120, 120, 120},
const transform_type& trf = transform_type::Identity()) const;
template <std::size_t D = DIM>
requires(D == 3) void draw(
IVisualization3D& helper, std::array<int, 3> color = {120, 120, 120},
const transform_type& trf = transform_type::Identity()) const;

/**
* Draw this bounding box as SVG. This method is only available for the 2D
Expand All @@ -307,19 +307,23 @@ class AxisAlignedBoundingBox {
* @param fillcolor Color to fill the box with.
* @return The outstream given in @p os.
*/
template <std::size_t D = DIM, std::enable_if_t<D == 2, int> = 0>
std::ostream& svg(std::ostream& os, value_type w, value_type h,
value_type unit = 10, const std::string& label = "",
const std::string& fillcolor = "grey") const;
template <std::size_t D = DIM>
requires(D ==
2) std::ostream& svg(std::ostream& os, value_type w, value_type h,
value_type unit = 10,
const std::string& label = "",
const std::string& fillcolor = "grey") const;

private:
template <std::size_t D = DIM, std::enable_if_t<D == 2, int> = 0>
std::pair<VertexType, VertexType> transformVertices(
const transform_type& trf) const;

template <std::size_t D = DIM, std::enable_if_t<D == 3, int> = 0>
std::pair<VertexType, VertexType> transformVertices(
const transform_type& trf) const;
template <std::size_t D = DIM>
requires(D == 2) std::pair<VertexType, VertexType> transformVertices(
const transform_type& trf)
const;

template <std::size_t D = DIM>
requires(D == 3) std::pair<VertexType, VertexType> transformVertices(
const transform_type& trf)
const;

const entity_t* m_entity;
VertexType m_vmin;
Expand Down
Loading

0 comments on commit 019f2db

Please sign in to comment.