Class: cUID

Declaration: NPTrackingTools.h[edit]

namespace Core
{
    /// <summary>
    /// A platform-neutral 128-bit universal identifier. It is essentially guaranteed to never
    /// generate the same ID twice.
    /// </summary>
    class cUID
    {
    public:
        typedef unsigned long long int uint64;

        /// <summary>
        /// Create a default UID. In order to create a UID that has a valid unique identifier you
        /// must call Generate().
        /// </summary>
        cUID() : mHighBits( 0 ), mLowBits( 0 ) { }

        cUID( uint64 high, uint64 low ) : mHighBits( high ), mLowBits( low ) { }
        cUID( const cUID & obj ) : mHighBits( obj.mHighBits ), mLowBits( obj.mLowBits ) { }
        cUID&           operator=( const cUID & rhs ) { mHighBits = rhs.mHighBits; mLowBits = rhs.mLowBits; return *this; }

        /// <summary>
        /// Set the value of the UID from two long integer values. It is up to the caller to ensure that
        /// the resulting UID is unique.
        /// </summary>
        void            SetValue( uint64 highBits, uint64 lowBits ) { mHighBits = highBits; mLowBits = lowBits; }

        /// <summary>Get the low 64 bits of the UID.</summary>
        uint64          LowBits() const { return mLowBits; }

        /// <summary>Get the high 64 bits of the UID.</summary>
        uint64          HighBits() const { return mHighBits; }

        /// <summary>Returns true if the ID is valid.</summary>
        bool            Valid() const { return ( mHighBits != 0 && mLowBits != 0 ); }

        /// <summary>Generate a new UID value.</summary>
        static cUID     Generate();

        //====================================================================================
        // Comparison operators
        //====================================================================================

        bool            operator<( const cUID & rhs ) const
        {
            return ( ( mHighBits < rhs.mHighBits ) ? true : ( mHighBits == rhs.mHighBits ? ( mLowBits < rhs.mLowBits ) : false ) );
        }
        bool            operator<=( const cUID & rhs ) const
        {
            return ( ( mHighBits < rhs.mHighBits ) ? true : ( mHighBits == rhs.mHighBits ? ( mLowBits <= rhs.mLowBits ) : false ) );
        }
        bool            operator>( const cUID & rhs ) const { return !( *this <= rhs ); }
        bool            operator>=( const cUID & rhs ) const { return !( *this < rhs ); }
        bool            operator==( const cUID & rhs ) const
        {
            return ( ( mHighBits == rhs.mHighBits ) && ( mLowBits == rhs.mLowBits ) );
        }
        bool            operator!=( const cUID & rhs ) const { return !( *this == rhs ); }

        //====================================================================================
        // Constants
        //====================================================================================

        static const cUID Invalid;

    private:
        uint64          mHighBits;
        uint64          mLowBits;
    };
}
</code>