Equalizer  1.2.1
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         explicit 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         uint128_t& operator = ( const uint64_t rhs )
00066             {
00067                 _high = 0;
00068                 _low = rhs;
00069                 return *this;
00070             }
00071 
00073         COBASE_API uint128_t& operator = ( const std::string& from );
00074 
00079         bool operator == ( const uint128_t& rhs ) const
00080             { return _high == rhs._high && _low == rhs._low; }
00081 
00086         bool operator != ( const uint128_t& rhs ) const
00087             { return _high != rhs._high || _low != rhs._low; }
00088 
00093         bool operator < ( const uint128_t& rhs ) const
00094             { 
00095                 if( _high < rhs._high )
00096                     return true;
00097                 if( _high > rhs._high )
00098                     return false;
00099                 return _low < rhs._low; 
00100             }
00101 
00106         bool operator > ( const uint128_t& rhs ) const
00107             { 
00108                 if( _high > rhs._high )
00109                     return true;
00110                 if( _high < rhs._high )
00111                     return false;
00112                 return _low > rhs._low; 
00113             }
00114 
00120         bool operator <= ( const uint128_t& rhs ) const
00121             { 
00122                 if( _high < rhs._high )
00123                     return true;
00124                 if( _high > rhs._high )
00125                     return false;
00126                 return _low <= rhs._low; 
00127             }
00128 
00134         bool operator >= ( const uint128_t& rhs ) const
00135             { 
00136                 if( _high > rhs._high )
00137                     return true;
00138                 if( _high < rhs._high )
00139                     return false;
00140                 return _low >= rhs._low; 
00141             }
00142 
00144         uint128_t& operator ++()
00145             { 
00146                 ++_low;
00147                 if( !_low )
00148                     ++_high;
00149                 
00150                 return *this;
00151             }
00152         
00154         uint128_t& operator --()
00155             { 
00156                 if( !_low )
00157                     --_high;
00158                 --_low;
00159                 return *this;
00160             }
00161 
00163         const uint64_t& low() const { return _low; }
00165         const uint64_t& high() const { return _high; }
00166 
00168         uint64_t& low() { return _low; }
00170         uint64_t& high() { return _high; }
00171 
00173         std::string getShortString() const
00174             {
00175                 std::stringstream stream;
00176                 stream << std::hex << _high << _low;
00177                 const std::string str = stream.str();
00178                 return str.substr( 0, 3 ) + ".." +
00179                     str.substr( str.length() - 3, std::string::npos );
00180             }
00181 
00183         static COBASE_API const uint128_t ZERO;
00184 
00185     private:
00186         uint64_t _high;
00187         uint64_t _low;
00188     };
00189 
00191     inline std::ostream& operator << ( std::ostream& os, const uint128_t& id )
00192     {
00193         if( id.high() == 0 )
00194             os << std::hex << id.low() << std::dec;
00195         else
00196             os << std::hex << id.high() << ':' << id.low() << std::dec;
00197         return os;
00198     }
00199 
00201     inline uint128_t operator+ ( const uint128_t& a, const uint64_t& b ) 
00202     {
00203         uint128_t result = a;
00204         result.low() += b;
00205         if( result.low() < a.low( ))
00206             ++result.high();
00207         return result;
00208     };
00209 
00211     inline uint128_t operator- ( const uint128_t& a, const uint64_t& b ) 
00212     {
00213         uint128_t result = a;
00214         result.low() -= b;
00215         if( result.low() > a.low( ))
00216             --result.high();
00217         return result;
00218     };
00219 
00221     inline uint128_t operator & ( const uint128_t& a, const uint128_t& b )
00222     {
00223         uint128_t result = a;
00224         result.low() &= b.low();
00225         result.high() &= b.high();
00226         return result;
00227     }
00228 
00230     inline uint128_t operator | ( const uint128_t& a, const uint128_t& b )
00231     {
00232         uint128_t result = a;
00233         result.low() |= b.low();
00234         result.high() |= b.high();
00235         return result;
00236     }
00237 
00238 }
00239 }
00240 
00241 #endif // COBASE_UINT128_H
Generated on Fri Jun 8 2012 15:44:32 for Equalizer 1.2.1 by  doxygen 1.8.0