spot  2.12.1
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;
38  enum value_t : repr_t { no_value = -1, maybe_value = 0, yes_value = 1 };
39  private:
40  value_t val_;
41  public:
42  constexpr trival()
43  : val_(maybe_value)
44  {
45  }
46 
47  constexpr trival(bool v)
48  : val_(v ? yes_value : no_value)
49  {
50  }
51 
52 #ifndef SWIG
53  // This is needed internally by Spot to work around the bitfield
54  // issue mentioned earlier, it makes no sense to use it in Python.
55  static trival from_repr_t(repr_t v)
56  {
57  return trival(static_cast<value_t>(v));
58  }
59 #endif
60 
61  constexpr explicit trival(value_t v)
62  : val_(v)
63  {
64  }
65 
66  static constexpr trival maybe() noexcept
67  {
68  return trival();
69  }
70 
72  constexpr bool is_known() const
73  {
74  return val_ != maybe_value;
75  }
76 
77  constexpr bool is_maybe() const
78  {
79  return val_ == maybe_value;
80  }
81 
82  constexpr bool is_true() const
83  {
84  return val_ == yes_value;
85  }
86 
87  constexpr bool is_false() const
88  {
89  return val_ == no_value;
90  }
91 
92  constexpr value_t val() const
93  {
94  return val_;
95  }
96 
97 #ifndef SWIG
98  // constexpr explicit only supported in SWIG >= 3.0.4
99  constexpr
100 #endif
101  explicit operator bool() const
102  {
103  return val_ == yes_value;
104  }
105 
106  constexpr trival operator!() const
107  {
108  return trival((val_ == yes_value) ? no_value :
109  (val_ == no_value) ? yes_value :
110  maybe_value);
111  }
112  };
113 
114  // We prefer a global version of the operator so that the left
115  // argument can be promoted (think "bool == trival" being promoted
116  // to "trival == trival"). However Swig's generated Python bindings
117  // cannot deal with operators in the global namespace, so we use an
118  // in-class version (coded in impl.i) in this case. This will fail
119  // on a "bool == trival" comparison in Python, but we usually write
120  // "trival == bool" and that works.
121 #ifndef SWIG
122  constexpr bool operator==(trival a, trival b)
123  {
124  return a.val() == b.val();
125  }
126 
127  constexpr bool operator!=(trival a, trival b)
128  {
129  return !(a == b);
130  }
131 #endif
132 
133  constexpr trival operator&&(trival a, trival b)
134  {
135  return
136  (a.val() == trival::no_value || b.val() == trival::no_value)
137  ? trival(false)
138  : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
139  ? trival::maybe()
140  : trival(true);
141  }
142 
143  constexpr trival operator&&(bool a, trival b)
144  {
145  return trival(a) && b;
146  }
147 
148  constexpr trival operator&&(trival a, bool b)
149  {
150  return a && trival(b);
151  }
152 
153  constexpr trival operator||(trival a, trival b)
154  {
155  return
156  (a.val() == trival::yes_value || b.val() == trival::yes_value)
157  ? trival(true)
158  : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
159  ? trival::maybe()
160  : trival(false);
161  }
162 
163  constexpr trival operator||(bool a, trival b)
164  {
165  return trival(a) || b;
166  }
167 
168  constexpr trival operator||(trival a, bool b)
169  {
170  return a || trival(b);
171  }
172 
173  inline std::ostream& operator<<(std::ostream& os, trival v)
174  {
175  return os << ((v.val() == trival::no_value) ? "no"
176  : (v.val() == trival::maybe_value) ? "maybe"
177  : "yes");
178  }
179 
181 }
A class implementing Kleene's three-valued logic.
Definition: trival.hh:33
constexpr bool is_known() const
Is true or false, but not maybe.
Definition: trival.hh:72
Definition: automata.hh:26

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