OpenGM  2.3.x
Discrete Graphical Model Library
tribool.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_TRIBOOL_HXX
3 #define OPENGM_TRIBOOL_HXX
4 
5 namespace opengm {
6 
8  class Tribool
9  {
10  public:
11  enum State {True=1, False=0, Maybe=-1};
12 
13  Tribool();
14  Tribool(const Tribool&);
15  template<class T>
16  Tribool(const T);
17  Tribool(Tribool::State state);
18 
19  Tribool& operator=(const Tribool&);
20  template<class T>
21  Tribool& operator=(T);
23 
24  bool operator==(const bool a) const;
25  template<class T>
26  bool operator==(T a) const;
27  bool operator!=(const bool a) const;
28  operator bool() const;
29  bool operator!() const;
30  bool maybe() const;
31 
32  void operator&=(Tribool::State state);
33 
34  private:
35  char value_;
36  friend std::ostream& operator<<(std::ostream& out, const Tribool& t );
37  };
38 
40  : value_(Tribool::Maybe)
41  {}
42 
43  inline Tribool::Tribool
44  (
45  const Tribool& val
46  )
47  : value_(val.value_)
48  {}
49 
50  inline Tribool::Tribool
51  (
52  Tribool::State state
53  )
54  : value_(state)
55  {}
56 
57  template<class T>
58  inline Tribool::Tribool
59  (
60  const T val
61  )
62  : value_(static_cast<char>(val) == Tribool::Maybe
64  : static_cast<char>(static_cast<bool>(val)))
65  {}
66 
67  inline Tribool&
68  Tribool::operator=
69  (
70  const Tribool& rhs
71  )
72  {
73  if(this != &rhs) {
74  value_ = rhs.value_;
75  }
76  return *this;
77  }
78 
79  template<class T>
80  inline Tribool&
81  Tribool::operator=
82  (
83  const T val
84  )
85  {
86  static_cast<char>(val) == Tribool::Maybe
87  ? value_ = Tribool::Maybe
88  : value_ = static_cast<char>(static_cast<bool>(val));
89  return *this;
90  }
91 
92  inline Tribool&
93  Tribool::operator=
94  (
95  Tribool::State val
96  )
97  {
98  value_ = static_cast<char>(val);
99  return *this;
100  }
101 
102  inline bool
103  Tribool::operator==
104  (
105  const bool a
106  ) const
107  {
108  return bool( (value_ == Tribool::True && a == true)
109  || (value_ == Tribool::False && a == false));
110  }
111 
112  template<class T>
113  inline bool
114  Tribool::operator==
115  (
116  T a
117  ) const
118  {
119  return static_cast<char>(a) == value_;
120  }
121 
122  inline bool
123  Tribool::operator!=
124  (
125  const bool a
126  ) const
127  {
128  return (value_ != Tribool::True && a == true)
129  || (value_ != Tribool::True && a == false);
130  }
131 
132  inline Tribool::operator bool() const
133  {
134  return value_ == Tribool::True;
135  }
136 
137  inline bool
139  {
140  return value_ == Tribool::False;
141  }
142 
143  inline bool
145  {
146  return value_ == Tribool::Maybe;
147  }
148 
149  inline std::ostream&
150  operator<<
151  (
152  std::ostream& out,
153  const Tribool& t
154  )
155  {
156  out << static_cast<int>(t.value_);
157  return out;
158  }
159 
160  inline void
162  {
163  if(state==Tribool::True && value_!=Tribool::False) value_=Tribool::True;
164  if(state==Tribool::False) value_=Tribool::False;
165  }
166 
167 } // namespace opengm
168 
169 #endif // #ifndef OPENGM_TRIBOOL_HXX
170 
bool operator!() const
Definition: tribool.hxx:138
bool maybe() const
Definition: tribool.hxx:144
The OpenGM namespace.
Definition: config.hxx:43
Tribool & operator=(const Tribool &)
Definition: tribool.hxx:69
friend std::ostream & operator<<(std::ostream &out, const Tribool &t)
Definition: tribool.hxx:151
bool operator!=(const bool a) const
Definition: tribool.hxx:124
bool operator==(const bool a) const
Definition: tribool.hxx:104
Variable with three values (true=1, false=0, maybe=-1)
Definition: tribool.hxx:8
void operator&=(Tribool::State state)
Definition: tribool.hxx:161