LogicalTriggers.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <memory>
7 
8 #include "Evolution/EventsAndTriggers/Trigger.hpp"
9 #include "Options/Options.hpp"
11 #include "Utilities/TMPL.hpp"
12 
13 namespace Triggers {
14 /// \ingroup EventsAndTriggersGroup
15 /// Always triggers. This trigger is automatically registered.
16 template <typename TriggerRegistrars = tmpl::list<>>
17 class Always : public Trigger<TriggerRegistrars> {
18  public:
19  /// \cond
20  explicit Always(CkMigrateMessage* /*unused*/) noexcept {}
21  using PUP::able::register_constructor;
23  /// \endcond
24 
25  using options = tmpl::list<>;
26  static constexpr OptionString help = {"Always trigger."};
27 
28  Always() = default;
29 
30  using argument_tags = tmpl::list<>;
31 
32  bool operator()() const noexcept { return true; }
33 };
34 
35 /// \ingroup EventsAndTriggersGroup
36 /// Negates another trigger. This trigger is automatically registered.
37 template <typename TriggerRegistrars>
38 class Not : public Trigger<TriggerRegistrars> {
39  public:
40  /// \cond
41  Not() = default;
42  explicit Not(CkMigrateMessage* /*unused*/) noexcept {}
43  using PUP::able::register_constructor;
45  /// \endcond
46 
47  static constexpr OptionString help = {"Negates another trigger."};
48 
49  explicit Not(
50  std::unique_ptr<Trigger<TriggerRegistrars>> negated_trigger) noexcept
51  : negated_trigger_(std::move(negated_trigger)) {}
52 
53  using argument_tags = tmpl::list<Tags::DataBox>;
54 
55  template <typename DbTags>
56  bool operator()(const db::DataBox<DbTags>& box) noexcept {
57  return not negated_trigger_->is_triggered(box);
58  }
59 
60  // clang-tidy: google-runtime-references
61  void pup(PUP::er& p) noexcept { // NOLINT
62  p | negated_trigger_;
63  }
64 
65  private:
67 };
68 
69 /// \ingroup EventsAndTriggersGroup
70 /// Short-circuiting logical AND of other triggers. This trigger is
71 /// automatically registered.
72 template <typename TriggerRegistrars>
73 class And : public Trigger<TriggerRegistrars> {
74  public:
75  /// \cond
76  And() = default;
77  explicit And(CkMigrateMessage* /*unused*/) noexcept {}
78  using PUP::able::register_constructor;
80  /// \endcond
81 
82  static constexpr OptionString help = {
83  "Short-circuiting logical AND of other triggers."};
84 
86  combined_triggers) noexcept
87  : combined_triggers_(std::move(combined_triggers)) {}
88 
89  using argument_tags = tmpl::list<Tags::DataBox>;
90 
91  template <typename DbTags>
92  bool operator()(const db::DataBox<DbTags>& box) noexcept {
93  for (auto& trigger : combined_triggers_) {
94  if (not trigger->is_triggered(box)) {
95  return false;
96  }
97  }
98  return true;
99  }
100 
101  // clang-tidy: google-runtime-references
102  void pup(PUP::er& p) noexcept { // NOLINT
103  p | combined_triggers_;
104  }
105 
106  private:
108 };
109 
110 /// \ingroup EventsAndTriggersGroup
111 /// Short-circuiting logical OR of other triggers. This trigger is
112 /// automatically registered.
113 template <typename TriggerRegistrars>
114 class Or : public Trigger<TriggerRegistrars> {
115  public:
116  /// \cond
117  Or() = default;
118  explicit Or(CkMigrateMessage* /*unused*/) noexcept {}
119  using PUP::able::register_constructor;
121  /// \endcond
122 
123  static constexpr OptionString help = {
124  "Short-circuiting logical OR of other triggers."};
125 
127  combined_triggers) noexcept
128  : combined_triggers_(std::move(combined_triggers)) {}
129 
130  using argument_tags = tmpl::list<Tags::DataBox>;
131 
132  template <typename DbTags>
133  bool operator()(const db::DataBox<DbTags>& box) noexcept {
134  for (auto& trigger : combined_triggers_) {
135  if (trigger->is_triggered(box)) {
136  return true;
137  }
138  }
139  return false;
140  }
141 
142  // clang-tidy: google-runtime-references
143  void pup(PUP::er& p) noexcept { // NOLINT
144  p | combined_triggers_;
145  }
146 
147  private:
149 };
150 
151 /// \cond
152 template <typename TriggerRegistrars>
153 PUP::able::PUP_ID Always<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
154 template <typename TriggerRegistrars>
155 PUP::able::PUP_ID Not<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
156 template <typename TriggerRegistrars>
157 PUP::able::PUP_ID And<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
158 template <typename TriggerRegistrars>
159 PUP::able::PUP_ID Or<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
160 /// \endcond
161 } // namespace Triggers
162 
163 template <typename TriggerRegistrars>
164 struct create_from_yaml<Triggers::Not<TriggerRegistrars>> {
165  template <typename Metavariables>
166  static Triggers::Not<TriggerRegistrars> create(const Option& options) {
169  }
170 };
171 
172 template <typename TriggerRegistrars>
173 struct create_from_yaml<Triggers::And<TriggerRegistrars>> {
174  template <typename Metavariables>
175  static Triggers::And<TriggerRegistrars> create(const Option& options) {
177  options.parse_as<
179  }
180 };
181 
182 template <typename TriggerRegistrars>
183 struct create_from_yaml<Triggers::Or<TriggerRegistrars>> {
184  template <typename Metavariables>
185  static Triggers::Or<TriggerRegistrars> create(const Option& options) {
187  options.parse_as<
189  }
190 };
The type that options are passed around as. Contains YAML node data and an OptionContext.
Definition: Options.hpp:104
Used by the parser to create an object. The default action is to parse options using T::options...
Definition: Options.hpp:144
Always triggers. This trigger is automatically registered.
Definition: LogicalTriggers.hpp:17
Defines classes and functions for making classes creatable from input files.
Defines macros to allow serialization of abstract template base classes.
constexpr auto create(Args &&... args)
Create a new DataBox.
Definition: DataBox.hpp:1259
#define WRAPPED_PUPable_decl_template(className)
Mark derived classes as serializable.
Definition: CharmPupable.hpp:22
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:27
Negates another trigger. This trigger is automatically registered.
Definition: LogicalTriggers.hpp:38
Definition: InterpolationTargetWedgeSectionTorus.hpp:24
T parse_as() const
Convert to an object of type T.
Definition: ParseOptions.hpp:70
Short-circuiting logical AND of other triggers. This trigger is automatically registered.
Definition: LogicalTriggers.hpp:73
Short-circuiting logical OR of other triggers. This trigger is automatically registered.
Definition: LogicalTriggers.hpp:114
Wraps the template metaprogramming library used (brigand)
Base class for checking whether to run an Event.
Definition: Trigger.hpp:35
Definition: LogicalTriggers.hpp:13