Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-26 07:05:46

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>
0012 class JOmniFactoryGeneratorT : public JFactoryGenerator {
0013 public:
0014     using FactoryConfigType = typename FactoryT::ConfigType;
0015 
0016 private:
0017     struct TypedWiring {
0018         std::string m_tag;
0019         std::vector<std::string> m_default_input_tags;
0020         std::vector<std::string> m_default_output_tags;
0021         FactoryConfigType m_default_cfg; /// Must be properly copyable!
0022     };
0023 
0024     struct UntypedWiring {
0025         std::string m_tag;
0026         std::vector<std::string> m_default_input_tags;
0027         std::vector<std::string> m_default_output_tags;
0028         std::map<std::string, std::string> m_config_params;
0029     };
0030 
0031 public:
0032 
0033 
0034     explicit JOmniFactoryGeneratorT(std::string tag,
0035                                     std::vector<std::string> default_input_tags,
0036                                     std::vector<std::string> default_output_tags,
0037                                     FactoryConfigType cfg,
0038                                     JApplication* app) {
0039         m_app = app;
0040         m_wirings.push_back({.m_tag=tag,
0041                              .m_default_input_tags=default_input_tags,
0042                              .m_default_output_tags=default_output_tags,
0043                              .m_default_cfg=cfg
0044                             });
0045     };
0046 
0047     explicit JOmniFactoryGeneratorT(std::string tag,
0048                                     std::vector<std::string> default_input_tags,
0049                                     std::vector<std::string> default_output_tags,
0050                                     JApplication* app) {
0051         m_app = app;
0052         m_wirings.push_back({.m_tag=tag,
0053                                 .m_default_input_tags=default_input_tags,
0054                                 .m_default_output_tags=default_output_tags
0055                                 });
0056 
0057     }
0058 
0059     explicit JOmniFactoryGeneratorT(JApplication* app) : m_app(app) {
0060     }
0061 
0062     void AddWiring(std::string tag,
0063                    std::vector<std::string> default_input_tags,
0064                    std::vector<std::string> default_output_tags,
0065                    FactoryConfigType cfg) {
0066 
0067         m_wirings.push_back({.m_tag=tag,
0068                              .m_default_input_tags=default_input_tags,
0069                              .m_default_output_tags=default_output_tags,
0070                              .m_default_cfg=cfg
0071                             });
0072     }
0073 
0074     void AddWiring(std::string tag,
0075                    std::vector<std::string> default_input_tags,
0076                    std::vector<std::string> default_output_tags,
0077                    std::map<std::string, std::string> config_params) {
0078 
0079         // Create throwaway factory so we can populate its config using our map<string,string>.
0080         FactoryT factory;
0081         factory.ConfigureAllParameters(config_params);
0082         auto config = factory.config();
0083 
0084         m_wirings.push_back({.m_tag=tag,
0085                              .m_default_input_tags=default_input_tags,
0086                              .m_default_output_tags=default_output_tags,
0087                              .m_default_cfg=config
0088                             });
0089 
0090     }
0091 
0092     void GenerateFactories(JFactorySet *factory_set) override {
0093 
0094         for (const auto& wiring : m_wirings) {
0095 
0096 
0097             FactoryT *factory = new FactoryT;
0098             factory->SetApplication(m_app);
0099             factory->SetPluginName(this->GetPluginName());
0100             factory->SetFactoryName(JTypeInfo::demangle<FactoryT>());
0101             // factory->SetTag(wiring.m_tag);
0102             // We do NOT want to do this because JMF will use the tag to suffix the collection names
0103             // TODO: NWB: Change this in JANA
0104             factory->config() = wiring.m_default_cfg;
0105 
0106             // Set up all of the wiring prereqs so that Init() can do its thing
0107             // Specifically, it needs valid input/output tags, a valid logger, and
0108             // valid default values in its Config object
0109             factory->PreInit(wiring.m_tag, wiring.m_default_input_tags, wiring.m_default_output_tags);
0110 
0111             // Factory is ready
0112             factory_set->Add(factory);
0113         }
0114     }
0115 
0116 private:
0117     std::vector<TypedWiring> m_wirings;
0118     JApplication* m_app;
0119 
0120 };