File indexing completed on 2025-01-18 10:01:27
0001
0002
0003
0004
0005 #pragma once
0006
0007 #include <JANA/JEvent.h>
0008
0009 namespace jana::components {
0010
0011
0012 class JHasRunCallbacks {
0013
0014 protected:
0015 struct ResourceBase {
0016 virtual void ChangeRun(int32_t run_nr, JApplication* app) = 0;
0017 };
0018
0019 std::vector<ResourceBase*> m_resources;
0020 int32_t m_last_run_number = -1;
0021
0022 public:
0023 void RegisterResource(ResourceBase* resource) {
0024 m_resources.push_back(resource);
0025 }
0026
0027 template <typename ServiceT, typename ResourceT, typename LambdaT>
0028 class Resource : public ResourceBase {
0029 ResourceT m_data;
0030 LambdaT m_lambda;
0031
0032 public:
0033
0034 Resource(JHasRunCallbacks* owner, LambdaT lambda) : m_lambda(lambda) {
0035 owner->RegisterResource(this);
0036 };
0037
0038 const ResourceT& operator()() { return m_data; }
0039
0040 protected:
0041
0042 void ChangeRun(int32_t run_nr, JApplication* app) override {
0043 std::shared_ptr<ServiceT> service = app->template GetService<ServiceT>();
0044 m_data = m_lambda(service, run_nr);
0045 }
0046 };
0047
0048
0049 virtual void ChangeRun(int32_t ) {}
0050
0051
0052 virtual void ChangeRun(const JEvent&) {}
0053
0054
0055 virtual void BeginRun(const std::shared_ptr<const JEvent>&) {}
0056
0057 virtual void ChangeRun(const std::shared_ptr<const JEvent>&) {}
0058
0059 virtual void EndRun() {}
0060
0061 };
0062
0063
0064 }