Comparator.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
7 
8 /// \cond
9 namespace Options {
10 class Option;
11 template <typename T>
12 struct create_from_yaml;
13 } // namespace Options
14 namespace PUP {
15 class er;
16 } // namespace PUP
17 /// \endcond
18 
19 namespace Options {
20 /// An option-creatable mathematical comparison.
21 class Comparator {
22  public:
23  enum class Comparison {
24  EqualTo,
25  NotEqualTo,
26  LessThan,
27  GreaterThan,
28  LessThanOrEqualTo,
29  GreaterThanOrEqualTo
30  };
31 
32  Comparator() noexcept = default;
33  Comparator(Comparison comparison) noexcept;
34 
35  template <typename T1, typename T2>
36  bool operator()(const T1& t1, const T2& t2) const noexcept {
37  switch (comparison_) {
38  case Comparison::EqualTo:
39  return t1 == t2;
40  case Comparison::NotEqualTo:
41  return t1 != t2;
42  case Comparison::LessThan:
43  return t1 < t2;
44  case Comparison::GreaterThan:
45  return t1 > t2;
46  case Comparison::LessThanOrEqualTo:
47  return t1 <= t2;
48  case Comparison::GreaterThanOrEqualTo:
49  return t1 >= t2;
50  default:
51  ERROR("Invalid comparison");
52  }
53  }
54 
55  // NOLINTNEXTLINE(google-runtime-references)
56  void pup(PUP::er& p) noexcept;
57 
58  private:
59  Comparison comparison_;
60 };
61 
62 template <>
64  template <typename Metavariables>
65  static Comparator create(const Option& options) {
66  return create_impl(options);
67  }
68 
69  private:
70  static Comparator create_impl(const Option& options);
71 };
72 } // namespace Options
Error.hpp
Options::Option
Definition: Options.hpp:108
ERROR
#define ERROR(m)
prints an error message to the standard error stream and aborts the program.
Definition: Error.hpp:37
Options
Utilities for parsing input files.
Definition: MinmodType.hpp:8
Options::create_from_yaml
Definition: MinmodType.hpp:11
Options::Comparator
An option-creatable mathematical comparison.
Definition: Comparator.hpp:21