Audacity 3.2.0
|
A facade-like class, that implements buffered reading from the underlying data stream. More...
#include <BufferedStreamReader.h>
Public Member Functions | |
BufferedStreamReader (size_t bufferSize=4096) | |
size_t | Read (void *buffer, size_t maxBytes) |
Read up to maxBytes into the buffer. Returns the number of bytes read. More... | |
Public Attributes | |
template<typename ValueType > | |
std::enable_if_t< sizeof(ValueType)<=RequiredAlignment, bool > ReadValue(ValueType &value) { constexpr size_t valueSize=sizeof(ValueType);const size_t availableBytes=mCurrentBytes - mCurrentIndex;if(availableBytes< valueSize) return valueSize==Read(&value, valueSize);if constexpr(valueSize==1) value=mBufferStart[mCurrentIndex];else value=UncheckedRead< ValueType >();mCurrentIndex+=valueSize;return true;} bool Eof() const ;int GetC();protected:virtual bool HasMoreData() const=0;virtual size_t ReadData(void *buffer, size_t maxBytes)=0;private:bool HandleUnderflow();template< typename T > T UncheckedRead() noexcept { T result;if((mCurrentIndex % sizeof(T))==0) { const void *ptr=mBufferStart+mCurrentIndex;result=*static_cast< const T * >(ptr);} else { const uint8_t *begin=mBufferStart+mCurrentIndex;const uint8_t *end=begin+sizeof(T);void *out=&result;std::copy(begin, end, static_cast< uint8_t * >(out));} return result;} std::vector< uint8_t > | mBufferData |
Read a single value of ValueType, where sizeof(ValueType) <= 8 and value is aligned to the size boundary. More... | |
uint8_t * | mBufferStart |
size_t | mBufferSize |
size_t | mCurrentIndex { 0 } |
size_t | mCurrentBytes { 0 } |
Static Public Attributes | |
static constexpr size_t | RequiredAlignment = 8 |
A facade-like class, that implements buffered reading from the underlying data stream.
BufferedStreamReader provides optimal read performance for the built-in types of up to 8 bytes long.
Definition at line 23 of file BufferedStreamReader.h.
|
explicit |
Definition at line 17 of file BufferedStreamReader.cpp.
References mBufferData, mBufferSize, mBufferStart, and RequiredAlignment.
size_t BufferedStreamReader::Read | ( | void * | buffer, |
size_t | maxBytes | ||
) |
Read up to maxBytes into the buffer. Returns the number of bytes read.
Definition at line 30 of file BufferedStreamReader.cpp.
References mBufferStart, mCurrentBytes, mCurrentIndex, and min().
Referenced by ProjectSerializer::Decode().
std::enable_if_t<sizeof(ValueType) <= RequiredAlignment, bool> ReadValue(ValueType& value) { constexpr size_t valueSize = sizeof(ValueType); const size_t availableBytes = mCurrentBytes - mCurrentIndex; if (availableBytes < valueSize) return valueSize == Read(&value, valueSize); if constexpr (valueSize == 1) value = mBufferStart[mCurrentIndex]; else value = UncheckedRead<ValueType>(); mCurrentIndex += valueSize; return true; } bool Eof() const; int GetC();protected: virtual bool HasMoreData() const = 0; virtual size_t ReadData(void* buffer, size_t maxBytes) = 0;private: bool HandleUnderflow(); template<typename T> T UncheckedRead() noexcept { T result; if ((mCurrentIndex % sizeof(T)) == 0) { const void* ptr = mBufferStart + mCurrentIndex; result = *static_cast<const T*>(ptr); } else { const uint8_t* begin = mBufferStart + mCurrentIndex; const uint8_t* end = begin + sizeof(T); void* out = &result; std::copy(begin, end, static_cast<uint8_t*>(out)); } return result; } std::vector<uint8_t> BufferedStreamReader::mBufferData |
Read a single value of ValueType, where sizeof(ValueType) <= 8 and value is aligned to the size boundary.
Definition at line 103 of file BufferedStreamReader.h.
Referenced by BufferedStreamReader().
size_t BufferedStreamReader::mBufferSize |
Definition at line 108 of file BufferedStreamReader.h.
Referenced by BufferedStreamReader().
uint8_t* BufferedStreamReader::mBufferStart |
Definition at line 107 of file BufferedStreamReader.h.
Referenced by BufferedStreamReader(), and Read().
size_t BufferedStreamReader::mCurrentBytes { 0 } |
Definition at line 111 of file BufferedStreamReader.h.
Referenced by Read().
size_t BufferedStreamReader::mCurrentIndex { 0 } |
Definition at line 110 of file BufferedStreamReader.h.
Referenced by Read().
|
staticconstexpr |
Definition at line 26 of file BufferedStreamReader.h.
Referenced by BufferedStreamReader().