c++ - 这个 C++ 反序列化习惯用法是什么?使用整数 ID 变量调用文件读取器函数作为 `reinterpret_cast<char *>(&id)?`

标签 c++ serialization deserialization bioinformatics seqan

我正在阅读内部的 SeqAn 库(它处理特定于生物学的文件格式和数据结构),我遇到了我不太理解的 c++ 惯用语。

有一个唯一的 id 变量 record.rID,它是一个 __int32。指向它的指针被传递给另一个函数,该函数从文件中读取一堆数据并改变 id。

调用如下:

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

函数实现如下:

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;
}

做一些跟踪,我可以看到 record.rID 在那里被分配,我猜 memcpy(destPtr, buffer + stream._blockOffset, copyLength); 发生在哪里,但我不太了解发生了什么以及如何分配有意义的记录 ID(但是我没有太多处理这种反序列化代码的经验)。

最佳答案

这是一种写入 int 的聪明方法。通过将 record.rID 的地址转换为指向 char 的指针,您可以使用 memcpy 直接向其写入字节。

关于c++ - 这个 C++ 反序列化习惯用法是什么?使用整数 ID 变量调用文件读取器函数作为 `reinterpret_cast<char *>(&id)?`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22289512/

相关文章:

c++ - 如何在 SIGSEGV 之后销毁在 main() 中创建的对象

c++ - 为分数重载运算符 +(使用 lcm)

c# - 为什么我不能在 .NET asmx Web 服务中公开接口(interface)?

c# - 如何使用 XElement 在 C# 中使用 List 序列化对象?

scala - Flink 通用 Avro 解串器 : override getProducedType

c# - 在 C# 中反序列化多个具有相同名称的 XML 元素

c++ - extern和extern “C”用于变量

c++ - 文件访问 -Java 或 C++

c# - OptionalFieldAttribute 真的有效吗?

java - RedisTemplate 导致 Fortify 动态代码评估中的不安全反序列化