forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTroolean.cpp
More file actions
147 lines (122 loc) · 2.81 KB
/
Copy pathTroolean.cpp
File metadata and controls
147 lines (122 loc) · 2.81 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// FILE: Troolean.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: class for troolean math (true false unknown)
// URL:
//
// HISTORY:
// 0.1.0 initial version
// 0.1.1 adjust (in)equality so that unknown == unknown
// although there could be arguments that unknown != unknown
// added isTrue(), isFalse(), isUnknown()
// first public release
//
#include "Troolean.h"
/////////////////////////////////////////////////////
//
// PUBLIC
//
Troolean::Troolean()
{
_value = -1;
}
Troolean::Troolean(const int8_t val)
{
if (val == 0) _value = 0;
else if (val == -1) _value = -1;
else _value = 1;
}
Troolean::Troolean(const Troolean &t)
{
_value = t._value;
}
// PRINTING
size_t Troolean::printTo(Print& p) const
{
size_t n = 0;
if (_value == 0) n += p.print("false");
else if (_value == -1) n += p.print("unknown");
else n += p.print("true");
return n;
};
// EQUALITIES
// t == t
// f == f
// u == u
bool Troolean::operator == (const Troolean &t)
{
return (_value == t._value);
}
bool Troolean::operator == (const bool &b)
{
if (_value == 0 && !b) return true;
if (_value == 1 && b) return true;
return false;
}
bool Troolean::operator == (const int &i)
{
if (_value == 0 && i == 0) return true;
if (_value == -1 && i == -1) return true;
if (_value == 1 && i != 0 && i != -1) return true;
return false;
}
bool Troolean::operator != (const Troolean &t)
{
return (_value != t._value);
}
bool Troolean::operator != (const bool &b)
{
if (_value == 0 && !b) return false;
if (_value == 1 && b) return false;
return true;
}
bool Troolean::operator != (const int &i)
{
if (_value == 0 && i != 0) return true;
if (_value == -1 && i != -1) return true;
if (_value == 1 && (i == 0 || i == -1)) return true;
return false;
}
Troolean::operator bool() const
{
if (_value == 1) return true;
if (_value == 0) return false;
return false;
}
// NEGATE
// t -> f
// f -> t
// u -> u
Troolean Troolean::operator ! ()
{
if (_value == -1) return Troolean(-1); // random 0 1 :)
if (_value == 1) return Troolean(0);
return Troolean(1);
}
// LOGICAL OPERATORS
Troolean Troolean::operator && (const Troolean &t)
{
if (_value == 0 || t._value == 0) return Troolean(0);
if (_value == 1 && t._value == 1) return Troolean(1);
return Troolean(-1);
}
Troolean Troolean::operator && (const bool &b)
{
if (_value == 0 || !b) return Troolean(0);
if (_value == 1 && b) return Troolean(1);
return Troolean(-1);
}
Troolean Troolean::operator || (const Troolean &t)
{
if (_value == 1 || t._value == 1) return Troolean(1);
if (_value == 0 && t._value == 0) return Troolean(0);
return Troolean(-1);
}
Troolean Troolean::operator || (const bool &b)
{
if (_value == 1 || b) return Troolean(0);
if (_value == 0 && !b) return Troolean(1);
return Troolean(-1);
}
// END OF FILE