-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from DUNE-DAQ/aeoranday/factory-abstraction
TA & TC Factory Method Abstraction
- Loading branch information
Showing
26 changed files
with
168 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* @file: AbstractFactory.hpp | ||
* | ||
* This is part of the DUNE DAQ Application Framework, copyright 2023. | ||
* Licensing/copyright details are in the COPYING file that you should have | ||
* received with this code. | ||
*/ | ||
|
||
#ifndef TRIGGERALGS_ABSTRACT_FACTORY_HPP_ | ||
#define TRIGGERALGS_ABSTRACT_FACTORY_HPP_ | ||
|
||
#include "logging/Logging.hpp" | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace triggeralgs { | ||
|
||
template <typename T> | ||
class AbstractFactory | ||
{ | ||
using maker_creator = std::function<std::unique_ptr<T>()>; | ||
using creation_map = std::unordered_map<std::string, maker_creator>; | ||
|
||
public: | ||
AbstractFactory() {} | ||
AbstractFactory(const AbstractFactory&) = delete; | ||
AbstractFactory& operator=(const AbstractFactory&) = delete; | ||
virtual ~AbstractFactory() {} | ||
|
||
std::unique_ptr<T> build_maker(const std::string& alg_name); | ||
|
||
static void register_creator(const std::string alg_name, maker_creator creator); | ||
|
||
static std::shared_ptr<AbstractFactory<T>> get_instance(); | ||
|
||
protected: | ||
static std::shared_ptr<AbstractFactory<T>> s_single_factory; | ||
|
||
private: | ||
static creation_map& get_makers(); | ||
}; | ||
|
||
} /* namespace triggeralgs */ | ||
|
||
#include "triggeralgs/AbstractFactory.hxx" | ||
|
||
#endif // TRIGGERALGS_ABSTRACT_FACTORY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* @file: AbstractFactory.hxx | ||
* | ||
* This is part of the DUNE DAQ Application Framework, copyright 2023. | ||
* Licensing/copyright details are in the COPYING file that you should have | ||
* received with this code. | ||
*/ | ||
|
||
#ifndef TRIGGERALGS_ABSTRACT_FACTORY_HXX_ | ||
#define TRIGGERALGS_ABSTRACT_FACTORY_HXX_ | ||
|
||
namespace triggeralgs { | ||
|
||
template <typename T> | ||
std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::s_single_factory = nullptr; | ||
|
||
template <typename T> | ||
typename AbstractFactory<T>::creation_map& AbstractFactory<T>::get_makers(){ | ||
static creation_map s_makers; | ||
return s_makers; | ||
} | ||
|
||
template <typename T> | ||
void AbstractFactory<T>::register_creator(const std::string alg_name, maker_creator creator) | ||
{ | ||
creation_map& makers = get_makers(); | ||
auto it = makers.find(alg_name); | ||
|
||
if (it == makers.end()) { | ||
makers[alg_name] = creator; | ||
return; | ||
} | ||
TLOG(0) << "Attempted to overwrite a creator in factory with " << alg_name << "."; | ||
throw; // creators should not be overwritten. | ||
return; | ||
} | ||
|
||
template <typename T> | ||
std::unique_ptr<T> AbstractFactory<T>::build_maker(const std::string& alg_name) | ||
{ | ||
creation_map& makers = get_makers(); | ||
auto it = makers.find(alg_name); | ||
|
||
if (it != makers.end()) { | ||
TLOG(0) << "Factory building " << alg_name << "."; | ||
return it->second(); | ||
} | ||
|
||
TLOG(0) << "Factory couldn't find " << alg_name << "."; | ||
return nullptr; | ||
} | ||
|
||
template <typename T> | ||
std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::get_instance() | ||
{ | ||
if (s_single_factory == nullptr) { | ||
s_single_factory = std::make_shared<AbstractFactory<T>>(); | ||
} | ||
return s_single_factory; | ||
} | ||
|
||
} // namespace triggeralgs | ||
|
||
#endif // TRIGGERALGS_ABSTRACT_FACTORY_HXX_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* @file: TriggerCandidateFactory.hpp | ||
* | ||
* This is part of the DUNE DAQ Application Framework, copyright 2023. | ||
* Licensing/copyright details are in the COPYING file that you should have | ||
* received with this code. | ||
*/ | ||
|
||
#ifndef TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_ | ||
#define TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_ | ||
|
||
#include "triggeralgs/TriggerCandidateMaker.hpp" | ||
#include "triggeralgs/AbstractFactory.hpp" | ||
|
||
#define REGISTER_TRIGGER_CANDIDATE_MAKER(tcm_name, tcm_class) \ | ||
static struct tcm_class##Registrar { \ | ||
tcm_class##Registrar() { \ | ||
TriggerCandidateFactory::register_creator(tcm_name, []() -> std::unique_ptr<TriggerCandidateMaker> {return std::make_unique<tcm_class>();}); \ | ||
} \ | ||
} tcm_class##_registrar; | ||
|
||
namespace triggeralgs { | ||
|
||
class TriggerCandidateFactory : public AbstractFactory<TriggerCandidateMaker> {}; | ||
|
||
} /* namespace triggeralgs */ | ||
|
||
#endif // TRIGGERALGS_TRIGGER_CANDIDATE_FACTORY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.