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 21, 2024
1 parent 5725e8b commit af3b230
Show file tree
Hide file tree
Showing 27 changed files with 326 additions and 247 deletions.
14 changes: 8 additions & 6 deletions Core/include/Acts/EventData/ProxyAccessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ 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>
T& operator()(proxy_t proxy) const
requires(!ReadOnly)
{
static_assert(!proxy_t::ReadOnly,
"Cannot get mutable ref for const track proxy");
return proxy.template component<T>(key);
Expand All @@ -92,9 +93,10 @@ 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>
const T& operator()(proxy_t proxy) const
requires(ReadOnly)
{
if constexpr (proxy_t::ReadOnly) {
return proxy.template component<T>(key);

Expand Down
8 changes: 5 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,10 @@ 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) : m_upstream(std::forward<T>(upstream)) {
template <typename T>
explicit SourceLink(T&& upstream)
requires(!std::same_as<std::decay_t<T>, SourceLink>)
: m_upstream(std::forward<T>(upstream)) {
static_assert(!std::is_same_v<std::decay_t<T>, SourceLink>,
"Cannot wrap SourceLink in SourceLink");
}
Expand Down
10 changes: 6 additions & 4 deletions Core/include/Acts/EventData/TrackStateProxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ 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() {
double& pathLength()
requires(!ReadOnly)
{
return component<double, hashString("pathLength")>();
}

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

template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
Parameters predicted() {
Parameters predicted()
requires(!ReadOnly)
{
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 {
Vector3 getBinCenterImpl(std::size_t bin) const
requires(DIM != 1)
{
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 {
Vector3 getBinCenterImpl(std::size_t bin) const
requires(DIM == 1)
{
point_t pos = m_grid.binCenter(m_grid.localBinsFromGlobalBin(bin));
return m_localToGlobal(pos);
}
Expand Down
10 changes: 5 additions & 5 deletions Core/include/Acts/TrackFitting/GlobalChiSquareFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,15 @@ class Gx2Fitter {
/// @return the output as an output track
template <typename source_link_iterator_t, typename start_parameters_t,
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
typename track_container_t, template <typename> class holder_t>
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>> {
const -> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>
requires(!isDirectNavigator)
{
// 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
20 changes: 10 additions & 10 deletions Core/include/Acts/TrackFitting/KalmanFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,15 +1082,15 @@ class KalmanFitter {
/// @return the output as an output track
template <typename source_link_iterator_t, typename start_parameters_t,
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
typename track_container_t, template <typename> class holder_t>
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>> {
const -> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>
requires(!isDirectNavigator)
{
// 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 @@ -1172,16 +1172,16 @@ class KalmanFitter {
/// @return the output as an output track
template <typename source_link_iterator_t, typename start_parameters_t,
typename parameters_t = BoundTrackParameters,
typename track_container_t, template <typename> class holder_t,
bool _isdn = isDirectNavigator>
typename track_container_t, template <typename> class holder_t>
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>> {
const -> Result<typename TrackContainer<track_container_t, traj_t,
holder_t>::TrackProxy>
requires(isDirectNavigator)
{
// 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
4 changes: 2 additions & 2 deletions Core/include/Acts/Utilities/Any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,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>>>>
template <typename T>
explicit AnyBase(T&& value)
requires(!std::same_as<std::decay_t<T>, AnyBase<SIZE>>)
: AnyBase{std::in_place_type<T>, std::forward<T>(value)} {}

template <typename T>
Expand Down
90 changes: 48 additions & 42 deletions Core/include/Acts/Utilities/Axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ 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>
template <AxisBoundaryType T = bdt>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
std::pair<int, int> sizes = {-1,
1}) const
requires(T == AxisBoundaryType::Open)
{
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 +197,12 @@ 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>
template <AxisBoundaryType T = bdt>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
std::pair<int, int> sizes = {-1,
1}) const
requires(T == AxisBoundaryType::Bound)
{
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
}
Expand All @@ -221,11 +223,12 @@ 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>
template <AxisBoundaryType T = bdt>
NeighborHoodIndices neighborHoodIndices(std::size_t idx,
std::pair<int, int> sizes = {
-1, 1}) const {
std::pair<int, int> sizes = {-1,
1}) const
requires(T == AxisBoundaryType::Closed)
{
// Handle invalid indices
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
Expand Down Expand Up @@ -266,9 +269,10 @@ 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 {
template <AxisBoundaryType T = bdt>
std::size_t wrapBin(int bin) const
requires(T == AxisBoundaryType::Open)
{
return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0);
}

Expand All @@ -278,9 +282,10 @@ 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::Bound, int> = 0>
std::size_t wrapBin(int bin) const {
template <AxisBoundaryType T = bdt>
std::size_t wrapBin(int bin) const
requires(T == AxisBoundaryType::Bound)
{
return std::max(std::min(bin, static_cast<int>(getNBins())), 1);
}

Expand All @@ -290,9 +295,10 @@ 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::Closed, int> = 0>
std::size_t wrapBin(int bin) const {
template <AxisBoundaryType T = bdt>
std::size_t wrapBin(int bin) const
requires(T == AxisBoundaryType::Closed)
{
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 @@ -488,11 +494,11 @@ 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 {
std::pair<int, int> sizes = {-1,
1}) const
requires(bdt == AxisBoundaryType::Open)
{
constexpr int min = 0;
const int max = getNBins() + 1;
const int itmin = std::max(min, static_cast<int>(idx) + sizes.first);
Expand All @@ -509,11 +515,11 @@ 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 {
std::pair<int, int> sizes = {-1,
1}) const
requires(bdt == AxisBoundaryType::Bound)
{
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
}
Expand All @@ -533,11 +539,11 @@ 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 {
std::pair<int, int> sizes = {-1,
1}) const
requires(bdt == AxisBoundaryType::Closed)
{
// Handle invalid indices
if (idx <= 0 || idx >= (getNBins() + 1)) {
return NeighborHoodIndices();
Expand Down Expand Up @@ -578,9 +584,9 @@ 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 {
std::size_t wrapBin(int bin) const
requires(bdt == AxisBoundaryType::Open)
{
return std::max(std::min(bin, static_cast<int>(getNBins()) + 1), 0);
}

Expand All @@ -590,9 +596,9 @@ 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::Bound, int> = 0>
std::size_t wrapBin(int bin) const {
std::size_t wrapBin(int bin) const
requires(bdt == AxisBoundaryType::Bound)
{
return std::max(std::min(bin, static_cast<int>(getNBins())), 1);
}

Expand All @@ -602,9 +608,9 @@ 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::Closed, int> = 0>
std::size_t wrapBin(int bin) const {
std::size_t wrapBin(int bin) const
requires(bdt == AxisBoundaryType::Closed)
{
const int w = getNBins();
return 1 + (w + ((bin - 1) % w)) % w;
// return int(bin<1)*w - int(bin>w)*w + bin;
Expand Down
16 changes: 8 additions & 8 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;
const transform_type& trf = transform_type::Identity()) const
requires(DIM == 3);

/**
* Draw this bounding box as SVG. This method is only available for the 2D
Expand All @@ -307,19 +307,19 @@ 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;
const std::string& fillcolor = "grey") const
requires(DIM == 2);

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;
const transform_type& trf) const
requires(DIM == 2);

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

const entity_t* m_entity;
VertexType m_vmin;
Expand Down
Loading

0 comments on commit af3b230

Please sign in to comment.