Audacity 3.2.0
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
BufferedStreamReader Class Reference

A facade-like class, that implements buffered reading from the underlying data stream. More...

#include <BufferedStreamReader.h>

Inheritance diagram for BufferedStreamReader:
[legend]

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ BufferedStreamReader()

BufferedStreamReader::BufferedStreamReader ( size_t  bufferSize = 4096)
explicit

Definition at line 17 of file BufferedStreamReader.cpp.

18 : mBufferSize(std::max(bufferSize, RequiredAlignment))
19{
21
22 auto ptr = static_cast<void*>(mBufferData.data());
23 auto space = mBufferData.size();
24
25 std::align(RequiredAlignment, mBufferSize, ptr, space);
26
27 mBufferStart = static_cast<uint8_t*>(ptr);
28}
static constexpr size_t RequiredAlignment
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 bound...

References mBufferData, mBufferSize, mBufferStart, and RequiredAlignment.

Member Function Documentation

◆ Read()

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.

31{
32 size_t bytesWritten = 0;
33
34 while (maxBytes > 0)
35 {
37 {
38 if (!HandleUnderflow())
39 return bytesWritten;
40 }
41
42 const size_t availableBytes = mCurrentBytes - mCurrentIndex;
43 const size_t bytesToRead = std::min(maxBytes, availableBytes);
44
45 std::memcpy(static_cast<uint8_t*>(buffer) + bytesWritten, mBufferStart + mCurrentIndex, bytesToRead);
46
47 maxBytes -= bytesToRead;
48 bytesWritten += bytesToRead;
49 mCurrentIndex += bytesToRead;
50 }
51
52 return bytesWritten;
53}
int min(int a, int b)

References mBufferStart, mCurrentBytes, mCurrentIndex, and min().

Referenced by ProjectSerializer::Decode().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mBufferData

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> 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().

◆ mBufferSize

size_t BufferedStreamReader::mBufferSize

Definition at line 108 of file BufferedStreamReader.h.

Referenced by BufferedStreamReader().

◆ mBufferStart

uint8_t* BufferedStreamReader::mBufferStart

Definition at line 107 of file BufferedStreamReader.h.

Referenced by BufferedStreamReader(), and Read().

◆ mCurrentBytes

size_t BufferedStreamReader::mCurrentBytes { 0 }

Definition at line 111 of file BufferedStreamReader.h.

Referenced by Read().

◆ mCurrentIndex

size_t BufferedStreamReader::mCurrentIndex { 0 }

Definition at line 110 of file BufferedStreamReader.h.

Referenced by Read().

◆ RequiredAlignment

constexpr size_t BufferedStreamReader::RequiredAlignment = 8
staticconstexpr

Definition at line 26 of file BufferedStreamReader.h.

Referenced by BufferedStreamReader().


The documentation for this class was generated from the following files: