kexi

tristate.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 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     operator bool() const { return m_value==True; }
00144 
00149     bool operator!() const { return m_value==False; }
00150 
00155     bool operator~() const { return m_value==Cancelled; }
00156 
00157     tristate& operator=(const tristate& tsValue) { m_value = tsValue.m_value; return *this; }
00158 
00159     friend inline bool operator!=(bool boolValue, tristate tsValue);
00160     friend inline bool operator!=(tristate tsValue, bool boolValue);
00161 
00162 private:
00167     enum Value {
00168         False = 0,
00169         True = 1,
00170         Cancelled = 2
00171     };
00172 
00176     Value m_value;
00177 };
00178 
00186 inline bool operator!=(bool boolValue, tristate tsValue) 
00187 {
00188     return !( (tsValue.m_value==tristate::True && boolValue) 
00189     || (tsValue.m_value==tristate::False && !boolValue) );
00190 }
00191 
00196 inline bool operator!=(tristate tsValue, bool boolValue) 
00197 {
00198     return !( (tsValue.m_value==tristate::True && boolValue) 
00199     || (tsValue.m_value==tristate::False && !boolValue) );
00200 }
00201 
00202 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys