From c87a2d9afcfe233fb85738e591b345b80d6b4796 Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Thu, 16 Jan 2025 16:20:21 +0100 Subject: [PATCH 1/2] feat: Allow multiple aliases for one whiteboard entry in Examples --- .../ActsExamples/Framework/Sequencer.hpp | 2 +- .../ActsExamples/Framework/WhiteBoard.hpp | 18 +++++++----- .../Framework/src/Framework/Sequencer.cpp | 29 ++++++++++--------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/Examples/Framework/include/ActsExamples/Framework/Sequencer.hpp b/Examples/Framework/include/ActsExamples/Framework/Sequencer.hpp index a8f2ad27950..e7ec0285c0e 100644 --- a/Examples/Framework/include/ActsExamples/Framework/Sequencer.hpp +++ b/Examples/Framework/include/ActsExamples/Framework/Sequencer.hpp @@ -177,7 +177,7 @@ class Sequencer { std::vector m_sequenceElements; std::unique_ptr m_logger; - std::unordered_map m_whiteboardObjectAliases; + std::unordered_multimap m_whiteboardObjectAliases; std::unordered_map m_whiteBoardState; diff --git a/Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp b/Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp index 777d27fe898..8eaffb77ed3 100644 --- a/Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp +++ b/Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -34,9 +33,10 @@ namespace ActsExamples { /// Its lifetime is bound to the lifetime of the white board. class WhiteBoard { public: - WhiteBoard(std::unique_ptr logger = - Acts::getDefaultLogger("WhiteBoard", Acts::Logging::INFO), - std::unordered_map objectAliases = {}); + WhiteBoard( + std::unique_ptr logger = + Acts::getDefaultLogger("WhiteBoard", Acts::Logging::INFO), + std::unordered_multimap objectAliases = {}); // A WhiteBoard holds unique elements and can not be copied WhiteBoard(const WhiteBoard& other) = delete; @@ -82,7 +82,7 @@ class WhiteBoard { std::unique_ptr m_logger; std::unordered_map> m_store; - std::unordered_map m_objectAliases; + std::unordered_multimap m_objectAliases; const Acts::Logger& logger() const { return *m_logger; } @@ -100,7 +100,7 @@ class WhiteBoard { inline ActsExamples::WhiteBoard::WhiteBoard( std::unique_ptr logger, - std::unordered_map objectAliases) + std::unordered_multimap objectAliases) : m_logger(std::move(logger)), m_objectAliases(std::move(objectAliases)) {} template @@ -111,10 +111,14 @@ inline void ActsExamples::WhiteBoard::add(const std::string& name, T&& object) { if (m_store.contains(name)) { throw std::invalid_argument("Object '" + name + "' already exists"); } + auto holder = std::make_shared>(std::forward(object)); m_store.emplace(name, holder); ACTS_VERBOSE("Added object '" << name << "' of type " << typeid(T).name()); - if (auto it = m_objectAliases.find(name); it != m_objectAliases.end()) { + + // deal with aliases + auto range = m_objectAliases.equal_range(name); + for (auto it = range.first; it != range.second; ++it) { m_store[it->second] = holder; ACTS_VERBOSE("Added alias object '" << it->second << "'"); } diff --git a/Examples/Framework/src/Framework/Sequencer.cpp b/Examples/Framework/src/Framework/Sequencer.cpp index eb9bf8fd1c0..a16ef785c48 100644 --- a/Examples/Framework/src/Framework/Sequencer.cpp +++ b/Examples/Framework/src/Framework/Sequencer.cpp @@ -9,7 +9,6 @@ #include "ActsExamples/Framework/Sequencer.hpp" #include "Acts/Plugins/FpeMonitoring/FpeMonitor.hpp" -#include "Acts/Utilities/Helpers.hpp" #include "Acts/Utilities/Logger.hpp" #include "ActsExamples/Framework/AlgorithmContext.hpp" #include "ActsExamples/Framework/DataHandle.hpp" @@ -26,9 +25,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -256,20 +253,26 @@ void Sequencer::addElement(const std::shared_ptr& element) { void Sequencer::addWhiteboardAlias(const std::string& aliasName, const std::string& objectName) { - auto [it, success] = - m_whiteboardObjectAliases.insert({objectName, aliasName}); - if (!success) { - ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName - << "' already set"); - return; + const auto range = m_whiteboardObjectAliases.equal_range(objectName); + for (auto it = range.first; it != range.second; ++it) { + const auto& [key, value] = *it; + if (value == aliasName) { + ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName + << "' already set"); + return; + } } - ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName << "'"); + m_whiteboardObjectAliases.insert({objectName, aliasName}); - if (auto oit = m_whiteBoardState.find(objectName); - oit != m_whiteBoardState.end()) { - m_whiteBoardState[aliasName] = oit->second; + auto oit = m_whiteBoardState.find(objectName); + if (oit == m_whiteBoardState.end()) { + ACTS_ERROR("Key '" << objectName << "' does not exist"); + return; } + + ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName << "'"); + m_whiteBoardState[aliasName] = oit->second; } std::vector Sequencer::listAlgorithmNames() const { From e39450290b7c7cde2332f9c226303d8707ea099c Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Fri, 17 Jan 2025 15:21:55 +0100 Subject: [PATCH 2/2] fix segfault --- Examples/Framework/src/Framework/WhiteBoard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Framework/src/Framework/WhiteBoard.cpp b/Examples/Framework/src/Framework/WhiteBoard.cpp index 1d5fa631380..2d620c1ce43 100644 --- a/Examples/Framework/src/Framework/WhiteBoard.cpp +++ b/Examples/Framework/src/Framework/WhiteBoard.cpp @@ -107,7 +107,7 @@ void ActsExamples::WhiteBoard::addHolder(const std::string &name, throw std::invalid_argument("Object '" + name + "' is nullptr"); } - auto [storeIt, success] = m_store.insert({name, std::move(holder)}); + auto [storeIt, success] = m_store.insert({name, holder}); if (!success) { throw std::invalid_argument("Object '" + name + "' already exists");