uuid.h

00001 
00002 /* Copyright (c) 2006-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQBASE_UUID_H
00019 #define EQBASE_UUID_H
00020 
00021 #include <eq/base/hash.h>
00022 #include <eq/base/log.h>
00023 
00024 #ifdef WIN32
00025 #  include <rpc.h>
00026 #  ifdef __CYGWIN__
00027 #    include <byteswap.h>
00028 #    define _byteswap_ushort bswap_16
00029 #    define _byteswap_ulong  bswap_32
00030 #  endif
00031 #elif defined (NetBSD) || defined (FreeBSD)
00032 #  include <uuid.h>
00033 #else
00034 #  include <uuid/uuid.h>
00035 #endif
00036 
00037 namespace eq
00038 {
00039 namespace base
00040 {
00046     class EQ_EXPORT UUID
00047     {
00048 #ifdef WIN32
00049     public:
00051         typedef ::UUID Data;
00052 
00053         UUID( const bool generate = false )
00054             { generate ? UuidCreate( &_id ) : UuidCreateNil( &_id ); }
00055         UUID( const UUID& from ) { _id = from._id; }
00056         UUID( const Data& from )   { _id = from; }
00057 
00058         void getData( Data& data ) const { data = _id; }
00059 
00060         UUID& operator = ( const UUID& from )
00061             {
00062                 _id = from._id;
00063                 return *this;
00064             }
00065         
00066         UUID& operator = ( const std::string& from )
00067             {
00068                 UuidFromString( (unsigned char*)from.c_str(), &_id );
00069                 return *this;
00070             }
00071         
00072         bool operator == ( const UUID& rhs ) const
00073             { 
00074                 RPC_STATUS status; 
00075                 return ( UuidEqual( const_cast< ::UUID* >( &_id ), 
00076                                     const_cast< ::UUID* >( &rhs._id ),
00077                                     &status ) == TRUE );
00078             }
00079         bool operator != ( const UUID& rhs ) const
00080             { 
00081                 RPC_STATUS status; 
00082                 return ( UuidEqual( const_cast< ::UUID* >( &_id ), 
00083                                     const_cast< ::UUID* >( &rhs._id ),
00084                                     &status ) == FALSE );
00085             }
00086 
00087         bool operator <  ( const UUID& rhs ) const
00088             { 
00089                 RPC_STATUS status; 
00090                 return UuidCompare( const_cast< ::UUID* >( &_id ), 
00091                                     const_cast< ::UUID* >( &rhs._id ),
00092                                     &status ) < 0;
00093             }
00094         bool operator >  ( const UUID& rhs ) const
00095             { 
00096                 RPC_STATUS status; 
00097                 return UuidCompare( const_cast< ::UUID* >( &_id ), 
00098                                     const_cast< ::UUID* >( &rhs._id ),
00099                                     &status ) > 0;
00100             }
00101 
00102         bool operator ! () const 
00103             {
00104                 RPC_STATUS status; 
00105                 return ( UuidIsNil( const_cast< ::UUID* >( &_id ), &status )==TRUE);
00106             }
00107 
00108         void convertToNetwork()
00109             { 
00110                 _id.Data1 = _byteswap_ulong( _id.Data1 ); 
00111                 _id.Data2 = _byteswap_ushort( _id.Data2 );
00112                 _id.Data3 = _byteswap_ushort( _id.Data3 );
00113             }
00114         void convertToHost() { convertToNetwork(); }
00115 
00116     private:
00117         ::UUID _id;
00118 #else // WIN32
00119     public:
00121         struct Data
00122         {
00123             uuid_t id;
00124         };
00125 
00132         UUID( const bool generate = false )
00133             { generate ? uuid_generate( _id ) : uuid_clear( _id ); }
00134 
00136         UUID( const UUID& from ) { uuid_copy( _id, from._id ); }
00137 
00139         UUID( const Data& from )   { uuid_copy( _id, from.id ); }
00140 
00142         void getData( Data& data ) const { uuid_copy( data.id, _id ); }
00143 
00145         UUID& operator = ( const UUID& from )
00146             {
00147                 uuid_copy( _id, from._id );
00148                 return *this;
00149             }
00150 
00152         UUID& operator = ( const std::string& from )
00153             {
00154                 uuid_parse( from.c_str(), _id );
00155                 return *this;
00156             }
00157         
00159         bool operator == ( const UUID& rhs ) const
00160             { return uuid_compare( _id, rhs._id ) == 0; }
00161 
00163         bool operator != ( const UUID& rhs ) const
00164             { return uuid_compare( _id, rhs._id ) != 0; }
00165 
00167         bool operator <  ( const UUID& rhs ) const
00168             { return uuid_compare( _id, rhs._id ) < 0; }
00169 
00171         bool operator >  ( const UUID& rhs ) const
00172             { return uuid_compare( _id, rhs._id ) > 0; }
00173 
00175         bool operator ! () const { return uuid_is_null( _id ); }
00176 
00178         void convertToNetwork() {}
00179 
00181         void convertToHost() {}
00182 
00183     private:
00184         uuid_t _id;
00185 #endif // WIN32
00186 
00187     public:
00189         static const UUID ZERO;
00190 
00191     private:
00192         friend std::ostream& operator << ( std::ostream& os, const UUID& id );
00193 #ifdef WIN32_VC
00194         friend size_t stde::hash_compare< eq::base::UUID >::operator() 
00195             ( const eq::base::UUID& key ) const;
00196 #else
00197         friend struct stde::hash< const eq::base::UUID >;
00198 #endif
00199     };
00200 
00202     template<class T> class UUIDHash 
00203         : public stde::hash_map< const UUID, T >
00204     {};
00205 
00207     inline std::ostream& operator << ( std::ostream& os, const UUID& id )
00208     {
00209 #ifdef WIN32
00210         unsigned char* uuid;
00211         UuidToString( const_cast< ::UUID* >( &id._id ), &uuid );
00212         os << uuid;
00213         RpcStringFree( &uuid );
00214 #else
00215         char string[40];
00216         uuid_unparse( id._id, string );
00217         os << string;
00218 #endif
00219         return os;
00220     }
00221 }
00222 }
00223 
00224 #ifdef WIN32_VC
00225 template<>
00226 inline size_t stde::hash_compare< eq::base::UUID >::operator() 
00227     ( const eq::base::UUID& key ) const
00228 {
00229     return key._id.Data1;
00230 }
00231 
00232 template<>
00233 inline size_t stde::hash_value( const eq::base::UUID& key )
00234 {
00235     stde::hash_compare< eq::base::UUID > hash;
00236     return hash( key );
00237 }
00238 
00239 #elif defined (WIN32)
00240 
00241 namespace __gnu_cxx
00242 {
00243     template<> 
00244     struct hash< const eq::base::UUID >
00245     {
00246         size_t operator()( const eq::base::UUID& key ) const
00247         {
00248             return key._id.Data1;
00249         }
00250     };
00251 }
00252 
00253 #else // POSIX
00254 
00255 #  ifdef __GNUC__              // GCC 3.1 and later
00256 namespace __gnu_cxx
00257 #  else //  other compilers
00258 namespace std
00259 #  endif
00260 {
00261     template<> 
00262     struct hash< const eq::base::UUID >
00263     {
00264         size_t operator()( const eq::base::UUID& key ) const
00265         {
00266             return (size_t)(*key._id);
00267         }
00268     };
00269 }
00270 
00271 #endif // WIN32_VC
00272 #endif // EQBASE_NODE_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8