Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:15:21

0001 // Copyright 2023, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 // Created by Nathan Brei
0004 
0005 #pragma once
0006 
0007 #include <JANA/JFactorySet.h>
0008 #include <JANA/JFactoryGenerator.h>
0009 #include <vector>
0010 
0011 template <class FactoryT> class JOmniFactoryGeneratorT : public JFactoryGenerator {
0012 public:
0013   using FactoryConfigType = typename FactoryT::ConfigType;
0014 
0015 private:
0016   struct TypedWiring {
0017     std::string m_tag;
0018     std::vector<std::string> m_default_input_tags;
0019     std::vector<std::string> m_default_output_tags;
0020     FactoryConfigType m_default_cfg; /// Must be properly copyable!
0021   };
0022 
0023   struct UntypedWiring {
0024     std::string m_tag;
0025     std::vector<std::string> m_default_input_tags;
0026     std::vector<std::string> m_default_output_tags;
0027     std::map<std::string, std::string> m_config_params;
0028   };
0029 
0030 public:
0031   explicit JOmniFactoryGeneratorT(std::string tag, std::vector<std::string> default_input_tags,
0032                                   std::vector<std::string> default_output_tags,
0033                                   FactoryConfigType cfg, JApplication* app) {
0034     m_app = app;
0035     m_wirings.push_back({.m_tag                 = tag,
0036                          .m_default_input_tags  = default_input_tags,
0037                          .m_default_output_tags = default_output_tags,
0038                          .m_default_cfg         = cfg});
0039   };
0040 
0041   explicit JOmniFactoryGeneratorT(std::string tag, std::vector<std::string> default_input_tags,
0042                                   std::vector<std::string> default_output_tags, JApplication* app) {
0043     m_app = app;
0044     m_wirings.push_back({.m_tag                 = tag,
0045                          .m_default_input_tags  = default_input_tags,
0046                          .m_default_output_tags = default_output_tags,
0047                          .m_default_cfg         = {}});
0048   }
0049 
0050   explicit JOmniFactoryGeneratorT(JApplication* app) : m_app(app) {}
0051 
0052   void AddWiring(std::string tag, std::vector<std::string> default_input_tags,
0053                  std::vector<std::string> default_output_tags, FactoryConfigType cfg) {
0054 
0055     m_wirings.push_back({.m_tag                 = tag,
0056                          .m_default_input_tags  = default_input_tags,
0057                          .m_default_output_tags = default_output_tags,
0058                          .m_default_cfg         = cfg});
0059   }
0060 
0061   void AddWiring(std::string tag, std::vector<std::string> default_input_tags,
0062                  std::vector<std::string> default_output_tags,
0063                  std::map<std::string, std::string> config_params) {
0064 
0065     // Create throwaway factory so we can populate its config using our map<string,string>.
0066     FactoryT factory;
0067     factory.ConfigureAllParameters(config_params);
0068     auto config = factory.config();
0069 
0070     m_wirings.push_back({.m_tag                 = tag,
0071                          .m_default_input_tags  = default_input_tags,
0072                          .m_default_output_tags = default_output_tags,
0073                          .m_default_cfg         = config});
0074   }
0075 
0076   void GenerateFactories(JFactorySet* factory_set) override {
0077 
0078     for (const auto& wiring : m_wirings) {
0079 
0080       FactoryT* factory = new FactoryT;
0081       factory->SetApplication(m_app);
0082       factory->SetPluginName(this->GetPluginName());
0083       factory->SetFactoryName(JTypeInfo::demangle<FactoryT>());
0084       factory->config() = wiring.m_default_cfg;
0085 
0086       // Set up all of the wiring prereqs so that Init() can do its thing
0087       // Specifically, it needs valid input/output tags, a valid logger, and
0088       // valid default values in its Config object
0089       factory->PreInit(wiring.m_tag, wiring.m_default_input_tags, wiring.m_default_output_tags);
0090 
0091       // Factory is ready
0092       factory_set->Add(factory);
0093     }
0094   }
0095 
0096 private:
0097   std::vector<TypedWiring> m_wirings;
0098   JApplication* m_app;
0099 };