DNA Calib 1.1
Project brief
Public Types | Public Member Functions | Private Attributes | List of all members
terse::CharInputStreamBuf< TStream, BufferSize > Class Template Reference

#include <CharInputStreamBuf.h>

Inheritance diagram for terse::CharInputStreamBuf< TStream, BufferSize >:
Inheritance graph
Collaboration diagram for terse::CharInputStreamBuf< TStream, BufferSize >:
Collaboration graph

Public Types

using PosType = std::streambuf::pos_type
 
using OffType = std::streambuf::off_type
 
using CharType = std::streambuf::char_type
 
using IntType = std::streambuf::int_type
 
using TraitsType = std::streambuf::traits_type
 

Public Member Functions

 CharInputStreamBuf (TStream *stream_)
 
std::streamsize xsgetn (CharType *destination, std::streamsize size) override
 
IntType underflow () override
 
std::streamsize showmanyc () override
 

Private Attributes

TStream * stream
 
std::array< char, BufferSize > buffer
 

Member Typedef Documentation

◆ CharType

template<class TStream , std::size_t BufferSize = 4096ul>
using terse::CharInputStreamBuf< TStream, BufferSize >::CharType = std::streambuf::char_type

◆ IntType

template<class TStream , std::size_t BufferSize = 4096ul>
using terse::CharInputStreamBuf< TStream, BufferSize >::IntType = std::streambuf::int_type

◆ OffType

template<class TStream , std::size_t BufferSize = 4096ul>
using terse::CharInputStreamBuf< TStream, BufferSize >::OffType = std::streambuf::off_type

◆ PosType

template<class TStream , std::size_t BufferSize = 4096ul>
using terse::CharInputStreamBuf< TStream, BufferSize >::PosType = std::streambuf::pos_type

◆ TraitsType

template<class TStream , std::size_t BufferSize = 4096ul>
using terse::CharInputStreamBuf< TStream, BufferSize >::TraitsType = std::streambuf::traits_type

Constructor & Destructor Documentation

◆ CharInputStreamBuf()

template<class TStream , std::size_t BufferSize = 4096ul>
terse::CharInputStreamBuf< TStream, BufferSize >::CharInputStreamBuf ( TStream *  stream_)
inlineexplicit
27 : stream{stream_}, buffer{} {
28 char* bufferEnd = buffer.data() + buffer.size();
29 setg(bufferEnd, bufferEnd, bufferEnd);
30 }
TStream * stream
Definition: CharInputStreamBuf.h:93
std::array< char, BufferSize > buffer
Definition: CharInputStreamBuf.h:94

References terse::CharInputStreamBuf< TStream, BufferSize >::buffer.

Member Function Documentation

◆ showmanyc()

template<class TStream , std::size_t BufferSize = 4096ul>
std::streamsize terse::CharInputStreamBuf< TStream, BufferSize >::showmanyc ( )
inlineoverride
88 {
89 return static_cast<std::streamsize>(stream->size() - stream->tell());
90 }

References terse::CharInputStreamBuf< TStream, BufferSize >::stream.

◆ underflow()

template<class TStream , std::size_t BufferSize = 4096ul>
IntType terse::CharInputStreamBuf< TStream, BufferSize >::underflow ( )
inlineoverride
62 {
63 if (gptr() < egptr()) {
64 return TraitsType::to_int_type(*gptr());
65 }
66
67 char* base = buffer.data();
68 char* start = base;
69 // Initially eback() == buffer.end() , but if that's not the case, buffer has already been
70 // filled before, in which case first copy the data from the put-back area (located at the end of
71 // the buffer) into the beginning of the buffer, making them the next characters to be read from it.
72 // Put-back area is just one byte in this implementation currently.
73 if (eback() == base) {
74 std::memcpy(base, egptr() - 1ul, 1ul);
75 ++start;
76 }
77
78 // Now fill the rest of the buffer after the put-back area is moved to the beginning of the buffer
79 const std::size_t bytesRead = stream->read(start, buffer.size() - static_cast<std::size_t>(start - base));
80 if (bytesRead == 0ul) {
81 return TraitsType::eof();
82 }
83
84 setg(base, start, start + bytesRead);
85 return TraitsType::to_int_type(*gptr());
86 }

References terse::CharInputStreamBuf< TStream, BufferSize >::buffer, and terse::CharInputStreamBuf< TStream, BufferSize >::stream.

Referenced by terse::CharInputStreamBuf< TStream, BufferSize >::xsgetn().

◆ xsgetn()

template<class TStream , std::size_t BufferSize = 4096ul>
std::streamsize terse::CharInputStreamBuf< TStream, BufferSize >::xsgetn ( CharType destination,
std::streamsize  size 
)
inlineoverride
32 {
33 if (size <= 0) {
34 return 0;
35 }
36
37 std::streamsize bytesNeeded = size;
38
39 // Exhaust buffer contents first
40 if (gptr() < egptr()) {
41 const std::size_t bytesInBuffer = static_cast<std::size_t>(egptr() - gptr());
42 const std::size_t bytesToCopy = std::min(static_cast<std::size_t>(bytesNeeded), bytesInBuffer);
43 std::memcpy(destination, gptr(), bytesToCopy);
44 destination += bytesToCopy;
45 bytesNeeded -= static_cast<std::streamsize>(bytesToCopy);
46 gbump(static_cast<IntType>(bytesToCopy));
47 }
48
49 // If there are still bytes needed (more data than buffer contained), read the rest directly from the stream
50 if (bytesNeeded > 0) {
51 const auto bytesRead =
52 static_cast<std::streamsize>(stream->read(destination, static_cast<std::size_t>(bytesNeeded)));
53 bytesNeeded -= bytesRead;
54 }
55
56 // Refill buffer for subsequent accesses (if possible)
57 underflow();
58
59 return size - bytesNeeded;
60 }
std::streambuf::int_type IntType
Definition: CharInputStreamBuf.h:23
IntType underflow() override
Definition: CharInputStreamBuf.h:62

References terse::CharInputStreamBuf< TStream, BufferSize >::stream, and terse::CharInputStreamBuf< TStream, BufferSize >::underflow().

Member Data Documentation

◆ buffer

template<class TStream , std::size_t BufferSize = 4096ul>
std::array<char, BufferSize> terse::CharInputStreamBuf< TStream, BufferSize >::buffer
private

◆ stream

template<class TStream , std::size_t BufferSize = 4096ul>
TStream* terse::CharInputStreamBuf< TStream, BufferSize >::stream
private

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