Skip to content

Commit 49febe2

Browse files
refactor: Explicit default construction for GeometryContext (acts-project#4957)
This pull request refactors the way default `GeometryContext` objects are constructed and used throughout the codebase. The main change is to deprecate the use of the default `GeometryContext` constructor and replace it with an explicit factory method, `GeometryContext::dangerouslyDefaultConstruct()`. This makes the creation of empty or default geometry contexts more intentional and clear, especially for testing and simple use cases.
1 parent 4c01baf commit 49febe2

File tree

217 files changed

+506
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+506
-292
lines changed

Core/include/Acts/EventData/detail/MultiTrajectoryTestsCommon.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class MultiTrajectoryTestsCommon {
466466

467467
// use temporary measurement to reset calibrated data
468468
TestTrackState ttsb(rng, 2u);
469-
Acts::GeometryContext gctx;
469+
const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
470470
Acts::CalibrationContext cctx;
471471
BOOST_CHECK_EQUAL(
472472
ts.getUncalibratedSourceLink().template get<TestSourceLink>().sourceId,

Core/include/Acts/EventData/detail/TestTrackState.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ void fillTrackState(const TestTrackState& pc, TrackStatePropMask mask,
126126
// source link defines the uncalibrated measurement
127127
// create calibrated measurements from source link
128128
if (ACTS_CHECK_BIT(mask, TrackStatePropMask::Calibrated)) {
129-
testSourceLinkCalibrator<trajectory_t>(Acts::GeometryContext{},
130-
Acts::CalibrationContext{},
131-
SourceLink{pc.sourceLink}, ts);
129+
testSourceLinkCalibrator<trajectory_t>(
130+
Acts::GeometryContext::dangerouslyDefaultConstruct(),
131+
Acts::CalibrationContext{}, SourceLink{pc.sourceLink}, ts);
132132
assert(ts.hasUncalibratedSourceLink());
133133
}
134134
}

Core/include/Acts/Geometry/GeometryContext.hpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
// Set the Geometry Context PLUGIN
12+
#include "Acts/Utilities/Diagnostics.hpp"
1213
#ifdef ACTS_CORE_GEOMETRYCONTEXT_PLUGIN
1314
#include ACTS_CORE_GEOMETRYCONTEXT_PLUGIN
1415
#else
@@ -25,11 +26,57 @@ namespace Acts {
2526
/// @ingroup context
2627
///
2728
/// It is propagated through the code to allow for event/thread
28-
/// dependent geometry changes
29+
/// dependent geometry changes.
30+
///
31+
/// ## Construction
32+
///
33+
/// For typical use cases with alignment or conditions data:
34+
/// @code
35+
/// MyAlignmentData alignment = ...;
36+
/// GeometryContext gctx{alignment};
37+
/// @endcode
38+
///
39+
/// For testing or simple applications without alignment:
40+
/// @code
41+
/// auto gctx = GeometryContext::dangerouslyDefaultConstruct();
42+
/// @endcode
43+
///
44+
/// @note The default constructor is deprecated. Use the factory method
45+
/// dangerouslyDefaultConstruct() to make empty context creation explicit.
2946
class GeometryContext : public ContextType {
3047
public:
31-
/// Inherit all constructors
32-
using ContextType::ContextType;
48+
/// Static factory method for default construction
49+
/// @note Use this when you need a default context for testing or
50+
/// simple applications without alignment/conditions data
51+
static GeometryContext dangerouslyDefaultConstruct() {
52+
ACTS_PUSH_IGNORE_DEPRECATED()
53+
return GeometryContext();
54+
ACTS_POP_IGNORE_DEPRECATED()
55+
}
56+
57+
/// Default constructor
58+
/// @deprecated Use GeometryContext::dangerouslyDefaultConstruct() instead
59+
/// to make empty context construction explicit
60+
[[deprecated("Use GeometryContext::dangerouslyDefaultConstruct() instead")]]
61+
GeometryContext() = default;
62+
63+
/// Move construct from arbitrary type (inherited from ContextType)
64+
/// @tparam T The type of the value to construct from
65+
/// @param value The value to construct from
66+
template <typename T>
67+
requires(!std::is_same_v<std::decay_t<T>, GeometryContext> &&
68+
!std::is_base_of_v<ContextType, std::decay_t<T> >)
69+
explicit GeometryContext(T&& value) : ContextType(std::forward<T>(value)) {}
70+
71+
/// Copy construct from arbitrary type (inherited from ContextType)
72+
/// @tparam T The type of the value to construct from
73+
/// @param value The value to construct from
74+
template <typename T>
75+
requires(!std::is_same_v<std::decay_t<T>, GeometryContext> &&
76+
!std::is_base_of_v<ContextType, std::decay_t<T> >)
77+
explicit GeometryContext(const T& value) : ContextType(value) {}
78+
79+
/// Inherit assignment operators
3380
using ContextType::operator=;
3481
};
3582

Core/include/Acts/Material/BinnedSurfaceMaterialAccumulater.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BinnedSurfaceMaterialAccumulater final
2626
/// @brief Nested config struct
2727
struct Config {
2828
/// Geometry context for coordinate transformations
29-
GeometryContext geoContext;
29+
GeometryContext geoContext = GeometryContext::dangerouslyDefaultConstruct();
3030

3131
/// Correct for empty bins (recommended)
3232
bool emptyBinCorrection = true;

Core/include/Acts/Surfaces/SurfaceArray.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class SurfaceArray {
203203
/// @param bin the global bin index
204204
/// @return The bin center
205205
Vector3 getBinCenter(std::size_t bin) const override {
206-
GeometryContext gctx;
206+
auto gctx = GeometryContext::dangerouslyDefaultConstruct();
207207
return getBinCenterImpl(gctx, bin);
208208
}
209209

@@ -355,7 +355,7 @@ class SurfaceArray {
355355

356356
std::size_t findGlobalBin(const Vector3& position, const Vector3& direction,
357357
double tolerance) const {
358-
GeometryContext gctx;
358+
auto gctx = GeometryContext::dangerouslyDefaultConstruct();
359359

360360
const Intersection3D intersection =
361361
m_representative

Core/include/Acts/TrackFinding/TrackParamsLookupAccumulator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class TrackParamsLookupAccumulator {
7878
LookupGrid finalizeLookup() {
7979
auto meanTrack = [&](const TrackParameters& track, std::size_t count) {
8080
if constexpr (detail::isGenericBoundTrackParams<TrackParameters>) {
81-
Acts::GeometryContext gctx;
81+
const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
8282

8383
auto res = TrackParameters::create(
8484
gctx, track.referenceSurface().getSharedPtr(),
@@ -146,7 +146,7 @@ class TrackParamsLookupAccumulator {
146146

147147
// Assume track parameters being i.i.d.
148148
if constexpr (detail::isGenericBoundTrackParams<TrackParameters>) {
149-
Acts::GeometryContext gctx;
149+
const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
150150

151151
Acts::Vector4 fourPosition = a.fourPosition(gctx) + b.fourPosition(gctx);
152152

Core/include/Acts/TrackFitting/GaussianSumFitter.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ struct GaussianSumFitter {
245245
// Check if the start parameters are on the start surface
246246
IntersectionStatus intersectionStatusStartSurface =
247247
sParameters.referenceSurface()
248-
.intersect(GeometryContext{},
249-
sParameters.position(GeometryContext{}),
248+
.intersect(GeometryContext::dangerouslyDefaultConstruct(),
249+
sParameters.position(
250+
GeometryContext::dangerouslyDefaultConstruct()),
250251
sParameters.direction(), BoundaryTolerance::None())
251252
.closest()
252253
.status();

Core/include/Acts/Utilities/BinAdjustment.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static inline BinUtility adjustBinUtility(const BinUtility& bu,
256256
}
257257

258258
std::stringstream ss;
259-
ss << surface.toStream({});
259+
ss << surface.toStream(GeometryContext::dangerouslyDefaultConstruct());
260260
throw std::invalid_argument(
261261
"Bin adjustment not implemented for this surface yet:\n" + ss.str());
262262
}

Core/include/Acts/Utilities/Diagnostics.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@
3232
#define ACTS_DIAGNOSTIC_POP() _ACTS_DO_PRAGMA(GCC diagnostic pop)
3333

3434
#endif
35+
36+
#define ACTS_PUSH_IGNORE_DEPRECATED() \
37+
ACTS_DIAGNOSTIC_PUSH() \
38+
ACTS_DIAGNOSTIC_IGNORE("-Wdeprecated-declarations")
39+
40+
#define ACTS_POP_IGNORE_DEPRECATED() ACTS_DIAGNOSTIC_POP()

Core/include/Acts/Utilities/detail/ContextType.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class ContextType {
3030
/// @tparam T The type of the value to construct from
3131
/// @param value The value to construct from
3232
template <typename T>
33+
requires(!std::is_base_of_v<ContextType, std::decay_t<T>>)
3334
explicit ContextType(T&& value) : m_data{std::forward<T>(value)} {}
3435

3536
/// Copy construct a new Context Type object from anything. Must be explicit.
3637
///
3738
/// @tparam T The type of the value to construct from
3839
/// @param value The value to construct from
3940
template <typename T>
41+
requires(!std::is_base_of_v<ContextType, std::decay_t<T>>)
4042
explicit ContextType(const T& value) : m_data{value} {}
4143

4244
/// Move assignment of anything to this object is allowed.
@@ -45,6 +47,7 @@ class ContextType {
4547
/// @param value The value to assign
4648
/// @return ContextType&
4749
template <typename T>
50+
requires(!std::is_base_of_v<ContextType, std::decay_t<T>>)
4851
ContextType& operator=(T&& value) {
4952
m_data = std::forward<T>(value);
5053
return *this;
@@ -56,6 +59,7 @@ class ContextType {
5659
/// @param value The value to assign
5760
/// @return ContextType&
5861
template <typename T>
62+
requires(!std::is_base_of_v<ContextType, std::decay_t<T>>)
5963
ContextType& operator=(const T& value) {
6064
m_data = value;
6165
return *this;

0 commit comments

Comments
 (0)