From ff612238626355f9fba877bb2b7a342186137126 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Mon, 15 Apr 2024 15:44:01 -0400 Subject: [PATCH] Rename JFactoryT::GetAndCreate() to CreateAndGetData The new name more accurately reflects the relationship between Get() and Create(). The caching logic always lives inside Create(), so that untyped retrievals via podio::Frame or JFactoryT::GetAs behave consistently. --- src/libraries/JANA/JEvent.h | 18 +++++++------- src/libraries/JANA/JFactoryT.h | 4 ++-- src/programs/unit_tests/JFactoryTests.cc | 30 ++++++++++++------------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/libraries/JANA/JEvent.h b/src/libraries/JANA/JEvent.h index 11fd582b5..749c5fd70 100644 --- a/src/libraries/JANA/JEvent.h +++ b/src/libraries/JANA/JEvent.h @@ -295,7 +295,7 @@ inline JMetadata JEvent::GetMetadata(const std::string& tag) const { auto factory = GetFactory(tag, true); // Make sure that JFactoryT::Process has already been called before returning the metadata - factory->GetOrCreate(this->shared_from_this()); + factory->CreateAndGetData(this->shared_from_this()); return factory->GetMetadata(); } @@ -314,7 +314,7 @@ JFactoryT* JEvent::Get(const T** destination, const std::string& tag) const { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iterators = factory->GetOrCreate(this->shared_from_this()); + auto iterators = factory->CreateAndGetData(this->shared_from_this()); if (std::distance(iterators.first, iterators.second) == 0) { *destination = nullptr; } @@ -330,7 +330,7 @@ JFactoryT* JEvent::Get(std::vector& destination, const std::string& { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iterators = factory->GetOrCreate(this->shared_from_this()); + auto iterators = factory->CreateAndGetData(this->shared_from_this()); for (auto it=iterators.first; it!=iterators.second; it++) { destination.push_back(*it); } @@ -350,7 +350,7 @@ JFactoryT* JEvent::Get(std::vector& destination, const std::string& template const T* JEvent::GetSingle(const std::string& tag) const { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iterators = factory->GetOrCreate(this->shared_from_this()); + auto iterators = factory->CreateAndGetData(this->shared_from_this()); if (std::distance(iterators.first, iterators.second) == 0) { return nullptr; } @@ -366,7 +366,7 @@ template const T* JEvent::GetSingle(const std::string& tag) const { template const T* JEvent::GetSingleStrict(const std::string& tag) const { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iterators = factory->GetOrCreate(this->shared_from_this()); + auto iterators = factory->CreateAndGetData(this->shared_from_this()); if (std::distance(iterators.first, iterators.second) == 0) { JException ex("GetSingle failed due to missing %d", NAME_OF(T)); ex.show_stacktrace = false; @@ -386,7 +386,7 @@ std::vector JEvent::Get(const std::string& tag) const { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iters = factory->GetOrCreate(this->shared_from_this()); + auto iters = factory->CreateAndGetData(this->shared_from_this()); std::vector vec; for (auto it=iters.first; it!=iters.second; ++it) { vec.push_back(*it); @@ -415,7 +415,7 @@ template void JEvent::GetAll(std::vector& destination) const { auto factories = GetFactoryAll(true); for (auto factory : factories) { - auto iterators = factory->GetOrCreate(this->shared_from_this()); + auto iterators = factory->CreateAndGetData(this->shared_from_this()); for (auto it = iterators.first; it != iterators.second; it++) { destination.push_back(*it); } @@ -429,7 +429,7 @@ std::vector JEvent::GetAll() const { auto factories = GetFactoryAll(true); for (auto factory : factories) { - auto iters = factory->GetOrCreate(this->shared_from_this()); + auto iters = factory->CreateAndGetData(this->shared_from_this()); std::vector vec; for (auto it = iters.first; it != iters.second; ++it) { vec.push_back(*it); @@ -462,7 +462,7 @@ template typename JFactoryT::PairType JEvent::GetIterators(const std::string& tag) const { auto factory = GetFactory(tag, true); JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope - auto iters = factory->GetOrCreate(this->shared_from_this()); + auto iters = factory->CreateAndGetData(this->shared_from_this()); return iters; } diff --git a/src/libraries/JANA/JFactoryT.h b/src/libraries/JANA/JFactoryT.h index c406620a7..f7730b5ca 100644 --- a/src/libraries/JANA/JFactoryT.h +++ b/src/libraries/JANA/JFactoryT.h @@ -74,11 +74,11 @@ class JFactoryT : public JFactory { return mData.size(); } - /// GetOrCreate handles all the preconditions and postconditions involved in calling the user-defined Open(), + /// CreateAndGetData handles all the preconditions and postconditions involved in calling the user-defined Open(), /// ChangeRun(), and Process() methods. These include making sure the JFactory JApplication is set, Init() is called /// exactly once, exceptions are tagged with the originating plugin and eventsource, ChangeRun() is /// called if and only if the run number changes, etc. - PairType GetOrCreate(const std::shared_ptr& event) { + PairType CreateAndGetData(const std::shared_ptr& event) { Create(event); return std::make_pair(mData.cbegin(), mData.cend()); } diff --git a/src/programs/unit_tests/JFactoryTests.cc b/src/programs/unit_tests/JFactoryTests.cc index aaaaeff24..6cb6cba9a 100644 --- a/src/programs/unit_tests/JFactoryTests.cc +++ b/src/programs/unit_tests/JFactoryTests.cc @@ -12,27 +12,27 @@ TEST_CASE("JFactoryTests") { - SECTION("GetOrCreate calls Init, ChangeRun, and Process as needed") { + SECTION("CreateAndGetData calls Init, ChangeRun, and Process as needed") { JFactoryTestDummyFactory sut; auto event = std::make_shared(); // The first time it is run, Init, ChangeRun, and Process each get run once - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.init_call_count == 1); REQUIRE(sut.change_run_call_count == 1); REQUIRE(sut.process_call_count == 1); // We can getOrCreate as many times as we want and nothing will happen // until somebody clears the factory (assuming persistence flag is unset) - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.init_call_count == 1); REQUIRE(sut.change_run_call_count == 1); REQUIRE(sut.process_call_count == 1); // Once we clear the factory, Process gets called again but Init and ChangeRun do not. sut.ClearData(); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.init_call_count == 1); REQUIRE(sut.change_run_call_count == 1); REQUIRE(sut.process_call_count == 2); @@ -62,31 +62,31 @@ TEST_CASE("JFactoryTests") { // For the first event, ChangeRun() always gets called event->SetEventNumber(1); event->SetRunNumber(22); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.change_run_call_count == 1); // Subsequent events with the same run number do not trigger ChangeRun() event->SetEventNumber(2); sut.ClearData(); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); event->SetEventNumber(3); sut.ClearData(); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.change_run_call_count == 1); // As soon as the run number changes, ChangeRun() gets called again event->SetEventNumber(4); event->SetRunNumber(49); sut.ClearData(); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.change_run_call_count == 2); // This keeps happening event->SetEventNumber(5); event->SetRunNumber(6180); sut.ClearData(); - sut.GetOrCreate(event); + sut.CreateAndGetData(event); REQUIRE(sut.change_run_call_count == 3); } @@ -98,7 +98,7 @@ TEST_CASE("JFactoryTests") { sut.ClearFactoryFlag(JFactory::NOT_OBJECT_OWNER); sut.Insert(new JFactoryTestDummyObject(42, &deleted_flag)); sut.ClearData(); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); REQUIRE(std::distance(results.first, results.second) == 0); REQUIRE(deleted_flag == true); } @@ -111,7 +111,7 @@ TEST_CASE("JFactoryTests") { sut.SetFactoryFlag(JFactory::NOT_OBJECT_OWNER); sut.Insert(new JFactoryTestDummyObject(42, &deleted_flag)); sut.ClearData(); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); REQUIRE(std::distance(results.first, results.second) == 0); REQUIRE(deleted_flag == false); } @@ -124,7 +124,7 @@ TEST_CASE("JFactoryTests") { sut.ClearFactoryFlag(JFactory::NOT_OBJECT_OWNER); sut.Insert(new JFactoryTestDummyObject(42, &deleted_flag)); sut.ClearData(); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); REQUIRE(std::distance(results.first, results.second) == 1); REQUIRE(deleted_flag == false); } @@ -137,7 +137,7 @@ TEST_CASE("JFactoryTests") { sut.SetFactoryFlag(JFactory::NOT_OBJECT_OWNER); sut.Insert(new JFactoryTestDummyObject(42, &deleted_flag)); sut.ClearData(); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); REQUIRE(std::distance(results.first, results.second) == 1); REQUIRE(deleted_flag == false); } @@ -160,7 +160,7 @@ TEST_CASE("JFactoryTests") { Issue135Factory sut; auto event = std::make_shared(); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); REQUIRE(sut.GetNumObjects() == 3); REQUIRE(std::distance(results.first, results.second) == 3); @@ -192,7 +192,7 @@ TEST_CASE("JFactoryTests") { REQUIRE(sut.GetStatus() == JFactory::Status::Inserted); - auto results = sut.GetOrCreate(event); + auto results = sut.CreateAndGetData(event); auto it = results.first; REQUIRE((*it)->data == 49); REQUIRE(sut.GetNumObjects() == 1);