File indexing completed on 2025-01-18 10:01:41
0001
0002
0003
0004
0005 #pragma once
0006
0007 #include <string>
0008 #include <typeindex>
0009 #include <map>
0010
0011 #include <JANA/JFactoryT.h>
0012 #include <JANA/Utils/JEventLevel.h>
0013 #include <JANA/Components/JComponentSummary.h>
0014
0015 class JFactoryGenerator;
0016 class JFactory;
0017 class JMultifactory;
0018
0019
0020 class JFactorySet {
0021
0022 public:
0023 JFactorySet();
0024 JFactorySet(const std::vector<JFactoryGenerator*>& aFactoryGenerators);
0025 virtual ~JFactorySet();
0026
0027 bool Add(JFactory* aFactory);
0028 bool Add(JMultifactory* multifactory);
0029 void Print(void) const;
0030 void Release(void);
0031
0032 JFactory* GetFactory(const std::string& object_name, const std::string& tag="") const;
0033 template<typename T> JFactoryT<T>* GetFactory(const std::string& tag = "") const;
0034 std::vector<JFactory*> GetAllFactories() const;
0035 std::vector<JMultifactory*> GetAllMultifactories() const;
0036 template<typename T> std::vector<JFactoryT<T>*> GetAllFactories() const;
0037
0038 JEventLevel GetLevel() const { return mLevel; }
0039 void SetLevel(JEventLevel level) { mLevel = level; }
0040
0041 protected:
0042 std::map<std::pair<std::type_index, std::string>, JFactory*> mFactories;
0043 std::map<std::pair<std::string, std::string>, JFactory*> mFactoriesFromString;
0044 std::vector<JMultifactory*> mMultifactories;
0045 bool mIsFactoryOwner = true;
0046 JEventLevel mLevel = JEventLevel::PhysicsEvent;
0047
0048 };
0049
0050
0051 template<typename T>
0052 JFactoryT<T>* JFactorySet::GetFactory(const std::string& tag) const {
0053
0054 auto typed_key = std::make_pair(std::type_index(typeid(T)), tag);
0055 auto typed_iter = mFactories.find(typed_key);
0056 if (typed_iter != std::end(mFactories)) {
0057 JEventLevel found_level = typed_iter->second->GetLevel();
0058 if (found_level != mLevel) {
0059 throw JException("Factory belongs to a different level on the event hierarchy. Expected: %s, Found: %s", toString(mLevel).c_str(), toString(found_level).c_str());
0060 }
0061 return static_cast<JFactoryT<T>*>(typed_iter->second);
0062 }
0063
0064 auto untyped_key = std::make_pair(JTypeInfo::demangle<T>(), tag);
0065 auto untyped_iter = mFactoriesFromString.find(untyped_key);
0066 if (untyped_iter != std::end(mFactoriesFromString)) {
0067 JEventLevel found_level = untyped_iter->second->GetLevel();
0068 if (found_level != mLevel) {
0069 throw JException("Factory belongs to a different level on the event hierarchy. Expected: %s, Found: %s", toString(mLevel).c_str(), toString(found_level).c_str());
0070 }
0071 return static_cast<JFactoryT<T>*>(untyped_iter->second);
0072 }
0073 return nullptr;
0074 }
0075
0076 template<typename T>
0077 std::vector<JFactoryT<T>*> JFactorySet::GetAllFactories() const {
0078 auto sKey = std::type_index(typeid(T));
0079 std::vector<JFactoryT<T>*> data;
0080 for (auto it=std::begin(mFactories);it!=std::end(mFactories);it++){
0081 if (it->first.first==sKey){
0082 if (it->second->GetLevel() == mLevel) {
0083 data.push_back(static_cast<JFactoryT<T>*>(it->second));
0084 }
0085 }
0086 }
0087 return data;
0088 }
0089
0090
0091