MARLEY (Model of Argon Reaction Low Energy Yields)  v1.2.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Parity.hh
1 //
5 // This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6 //
7 // MARLEY is free software: you can redistribute it and/or modify it under the
8 // terms of version 3 of the GNU General Public License as published by the
9 // Free Software Foundation.
10 //
11 // For the full text of the license please see COPYING or
12 // visit http://opensource.org/licenses/GPL-3.0
13 //
14 // Please respect the MCnet academic usage guidelines. See GUIDELINES
15 // or visit https://www.montecarlonet.org/GUIDELINES for details.
16 
17 #pragma once
18 
19 // Standard library includes
20 #include <ostream>
21 
22 namespace marley {
23 
25  class Parity {
26 
27  public:
28 
30  constexpr Parity() : is_positive_(true) {}
31 
35  constexpr explicit Parity(bool is_positive)
36  : is_positive_(is_positive) {}
37 
41  explicit Parity(int i);
42 
44  constexpr explicit operator bool() const { return is_positive_; }
45 
47  constexpr marley::Parity operator-() const {
49  }
50 
52  inline Parity& operator!() {
54  return *this;
55  }
56 
60  marley::Parity& operator=(const int& i);
61 
62  inline bool operator==(const marley::Parity& p) const
63  {
64  return is_positive_ == p.is_positive_;
65  }
66 
67  inline bool operator!=(const marley::Parity& p) const
68  {
69  return is_positive_ != p.is_positive_;
70  }
71 
72  inline marley::Parity operator*(const marley::Parity& p) const
73  {
75  }
76 
77  inline int operator*(const int& i) const
78  {
79  if (is_positive_) return i;
80  else return -i;
81  }
82 
83  // Allows explicit casts of marley::Parity to int
86  inline explicit operator int() const {
87  if (is_positive_) return 1;
88  else return -1;
89  }
90 
92  inline char to_char() const {
93  if (is_positive_) return '+';
94  else return '-';
95  }
96 
98  void from_char(const char c);
99 
100  protected:
101 
105  };
106 
107 }
108 
109 inline int operator*(const int& i, const marley::Parity& p)
110 {
111  if (static_cast<bool>(p)) return i;
112  else return -i;
113 }
114 
115 inline std::ostream& operator<<(std::ostream& out, const marley::Parity& p)
116 {
117  out << p.to_char();
118  return out;
119 }
120 
121 std::istream& operator>> (std::istream& in, marley::Parity& p);
Type-safe representation of a parity value (either +1 or -1)
Definition: Parity.hh:25
bool is_positive_
Boolean representation of the parity value that is true when the parity is +1 and false when it is -1...
Definition: Parity.hh:104
constexpr Parity(bool is_positive)
Create a Parity object from a boolean value.
Definition: Parity.hh:35
constexpr marley::Parity operator-() const
Creates a copy of the Parity object with a flipped value.
Definition: Parity.hh:47
char to_char() const
Convert the Parity object to a char.
Definition: Parity.hh:92
void from_char(const char c)
Assign a value to this Parity object from a char.
Definition: Parity.cc:45
constexpr Parity()
Default constructor chooses positive parity.
Definition: Parity.hh:30
Parity & operator!()
Unary operator that flips a Parity value in place.
Definition: Parity.hh:52
marley::Parity & operator=(const int &i)
Assigns a parity value using an integer.
Definition: Parity.cc:34