Equalizer 1.0

uint128_t.h

00001 
00002 /* Copyright (c) 2010, Cedric Stalder <cedric.stalder@gmail.com>
00003  *               2010-2011, Stefan Eilemann <eile@eyescale.ch>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License version 2.1 as published
00007  * by the Free Software Foundation.
00008  *  
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00012  * details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this library; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #ifndef COBASE_UINT128_H
00020 #define COBASE_UINT128_H
00021 
00022 #include <co/base/api.h>
00023 #include <sstream>
00024 
00025 #ifdef _MSC_VER
00026 // Don't include <co/base/types.h> to be minimally intrusive for apps
00027 // using uint128_t
00028 #  include <basetsd.h>
00029 typedef UINT64     uint64_t;
00030 #else
00031 #  include <stdint.h>
00032 #endif
00033 
00034 namespace co
00035 {
00036 namespace base
00037 {
00039     class uint128_t
00040     {
00041     public:
00046         uint128_t( const uint64_t low_ = 0 ) 
00047             : _high( 0 ), _low( low_ ) {}
00048 
00053         uint128_t( const uint64_t high_, const uint64_t low_ ) 
00054             : _high( high_ ), _low( low_ ) {}
00055 
00057         uint128_t& operator = ( const uint128_t& rhs )
00058             {
00059                 _high = rhs._high;
00060                 _low = rhs._low;
00061                 return *this;
00062             }
00063 
00065         COBASE_API uint128_t& operator = ( const std::string& from );
00066 
00071         bool operator == ( const uint128_t& rhs ) const
00072             { return _high == rhs._high && _low == rhs._low; }
00073 
00078         bool operator != ( const uint128_t& rhs ) const
00079             { return _high != rhs._high || _low != rhs._low; }
00080 
00085         bool operator < ( const uint128_t& rhs ) const
00086             { 
00087                 if( _high < rhs._high )
00088                     return true;
00089                 if( _high > rhs._high )
00090                     return false;
00091                 return _low < rhs._low; 
00092             }
00093 
00098         bool operator > ( const uint128_t& rhs ) const
00099             { 
00100                 if( _high > rhs._high )
00101                     return true;
00102                 if( _high < rhs._high )
00103                     return false;
00104                 return _low > rhs._low; 
00105             }
00106 
00112         bool operator <= ( const uint128_t& rhs ) const
00113             { 
00114                 if( _high < rhs._high )
00115                     return true;
00116                 if( _high > rhs._high )
00117                     return false;
00118                 return _low <= rhs._low; 
00119             }
00120 
00126         bool operator >= ( const uint128_t& rhs ) const
00127             { 
00128                 if( _high > rhs._high )
00129                     return true;
00130                 if( _high < rhs._high )
00131                     return false;
00132                 return _low >= rhs._low; 
00133             }
00134 
00136         uint128_t& operator ++()
00137             { 
00138                 ++_low;
00139                 if( !_low )
00140                     ++_high;
00141                 
00142                 return *this;
00143             }
00144         
00146         uint128_t& operator --()
00147             { 
00148                 if( !_low )
00149                     --_high;
00150                 --_low;
00151                 return *this;
00152             }
00153 
00155         const uint64_t& low() const { return _low; }
00157         const uint64_t& high() const { return _high; }
00158 
00160         uint64_t& low() { return _low; }
00162         uint64_t& high() { return _high; }
00163 
00165         std::string getShortString() const
00166             {
00167                 std::stringstream stream;
00168                 stream << std::hex << _high << _low;
00169                 const std::string str = stream.str();
00170                 return str.substr( 0, 3 ) + ".." +
00171                     str.substr( str.length() - 3, std::string::npos );
00172             }
00173     private:
00174         uint64_t _high;
00175         uint64_t _low;
00176     };
00177 
00179     inline std::ostream& operator << ( std::ostream& os, const uint128_t& id )
00180     {
00181         if( id.high() == 0 )
00182             os << std::hex << id.low() << std::dec;
00183         else
00184             os << std::hex << id.high() << ':' << id.low() << std::dec;
00185         return os;
00186     }
00187 
00189     inline uint128_t operator+ ( const uint128_t& a, const uint64_t& b ) 
00190     {
00191         uint128_t result = a;
00192         result.low() += b;
00193         if( result.low() < a.low( ))
00194             ++result.high();
00195         return result;
00196     };
00197 
00199     inline uint128_t operator- ( const uint128_t& a, const uint64_t& b ) 
00200     {
00201         uint128_t result = a;
00202         result.low() -= b;
00203         if( result.low() > a.low( ))
00204             --result.high();
00205         return result;
00206     };
00207 
00208 }
00209 }
00210 
00211 #endif // COBASE_UINT128_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3