-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrit.h
109 lines (88 loc) · 2.23 KB
/
trit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2019 The Unit-e developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef UNITE_TRIT_H
#define UNITE_TRIT_H
#include <cstdint>
//! \brief A simple datatype that can hold yes/no/maybe kind of information.
class Trit final {
public:
static const Trit True;
static const Trit False;
static const Trit Unknown;
private:
enum value_t : std::uint8_t {
value_true,
value_false,
value_unknown
};
value_t value;
public:
//! \brief Will initialize to either Trit::TRUE or Trit::FALSE
//!
//! Useful for converting bool into Trit.
explicit Trit(bool truth) noexcept;
//! \brief Will initialize to Trit::UNKNOWN.
Trit() noexcept;
//! \brief Whether this is Trit::TRUE
inline bool IsTrue() const noexcept {
return value == value_true;
}
//! \brief Whether this is Trit::FALSE
inline bool IsFalse() const noexcept {
return value == value_false;
}
//! \brief Whether this is Trit::UNKNOWN
inline bool IsUnknown() const noexcept {
return value == value_unknown;
}
//! \brief Same as IsTrue() but useful in if statements.
explicit operator bool() const noexcept {
return IsTrue();
}
//! \brief Ternary AND.
Trit And(const Trit other) const noexcept {
if (IsTrue() && other.IsTrue()) {
return True;
}
if (IsFalse() || other.IsFalse()) {
return False;
}
return Unknown;
}
//! \brief Ternary OR.
Trit Or(Trit other) const noexcept {
if (IsFalse() && other.IsFalse()) {
return False;
}
if (IsTrue() || other.IsTrue()) {
return True;
}
return Unknown;
}
//! \brief Ternary NOT.
Trit Not() const noexcept {
if (IsTrue()) {
return False;
}
if (IsFalse()) {
return True;
}
return Unknown;
}
static Trit And(Trit t1, Trit t2) noexcept {
return t1.And(t2);
}
template <typename... T>
static Trit And(Trit t, T... ts) noexcept {
return t.And(And(ts...));
}
static Trit Or(Trit t1, Trit t2) noexcept {
return t1.Or(t2);
}
template <typename... T>
static Trit Or(Trit t, T... ts) noexcept {
return t.Or(Or(ts...));
}
};
#endif // UNITE_TRIT_H