сериализация — что это за десериализация C ++? Вызов функции чтения файлов с переменной целочисленного идентификатора как reinterpret_cast & lt; char * & gt; (& amp; id)? `

Я читаю внутреннюю библиотеку SeqAn (которая обрабатывает биологические форматы файлов и структуры данных), и я сталкиваюсь с тем, что должно быть языком c ++, который я не совсем понимаю.

Там есть уникальная переменная id record.rID это __int32. Указатель на него передается другой функции, которая считывает данные из файла и изменяет идентификатор.

Вот звонок:

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); происходит, но я не совсем понимаю, что происходит и как назначается значимый идентификатор записи (но тогда у меня нет большого опыта работы с этим видом кода десериализации).

1

Решение

Это умный способ записи в Int. Приведя адрес record.rID в качестве указателя на символ, вы можете напрямую записать в него байты с помощью memcpy.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector