File indexing completed on 2025-12-15 10:35:26
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 CallbackStyle { LegacyMode, ExpertMode };
0024
0025 struct ParameterBase;
0026 struct ServiceBase;
0027
0028 protected:
0029 std::vector<ParameterBase*> m_parameters;
0030 std::vector<ServiceBase*> m_services;
0031
0032 JEventLevel m_level = JEventLevel::PhysicsEvent;
0033 CallbackStyle m_callback_style = CallbackStyle::LegacyMode;
0034 std::string m_prefix;
0035 std::string m_plugin_name;
0036 std::string m_logger_name;
0037 std::string m_type_name;
0038 std::atomic_bool m_is_initialized {false};
0039 std::atomic_bool m_is_finalized {false};
0040 mutable std::mutex m_mutex;
0041 JApplication* m_app = nullptr;
0042 JLogger m_logger;
0043 bool m_is_enabled = true;
0044
0045 public:
0046 JComponent() = default;
0047 virtual ~JComponent() = default;
0048
0049
0050
0051
0052 void SetLevel(JEventLevel level) { m_level = level; }
0053
0054 void SetCallbackStyle(CallbackStyle style) { m_callback_style = style; }
0055
0056 void SetPrefix(std::string prefix) {
0057 m_prefix = prefix;
0058 }
0059
0060
0061 void SetTypeName(std::string type_name) { m_type_name = std::move(type_name); }
0062
0063 JApplication* GetApplication() const {
0064 if (m_app == nullptr) {
0065 throw JException("JApplication pointer hasn't been provided yet! Hint: Component configuration should happen inside Init(), not in the constructor.");
0066 }
0067 return m_app;
0068 }
0069
0070 JLogger& GetLogger() { return m_logger; }
0071
0072
0073
0074
0075
0076
0077 void Wire(JApplication* app);
0078
0079 bool IsEnabled() const { return m_is_enabled; }
0080
0081 void SetEnabled(bool is_enabled){ m_is_enabled = is_enabled; }
0082
0083 std::string GetPrefix() const { return m_prefix.empty() ? m_type_name : m_prefix; }
0084
0085 JEventLevel GetLevel() const { return m_level; }
0086
0087 std::string GetLoggerName() const {
0088 if (!m_logger_name.empty()) return m_logger_name;
0089 if (!m_prefix.empty()) return m_prefix;
0090 if (!m_type_name.empty()) return m_type_name;
0091 return "";
0092 }
0093
0094 std::string GetPluginName() const { return m_plugin_name; }
0095
0096 void SetLoggerName(std::string logger_name) { m_logger_name = std::move(logger_name); }
0097
0098 void SetPluginName(std::string plugin_name) { m_plugin_name = std::move(plugin_name); };
0099
0100 std::string GetTypeName() const { return m_type_name; }
0101
0102 virtual void Summarize(JComponentSummary&) const {};
0103
0104 CallbackStyle GetCallbackStyle() const { return m_callback_style; }
0105
0106 void SetApplication(JApplication* app) {
0107 if (app == nullptr) {
0108 throw JException("Attempting to set a null JApplication pointer!");
0109 }
0110 m_app = app;
0111 }
0112
0113 void SetLogger(JLogger logger) { m_logger = logger; }
0114
0115 template <typename F>
0116 inline void CallWithJExceptionWrapper(std::string func_name, F func);
0117
0118 void DoInit();
0119
0120
0121
0122 virtual void Init() {};
0123
0124
0125
0126
0127
0128 struct ParameterBase {
0129 std::string m_name;
0130 std::string m_description;
0131 bool m_is_shared = false;
0132
0133 void SetName(std::string name) { m_name = name; }
0134 void SetDescription(std::string description) { m_description = description; }
0135 void SetShared(bool is_shared) { m_is_shared = is_shared; }
0136
0137 const std::string& GetName() { return m_name; }
0138 const std::string& GetDescription() { return m_description; }
0139 bool IsShared() { return m_is_shared; }
0140
0141 virtual void Init(JParameterManager& parman, const std::string& prefix) = 0;
0142 virtual void Wire(const std::map<std::string, std::string>& isolated, const std::map<std::string, std::string>& shared) = 0;
0143 };
0144
0145 template <typename T>
0146 class ParameterRef;
0147
0148 template <typename T>
0149 class Parameter;
0150
0151 struct ServiceBase {
0152 virtual void Fetch(JApplication* app) = 0;
0153 };
0154
0155 template <typename T>
0156 class Service;
0157
0158 void RegisterParameter(ParameterBase* parameter) {
0159 m_parameters.push_back(parameter);
0160 }
0161
0162 void RegisterService(ServiceBase* service) {
0163 m_services.push_back(service);
0164 }
0165
0166 const std::vector<ParameterBase*> GetAllParameters() const {
0167 return this->m_parameters;
0168 }
0169
0170 };
0171
0172
0173 }
0174
0175