From e5913aaeb05f6eabb8962639800dec23f2b352dd Mon Sep 17 00:00:00 2001 From: Maximilian Reininghaus <maximilian.reininghaus@tu-dortmund.de> Date: Fri, 7 May 2021 19:15:44 +0200 Subject: [PATCH] take ownership of underlying process --- .../process/IntLengthModifyingProcess.inl | 15 +++++++++++-- .../process/IntLengthModifyingProcess.hpp | 7 ++++-- .../testIntLengthModifyingProcess.cpp | 22 ++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/corsika/detail/framework/process/IntLengthModifyingProcess.inl b/corsika/detail/framework/process/IntLengthModifyingProcess.inl index 6c22e45a3..1300439a4 100644 --- a/corsika/detail/framework/process/IntLengthModifyingProcess.inl +++ b/corsika/detail/framework/process/IntLengthModifyingProcess.inl @@ -17,9 +17,9 @@ namespace corsika { template <class TUnderlyingProcess> inline IntLengthModifyingProcess<TUnderlyingProcess>::IntLengthModifyingProcess( - TUnderlyingProcess& process, + TUnderlyingProcess&& process, std::function<IntLengthModifyingProcess::functor_signature> modifier) - : process_{process} + : process_{std::move(process)} , modifier_{std::move(modifier)} {} template <class TUnderlyingProcess> @@ -40,4 +40,15 @@ namespace corsika { return modifier_(original, pid, energy); } + template <class TUnderlyingProcess> + inline TUnderlyingProcess const& + IntLengthModifyingProcess<TUnderlyingProcess>::getProcess() const { + return process_; + } + + template <class TUnderlyingProcess> + inline TUnderlyingProcess& IntLengthModifyingProcess<TUnderlyingProcess>::getProcess() { + return process_; + } + } // namespace corsika diff --git a/corsika/framework/process/IntLengthModifyingProcess.hpp b/corsika/framework/process/IntLengthModifyingProcess.hpp index bec41381e..6507ba70c 100644 --- a/corsika/framework/process/IntLengthModifyingProcess.hpp +++ b/corsika/framework/process/IntLengthModifyingProcess.hpp @@ -35,7 +35,7 @@ namespace corsika { //! energy using functor_signature = GrammageType(GrammageType, corsika::Code, HEPEnergyType); - IntLengthModifyingProcess(TUnderlyingProcess& process, + IntLengthModifyingProcess(TUnderlyingProcess&& process, std::function<functor_signature> modifier); //! wrapper around internal process doInteraction @@ -46,8 +46,11 @@ namespace corsika { template <typename TParticle> GrammageType getInteractionLength(TParticle const& particle); + TUnderlyingProcess const& getProcess() const; + TUnderlyingProcess& getProcess(); + private: - TUnderlyingProcess& process_; + TUnderlyingProcess process_; std::function<functor_signature> const modifier_{non_modifying_functor}; }; diff --git a/tests/framework/testIntLengthModifyingProcess.cpp b/tests/framework/testIntLengthModifyingProcess.cpp index 16fe22b18..3748609b3 100644 --- a/tests/framework/testIntLengthModifyingProcess.cpp +++ b/tests/framework/testIntLengthModifyingProcess.cpp @@ -14,6 +14,8 @@ using namespace corsika; struct DummyProcess { + int id{0}; + template <typename TParticle> GrammageType getInteractionLength(TParticle const&) { return 100_g / 1_cm / 1_cm; @@ -23,6 +25,13 @@ struct DummyProcess { void doInteraction(TArgument& arg) { arg = 3; } + + DummyProcess() = default; + DummyProcess(DummyProcess&&) = default; + + // prevent copying + DummyProcess(DummyProcess const&) = delete; + DummyProcess& operator=(DummyProcess const&) = delete; }; struct DummyParticle { @@ -33,12 +42,14 @@ struct DummyParticle { TEST_CASE("IntLengthModifyingProcess", "[process]") { DummyProcess u; + u.id = 38; auto const modifier = [](GrammageType orig, Code, HEPEnergyType) -> GrammageType { return orig * 2; }; - IntLengthModifyingProcess mod{u, modifier}; + IntLengthModifyingProcess mod{std::move(u), modifier}; + REQUIRE(std::is_same_v<decltype(mod), IntLengthModifyingProcess<DummyProcess>>); SECTION("getInteractionLength") { DummyParticle const p; @@ -50,4 +61,13 @@ TEST_CASE("IntLengthModifyingProcess", "[process]") { mod.doInteraction(k); REQUIRE(k == 3); } + + SECTION("getProcess") { + DummyProcess& uRef = mod.getProcess(); + REQUIRE(uRef.id == 38); + + decltype(mod) const& modConstRef = mod; + DummyProcess const& uConstRef = modConstRef.getProcess(); + REQUIRE(uConstRef.id == 38); + } } -- GitLab