Line data Source code
1 : #ifndef CRPROPA_MODULE_H
2 : #define CRPROPA_MODULE_H
3 :
4 : #include "crpropa/Candidate.h"
5 : #include "crpropa/Referenced.h"
6 : #include "crpropa/Common.h"
7 :
8 : #include <string>
9 :
10 : namespace crpropa {
11 :
12 : class Candidate;
13 :
14 : /**
15 : @class Module
16 : @brief Abstract base class for modules
17 :
18 : This class is the abstract base for any module, that means
19 : if your module should be made usable over the ModuleList::add function
20 : it needs to be derived from Module
21 : */
22 : class Module: public Referenced {
23 : std::string description; //< Description of any module, can be overriden by setDescription
24 : public:
25 : /** Constructor
26 : * Automatically sets the description to the name of the derived class
27 : */
28 : Module();
29 : /** Destructor
30 : * Virtual destructor that overrides Referenced::~Referenced, does nothing per default.
31 : */
32 43 : virtual ~Module() override {
33 32 : }
34 : /// Returns description, can be overriden by derived class
35 : virtual std::string getDescription() const;
36 : /** Sets description, can be used instead of getDescription in derived class
37 : * @param description Description string
38 : */
39 : void setDescription(const std::string &description);
40 : /** Process function
41 : * This is the main function to work with, it is called during every ModuleList::run step.
42 : * Derived functions need to override this overload of process!
43 : * @param candidate Candidate raw pointer
44 : */
45 : virtual void process(Candidate *candidate) const = 0;
46 : /** Process function
47 : * Overload for process function to explicitly make a conversion from ref_ptr to a raw pointer
48 : */
49 : inline void process(ref_ptr<Candidate> candidate) const {
50 0 : process(candidate.get());
51 0 : }
52 : };
53 :
54 :
55 : /**
56 : @class AbstractCondition
57 : @brief Abstract Module providing common features for conditional modules.
58 :
59 : This abstract base class is a expansion on the Module class which only proves basic features.
60 : Over this class it is possible to set modules that should be called on rejection of the particle
61 : or on acception.
62 : */
63 : class AbstractCondition: public Module {
64 : protected:
65 : ref_ptr<Module> rejectAction; /**< Module to call when Candidate is rejected */
66 : ref_ptr<Module> acceptAction; /**< Module to call when Candidate is accepted */
67 : bool makeRejectedInactive; /**< If to make Candidate inactive on rejection */
68 : bool makeAcceptedInactive; /**< If to make Candidate inactive on acception */
69 : std::string rejectFlagKey; /**< key for the Candidate property that should be set on rejection */
70 : std::string rejectFlagValue; /**< value for the Candidate property that should be set on rejection */
71 : std::string acceptFlagKey; /**< key for the Candidate property that should be set on acception */
72 : std::string acceptFlagValue; /**< value for the Candidate property that should be set on acception */
73 :
74 : /** Function to reject particle
75 : * This function rejects the particle, what that means is determined by
76 : * the rejectAction and makeRejectedInactive variable.
77 : * @param candidate Candidate raw pointer to reject
78 : */
79 : void reject(Candidate *candidate) const;
80 : /** Overload for reject(Candidate *candidate)
81 : * This function wraps reject(Candidate *candidate) so a reference pointer
82 : * can be conversed explicitly
83 : */
84 : inline void reject(ref_ptr<Candidate> candidate) const {
85 0 : reject(candidate.get());
86 0 : }
87 :
88 : /** Function to accepts particle
89 : * This function accepts the particle, what that means is determined by
90 : * the acceptAction and makeAcceptedInactive variable.
91 : * @param candidate Candidate to reject
92 : */
93 : void accept(Candidate *candidate) const;
94 : /** Overload for accept(Candidate *candidate)
95 : * This function wraps accept(Candidate *candidate) so a reference pointer
96 : * can be conversed explicitly
97 : */
98 : inline void accept(ref_ptr<Candidate> candidate) const {
99 0 : accept(candidate.get());
100 0 : }
101 :
102 : public:
103 : /** Default constructor
104 : * Sets the following parameter:
105 : * @var makeRejectedInactive=true
106 : * @var makeAcceptedInactive=false
107 : * @var rejectFlagKey="Rejected"
108 : * @var rejectFlagValue=typeid(*this).name()
109 : */
110 : AbstractCondition();
111 : /** Sets the module that should be invoked on rejection
112 : * @param rejectAction Module that should be called on rejection
113 : */
114 : void onReject(Module *rejectAction);
115 : /** Sets the module that should be invoked on acception
116 : * @param rejectAction Module that should be called on acception
117 : */
118 : void onAccept(Module *acceptAction);
119 : /** Whether to make Candidate inactive on rejection */
120 : void setMakeRejectedInactive(bool makeInactive);
121 : /** Whether to make Candidate inactive on acception */
122 : void setMakeAcceptedInactive(bool makeInactive);
123 : /** sets Rejection flag
124 : * Sets the key and value that should be set when the Candidate is rejected
125 : * @param key property key
126 : * @param value property value
127 : */
128 : void setRejectFlag(std::string key, std::string value);
129 : /** sets the Acception flag
130 : * Sets the key and value that should be set when the Candidate is accepted
131 : * @param key property key
132 : * @param value property value
133 : */
134 : void setAcceptFlag(std::string key, std::string value);
135 :
136 : // return the reject flag (key & value), delimiter is the "&".
137 : std::string getRejectFlag();
138 :
139 : // return the accept flag (key & value), delimiter is the "&"
140 : std::string getAcceptFlag();
141 : };
142 :
143 : /**
144 : @class Deactivation
145 : @brief Direct deactivation of the candidate. Can be used for debuging.
146 : */
147 0 : class Deactivation: public AbstractCondition {
148 : public:
149 0 : void process(Candidate *cand) const { reject(cand); }
150 : };
151 :
152 :
153 : } // namespace crpropa
154 :
155 : #endif /* CRPROPA_MODULE_H */
|