From 262373b261d2811ecf9bc1e1ce80df4d2b370312 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 17 Jan 2024 15:22:01 -0500 Subject: [PATCH] fix: combine `Algorithm.h` and `AlgorithmFactory.h` (#83) --- src/iguana/algorithms/Algorithm.h | 31 ++++++++++++++++ src/iguana/algorithms/AlgorithmFactory.cc | 2 +- src/iguana/algorithms/AlgorithmFactory.h | 35 ------------------- src/iguana/algorithms/AlgorithmSequence.h | 2 +- .../algorithms/clas12/EventBuilderFilter.h | 2 +- .../algorithms/clas12/LorentzTransformer.h | 2 +- .../algorithms/example/ExampleAlgorithm.h | 4 +-- src/iguana/algorithms/meson.build | 1 - 8 files changed, 37 insertions(+), 42 deletions(-) delete mode 100644 src/iguana/algorithms/AlgorithmFactory.h diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 97f3f0ac..e42aa28a 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -161,4 +161,35 @@ namespace iguana { bool m_rows_only; }; + + ////////////////////////////////////////////////////////////////////////////// + + /// Algorithm pointer type + using algo_t = std::unique_ptr; + + /// @brief Factory to create an algorithm. + class AlgorithmFactory { + + public: + + /// Algorithm creator function type + using algo_creator_t = std::function; + + AlgorithmFactory() = delete; + + /// Register an algorithm with a unique name. Algorithms register themselves by calling this function. + /// @param name the name of the algorithm (not equivalent to `Object::m_name`) + /// @param creator the creator function + static bool Register(const std::string& name, algo_creator_t creator) noexcept; + + /// Create an algorithm. + /// @param name the name of the algorithm, which was used as an argument in the `AlgorithmFactory::Register` call + static algo_t Create(const std::string& name) noexcept; + + private: + + /// Association between the algorithm names and their creators + static std::unordered_map s_creators; + + }; } diff --git a/src/iguana/algorithms/AlgorithmFactory.cc b/src/iguana/algorithms/AlgorithmFactory.cc index 145bdb97..31811169 100644 --- a/src/iguana/algorithms/AlgorithmFactory.cc +++ b/src/iguana/algorithms/AlgorithmFactory.cc @@ -1,4 +1,4 @@ -#include "AlgorithmFactory.h" +#include "Algorithm.h" namespace iguana { diff --git a/src/iguana/algorithms/AlgorithmFactory.h b/src/iguana/algorithms/AlgorithmFactory.h deleted file mode 100644 index 69b0ac12..00000000 --- a/src/iguana/algorithms/AlgorithmFactory.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "Algorithm.h" - -namespace iguana { - - /// Algorithm pointer type - using algo_t = std::unique_ptr; - - /// @brief Factory to create an algorithm. - class AlgorithmFactory { - - public: - - /// Algorithm creator function type - using algo_creator_t = std::function; - - AlgorithmFactory() = delete; - - /// Register an algorithm with a unique name. Algorithms register themselves by calling this function. - /// @param name the name of the algorithm (not equivalent to `Object::m_name`) - /// @param creator the creator function - static bool Register(const std::string& name, algo_creator_t creator) noexcept; - - /// Create an algorithm. - /// @param name the name of the algorithm, which was used as an argument in the `AlgorithmFactory::Register` call - static algo_t Create(const std::string& name) noexcept; - - private: - - /// Association between the algorithm names and their creators - static std::unordered_map s_creators; - - }; -} diff --git a/src/iguana/algorithms/AlgorithmSequence.h b/src/iguana/algorithms/AlgorithmSequence.h index d48bb7b3..44e06cd0 100644 --- a/src/iguana/algorithms/AlgorithmSequence.h +++ b/src/iguana/algorithms/AlgorithmSequence.h @@ -1,6 +1,6 @@ #pragma once -#include "AlgorithmFactory.h" +#include "Algorithm.h" namespace iguana { diff --git a/src/iguana/algorithms/clas12/EventBuilderFilter.h b/src/iguana/algorithms/clas12/EventBuilderFilter.h index 11b8f995..0869a5b8 100644 --- a/src/iguana/algorithms/clas12/EventBuilderFilter.h +++ b/src/iguana/algorithms/clas12/EventBuilderFilter.h @@ -1,6 +1,6 @@ #pragma once -#include "iguana/algorithms/AlgorithmFactory.h" +#include "iguana/algorithms/Algorithm.h" namespace iguana::clas12 { diff --git a/src/iguana/algorithms/clas12/LorentzTransformer.h b/src/iguana/algorithms/clas12/LorentzTransformer.h index 599c82e8..21e8e02b 100644 --- a/src/iguana/algorithms/clas12/LorentzTransformer.h +++ b/src/iguana/algorithms/clas12/LorentzTransformer.h @@ -1,6 +1,6 @@ #pragma once -#include "iguana/algorithms/AlgorithmFactory.h" +#include "iguana/algorithms/Algorithm.h" #include namespace iguana::clas12 { diff --git a/src/iguana/algorithms/example/ExampleAlgorithm.h b/src/iguana/algorithms/example/ExampleAlgorithm.h index 279b75b8..f374fea8 100644 --- a/src/iguana/algorithms/example/ExampleAlgorithm.h +++ b/src/iguana/algorithms/example/ExampleAlgorithm.h @@ -4,9 +4,9 @@ #pragma once //############################################################################ -//# include `AlgorithmFactory`, so that we may register this algorithm for factory creation +//# include `Algorithm.h`, which defines the base class `Algorithm` //############################################################################ -#include "iguana/algorithms/AlgorithmFactory.h" +#include "iguana/algorithms/Algorithm.h" //############################################################################ //# define the namespace diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index becff3f4..aa361ddc 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -10,7 +10,6 @@ algo_sources = [ algo_public_headers = [ 'Algorithm.h', 'AlgorithmBoilerplate.h', - 'AlgorithmFactory.h', 'AlgorithmSequence.h', 'example/ExampleAlgorithm.h', 'clas12/EventBuilderFilter.h',