File indexing completed on 2025-01-18 10:17:41
0001
0002
0003
0004 #include <JANA/JEventSource.h>
0005 #include <JANA/JEventProcessor.h>
0006 #include <JANA/JApplication.h>
0007 #include "JANA/JFactoryGenerator.h"
0008 #include "catch.hpp"
0009
0010 namespace jana::components::getobjects_tests {
0011
0012 struct Obj : public JObject { int data; };
0013
0014 struct Src : public JEventSource {
0015 Src() {
0016 EnableGetObjects();
0017 SetCallbackStyle(CallbackStyle::ExpertMode);
0018 }
0019 Result Emit(JEvent&) override {
0020 return Result::Success;
0021 }
0022 bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* fac) override {
0023
0024 LOG_INFO(GetLogger()) << "GetObjects: Fac has object name '" << fac->GetObjectName() << "' and type name '" << fac->GetTypeName() << "'" << LOG_END;
0025
0026
0027 auto typed_fac = dynamic_cast<JFactoryT<Obj>*>(fac);
0028 if (typed_fac != nullptr) {
0029 auto obj = new Obj;
0030 obj->data = 22;
0031 typed_fac->Insert(obj);
0032 return true;
0033 }
0034 return false;
0035 }
0036 };
0037
0038 struct Fac : public JFactoryT<Obj> {
0039 void Process(const std::shared_ptr<const JEvent>&) override {
0040 auto obj = new Obj;
0041 obj->data = 23;
0042 Insert(obj);
0043 }
0044 };
0045
0046 struct RFac : public JFactoryT<Obj> {
0047 RFac() {
0048 SetFactoryFlag(REGENERATE);
0049 }
0050 void Process(const std::shared_ptr<const JEvent>&) override {
0051 auto obj = new Obj;
0052 obj->data = 23;
0053 Insert(obj);
0054 }
0055 };
0056
0057 struct Proc : public JEventProcessor {
0058 bool from_getobjects=true;
0059 void Process(const std::shared_ptr<const JEvent>& event) override {
0060 auto objs = event->Get<Obj>();
0061 REQUIRE(objs.size() == 1);
0062 if (from_getobjects) {
0063 REQUIRE(objs[0]->data == 22);
0064 }
0065 else {
0066 REQUIRE(objs[0]->data == 23);
0067 }
0068 }
0069 };
0070
0071 TEST_CASE("GetObjectsTests_NoFac") {
0072 JApplication app;
0073 app.SetParameterValue("jana:loglevel", "warn");
0074 app.SetParameterValue("jana:nevents", 1);
0075 app.Add(new Src);
0076 auto proc = new Proc;
0077 proc->from_getobjects = true;
0078 app.Add(proc);
0079 app.Add(new JFactoryGeneratorT<JFactoryT<Obj>>);
0080 app.Run();
0081 }
0082
0083 TEST_CASE("GetObjectsTests_OverrideFac") {
0084 JApplication app;
0085 app.SetParameterValue("jana:loglevel", "warn");
0086 app.SetParameterValue("jana:nevents", 1);
0087 app.Add(new Src);
0088 app.Add(new JFactoryGeneratorT<Fac>);
0089 auto proc = new Proc;
0090 proc->from_getobjects = true;
0091 app.Add(proc);
0092 app.Run();
0093 }
0094
0095 TEST_CASE("GetObjectsTests_Regenerate") {
0096 JApplication app;
0097 app.SetParameterValue("jana:loglevel", "warn");
0098 app.SetParameterValue("jana:nevents", 1);
0099 app.Add(new Src);
0100 app.Add(new JFactoryGeneratorT<RFac>);
0101 auto proc = new Proc;
0102 proc->from_getobjects = false;
0103 app.Add(proc);
0104 app.Run();
0105 }
0106
0107 }