Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2002-11-24 03:46:20


Hi,

I have developed a simple cast function which I found very useful. Here it is:

        template< typename T >
        inline T offset_cast( void* p, unsigned int offset=0 )
        {
                return reinterpret_cast< T >( static_cast<unsigned char*>( p )+offset );
        }
        
        template< typename T >
        inline T offset_cast( const void* p, unsigned int offset=0 )
        {
                return reinterpret_cast< T >( static_cast<const unsigned char*>( p )+offset );
        }

Its purposes is to simplify a mapping of C-like structure onto binary data. Here is
the example: Assume thath you have a network packet in raw format, and you want
to read IP-header information. You could do following:

        ip_header* ip=offset_cast<ip_header>( packet, ip_header_offset );

instead of ugly pointer arithmetic.

Is there any interest for something like this in Boost?

Pavol


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk