library of assembled shared sources

http://lass.cocamware.com

tri_bool.cpp

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 #include "num_common.h"
00046 #include "tri_bool.h"
00047 #include "../util/bit_manip.h"
00048 
00049 namespace lass
00050 {
00051 namespace num
00052 {
00053 
00054 // --- public --------------------------------------------------------------------------------------
00055 
00056 /** Construct a TriBool, with as default state the "unknown".
00057  */
00058 TriBool::TriBool(State iState):
00059     state_(iState)
00060 {
00061 }
00062 
00063 
00064 
00065 /** Converts a C++ bool to a TriBool.
00066  */
00067 TriBool::TriBool(bool iBool):
00068     state_(iBool ? sTrue : sFalse)
00069 {
00070 }
00071 
00072 
00073 
00074 /** return state
00075  */
00076 const TriBool::State TriBool::state() const
00077 {
00078     return state_;
00079 }
00080 
00081 
00082 
00083 /** access state
00084  */
00085 TriBool::State& TriBool::state()
00086 {
00087     return state_;
00088 }
00089 
00090 
00091 
00092 /** Negation: true becomes false, false becomes true, unknown remains unknown.
00093  *  <table>
00094  *      <tr><td>!</td>  <td>TRUE</td>   <td>FALSE</td>  <td>UNKNOWN</td></tr>
00095  *      <tr><td></td>   <td>false</td>  <td>true</td>   <td>unknown</td></tr>
00096  *  </table>
00097  */
00098 TriBool TriBool::operator!() const
00099 {
00100     if (state_ == sUnknown)
00101     {
00102         return sUnknown;
00103     }
00104     return state_ == sTrue ? sFalse : sTrue;
00105 }
00106 
00107 
00108 
00109 /** Convert TriBool to a bool in a boolean context.
00110  *
00111  *  Will evaluate true if a boolean context only when the TriBool is definitely true. By this, you
00112  *  can use TriBools in if-statements:
00113  *
00114  *  @code
00115  *  TriBool a;
00116  *  if (a)
00117  *  {
00118  *      LASS_ASSERT(a.isTrue());
00119  *  }
00120  *  @endcode
00121  *
00122  *  Because @c !a makes @c true @c false, @c false @c true and @c unknown @c unknown, the following
00123  *  also works:
00124  *
00125  *  @code
00126  *  TriBool a;
00127  *  if (!a)
00128  *  {
00129  *      LASS_ASSERT(a.isFalse());
00130  *  }
00131  */
00132 TriBool::operator SafeBool() const
00133 {
00134     return state_ == sTrue ? safeTrue : safeFalse;
00135 }
00136 
00137 
00138 
00139 /** return true if state is @c true, return false otherwise.
00140  */
00141 bool TriBool::isTrue() const
00142 {
00143     return state_ == sTrue;
00144 }
00145 
00146 
00147 
00148 /** return true if state is @c false, return false otherwise.
00149  */
00150 bool TriBool::isFalse() const
00151 {
00152     return state_ == sFalse;
00153 }
00154 
00155 
00156 
00157 /** return true if state is @c unknown, return false otherwise.
00158  */
00159 bool TriBool::isUnknown() const
00160 {
00161     return state_ == sUnknown;
00162 }
00163 
00164 
00165 
00166 // --- protected -----------------------------------------------------------------------------------
00167 
00168 
00169 
00170 // --- private -------------------------------------------------------------------------------------
00171 
00172 
00173 
00174 // --- free ----------------------------------------------------------------------------------------
00175 
00176 /** Evaluates if two TriBool's are equal, if at least one is unknown then we don't know the result.
00177  *  @relates TriBool
00178  *  <table>
00179  *      <tr><td>==></td>        <td>TRUE</td>       <td>FALSE</td>      <td>UNKNOWN</td></tr>
00180  *      <tr><td>TRUE</td>       <td>true</td>       <td>false</td>      <td>unknown</td></tr>
00181  *      <tr><td>FALSE</td>      <td>false</td>      <td>true</td>       <td>unknown</td></tr>
00182  *      <tr><td>UNKNOWN</td>    <td>unknown</td>    <td>unknown</td>    <td>unknown</td></tr>
00183  *  </table>
00184  *
00185  *  @warning don't expect @c unknown @c == @c unknown yields @c true!  Use @c unknown.isUnknown()
00186  *  instead.
00187  */
00188 TriBool operator==(TriBool iA, TriBool iB)
00189 {
00190     if (iA.isUnknown() || iB.isUnknown())
00191     {
00192         return TriBool::sUnknown;
00193     }
00194     return iA.state() == iB.state();
00195 }
00196 
00197 
00198 
00199 /** Evaluates if two TriBool's differ, if at least one is unknown then we don't know the result.
00200  *  @relates TriBool
00201  *  <table>
00202  *      <tr><td>!=</td>         <td>TRUE</td>       <td>FALSE</td>      <td>UNKNOWN</td></tr>
00203  *      <tr><td>TRUE</td>       <td>false</td>      <td>true</td>       <td>unknown</td></tr>
00204  *      <tr><td>FALSE</td>      <td>true</td>       <td>false</td>      <td>unknown</td></tr>
00205  *      <tr><td>UNKNOWN</td>    <td>unknown</td>    <td>unknown</td>    <td>unknown</td></tr>
00206  *  </table>
00207  */
00208 TriBool operator!=(TriBool iA, TriBool iB)
00209 {
00210     return !(iA == iB);
00211 }
00212 
00213 
00214 
00215 /** Evaluates to true if both are true, unknown if at least one is unknown, false otherwise.
00216  *  @relates TriBool
00217  *  <table>
00218  *      <tr><td>&amp;&amp;</td> <td>TRUE</td>       <td>FALSE</td>      <td>UNKNOWN</td></tr>
00219  *      <tr><td>TRUE</td>       <td>true</td>       <td>false</td>      <td>unknown</td></tr>
00220  *      <tr><td>FALSE</td>      <td>false</td>      <td>false</td>      <td>@b false</td></tr>
00221  *      <tr><td>UNKNOWN</td>    <td>unknown</td>    <td>@b false</td>   <td>unknown</td></tr>
00222  *  </table>
00223  */
00224 TriBool operator&&(TriBool iA, TriBool iB)
00225 {
00226     if (iA.isFalse() || iB.isFalse())
00227     {
00228         return TriBool::sFalse;
00229     }
00230     if (iA.isTrue() && iB.isTrue())
00231     {
00232         return TriBool::sTrue;
00233     }
00234     return TriBool::sUnknown;
00235 }
00236 
00237 
00238 
00239 /** Evaluates to true if at least one is true, false if both are false, unknown otherwise.
00240  *  @relates TriBool
00241  *  <table>
00242  *      <tr><td>||</td>         <td>TRUE</td>       <td>false</td>      <td>unknown</td></tr>
00243  *      <tr><td>TRUE</td>       <td>true</td>       <td>true</td>       <td>@b true</td></tr>
00244  *      <tr><td>FALSE</td>      <td>true</td>       <td>false</td>      <td>unknown</td></tr>
00245  *      <tr><td>UNKNOWN</td>    <td>@b true</td>    <td>unknown</td>    <td>unknown</td></tr>
00246  *  </table>
00247  */
00248 TriBool operator||(TriBool iA, TriBool iB)
00249 {
00250     if (iA.isTrue() || iB.isTrue())
00251     {
00252         return TriBool::sTrue;
00253     }
00254     if (iA.isFalse() && iB.isFalse())
00255     {
00256         return TriBool::sFalse;
00257     }
00258     return TriBool::sUnknown;
00259 }
00260 
00261 
00262 
00263 /** @relates TriBool
00264  */
00265 std::ostream& operator<<(std::ostream& ioS, TriBool iB)
00266 {
00267     switch (iB.state())
00268     {
00269     case TriBool::sTrue:
00270         ioS << true;
00271         break;
00272 
00273     case TriBool::sFalse:
00274         ioS << false;
00275         break;
00276 
00277     default:
00278         LASS_ASSERT(iB.state() == TriBool::sUnknown);
00279         ioS << (ioS.flags() & std::ios::boolalpha ? "unknown" : "?");
00280         break;
00281     }
00282 
00283     return ioS;
00284 }
00285 
00286 
00287 
00288 }
00289 
00290 }
00291 
00292 // EOF

Generated on Mon Nov 10 14:21:40 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo