-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTriState.h
126 lines (106 loc) · 2.68 KB
/
TriState.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
This file is part of the Util library.
Copyright (C) 2013 Benjamin Eikel <[email protected]>
Copyright (C) 2013 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef TRISTATE_H
#define TRISTATE_H
namespace Util{
/**
* @brief Class that can hold one of three values: true, fals, undefined
*
* this class is kind of an extended bool and can additionally have the value undefined
* it can be constructed from, assigned from and compared to bool values and other TriStates
*
* as well as bool on most systems this class uses one byte of memory
*
* @author Ralf Petring
* @date 2013-01-16
* @ingroup util_helper
*/
class TriState {
private:
enum STATE : uint8_t {OFF = 0, ON = 1, UNDEFINED = 2} state;
public:
//! default constructor, constructs a new TriState with value undefined
TriState() : state(UNDEFINED){
}
//! constructor, constructs a new TriState with the given bool as value
explicit TriState(const bool b) : state(b?ON:OFF){
}
/**
* assignment operator for bool
* @note this does not remove default constructors and default assignment operators etc.
*/
TriState & operator=(const bool & b) {
state = b?ON:OFF;
return *this;
}
/**
* equality operator
* @param other any TriState
* @return true iff other is equal to this
*/
bool operator==(const TriState & other) const {
return state == other.state;
}
/**
* inequality operator
* @param other any TriState
* @return true iff other is not equal to this
*/
bool operator!=(const TriState & other) const {
return state != other.state;
}
/**
* equality operator to bool
* @param other any bool
* @return true iff other is equal to this
*/
bool operator==(const bool other) const {
return state == (other?ON:OFF);
}
/**
* inequality operator to bool
* @param other any bool
* @return true iff other is not equal to this
*/
bool operator!=(const bool other) const {
return state != (other?ON:OFF);
}
/**
* @return true iff current value is true
*/
bool isTrue() const {
return state == ON;
}
/**
* @return true iff current value is false
*/
bool isFalse() const {
return state == OFF;
}
/**
* @return true iff current value is undefined
*/
bool isUndefined() const {
return state == UNDEFINED;
}
/**
* @return true iff current value is true OR false
*/
bool isDefined() const {
return state != UNDEFINED;
}
/**
* sets the current value to undefined
*/
void undefine(){
state = UNDEFINED;
}
};
}
#endif // TRISTATE_H