From ac6d156564cd7eda26bacc1bf3260915c512d977 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Fri, 8 Dec 2023 21:09:03 -0500 Subject: [PATCH] fix: templated algorithm accessor in `AlgorithmSequence` (#50) --- src/iguana/AlgorithmSequence.cc | 9 +-------- src/iguana/AlgorithmSequence.h | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/iguana/AlgorithmSequence.cc b/src/iguana/AlgorithmSequence.cc index 52046a00..f0a8a82c 100644 --- a/src/iguana/AlgorithmSequence.cc +++ b/src/iguana/AlgorithmSequence.cc @@ -13,15 +13,8 @@ namespace iguana { } } - algo_t& AlgorithmSequence::Get(const std::string name) { - if(auto it{m_algo_names.find(name)}; it != m_algo_names.end()) - return m_sequence[it->second]; - m_log->Error("cannot find algorithm '{}' in sequence", name); - throw std::runtime_error("cannot Get algorithm"); - } - void AlgorithmSequence::SetOption(const std::string algo, const std::string key, const option_t val) { - Get(algo)->SetOption(key,val); + Get(algo)->SetOption(key,val); } void AlgorithmSequence::SetName(const std::string name) { diff --git a/src/iguana/AlgorithmSequence.h b/src/iguana/AlgorithmSequence.h index b7137528..74aaab8a 100644 --- a/src/iguana/AlgorithmSequence.h +++ b/src/iguana/AlgorithmSequence.h @@ -38,11 +38,21 @@ namespace iguana { /// @param algo the algorithm void Add(algo_t&& algo); - /// Get an algorithm by name + /// + /// **Example** + /// @code + /// Get("my_algorithm_name"); + /// @endcode /// @param name the name of the algorithm /// @return a reference to the algorithm - algo_t& Get(const std::string name); + template + ALGORITHM* Get(const std::string name) { + if(auto it{m_algo_names.find(name)}; it != m_algo_names.end()) + return dynamic_cast(m_sequence[it->second].get()); + m_log->Error("cannot find algorithm '{}' in sequence", name); + throw std::runtime_error("cannot Get algorithm"); + } /// Set an algorithm option /// @see `Algorithm::SetOption`