Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <memory>
7 : #include <pup_stl.h>
8 : #include <vector>
9 :
10 : #include "Options/Options.hpp"
11 : #include "Options/String.hpp"
12 : #include "ParallelAlgorithms/EventsAndTriggers/Trigger.hpp"
13 : #include "Utilities/Serialization/CharmPupable.hpp"
14 : #include "Utilities/TMPL.hpp"
15 :
16 : namespace Triggers {
17 : /// \ingroup EventsAndTriggersGroup
18 : /// Always triggers.
19 1 : class Always : public Trigger {
20 : public:
21 : /// \cond
22 : explicit Always(CkMigrateMessage* /*unused*/) {}
23 : using PUP::able::register_constructor;
24 : WRAPPED_PUPable_decl_template(Always); // NOLINT
25 : /// \endcond
26 :
27 0 : using options = tmpl::list<>;
28 0 : static constexpr Options::String help = {"Always trigger."};
29 :
30 0 : Always() = default;
31 :
32 0 : using argument_tags = tmpl::list<>;
33 :
34 0 : bool operator()() const { return true; }
35 : };
36 :
37 : /// \ingroup EventsAndTriggersGroup
38 : /// Negates another trigger.
39 1 : class Not : public Trigger {
40 : public:
41 : /// \cond
42 : Not() = default;
43 : explicit Not(CkMigrateMessage* /*unused*/) {}
44 : using PUP::able::register_constructor;
45 : WRAPPED_PUPable_decl_template(Not); // NOLINT
46 : /// \endcond
47 :
48 0 : static constexpr Options::String help = {"Negates another trigger."};
49 :
50 0 : explicit Not(std::unique_ptr<Trigger> negated_trigger)
51 : : negated_trigger_(std::move(negated_trigger)) {}
52 :
53 0 : using argument_tags = tmpl::list<Tags::DataBox>;
54 :
55 : template <typename DbTags>
56 0 : bool operator()(const db::DataBox<DbTags>& box) const {
57 : return not negated_trigger_->is_triggered(box);
58 : }
59 :
60 : // NOLINTNEXTLINE(google-runtime-references)
61 0 : void pup(PUP::er& p) { p | negated_trigger_; }
62 :
63 : private:
64 0 : std::unique_ptr<Trigger> negated_trigger_;
65 : };
66 :
67 : /// \ingroup EventsAndTriggersGroup
68 : /// Short-circuiting logical AND of other triggers.
69 1 : class And : public Trigger {
70 : public:
71 : /// \cond
72 : And() = default;
73 : explicit And(CkMigrateMessage* /*unused*/) {}
74 : using PUP::able::register_constructor;
75 : WRAPPED_PUPable_decl_template(And); // NOLINT
76 : /// \endcond
77 :
78 0 : static constexpr Options::String help = {
79 : "Short-circuiting logical AND of other triggers."};
80 :
81 0 : explicit And(std::vector<std::unique_ptr<Trigger>> combined_triggers)
82 : : combined_triggers_(std::move(combined_triggers)) {}
83 :
84 0 : using argument_tags = tmpl::list<Tags::DataBox>;
85 :
86 : template <typename DbTags>
87 0 : bool operator()(const db::DataBox<DbTags>& box) const {
88 : for (auto& trigger : combined_triggers_) {
89 : if (not trigger->is_triggered(box)) {
90 : return false;
91 : }
92 : }
93 : return true;
94 : }
95 :
96 : // NOLINTNEXTLINE(google-runtime-references)
97 0 : void pup(PUP::er& p) { p | combined_triggers_; }
98 :
99 : private:
100 0 : std::vector<std::unique_ptr<Trigger>> combined_triggers_;
101 : };
102 :
103 : /// \ingroup EventsAndTriggersGroup
104 : /// Short-circuiting logical OR of other triggers.
105 1 : class Or : public Trigger {
106 : public:
107 : /// \cond
108 : Or() = default;
109 : explicit Or(CkMigrateMessage* /*unused*/) {}
110 : using PUP::able::register_constructor;
111 : WRAPPED_PUPable_decl_template(Or); // NOLINT
112 : /// \endcond
113 :
114 0 : static constexpr Options::String help = {
115 : "Short-circuiting logical OR of other triggers."};
116 :
117 0 : explicit Or(std::vector<std::unique_ptr<Trigger>> combined_triggers)
118 : : combined_triggers_(std::move(combined_triggers)) {}
119 :
120 0 : using argument_tags = tmpl::list<Tags::DataBox>;
121 :
122 : template <typename DbTags>
123 0 : bool operator()(const db::DataBox<DbTags>& box) const {
124 : for (auto& trigger : combined_triggers_) {
125 : if (trigger->is_triggered(box)) {
126 : return true;
127 : }
128 : }
129 : return false;
130 : }
131 :
132 : // NOLINTNEXTLINE(google-runtime-references)
133 0 : void pup(PUP::er& p) { p | combined_triggers_; }
134 :
135 : private:
136 0 : std::vector<std::unique_ptr<Trigger>> combined_triggers_;
137 : };
138 :
139 : /// A list of all the logical triggers.
140 1 : using logical_triggers = tmpl::list<Always, And, Not, Or>;
141 : } // namespace Triggers
142 :
143 : template <>
144 0 : struct Options::create_from_yaml<Triggers::Not> {
145 : template <typename Metavariables>
146 0 : static Triggers::Not create(const Options::Option& options) {
147 : return Triggers::Not(
148 : options.parse_as<std::unique_ptr<Trigger>, Metavariables>());
149 : }
150 : };
151 :
152 : template <>
153 0 : struct Options::create_from_yaml<Triggers::And> {
154 : template <typename Metavariables>
155 0 : static Triggers::And create(const Options::Option& options) {
156 : return Triggers::And(
157 : options
158 : .parse_as<std::vector<std::unique_ptr<Trigger>>, Metavariables>());
159 : }
160 : };
161 :
162 : template <>
163 0 : struct Options::create_from_yaml<Triggers::Or> {
164 : template <typename Metavariables>
165 0 : static Triggers::Or create(const Options::Option& options) {
166 : return Triggers::Or(
167 : options
168 : .parse_as<std::vector<std::unique_ptr<Trigger>>, Metavariables>());
169 : }
170 : };
|