Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:42

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #ifndef JANA2_JFACTORYTESTS_H
0006 #define JANA2_JFACTORYTESTS_H
0007 
0008 #include <JANA/JObject.h>
0009 #include <JANA/JFactoryT.h>
0010 #include <JANA/JEventSource.h>
0011 
0012 /// DummyObject is a trivial JObject which reports its own destruction.
0013 struct JFactoryTestDummyObject : public JObject {
0014 
0015     int data;
0016     bool* is_destroyed_flag = nullptr;
0017 
0018     JFactoryTestDummyObject(int data, bool* is_destroyed_flag=nullptr) : data(data), is_destroyed_flag(is_destroyed_flag) {}
0019     ~JFactoryTestDummyObject() {
0020         if (is_destroyed_flag != nullptr) {
0021             *is_destroyed_flag = true;
0022         }
0023     }
0024 };
0025 
0026 struct DifferentDummyObject : public JObject {
0027     double E;
0028     DifferentDummyObject(double E) : E(E) {}
0029 };
0030 
0031 
0032 
0033 /// DummyFactory is a trivial JFactory which reports how often its member functions get called
0034 /// and whether any of the JObjects were destroyed
0035 struct JFactoryTestDummyFactory : public JFactoryT<JFactoryTestDummyObject> {
0036 
0037     int init_call_count = 0;
0038     int change_run_call_count = 0;
0039     int process_call_count = 0;
0040     bool destroy_flags[3] = {false, false, false};
0041 
0042     void Init() override {
0043         ++init_call_count;
0044     }
0045 
0046     void ChangeRun(const std::shared_ptr<const JEvent>&) override {
0047         ++change_run_call_count;
0048     }
0049 
0050     void Process(const std::shared_ptr<const JEvent>&) override {
0051         ++process_call_count;
0052         Insert(new JFactoryTestDummyObject (7, &destroy_flags[0]));
0053         Insert(new JFactoryTestDummyObject (22, &destroy_flags[1]));
0054         Insert(new JFactoryTestDummyObject (23, &destroy_flags[2]));
0055     }
0056 };
0057 
0058 struct JFactoryTestExceptingFactory : public JFactoryT<JFactoryTestDummyObject> {
0059 
0060     void Process(const std::shared_ptr<const JEvent>&) override {
0061         throw JException("This was never going to work!");
0062     }
0063 };
0064 
0065 struct JFactoryTestExceptingInInitFactory : public JFactoryT<JFactoryTestDummyObject> {
0066 
0067     void Init() override {
0068         throw JException("This was never going to initialize even");
0069     }
0070     void Process(const std::shared_ptr<const JEvent>&) override {
0071     }
0072 };
0073 
0074 
0075 struct JFactoryTestDummySource: public JEventSource {
0076 
0077     int get_objects_count = 0;
0078     int get_objects_dummy_count = 0;
0079     int get_objects_different_count = 0;
0080 
0081     JFactoryTestDummySource() {
0082         SetCallbackStyle(CallbackStyle::ExpertMode);
0083         EnableGetObjects();
0084     }
0085 
0086     Result Emit(JEvent&) override {
0087         return Result::Success;
0088     };
0089 
0090     bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* aFactory) override {
0091         get_objects_count += 1;
0092         auto dummy_factory = dynamic_cast<JFactoryT<JFactoryTestDummyObject>*>(aFactory);
0093         if (dummy_factory != nullptr) {
0094             get_objects_dummy_count += 1;
0095             dummy_factory->Insert(new JFactoryTestDummyObject(8));
0096             dummy_factory->Insert(new JFactoryTestDummyObject(88));
0097             return true;
0098         }
0099         auto different_factory = dynamic_cast<JFactoryT<DifferentDummyObject>*>(aFactory);
0100         if (different_factory != nullptr) {
0101             get_objects_different_count += 1;
0102             different_factory->Insert(new DifferentDummyObject(123.));
0103             return true;
0104         }
0105         return false;
0106     }
0107 };
0108 
0109 
0110 #endif //JANA2_JFACTORYTESTS_H