Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Use strong types for context objects #673

Merged
merged 5 commits into from
Jan 28, 2021

Conversation

paulgessinger
Copy link
Member

@paulgessinger paulgessinger commented Jan 26, 2021

Move away from using a raw typedef to std::any. Doing that has certain drawbacks: std::any can be convert constructed from anything. This causes problems when constructing anything that only accepts a context any. GCC9 even has recursion trouble in the type_traits infrastructure leading to compile time failures.

Moving to wrappers can alleviate this. They only have an explicit constructor, meaning that they are not autoconstructed by conversion. This is a little bit more cumbersome to use, but much more robust I believe.

Performance should not be affected.

Previously, to break the problems of converting constructors with a single std::any argument, we resorted to std::reference_wrapper<std::any>, which only resolved it to some degree. This PR also removes this workaround. std::reference_wrapper<const {Geometry,MagneticField}Context> is still retained when used to store a context reference as a member, as for example in the stepper state.

BREAKING CHANGE: The public interface of the geometry and magnetic context type change. Before, they were raw std::anys, now they are of type Acts::ContextType. Construction needs to be explicit, and the contained std::any can be accessed via the Acts::ContextType::get() methods. std::any_cast to unpack the concrete context value is still supported, but needs to retrieve the internal std::any first. NOTE: If you forget to call .any() on the context object, std::any_cast will compile, but will throw std::bad_any_cast at runtime.

Move away from using a raw typedef to std::any. Doing that has certain
drawbacks: std::any can be convert constructed from anything. This
causes problems when constructing anything that only accepts a context
any. GCC9 even has recursion trouble in the type_traits infrastructure
leading to compile time failures.

Moving to wrappers can alleviate this. They only have an explicit
constructor, meaning that they are not autoconstructed by conversion.
This is a little bit more cumbersome to use, but much more robust I
believe.

Performance should not be affected.
@paulgessinger paulgessinger added this to the next milestone Jan 26, 2021
@paulgessinger paulgessinger changed the title feat: Use strong types for context objects feat!: Use strong types for context objects Jan 26, 2021
@codecov
Copy link

codecov bot commented Jan 26, 2021

Codecov Report

Merging #673 (b1474ae) into master (41c61f4) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #673      +/-   ##
==========================================
+ Coverage   49.06%   49.08%   +0.01%     
==========================================
  Files         329      330       +1     
  Lines       16438    16443       +5     
  Branches     7657     7657              
==========================================
+ Hits         8066     8071       +5     
  Misses       2998     2998              
  Partials     5374     5374              
Impacted Files Coverage Δ
...de/Acts/TrackFinding/CombinatorialKalmanFilter.hpp 30.27% <ø> (ø)
Core/include/Acts/Vertexing/VertexingOptions.hpp 100.00% <ø> (ø)
Core/include/Acts/MagneticField/ConstantBField.hpp 92.30% <100.00%> (ø)
...clude/Acts/MagneticField/InterpolatedBFieldMap.hpp 67.24% <100.00%> (ø)
Core/include/Acts/MagneticField/NullBField.hpp 75.00% <100.00%> (ø)
Core/include/Acts/MagneticField/SolenoidBField.hpp 100.00% <100.00%> (ø)
...re/include/Acts/Material/SurfaceMaterialMapper.hpp 50.00% <100.00%> (ø)
...ore/include/Acts/Material/VolumeMaterialMapper.hpp 42.85% <100.00%> (ø)
Core/include/Acts/Propagator/AtlasStepper.hpp 70.02% <100.00%> (ø)
...lude/Acts/Propagator/DenseEnvironmentExtension.hpp 100.00% <100.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 41c61f4...44c9c7b. Read the comment docs.

Copy link
Contributor

@HadrienG2 HadrienG2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@asalzburger
Copy link
Contributor

I approve, nice work Paul!

Let's wait for the CI and merge then.

@paulgessinger
Copy link
Member Author

I think I'm actually going to implement @benjaminhuth's suggestion before we merge this.

@paulgessinger paulgessinger added the 🚧 WIP Work-in-progress label Jan 28, 2021
@paulgessinger
Copy link
Member Author

Ok, I switched to an access method, and don't expose the std::any anymore at all.

@paulgessinger paulgessinger removed the 🚧 WIP Work-in-progress label Jan 28, 2021
@@ -84,7 +84,7 @@ inline const Acts::Transform3& AlignedDetectorElement::transform(
// Check if a different transform than the nominal exists
if (m_alignedTransforms.size()) {
// cast into the right context object
auto alignContext = std::any_cast<ContextType>(gctx);
auto alignContext = gctx.get<ContextType>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks better.

Copy link
Contributor

@asalzburger asalzburger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving.

@asalzburger asalzburger merged commit 8514b0c into acts-project:master Jan 28, 2021
@paulgessinger paulgessinger modified the milestones: next, v5.0.0 Jan 29, 2021
asalzburger pushed a commit to asalzburger/acts that referenced this pull request Feb 3, 2021
Move away from using a raw typedef to std::any. Doing that has certain drawbacks: std::any can be convert constructed from anything. This causes problems when constructing anything that only accepts a context any. GCC9 even has recursion trouble in the type_traits infrastructure leading to compile time failures.

Moving to wrappers can alleviate this. They only have an explicit constructor, meaning that they are not autoconstructed by conversion. This is a little bit more cumbersome to use, but much more robust I believe.

Performance should not be affected.

Previously, to break the problems of converting constructors with a single std::any argument, we resorted to std::reference_wrapper<std::any>, which only resolved it to some degree. This PR also removes this workaround. std::reference_wrapper<const {Geometry,MagneticField}Context> is still retained when used to store a context reference as a member, as for example in the stepper state.

BREAKING CHANGE: The public interface of the geometry and magnetic context type change. Before, they were raw std::anys, now they are of type Acts::ContextType. Construction needs to be explicit, and the contained std::any can be accessed via the Acts::ContextType::get() methods. std::any_cast to unpack the concrete context value is still supported, but needs to retrieve the internal std::any first. NOTE: If you forget to call .any() on the context object, std::any_cast will compile, but will throw std::bad_any_cast at runtime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants