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 
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
KDE Home | KDE Accessibility Home | Description of Access Keys