File indexing completed on 2025-10-29 08:46:53
0001 
0002 
0003 
0004 
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     
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     
0056     
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     
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     
0114     
0115 
0116     struct ParameterBase {
0117         std::string m_name; 
0118         std::string m_description;
0119         bool m_is_shared = false;
0120 
0121         void SetName(std::string name) { m_name = name; }
0122         void SetDescription(std::string description) { m_description = description; }
0123         void SetShared(bool is_shared) { m_is_shared = is_shared; }
0124 
0125         const std::string& GetName() { return m_name; }
0126         const std::string& GetDescription() { return m_description; }
0127         bool IsShared() { return m_is_shared; }
0128 
0129         virtual void Init(JParameterManager& parman, const std::string& prefix) = 0;
0130         virtual void Wire(const std::map<std::string, std::string>& isolated, const std::map<std::string, std::string>& shared) = 0;
0131     };
0132 
0133     template <typename T> 
0134     class ParameterRef;
0135 
0136     template <typename T> 
0137     class Parameter;
0138 
0139     struct ServiceBase {
0140         virtual void Fetch(JApplication* app) = 0;
0141     };
0142 
0143     template <typename T> 
0144     class Service;
0145 
0146     void RegisterParameter(ParameterBase* parameter) {
0147         m_parameters.push_back(parameter);
0148     }
0149 
0150     void RegisterService(ServiceBase* service) {
0151         m_services.push_back(service);
0152     }
0153 
0154     const std::vector<ParameterBase*> GetAllParameters() const {
0155         return this->m_parameters;
0156     }
0157 
0158 };
0159 
0160 
0161 } 
0162 
0163