我正在阅读内部的 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/