kexi

tristate.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef _TRISTATE_TYPE_H_
00021 #define _TRISTATE_TYPE_H_
00022 
00039 static const char cancelled = 2;
00040 
00044 static const char dontKnow = cancelled;
00045 
00097 class tristate
00098 {
00099 public:
00103     tristate()
00104      : m_value(Cancelled)
00105     {
00106     }
00107 
00111     tristate(bool boolValue)
00112      : m_value(boolValue ? True : False)
00113     {
00114     }
00115 
00123     tristate(char c)
00124      : m_value(c==cancelled ? tristate::Cancelled : (c==1 ? True : False))
00125     {
00126     }
00127 
00134     tristate(int intValue)
00135      : m_value(intValue==(int)cancelled ? tristate::Cancelled : (intValue==1 ? True : False))
00136     {
00137     }
00138 
00143     bool operator!() const { return m_value==False; }
00144 
00149     bool operator~() const { return m_value==Cancelled; }
00150 
00151     tristate& operator=(const tristate& tsValue) { m_value = tsValue.m_value; return *this; }
00152 
00153     friend inline bool operator==(bool boolValue, tristate tsValue);
00154 
00155     friend inline bool operator==(tristate tsValue, bool boolValue);
00156 
00157     friend inline bool operator!=(bool boolValue, tristate tsValue);
00158 
00159     friend inline bool operator!=(tristate tsValue, bool boolValue);
00160 
00164     QString toString() const {
00165         if (m_value==False)
00166             return QString::fromLatin1("false");
00167         return m_value==True ? QString::fromLatin1("true") : QString::fromLatin1("cancelled");
00168     }
00169 
00170 private:
00175     enum Value {
00176         False = 0,
00177         True = 1,
00178         Cancelled = 2
00179     };
00180 
00184     Value m_value;
00185 };
00186 
00194 inline bool operator!=(bool boolValue, tristate tsValue) 
00195 {
00196     return !( (tsValue.m_value==tristate::True && boolValue) 
00197     || (tsValue.m_value==tristate::False && !boolValue) );
00198 }
00199 
00204 inline bool operator!=(tristate tsValue, bool boolValue) 
00205 {
00206     return !( (tsValue.m_value==tristate::True && boolValue) 
00207     || (tsValue.m_value==tristate::False && !boolValue) );
00208 }
00209 
00217 inline bool operator==(tristate tsValue, bool boolValue)
00218 {
00219     return (tsValue.m_value==tristate::True && boolValue) 
00220     || (tsValue.m_value==tristate::False && !boolValue);
00221 }
00222 
00230 inline bool operator==(bool boolValue, tristate tsValue)
00231 {
00232     return (tsValue.m_value==tristate::True && boolValue) 
00233     || (tsValue.m_value==tristate::False && !boolValue);
00234 }
00235 
00236 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys