File indexing completed on 2025-01-18 10:17:33
0001
0002
0003
0004
0005
0006 #pragma once
0007 #include <JANA/JApplication.h>
0008 #include <JANA/Components/JComponentFwd.h>
0009 #include <JANA/Utils/JTypeInfo.h>
0010
0011 namespace jana::components {
0012
0013 template <typename T>
0014 class JComponent::ParameterRef : public JComponent::ParameterBase {
0015
0016 T* m_data;
0017
0018 public:
0019 ParameterRef(JComponent* owner, std::string name, T& slot, std::string description="") {
0020 owner->RegisterParameter(this);
0021 this->m_name = name;
0022 this->m_description = description;
0023 m_data = &slot;
0024 }
0025
0026 const T& operator()() { return *m_data; }
0027 const T& operator*() { return *m_data; }
0028
0029 protected:
0030
0031 void Configure(JParameterManager& parman, const std::string& prefix) override {
0032 if (prefix.empty()) {
0033 parman.SetDefaultParameter(this->m_name, *m_data, this->m_description);
0034 }
0035 else {
0036 parman.SetDefaultParameter(prefix + ":" + this->m_name, *m_data, this->m_description);
0037 }
0038 }
0039 void Configure(std::map<std::string, std::string> fields) override {
0040 auto it = fields.find(this->m_name);
0041 if (it != fields.end()) {
0042 const auto& value_str = it->second;
0043 JParameterManager::Parse(value_str, *m_data);
0044 }
0045 }
0046 };
0047
0048 template <typename T>
0049 class JComponent::Parameter : public JComponent::ParameterBase {
0050
0051 T m_data;
0052
0053 public:
0054 Parameter(JComponent* owner, std::string name, T default_value, std::string description) {
0055 owner->RegisterParameter(this);
0056 this->m_name = name;
0057 this->m_description = description;
0058 m_data = default_value;
0059 }
0060
0061 const T& operator()() { return m_data; }
0062 const T& operator*() { return m_data; }
0063
0064 protected:
0065
0066 void Configure(JParameterManager& parman, const std::string& prefix) override {
0067 if (prefix.empty()) {
0068 parman.SetDefaultParameter(this->m_name, m_data, this->m_description);
0069 }
0070 else {
0071 parman.SetDefaultParameter(prefix + ":" + this->m_name, m_data, this->m_description);
0072 }
0073 }
0074 void Configure(std::map<std::string, std::string> fields) override {
0075 auto it = fields.find(this->m_name);
0076 if (it != fields.end()) {
0077 const auto& value_str = it->second;
0078 JParameterManager::Parse(value_str, m_data);
0079 }
0080 }
0081 };
0082
0083
0084 template <typename ServiceT>
0085 class JComponent::Service : public JComponent::ServiceBase {
0086
0087 std::shared_ptr<ServiceT> m_data;
0088
0089 public:
0090
0091 Service(JComponent* owner) {
0092 owner->RegisterService(this);
0093 }
0094
0095 ServiceT& operator()() {
0096 if (m_data == nullptr) {
0097 throw JException("Attempted to access a Service which hasn't been attached to this Component yet!");
0098 }
0099 return *m_data;
0100 }
0101
0102 ServiceT* operator->() {
0103 if (m_data == nullptr) {
0104 throw JException("Attempted to access a Service which hasn't been attached to this Component yet!");
0105 }
0106 return m_data.get();
0107 }
0108
0109 protected:
0110
0111 void Fetch(JApplication* app) {
0112 m_data = app->GetService<ServiceT>();
0113 }
0114 };
0115
0116
0117 template <typename F>
0118 inline void JComponent::CallWithJExceptionWrapper(std::string func_name, F func) {
0119 try {
0120 func();
0121 }
0122 catch (JException& ex) {
0123 if (ex.function_name.empty()) ex.function_name = func_name;
0124 if (ex.type_name.empty()) ex.type_name = m_type_name;
0125 if (ex.instance_name.empty()) ex.instance_name = m_prefix;
0126 if (ex.plugin_name.empty()) ex.plugin_name = m_plugin_name;
0127 throw ex;
0128 }
0129 catch (std::exception& e) {
0130 auto ex = JException(e.what());
0131 ex.exception_type = JTypeInfo::demangle_current_exception_type();
0132 ex.nested_exception = std::current_exception();
0133 ex.function_name = func_name;
0134 ex.type_name = m_type_name;
0135 ex.instance_name = m_prefix;
0136 ex.plugin_name = m_plugin_name;
0137 throw ex;
0138 }
0139 catch (...) {
0140 auto ex = JException("Unknown exception");
0141 ex.exception_type = JTypeInfo::demangle_current_exception_type();
0142 ex.nested_exception = std::current_exception();
0143 ex.function_name = func_name;
0144 ex.type_name = m_type_name;
0145 ex.instance_name = m_prefix;
0146 ex.plugin_name = m_plugin_name;
0147 throw ex;
0148 }
0149 }
0150
0151
0152 }
0153
0154