OpaqueInteger.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/common/OpaqueInteger.h#10 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #ifndef Fennel_OpaqueInteger_Included
00025 #define Fennel_OpaqueInteger_Included
00026 
00027 #include <iostream>
00028 
00029 FENNEL_BEGIN_NAMESPACE
00030 
00062 template<class T,class Dummy>
00063 class OpaqueInteger
00064 {
00065 protected:
00066     T val;
00067 public:
00068     OpaqueInteger()
00069     {
00070         // NOTE:  the default constructor should NOT
00071         // initialize to 0, because this doesn't match the non-DEBUG
00072         // behavior, AND actually breaks some  code (scary but true)
00073     }
00074 
00075     explicit OpaqueInteger(T t)
00076     {
00077         val = t;
00078     }
00079 
00080     int operator == (OpaqueInteger<T,Dummy> other) const
00081     {
00082         return val == other.val;
00083     }
00084 
00085     int operator != (OpaqueInteger<T,Dummy> other) const
00086     {
00087         return val != other.val;
00088     }
00089 
00090     int operator < (OpaqueInteger<T,Dummy> other) const
00091     {
00092         return val < other.val;
00093     }
00094 
00095     int operator > (OpaqueInteger<T,Dummy> other) const
00096     {
00097         return val > other.val;
00098     }
00099 
00100     int operator <= (OpaqueInteger<T,Dummy> other) const
00101     {
00102         return val <= other.val;
00103     }
00104 
00105     int operator >= (OpaqueInteger<T,Dummy> other) const
00106     {
00107         return val >= other.val;
00108     }
00109 
00110     OpaqueInteger<T,Dummy> operator ++ ()
00111     {
00112         return OpaqueInteger<T,Dummy>(++val);
00113     }
00114 
00115     OpaqueInteger<T,Dummy> operator ++ (int)
00116     {
00117         return OpaqueInteger<T,Dummy>(val++);
00118     }
00119 
00120     OpaqueInteger<T,Dummy> operator -- ()
00121     {
00122         return OpaqueInteger<T,Dummy>(--val);
00123     }
00124 
00125     OpaqueInteger<T,Dummy> operator -- (int)
00126     {
00127         return OpaqueInteger<T,Dummy>(val--);
00128     }
00129 
00130     OpaqueInteger<T,Dummy> operator *= (OpaqueInteger<T,Dummy> i)
00131     {
00132         val *= i.val;
00133         return *this;
00134     }
00135 
00136     OpaqueInteger<T,Dummy> operator /= (OpaqueInteger<T,Dummy> i)
00137     {
00138         val /= i.val;
00139         return *this;
00140     }
00141 
00142     OpaqueInteger<T,Dummy> operator += (OpaqueInteger<T,Dummy> i)
00143     {
00144         val += i.val;
00145         return *this;
00146     }
00147 
00148     OpaqueInteger<T,Dummy> operator -= (OpaqueInteger<T,Dummy> i)
00149     {
00150         val -= i.val;
00151         return *this;
00152     }
00153 
00154     OpaqueInteger<T,Dummy> operator %= (OpaqueInteger<T,Dummy> i)
00155     {
00156         val %= i.val;
00157         return *this;
00158     }
00159 
00160     OpaqueInteger<T,Dummy> operator *= (T t)
00161     {
00162         val *= t;
00163         return *this;
00164     }
00165 
00166     OpaqueInteger<T,Dummy> operator /= (T t)
00167     {
00168         val /= t;
00169         return *this;
00170     }
00171 
00172     OpaqueInteger<T,Dummy> operator += (T t)
00173     {
00174         val += t;
00175         return *this;
00176     }
00177 
00178     OpaqueInteger<T,Dummy> operator -= (T t)
00179     {
00180         val -= t;
00181         return *this;
00182     }
00183 
00184     OpaqueInteger<T,Dummy> operator %= (T t)
00185     {
00186         val %= t;
00187         return *this;
00188     }
00189 
00190     OpaqueInteger<T,Dummy> operator >>= (int i)
00191     {
00192         val >>= i;
00193         return *this;
00194     }
00195 
00196     OpaqueInteger<T,Dummy> operator <<= (int i)
00197     {
00198         val >>= i;
00199         return *this;
00200     }
00201 
00202     OpaqueInteger<T,Dummy> operator >> (int i) const
00203     {
00204         return OpaqueInteger<T,Dummy>(val >> i);
00205     }
00206 
00207     OpaqueInteger<T,Dummy> operator << (int i) const
00208     {
00209         return OpaqueInteger<T,Dummy>(val << i);
00210     }
00211 
00212     OpaqueInteger<T,Dummy> operator % (T i) const
00213     {
00214         return OpaqueInteger<T,Dummy>(val % i);
00215     }
00216 
00217     OpaqueInteger<T,Dummy> operator / (T i) const
00218     {
00219         return OpaqueInteger<T,Dummy>(val / i);
00220     }
00221 
00222     OpaqueInteger<T,Dummy> operator * (T i) const
00223     {
00224         return OpaqueInteger<T,Dummy>(val * i);
00225     }
00226 
00227     OpaqueInteger<T,Dummy> operator + (T t) const
00228     {
00229         return OpaqueInteger<T,Dummy>(val+t);
00230     }
00231 
00232     OpaqueInteger<T,Dummy> operator - (T t) const
00233     {
00234         return OpaqueInteger<T,Dummy>(val-t);
00235     }
00236 
00237     OpaqueInteger<T,Dummy> operator % (OpaqueInteger<T,Dummy> i) const
00238     {
00239         return OpaqueInteger<T,Dummy>(val % i.val);
00240     }
00241 
00242     OpaqueInteger<T,Dummy> operator / (OpaqueInteger<T,Dummy> i) const
00243     {
00244         return OpaqueInteger<T,Dummy>(val / i.val);
00245     }
00246 
00247     OpaqueInteger<T,Dummy> operator * (OpaqueInteger<T,Dummy> i) const
00248     {
00249         return OpaqueInteger<T,Dummy>(val * i.val);
00250     }
00251 
00252     OpaqueInteger<T,Dummy> operator + (OpaqueInteger<T,Dummy> i) const
00253     {
00254         return OpaqueInteger<T,Dummy>(val+i.val);
00255     }
00256 
00257     OpaqueInteger<T,Dummy> operator - (OpaqueInteger<T,Dummy> i) const
00258     {
00259         return OpaqueInteger<T,Dummy>(val-i.val);
00260     }
00261 
00262     T getWrapped() const
00263     {
00264         return val;
00265     }
00266 };
00267 
00272 template<class T,class Dummy>
00273 inline T opaqueToInt(OpaqueInteger<T,Dummy> t)
00274 {
00275     return t.getWrapped();
00276 }
00277 
00281 template<class T>
00282 inline T opaqueToInt(T t)
00283 {
00284     return t;
00285 }
00286 
00287 #ifdef DEBUG
00288 #define OPAQUE_INTEGER_AS_CLASS
00289 #else
00290 #define OPAQUE_INTEGER_AS_PRIMITIVE
00291 #endif
00292 
00293 #ifdef OPAQUE_INTEGER_AS_CLASS
00294 // For debug build, define a dummy class to differentiate various
00295 // OpaqueInteger classes of the same size.
00296 #define DEFINE_OPAQUE_INTEGER(TypeName,TypeSize) \
00297 class FENNEL_COMMON_EXPORT Dummy##TypeName { \
00298 }; \
00299 typedef OpaqueInteger<TypeSize,Dummy##TypeName> TypeName; \
00300 typedef TypeSize TypeName##Primitive;
00301 
00302 template<class T,class Dummy>
00303 inline std::ostream &operator <<(std::ostream &o,OpaqueInteger<T,Dummy> t)
00304 {
00305     o << opaqueToInt(t);
00306     return o;
00307 }
00308 
00309 #else
00310 
00311 // For release build, just typedef because we don't trust the compiler.
00312 #define DEFINE_OPAQUE_INTEGER(TypeName,TypeSize) \
00313 typedef TypeSize TypeName; \
00314 typedef TypeSize TypeName##Primitive;
00315 
00316 #endif
00317 
00318 // To refer to the primitive corresponding to a given opaque type T
00319 // regardless of the build, use TPrimitive; e.g. if you
00320 // DEFINE_OPAQUE_INTEGER(Cookie,uint64_t), then CookiePrimitive is
00321 // always equivalent to uint64_t.
00322 
00323 FENNEL_END_NAMESPACE
00324 
00325 #endif
00326 
00327 // End OpaqueInteger.h

Generated on Mon Jun 22 04:00:18 2009 for Fennel by  doxygen 1.5.1