tristate.h
1/* This file is part of the KDE project
2 Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef _TRISTATE_TYPE_H_
21#define _TRISTATE_TYPE_H_
22
23#include <tqstring.h>
24
41static const char cancelled = 2;
42
46static const char dontKnow = cancelled;
47
100{
101public:
106 : m_value(Cancelled)
107 {
108 }
109
113 tristate(bool boolValue)
114 : m_value(boolValue ? True : False)
115 {
116 }
117
125 tristate(char c)
126 : m_value(c==cancelled ? tristate::Cancelled : (c==1 ? True : False))
127 {
128 }
129
136 tristate(int intValue)
137 : m_value(intValue==(int)cancelled ? tristate::Cancelled : (intValue==1 ? True : False))
138 {
139 }
140
145 bool operator!() const { return m_value==False; }
146
151 bool operator~() const { return m_value==Cancelled; }
152
153 tristate& operator=(const tristate& tsValue) { m_value = tsValue.m_value; return *this; }
154
155 friend inline bool operator==(bool boolValue, tristate tsValue);
156
157 friend inline bool operator==(tristate tsValue, bool boolValue);
158
159 friend inline bool operator!=(bool boolValue, tristate tsValue);
160
161 friend inline bool operator!=(tristate tsValue, bool boolValue);
162
166 TQString toString() const {
167 if (m_value==False)
168 return TQString::fromLatin1("false");
169 return m_value==True ? TQString::fromLatin1("true") : TQString::fromLatin1("cancelled");
170 }
171
172private:
177 enum Value {
178 False = 0,
179 True = 1,
180 Cancelled = 2
181 };
182
186 Value m_value;
187};
188
196inline bool operator!=(bool boolValue, tristate tsValue)
197{
198 return !( (tsValue.m_value==tristate::True && boolValue)
199 || (tsValue.m_value==tristate::False && !boolValue) );
200}
201
206inline bool operator!=(tristate tsValue, bool boolValue)
207{
208 return !( (tsValue.m_value==tristate::True && boolValue)
209 || (tsValue.m_value==tristate::False && !boolValue) );
210}
211
219inline bool operator==(tristate tsValue, bool boolValue)
220{
221 return (tsValue.m_value==tristate::True && boolValue)
222 || (tsValue.m_value==tristate::False && !boolValue);
223}
224
232inline bool operator==(bool boolValue, tristate tsValue)
233{
234 return (tsValue.m_value==tristate::True && boolValue)
235 || (tsValue.m_value==tristate::False && !boolValue);
236}
237
238#endif
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
bool operator~() const
Special casting to bool type: true is only returned if the original tristate value is equal to cancel...
Definition tristate.h:151
bool operator!() const
Casting to bool type with negation: true is only returned if the original tristate value is equal to ...
Definition tristate.h:145
friend bool operator==(bool boolValue, tristate tsValue)
Equality operator comparing a bool value boolValue and a tristate value tsValue.
Definition tristate.h:232
tristate(int intValue)
Constructor accepting an integer value.
Definition tristate.h:136
tristate()
Default constructor, object has cancelled value set.
Definition tristate.h:105
TQString toString() const
Definition tristate.h:166
friend bool operator!=(bool boolValue, tristate tsValue)
Inequality operator comparing a bool value boolValue and a tristate value tsValue.
Definition tristate.h:196
tristate(char c)
Constructor accepting a char value.
Definition tristate.h:125
tristate(bool boolValue)
Constructor accepting a boolean value.
Definition tristate.h:113