File indexing completed on 2025-11-21 10:02:19
0001
0002 #pragma once
0003 #include <JANA/Components/JDatabundle.h>
0004 #include <JANA/Components/JComponentSummary.h>
0005 #include <JANA/Utils/JEventLevel.h>
0006
0007 class JFactorySet;
0008
0009 namespace jana::components {
0010
0011 void UpdateFactoryStatusOnEulerianStore(JFactory* fac);
0012
0013 class JHasOutputs {
0014 public:
0015
0016 class OutputBase {
0017 private:
0018 JDatabundle* m_databundle;
0019 JEventLevel m_level = JEventLevel::None;
0020
0021 public:
0022 virtual ~OutputBase() {
0023 delete m_databundle;
0024 }
0025 JDatabundle* GetDatabundle() { return m_databundle; }
0026 JEventLevel GetLevel() const { return m_level; }
0027
0028 void SetDatabundle(JDatabundle* databundle) { m_databundle = databundle; }
0029 void SetLevel(JEventLevel level) { m_level = level; }
0030 void SetShortName(std::string short_name) { m_databundle->SetShortName(short_name); }
0031 void SetUniqueName(std::string unique_name) { m_databundle->SetUniqueName(unique_name); }
0032
0033 virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0034 virtual void EulerianStore(JFactorySet&) {}
0035
0036 void ClearData() { m_databundle->ClearData(); }
0037 };
0038
0039
0040 class VariadicOutputBase {
0041 private:
0042 std::vector<JDatabundle*> m_databundles;
0043 JEventLevel m_level = JEventLevel::None;
0044
0045 public:
0046 virtual ~VariadicOutputBase() {
0047 for (auto* db : m_databundles) {
0048 delete db;
0049 }
0050 }
0051 std::vector<JDatabundle*>& GetDatabundles() { return m_databundles; }
0052 JEventLevel GetLevel() const { return m_level; }
0053
0054 void SetLevel(JEventLevel level) { m_level = level; }
0055 virtual void SetShortNames(std::vector<std::string>) {}
0056 virtual void SetUniqueNames(std::vector<std::string>) {}
0057
0058 std::vector<std::string> GetUniqueNames() {
0059 std::vector<std::string> results;
0060 for (auto* databundle: m_databundles) {
0061 results.push_back(databundle->GetUniqueName());
0062 }
0063 return results;
0064 };
0065
0066 virtual void LagrangianStore(JFactorySet&, JDatabundle::Status) {}
0067 virtual void EulerianStore(JFactorySet&) {}
0068
0069 void ClearData() {
0070 for (auto* databundle : m_databundles) {
0071 databundle->ClearData();
0072 }
0073 }
0074 };
0075
0076 private:
0077 std::vector<OutputBase*> m_outputs;;
0078 std::vector<VariadicOutputBase*> m_variadic_outputs;
0079 std::vector<std::pair<OutputBase*, VariadicOutputBase*>> m_ordered_outputs;
0080
0081 public:
0082
0083 const std::vector<OutputBase*>& GetOutputs() const {
0084 return m_outputs;
0085 }
0086
0087 const std::vector<VariadicOutputBase*>& GetVariadicOutputs() const {
0088 return m_variadic_outputs;
0089 }
0090
0091 JDatabundle* GetFirstDatabundle() const {
0092 if (m_outputs.size() > 0) {
0093 return m_outputs.at(0)->GetDatabundle();
0094 }
0095 return m_variadic_outputs.at(0)->GetDatabundles().at(0);
0096
0097 }
0098
0099 void RegisterOutput(OutputBase* output) {
0100 m_outputs.push_back(output);
0101 m_ordered_outputs.push_back({output, nullptr});
0102 }
0103
0104 void RegisterOutput(VariadicOutputBase* output) {
0105 m_variadic_outputs.push_back(output);
0106 m_ordered_outputs.push_back({nullptr, output});
0107 }
0108
0109 void SummarizeOutputs(JComponentSummary::Component& summary) const {
0110
0111 for (auto* output : m_outputs) {
0112 auto* databundle = output->GetDatabundle();
0113 summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(),
0114 databundle->GetUniqueName(),
0115 databundle->GetTypeName(),
0116 output->GetLevel()));
0117 }
0118
0119 for (auto* output : m_variadic_outputs) {
0120 for (auto* databundle : output->GetDatabundles()) {
0121 summary.AddOutput(new JComponentSummary::Collection(databundle->GetShortName(),
0122 databundle->GetUniqueName(),
0123 databundle->GetTypeName(),
0124 output->GetLevel()));
0125 }
0126 }
0127 }
0128
0129 void WireOutputs(JEventLevel component_level,
0130 const std::vector<std::string>& single_output_databundle_names,
0131 const std::vector<std::vector<std::string>>& variadic_output_databundle_names,
0132 bool use_short_names) {
0133
0134 if (m_variadic_outputs.size() == 1 && variadic_output_databundle_names.size() == 0) {
0135
0136 int variadic_databundle_count = single_output_databundle_names.size() - m_outputs.size();
0137 int current_databundle_index = 0;
0138
0139 for (auto& pair : m_ordered_outputs) {
0140 auto* single_output = pair.first;
0141 auto* variadic_output = pair.second;
0142
0143 if (variadic_output != nullptr) {
0144
0145 variadic_output->SetLevel(component_level);
0146 std::vector<std::string> variadic_names;
0147 for (int i=0; i<variadic_databundle_count; ++i) {
0148 variadic_names.push_back(single_output_databundle_names.at(current_databundle_index+i));
0149 }
0150 if (use_short_names) {
0151 variadic_output->SetShortNames(variadic_names);
0152 } else {
0153 variadic_output->SetUniqueNames(variadic_names);
0154 }
0155 current_databundle_index += variadic_databundle_count;
0156 }
0157 else {
0158 single_output->SetLevel(component_level);
0159 if (use_short_names) {
0160 single_output->SetShortName(single_output_databundle_names.at(current_databundle_index));
0161 }
0162 else {
0163 single_output->SetUniqueName(single_output_databundle_names.at(current_databundle_index));
0164 }
0165 current_databundle_index += 1;
0166 }
0167 }
0168 }
0169 else {
0170
0171 if (!single_output_databundle_names.empty()) {
0172 if (single_output_databundle_names.size() != m_outputs.size()) {
0173 throw JException("Wrong number of (nonvariadic) output databundle names! Expected %d, found %d", m_outputs.size(), single_output_databundle_names.size());
0174 }
0175
0176 size_t i = 0;
0177 for (auto* output : m_outputs) {
0178 output->SetLevel(component_level);
0179 if (use_short_names) {
0180 output->SetShortName(single_output_databundle_names.at(i));
0181 }
0182 else {
0183 output->SetUniqueName(single_output_databundle_names.at(i));
0184 }
0185 i += 1;
0186 }
0187 }
0188
0189 if (!variadic_output_databundle_names.empty()) {
0190 if (variadic_output_databundle_names.size() != m_variadic_outputs.size()) {
0191 throw JException("Wrong number of lists of variadic output databundle names! Expected %d, found %d", m_variadic_outputs.size(), variadic_output_databundle_names.size());
0192 }
0193 size_t i = 0;
0194 for (auto* variadic_output : m_variadic_outputs) {
0195 variadic_output->SetLevel(component_level);
0196 if (use_short_names) {
0197 variadic_output->SetShortNames(variadic_output_databundle_names.at(i));
0198 }
0199 else {
0200 variadic_output->SetUniqueNames(variadic_output_databundle_names.at(i));
0201 }
0202 i += 1;
0203 }
0204 }
0205 }
0206 }
0207 };
0208
0209 }
0210