spot  2.16
trival.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <iostream>
22 
23 namespace spot
24 {
25 
28 
32  class trival
33  {
34  public:
35  // We use repr_t instead of value_t in bitfields to avoid a warning from gcc
36  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51242
37  typedef signed char repr_t;
39  enum value_t : repr_t { no_value = -1, maybe_value = 0, yes_value = 1 };
40  private:
41  value_t val_;
42  public:
43  constexpr trival() noexcept
44  : val_(maybe_value)
45  {
46  }
47 
49  constexpr trival(bool v) noexcept
50  : val_(v ? yes_value : no_value)
51  {
52  }
53 
54 #ifndef SWIG
55  // This is needed internally by Spot to work around the bitfield
56  // issue mentioned earlier, it makes no sense to use it in Python.
59  {
60  return trival(static_cast<value_t>(v));
61  }
62 #endif
63 
65  constexpr explicit trival(value_t v)
66  : val_(v)
67  {
68  }
69 
71  static constexpr trival maybe() noexcept
72  {
73  return trival();
74  }
75 
77  constexpr bool is_known() const
78  {
79  return val_ != maybe_value;
80  }
81 
83  constexpr bool is_maybe() const
84  {
85  return val_ == maybe_value;
86  }
87 
89  constexpr bool is_true() const
90  {
91  return val_ == yes_value;
92  }
93 
95  constexpr bool is_false() const
96  {
97  return val_ == no_value;
98  }
99 
101  constexpr value_t val() const
102  {
103  return val_;
104  }
105 
107 #ifndef SWIG
108  // constexpr explicit only supported in SWIG >= 3.0.4
109  constexpr
110 #endif
111  explicit operator bool() const
112  {
113  return val_ == yes_value;
114  }
115 
117  constexpr trival operator!() const
118  {
119  return trival((val_ == yes_value) ? no_value :
120  (val_ == no_value) ? yes_value :
121  maybe_value);
122  }
123  };
124 
125  // We prefer a global version of the operator so that the left
126  // argument can be promoted (think "bool == trival" being promoted
127  // to "trival == trival"). However Swig's generated Python bindings
128  // cannot deal with operators in the global namespace, so we use an
129  // in-class version (coded in impl.i) in this case. This will fail
130  // on a "bool == trival" comparison in Python, but we usually write
131  // "trival == bool" and that works.
132 #ifndef SWIG
134  constexpr bool operator==(trival a, trival b)
135  {
136  return a.val() == b.val();
137  }
138 
140  constexpr bool operator!=(trival a, trival b)
141  {
142  return !(a == b);
143  }
144 #endif
145 
147  constexpr trival operator&&(trival a, trival b)
148  {
149  return
150  (a.val() == trival::no_value || b.val() == trival::no_value)
151  ? trival(false)
152  : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
153  ? trival::maybe()
154  : trival(true);
155  }
156 
158  constexpr trival operator&&(bool a, trival b)
159  {
160  return trival(a) && b;
161  }
162 
164  constexpr trival operator&&(trival a, bool b)
165  {
166  return a && trival(b);
167  }
168 
170  constexpr trival operator||(trival a, trival b)
171  {
172  return
173  (a.val() == trival::yes_value || b.val() == trival::yes_value)
174  ? trival(true)
175  : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
176  ? trival::maybe()
177  : trival(false);
178  }
179 
181  constexpr trival operator||(bool a, trival b)
182  {
183  return trival(a) || b;
184  }
185 
187  constexpr trival operator||(trival a, bool b)
188  {
189  return a || trival(b);
190  }
191 
193  inline std::ostream& operator<<(std::ostream& os, trival v)
194  {
195  return os << ((v.val() == trival::no_value) ? "no"
196  : (v.val() == trival::maybe_value) ? "maybe"
197  : "yes");
198  }
199 
201 }
A class implementing Kleene's three-valued logic.
Definition: trival.hh:33
value_t
The three possible logical values.
Definition: trival.hh:39
constexpr bool is_known() const
Is true or false, but not maybe.
Definition: trival.hh:77
static constexpr trival maybe() noexcept
Return a trival representing the maybe (unknown) value.
Definition: trival.hh:71
constexpr trival(bool v) noexcept
Construct from a bool: false maps to no, true to yes.
Definition: trival.hh:49
signed char repr_t
Definition: trival.hh:37
static trival from_repr_t(repr_t v)
Construct from a raw repr_t value.
Definition: trival.hh:58
constexpr bool is_maybe() const
Return true iff the value is maybe.
Definition: trival.hh:83
constexpr trival operator!() const
Return the three-valued logical negation.
Definition: trival.hh:117
constexpr bool is_false() const
Return true iff the logical value is no.
Definition: trival.hh:95
constexpr trival(value_t v)
Construct from a value_t enumerator.
Definition: trival.hh:65
constexpr value_t val() const
Return the raw value_t enumerator.
Definition: trival.hh:101
constexpr bool is_true() const
Return true iff the logical value is yes.
Definition: trival.hh:89
Definition: automata.hh:26
constexpr trival operator||(trival a, trival b)
Three-valued Kleene logical OR.
Definition: trival.hh:170
std::ostream & operator<<(std::ostream &os, const mc_algorithm &ma)
Print an mc_algorithm value to a stream.
Definition: mc.hh:74
constexpr trival operator&&(trival a, trival b)
Three-valued Kleene logical AND.
Definition: trival.hh:147
constexpr bool operator==(trival a, trival b)
Equality comparison of two trival values.
Definition: trival.hh:134
constexpr bool operator!=(trival a, trival b)
Inequality comparison of two trival values.
Definition: trival.hh:140

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1