Skip to content

Commit

Permalink
fix: templated algorithm accessor in AlgorithmSequence (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks authored Dec 9, 2023
1 parent 4658e56 commit ac6d156
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 1 addition & 8 deletions src/iguana/AlgorithmSequence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Algorithm>(algo)->SetOption(key,val);
}

void AlgorithmSequence::SetName(const std::string name) {
Expand Down
14 changes: 12 additions & 2 deletions src/iguana/AlgorithmSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ namespace iguana {
/// @param algo the algorithm
void Add(algo_t&& algo);


/// Get an algorithm by name
///
/// **Example**
/// @code
/// Get<iguana::MyAlgorithm>("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 <class ALGORITHM>
ALGORITHM* Get(const std::string name) {
if(auto it{m_algo_names.find(name)}; it != m_algo_names.end())
return dynamic_cast<ALGORITHM*>(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`
Expand Down

0 comments on commit ac6d156

Please sign in to comment.