kexi
tristate.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _TRISTATE_TYPE_H_
00021 #define _TRISTATE_TYPE_H_
00022
00023 #include <qstring.h>
00024
00041 static const char cancelled = 2;
00042
00046 static const char dontKnow = cancelled;
00047
00099 class tristate
00100 {
00101 public:
00105 tristate()
00106 : m_value(Cancelled)
00107 {
00108 }
00109
00113 tristate(bool boolValue)
00114 : m_value(boolValue ? True : False)
00115 {
00116 }
00117
00125 tristate(char c)
00126 : m_value(c==cancelled ? tristate::Cancelled : (c==1 ? True : False))
00127 {
00128 }
00129
00136 tristate(int intValue)
00137 : m_value(intValue==(int)cancelled ? tristate::Cancelled : (intValue==1 ? True : False))
00138 {
00139 }
00140
00145 bool operator!() const { return m_value==False; }
00146
00151 bool operator~() const { return m_value==Cancelled; }
00152
00153 tristate& operator=(const tristate& tsValue) { m_value = tsValue.m_value; return *this; }
00154
00155 friend inline bool operator==(bool boolValue, tristate tsValue);
00156
00157 friend inline bool operator==(tristate tsValue, bool boolValue);
00158
00159 friend inline bool operator!=(bool boolValue, tristate tsValue);
00160
00161 friend inline bool operator!=(tristate tsValue, bool boolValue);
00162
00166 QString toString() const {
00167 if (m_value==False)
00168 return QString::fromLatin1("false");
00169 return m_value==True ? QString::fromLatin1("true") : QString::fromLatin1("cancelled");
00170 }
00171
00172 private:
00177 enum Value {
00178 False = 0,
00179 True = 1,
00180 Cancelled = 2
00181 };
00182
00186 Value m_value;
00187 };
00188
00196 inline bool operator!=(bool boolValue, tristate tsValue)
00197 {
00198 return !( (tsValue.m_value==tristate::True && boolValue)
00199 || (tsValue.m_value==tristate::False && !boolValue) );
00200 }
00201
00206 inline bool operator!=(tristate tsValue, bool boolValue)
00207 {
00208 return !( (tsValue.m_value==tristate::True && boolValue)
00209 || (tsValue.m_value==tristate::False && !boolValue) );
00210 }
00211
00219 inline bool operator==(tristate tsValue, bool boolValue)
00220 {
00221 return (tsValue.m_value==tristate::True && boolValue)
00222 || (tsValue.m_value==tristate::False && !boolValue);
00223 }
00224
00232 inline bool operator==(bool boolValue, tristate tsValue)
00233 {
00234 return (tsValue.m_value==tristate::True && boolValue)
00235 || (tsValue.m_value==tristate::False && !boolValue);
00236 }
00237
00238 #endif
|