spot 2.3.1

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
trival.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2016 Laboratoire de Recherche et Developpement de
3 // l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 #pragma once
21
22 #include <iostream>
23
24 namespace spot
25 {
26
29
33 class trival
34 {
35 public:
36 // We use repr_t instead of value_t in bitfields to avoid a warning from gcc
37 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51242
38 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()
44 : val_(maybe_value)
45 {
46 }
47
48 constexpr trival(bool v)
49 : val_(v ? yes_value : no_value)
50 {
51 }
52
53 trival(repr_t v)
54 {
55 val_ = static_cast<value_t>(v);
56 }
57
58 constexpr trival(value_t v)
59 : val_(v)
60 {
61 }
62
63 static constexpr trival maybe()
64 {
65 return trival();
66 }
67
69 constexpr bool is_known() const
70 {
71 return val_ != maybe_value;
72 }
73
74 constexpr bool is_maybe() const
75 {
76 return val_ == maybe_value;
77 }
78
79 constexpr bool is_true() const
80 {
81 return val_ == yes_value;
82 }
83
84 constexpr bool is_false() const
85 {
86 return val_ == no_value;
87 }
88
89 constexpr bool operator==(trival o) const
90 {
91 return val_ == o.val_;
92 }
93
94 constexpr bool operator!=(trival o) const
95 {
96 return val_ != o.val_;
97 }
98
99 constexpr value_t val() const
100 {
101 return val_;
102 }
103
104 #ifndef SWIG
105 // constexpr explicit only supported in SWIG >= 3.0.4
106 constexpr
107 #endif
108 explicit operator bool() const
109 {
110 return val_ == yes_value;
111 }
112
113 constexpr trival operator!() const
114 {
115 return trival((val_ == yes_value) ? no_value :
116 (val_ == no_value) ? yes_value :
117 maybe_value);
118 }
119 };
120
121 constexpr bool operator==(bool a, trival b)
122 {
123 return b == trival(a);
124 }
125
126 constexpr bool operator!=(bool a, trival b)
127 {
128 return b != trival(a);
129 }
130
131 constexpr trival operator&&(trival a, trival b)
132 {
133 return
134 (a.val() == trival::no_value || b.val() == trival::no_value)
135 ? trival(false)
136 : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
137 ? trival::maybe()
138 : trival(true);
139 }
140
141 constexpr trival operator&&(bool a, trival b)
142 {
143 return trival(a) && b;
144 }
145
146 constexpr trival operator&&(trival a, bool b)
147 {
148 return a && trival(b);
149 }
150
151 constexpr trival operator||(trival a, trival b)
152 {
153 return
154 (a.val() == trival::yes_value || b.val() == trival::yes_value)
155 ? trival(true)
156 : (a.val() == trival::maybe_value || b.val() == trival::maybe_value)
157 ? trival::maybe()
158 : trival(false);
159 }
160
161 constexpr trival operator||(bool a, trival b)
162 {
163 return trival(a) || b;
164 }
165
166 constexpr trival operator||(trival a, bool b)
167 {
168 return a || trival(b);
169 }
170
171 inline std::ostream& operator<<(std::ostream& os, trival v)
172 {
173 return os << ((v.val() == trival::no_value) ? "no"
174 : (v.val() == trival::maybe_value) ? "maybe"
175 : "yes");
176 }
177
179 }
spot
Definition: graph.hh:33
spot::trival::is_known
constexpr bool is_known() const
Is true or false, but not maybe.
Definition: trival.hh:69
spot::trival
A class implementing Kleene's three-valued logic.
Definition: trival.hh:33

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.