Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:39

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
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() const;
0030         void Clear();
0031         void Finish();
0032 
0033         JFactory* GetFactory(const std::string& object_name, const std::string& tag="") const;
0034         template<typename T> JFactoryT<T>* GetFactory(const std::string& tag = "") const;
0035         std::vector<JFactory*> GetAllFactories() const;
0036         std::vector<JMultifactory*> GetAllMultifactories() const;
0037         template<typename T> std::vector<JFactoryT<T>*> GetAllFactories() const;
0038 
0039         JEventLevel GetLevel() const { return mLevel; }
0040         void SetLevel(JEventLevel level) { mLevel = level; }
0041 
0042     protected:
0043         std::map<std::pair<std::type_index, std::string>, JFactory*> mFactories;        // {(typeid, tag) : factory}
0044         std::map<std::pair<std::string, std::string>, JFactory*> mFactoriesFromString;  // {(objname, tag) : factory}
0045         std::vector<JMultifactory*> mMultifactories;
0046         bool mIsFactoryOwner = true;
0047         JEventLevel mLevel = JEventLevel::PhysicsEvent;
0048         
0049 };
0050 
0051 
0052 template<typename T>
0053 JFactoryT<T>* JFactorySet::GetFactory(const std::string& tag) const {
0054 
0055     auto typed_key = std::make_pair(std::type_index(typeid(T)), tag);
0056     auto typed_iter = mFactories.find(typed_key);
0057     if (typed_iter != std::end(mFactories)) {
0058         JEventLevel found_level = typed_iter->second->GetLevel();
0059         if (found_level != mLevel) {
0060             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());
0061         }
0062         return static_cast<JFactoryT<T>*>(typed_iter->second);
0063     }
0064 
0065     auto untyped_key = std::make_pair(JTypeInfo::demangle<T>(), tag);
0066     auto untyped_iter = mFactoriesFromString.find(untyped_key);
0067     if (untyped_iter != std::end(mFactoriesFromString)) {
0068         JEventLevel found_level = untyped_iter->second->GetLevel();
0069         if (found_level != mLevel) {
0070             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());
0071         }
0072         return static_cast<JFactoryT<T>*>(untyped_iter->second);
0073     }
0074     return nullptr;
0075 }
0076 
0077 template<typename T>
0078 std::vector<JFactoryT<T>*> JFactorySet::GetAllFactories() const {
0079     auto sKey = std::type_index(typeid(T));
0080     std::vector<JFactoryT<T>*> data;
0081     for (auto it=std::begin(mFactories);it!=std::end(mFactories);it++){
0082         if (it->first.first==sKey){
0083             if (it->second->GetLevel() == mLevel) {
0084                 data.push_back(static_cast<JFactoryT<T>*>(it->second));
0085             }
0086         }
0087     }
0088     return data;
0089 }
0090 
0091 
0092