Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-30 10:18:17

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/Components/JDatabundle.h>
0012 #include <JANA/JFactoryT.h>
0013 #include <JANA/Utils/JEventLevel.h>
0014 #include <JANA/Components/JComponentSummary.h>
0015 
0016 class JFactoryGenerator;
0017 class JFactory;
0018 class JMultifactory;
0019 
0020 
0021 class JFactorySet {
0022 
0023     public:
0024         JFactorySet();
0025         JFactorySet(const std::vector<JFactoryGenerator*>& aFactoryGenerators);
0026         virtual ~JFactorySet();
0027 
0028         bool Add(JFactory* aFactory);
0029         bool Add(JMultifactory* multifactory);
0030         void Add(JDatabundle* databundle);
0031         void Print() const;
0032         void Clear();
0033         void Finish();
0034 
0035         JFactory* GetFactory(const std::string& object_name, const std::string& tag="") const;
0036         template<typename T> JFactoryT<T>* GetFactory(const std::string& tag = "") const;
0037         std::vector<JFactory*> GetAllFactories() const;
0038         std::vector<JMultifactory*> GetAllMultifactories() const;
0039         template<typename T> std::vector<JFactoryT<T>*> GetAllFactories() const;
0040 
0041         std::vector<std::string> GetAllDatabundleUniqueNames() const;
0042         JDatabundle* GetDatabundle(const std::string& unique_name) const;
0043 
0044         JEventLevel GetLevel() const { return mLevel; }
0045         void SetLevel(JEventLevel level) { mLevel = level; }
0046 
0047     protected:
0048 
0049         std::map<std::string, JDatabundle*> mDatabundlesFromUniqueName;
0050         std::map<std::pair<std::type_index, std::string>, JFactory*> mFactories;        // {(typeid, tag) : factory}
0051         std::map<std::pair<std::string, std::string>, JFactory*> mFactoriesFromString;  // {(objname, tag) : factory}
0052         std::vector<JMultifactory*> mMultifactories;
0053         bool mIsFactoryOwner = true;
0054         JEventLevel mLevel = JEventLevel::PhysicsEvent;
0055 };
0056 
0057 
0058 template<typename T>
0059 JFactoryT<T>* JFactorySet::GetFactory(const std::string& tag) const {
0060 
0061     auto typed_key = std::make_pair(std::type_index(typeid(T)), tag);
0062     auto typed_iter = mFactories.find(typed_key);
0063     if (typed_iter != std::end(mFactories)) {
0064         JEventLevel found_level = typed_iter->second->GetLevel();
0065         if (found_level != mLevel) {
0066             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());
0067         }
0068         return static_cast<JFactoryT<T>*>(typed_iter->second);
0069     }
0070 
0071     auto untyped_key = std::make_pair(JTypeInfo::demangle<T>(), tag);
0072     auto untyped_iter = mFactoriesFromString.find(untyped_key);
0073     if (untyped_iter != std::end(mFactoriesFromString)) {
0074         JEventLevel found_level = untyped_iter->second->GetLevel();
0075         if (found_level != mLevel) {
0076             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());
0077         }
0078         return static_cast<JFactoryT<T>*>(untyped_iter->second);
0079     }
0080     return nullptr;
0081 }
0082 
0083 template<typename T>
0084 std::vector<JFactoryT<T>*> JFactorySet::GetAllFactories() const {
0085     auto sKey = std::type_index(typeid(T));
0086     std::vector<JFactoryT<T>*> data;
0087     for (auto it=std::begin(mFactories);it!=std::end(mFactories);it++){
0088         if (it->first.first==sKey){
0089             if (it->second->GetLevel() == mLevel) {
0090                 data.push_back(static_cast<JFactoryT<T>*>(it->second));
0091             }
0092         }
0093     }
0094     return data;
0095 }
0096 
0097 
0098