Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:26

0001 
0002 // Copyright 2024, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 // Created by Nathan Brei
0005 
0006 #pragma once
0007 
0008 class JApplication;
0009 class JParameterManager;
0010 
0011 #include <JANA/Utils/JEventLevel.h>
0012 #include <JANA/JLogger.h>
0013 #include <JANA/JException.h>
0014 #include <JANA/Components/JComponentSummary.h>
0015 
0016 #include <vector>
0017 #include <mutex>
0018 
0019 namespace jana::components {
0020 
0021 
0022 struct JComponent {
0023     enum class Status { Uninitialized, Initialized, Opened, Closed, Finalized };
0024     enum class CallbackStyle { LegacyMode, ExpertMode, DeclarativeMode };
0025 
0026     struct ParameterBase;
0027     struct ServiceBase;
0028 
0029 protected:
0030     std::vector<ParameterBase*> m_parameters;
0031     std::vector<ServiceBase*> m_services;
0032     
0033     JEventLevel m_level = JEventLevel::PhysicsEvent;
0034     CallbackStyle m_callback_style = CallbackStyle::LegacyMode;
0035     std::string m_prefix;
0036     std::string m_plugin_name;
0037     std::string m_logger_name;
0038     std::string m_type_name;
0039     Status m_status = Status::Uninitialized;
0040     mutable std::mutex m_mutex;
0041     JApplication* m_app = nullptr;
0042     JLogger m_logger;
0043 
0044 public:
0045     // ---------------------
0046     // Meant to be called by users, or alternatively from a Generator
0047     // ---------------------
0048     void SetLevel(JEventLevel level) { m_level = level; }
0049 
0050     void SetCallbackStyle(CallbackStyle style) { m_callback_style = style; }
0051 
0052     void SetPrefix(std::string prefix) {
0053         m_prefix = prefix;
0054     }
0055     /// For convenience, we provide a NAME_OF_THIS macro so that the user doesn't have to store the type name as a string, 
0056     /// because that could get out of sync if automatic refactoring tools are used.
0057     void SetTypeName(std::string type_name) { m_type_name = std::move(type_name); }
0058 
0059     JApplication* GetApplication() const { 
0060         if (m_app == nullptr) {
0061             throw JException("JApplication pointer hasn't been provided yet! Hint: Component configuration should happen inside Init(), not in the constructor.");
0062         }
0063         return m_app; 
0064     }
0065 
0066     JLogger& GetLogger() { return m_logger; }
0067 
0068 
0069     // ---------------------
0070     // Meant to be called by JANA
0071     // ---------------------
0072     std::string GetPrefix() const { return m_prefix.empty() ? m_type_name : m_prefix; }
0073 
0074     JEventLevel GetLevel() const { return m_level; }
0075 
0076     std::string GetLoggerName() const {
0077         if (!m_logger_name.empty()) return m_logger_name;
0078         if (!m_prefix.empty()) return m_prefix;
0079         if (!m_type_name.empty()) return m_type_name;
0080         return "";
0081     }
0082 
0083     std::string GetPluginName() const { return m_plugin_name; }
0084 
0085     void SetLoggerName(std::string logger_name) { m_logger_name = std::move(logger_name); }
0086 
0087     void SetPluginName(std::string plugin_name) { m_plugin_name = std::move(plugin_name); };
0088 
0089     std::string GetTypeName() const { return m_type_name; }
0090 
0091     virtual void Summarize(JComponentSummary&) const {};
0092 
0093     CallbackStyle GetCallbackStyle() const { return m_callback_style; }
0094 
0095     Status GetStatus() const { 
0096         std::lock_guard<std::mutex> lock(m_mutex);
0097         return m_status; 
0098     }
0099 
0100     void SetApplication(JApplication* app) { 
0101         if (app == nullptr) {
0102             throw JException("Attempting to set a null JApplication pointer!");
0103         }
0104         m_app = app; 
0105     }
0106 
0107     void SetLogger(JLogger logger) { m_logger = logger; }
0108 
0109     template <typename F> 
0110     inline void CallWithJExceptionWrapper(std::string func_name, F func);
0111 
0112     // ---------------------
0113     // "Registered member" helpers
0114     // ---------------------
0115 
0116     struct ParameterBase {
0117         std::string m_name;
0118         std::string m_description;
0119         virtual void Configure(JParameterManager& parman, const std::string& prefix) = 0;
0120         virtual void Configure(std::map<std::string, std::string> fields) = 0;
0121     };
0122 
0123     template <typename T> 
0124     class ParameterRef;
0125 
0126     template <typename T> 
0127     class Parameter;
0128 
0129     struct ServiceBase {
0130         virtual void Fetch(JApplication* app) = 0;
0131     };
0132 
0133     template <typename T> 
0134     class Service;
0135 
0136     void RegisterParameter(ParameterBase* parameter) {
0137         m_parameters.push_back(parameter);
0138     }
0139 
0140     void RegisterService(ServiceBase* service) {
0141         m_services.push_back(service);
0142     }
0143 
0144     void ConfigureAllParameters(std::map<std::string, std::string> fields) {
0145         for (auto* parameter : this->m_parameters) {
0146             parameter->Configure(fields);
0147         }
0148     }
0149 };
0150 
0151 
0152 } // namespace jana::components
0153 
0154