what is this C++ deserialization idiom? calling a file-reader function with an integer ID variable as `reinterpret_cast<char *>(&id)?`

StackOverflow https://stackoverflow.com/questions/22289512

Question

I'm reading through the internals SeqAn library (which handles biology-specific file formats and data structures) and I'm coming across what must be a c++ idiom which I don't quite understand.

There's a unique id variable record.rID that is an __int32. A pointer to it gets passed to another function that reads a bunch of data from a file and mutates the id.

Here's the call:

res = streamReadBlock(reinterpret_cast<char *>(&record.rID), stream, 4);

Here's the function implementation:

inline size_t
streamReadBlock(char * target, Stream<Bgzf> & stream, size_t maxLen)
{
    if (!(stream._openMode & OPEN_RDONLY))
        return 0;  // File not open for reading.

    // Memoize number of read bytes and pointer into the buffer target.
    size_t bytesRead = 0;
    char * destPtr = target;

    // Read at most maxLen characters, each loop iteration corresponds to reading the end of the first, the beginning of
    // the last or the whole "middle" buffers.  Of course, the first and only iteration can also only read parts of the
    // first buffer.
    while (bytesRead < maxLen)
    {
        // If there are no more bytes left in the current block then read and decompress the next block.
        int available = stream._blockLength - stream._blockOffset;
        if (available <= 0)
        {
            if (_bgzfReadBlock(stream) != 0)
                return -1;  // Could not read next block.
            available = stream._blockLength - stream._blockOffset;
            if (available <= 0)
                break;
        }

        // Copy out the number of bytes to be read or the number of available bytes in the next buffer, whichever number
        // is smaller.
        int copyLength = std::min(static_cast<int>(maxLen - bytesRead), available);
        char * buffer = &stream._uncompressedBlock[0];
        memcpy(destPtr, buffer + stream._blockOffset, copyLength);

        // Advance to next block.
        stream._blockOffset += copyLength;
        destPtr += copyLength;
        bytesRead += copyLength;
    }

    // If we read to the end of the block above then switch the block address to the next block and mark it as unread.
    if (stream._blockOffset == stream._blockLength)
    {
        stream._blockPosition = tell(stream._file);
        stream._blockOffset = 0;
        stream._blockLength = 0;
    }

    return bytesRead;
}

Doing a bit of tracing, I can see that record.rID is getting assigned in there, I guess where that memcpy(destPtr, buffer + stream._blockOffset, copyLength); occurs, but I don't quite understand what's going on and how a meaningful record id is getting assigned (but then I don't have too much experience dealing with this kind of deserialization code).

Was it helpful?

Solution

It's a clever way of writing to an int. By casting the address of record.rID as a pointer to a char, you may directly write bytes to it with memcpy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top