neon/boolInVec.h

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2009 Sony Computer Entertainment Inc.
00003    All rights reserved.
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 
00015 */
00016 
00017 #ifndef _BOOLINVEC_H
00018 #define _BOOLINVEC_H
00019 
00020 #include <math.h>
00021 namespace Vectormath {
00022 
00023 class floatInVec;
00024 
00025 //--------------------------------------------------------------------------------------------------
00026 // boolInVec class
00027 //
00028 
00029 class boolInVec
00030 {
00031 private:
00032     unsigned int mData;
00033 
00034 public:
00035     // Default constructor; does no initialization
00036     //
00037     inline boolInVec( ) { };
00038 
00039     // Construct from a value converted from float
00040     //
00041     inline boolInVec(floatInVec vec);
00042 
00043     // Explicit cast from bool
00044     //
00045     explicit inline boolInVec(bool scalar);
00046 
00047     // Explicit cast to bool
00048     //
00049     inline bool getAsBool() const;
00050 
00051 #ifndef _VECTORMATH_NO_SCALAR_CAST
00052     // Implicit cast to bool
00053     //
00054     inline operator bool() const;
00055 #endif
00056 
00057     // Boolean negation operator
00058     //
00059     inline const boolInVec operator ! () const;
00060 
00061     // Assignment operator
00062     //
00063     inline boolInVec& operator = (boolInVec vec);
00064 
00065     // Boolean and assignment operator
00066     //
00067     inline boolInVec& operator &= (boolInVec vec);
00068 
00069     // Boolean exclusive or assignment operator
00070     //
00071     inline boolInVec& operator ^= (boolInVec vec);
00072 
00073     // Boolean or assignment operator
00074     //
00075     inline boolInVec& operator |= (boolInVec vec);
00076 
00077 };
00078 
00079 // Equal operator
00080 //
00081 inline const boolInVec operator == (boolInVec vec0, boolInVec vec1);
00082 
00083 // Not equal operator
00084 //
00085 inline const boolInVec operator != (boolInVec vec0, boolInVec vec1);
00086 
00087 // And operator
00088 //
00089 inline const boolInVec operator & (boolInVec vec0, boolInVec vec1);
00090 
00091 // Exclusive or operator
00092 //
00093 inline const boolInVec operator ^ (boolInVec vec0, boolInVec vec1);
00094 
00095 // Or operator
00096 //
00097 inline const boolInVec operator | (boolInVec vec0, boolInVec vec1);
00098 
00099 // Conditionally select between two values
00100 //
00101 inline const boolInVec select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1);
00102 
00103 
00104 } // namespace Vectormath
00105 
00106 
00107 //--------------------------------------------------------------------------------------------------
00108 // boolInVec implementation
00109 //
00110 
00111 #include "floatInVec.h"
00112 
00113 namespace Vectormath {
00114 
00115 inline
00116 boolInVec::boolInVec(floatInVec vec)
00117 {
00118     *this = (vec != floatInVec(0.0f));
00119 }
00120 
00121 inline
00122 boolInVec::boolInVec(bool scalar)
00123 {
00124     mData = -(int)scalar;
00125 }
00126 
00127 inline
00128 bool
00129 boolInVec::getAsBool() const
00130 {
00131     return (mData > 0);
00132 }
00133 
00134 #ifndef _VECTORMATH_NO_SCALAR_CAST
00135 inline
00136 boolInVec::operator bool() const
00137 {
00138     return getAsBool();
00139 }
00140 #endif
00141 
00142 inline
00143 const boolInVec
00144 boolInVec::operator ! () const
00145 {
00146     return boolInVec(!mData);
00147 }
00148 
00149 inline
00150 boolInVec&
00151 boolInVec::operator = (boolInVec vec)
00152 {
00153     mData = vec.mData;
00154     return *this;
00155 }
00156 
00157 inline
00158 boolInVec&
00159 boolInVec::operator &= (boolInVec vec)
00160 {
00161     *this = *this & vec;
00162     return *this;
00163 }
00164 
00165 inline
00166 boolInVec&
00167 boolInVec::operator ^= (boolInVec vec)
00168 {
00169     *this = *this ^ vec;
00170     return *this;
00171 }
00172 
00173 inline
00174 boolInVec&
00175 boolInVec::operator |= (boolInVec vec)
00176 {
00177     *this = *this | vec;
00178     return *this;
00179 }
00180 
00181 inline
00182 const boolInVec
00183 operator == (boolInVec vec0, boolInVec vec1)
00184 {
00185     return boolInVec(vec0.getAsBool() == vec1.getAsBool());
00186 }
00187 
00188 inline
00189 const boolInVec
00190 operator != (boolInVec vec0, boolInVec vec1)
00191 {
00192     return !(vec0 == vec1);
00193 }
00194 
00195 inline
00196 const boolInVec
00197 operator & (boolInVec vec0, boolInVec vec1)
00198 {
00199     return boolInVec(vec0.getAsBool() & vec1.getAsBool());
00200 }
00201 
00202 inline
00203 const boolInVec
00204 operator | (boolInVec vec0, boolInVec vec1)
00205 {
00206     return boolInVec(vec0.getAsBool() | vec1.getAsBool());
00207 }
00208 
00209 inline
00210 const boolInVec
00211 operator ^ (boolInVec vec0, boolInVec vec1)
00212 {
00213     return boolInVec(vec0.getAsBool() ^ vec1.getAsBool());
00214 }
00215 
00216 inline
00217 const boolInVec
00218 select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1)
00219 {
00220     return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
00221 }
00222 
00223 } // namespace Vectormath
00224 
00225 #endif // boolInVec_h
00226