DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Util::circularbuf Class Reference

Circular stream buffer to be used with the standard library's stream classes. Reading from or writing behind the buffer's end continues at the buffer's beginning. More...

+ Inheritance diagram for Util::circularbuf:

Public Types

using CharT = char
 Data type of a single character stored in the buffer. More...
 

Public Member Functions

 circularbuf (size_t size)
 Constructs a new circularbuf instance with a buffer of size size. The put area will span the entire buffer, the get area will be empty. More...
 
bool empty ()
 Indicates whether characters can be read from the get area. Not const since sync() needs to be called to get correct size after a character has been written recently. More...
 
size_t gsize ()
 Returns the size of the get area. Not const since sync() needs to be called to get correct size after a character has been written recently. More...
 
size_t psize () const noexcept
 Returns the size of the put area. More...
 
pos_type gtellp () const noexcept
 Indicates the get pointer's position within the get area. More...
 
pos_type ptellp () const noexcept
 Indicates the put pointer's position within the put area. More...
 
void clear ()
 Resets the put areas to the entire area of buffer. The get area will be empty. The get and put pointers will point to the beginning of their respective areas. Resets put_overflowed. More...
 
void resize (size_t size)
 Resizes the size of buffer to size. The put area will span the entire buffer. If the buffer is enlarged, the get area will maintain its size. If the buffer is shrunk, the get area will span the entire buffer. If the original put (get) pointer falls behind the new put (get) area's length, it is set to the put (get) area's end, otherwise it will maintain its old position. More...
 

Private Member Functions

std::streamsize avail_get_count () const noexcept
 Returns the amount of characters available to be read from the current get pointer position to the get area's end. More...
 
virtual pos_type seekoff (off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out) override
 Sets the position of the pointer(s) specified by which to the position pos relative to dir. Takes the stream's circularity into account. More...
 
virtual pos_type seekpos (pos_type pos, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out) override
 Sets the position of the pointer(s) specified by which to the absolute position pos. More...
 
template<typename T , std::enable_if_t< std::is_unsigned_v< T >, int > = 0>
pos_type to_pos_type (const T value)
 Converts an unsigned, integral number type to pos_type. More...
 
Overridden

Overridden from standard library. Refer to documentation of std::streambuf.

virtual int_type overflow (int_type c=traits_type::eof()) override
 Refer to documentation of std::streambuf. More...
 
virtual int_type underflow () override
 Refer to documentation of std::streambuf. More...
 
virtual std::streamsize showmanyc () override
 Refer to documentation of std::streambuf. More...
 
virtual int_type pbackfail (int_type c=traits_type::eof()) override
 Refer to documentation of std::streambuf. More...
 
virtual int sync () override
 Expands the get area to the size of the put area. Resets put_overflowed. More...
 

Private Attributes

std::vector< CharTbuffer
 The buffer itself. More...
 
bool put_overflowed
 Indicates wheter an overflow occurred writing to the buffer. If this was the case, subsequent calls to sync() have to enlarge the get area to the put area's size regardless of the current put pointer's position. More...
 

Detailed Description

Circular stream buffer to be used with the standard library's stream classes. Reading from or writing behind the buffer's end continues at the buffer's beginning.

Definition at line 19 of file circularbuf.h.

Member Typedef Documentation

◆ CharT

Data type of a single character stored in the buffer.

Definition at line 25 of file circularbuf.h.

Constructor & Destructor Documentation

◆ circularbuf()

Util::circularbuf::circularbuf ( size_t  size)

Constructs a new circularbuf instance with a buffer of size size. The put area will span the entire buffer, the get area will be empty.

Parameters
sizeInitial buffer size

Definition at line 8 of file circularbuf.cpp.

Member Function Documentation

◆ avail_get_count()

std::streamsize Util::circularbuf::avail_get_count ( ) const
privatenoexcept

Returns the amount of characters available to be read from the current get pointer position to the get area's end.

Returns
Amount of available characters

Definition at line 71 of file circularbuf.cpp.

◆ clear()

void Util::circularbuf::clear ( )

Resets the put areas to the entire area of buffer. The get area will be empty. The get and put pointers will point to the beginning of their respective areas. Resets put_overflowed.

Definition at line 44 of file circularbuf.cpp.

◆ empty()

bool Util::circularbuf::empty ( )

Indicates whether characters can be read from the get area. Not const since sync() needs to be called to get correct size after a character has been written recently.

Returns
Returns true if avail_get_count() returns 0, false otherwise.

Definition at line 15 of file circularbuf.cpp.

◆ gsize()

size_t Util::circularbuf::gsize ( )

Returns the size of the get area. Not const since sync() needs to be called to get correct size after a character has been written recently.

Returns
Get area size

Definition at line 22 of file circularbuf.cpp.

◆ gtellp()

circularbuf::pos_type Util::circularbuf::gtellp ( ) const
noexcept

Indicates the get pointer's position within the get area.

Returns
Returns the distance between the get area's beginning and the get pointer's position.

Definition at line 34 of file circularbuf.cpp.

◆ overflow()

circularbuf::int_type Util::circularbuf::overflow ( int_type  c = traits_type::eof())
overrideprivatevirtual

Refer to documentation of std::streambuf.

Definition at line 76 of file circularbuf.cpp.

◆ pbackfail()

circularbuf::int_type Util::circularbuf::pbackfail ( int_type  c = traits_type::eof())
overrideprivatevirtual

Refer to documentation of std::streambuf.

Definition at line 118 of file circularbuf.cpp.

◆ psize()

size_t Util::circularbuf::psize ( ) const
noexcept

Returns the size of the put area.

Returns
Put area size

Definition at line 29 of file circularbuf.cpp.

◆ ptellp()

circularbuf::pos_type Util::circularbuf::ptellp ( ) const
noexcept

Indicates the put pointer's position within the put area.

Returns
Returns the distance between the put area's beginning and the put pointer's position.

Definition at line 39 of file circularbuf.cpp.

◆ resize()

void Util::circularbuf::resize ( size_t  size)

Resizes the size of buffer to size. The put area will span the entire buffer. If the buffer is enlarged, the get area will maintain its size. If the buffer is shrunk, the get area will span the entire buffer. If the original put (get) pointer falls behind the new put (get) area's length, it is set to the put (get) area's end, otherwise it will maintain its old position.

Parameters
sizeNew buffer size to apply
Exceptions
std::overflow_erroris thrown if size does not fit into int.

Definition at line 52 of file circularbuf.cpp.

◆ seekoff()

circularbuf::pos_type Util::circularbuf::seekoff ( off_type  off,
std::ios_base::seekdir  dir,
std::ios_base::openmode  which = std::ios_base::in | std::ios_base::out 
)
overrideprivatevirtual

Sets the position of the pointer(s) specified by which to the position pos relative to dir. Takes the stream's circularity into account.

Parameters
offDestiny position (relative to dir).
dirSet position relative to stream's beginning (std::ios_base::beg), its end (std::ios_base::end) or the current position (std::ios_base::cur).
whichCombination of flags std::ios_base::in and std::ios_base::out specifying get or put pointers.
Returns
New position pos in case of success, -1 otherwise. If the get and the put pointers are both successfully moved, the new position of the get pointer is returned.

Definition at line 149 of file circularbuf.cpp.

◆ seekpos()

circularbuf::pos_type Util::circularbuf::seekpos ( pos_type  pos,
std::ios_base::openmode  which = std::ios_base::in | std::ios_base::out 
)
overrideprivatevirtual

Sets the position of the pointer(s) specified by which to the absolute position pos.

Parameters
posDestiny position (absolute).
whichCombination of flags std::ios_base::in and std::ios_base::out specifying get or put pointers.
Returns
New position pos in case of success, -1 otherwise.

Definition at line 200 of file circularbuf.cpp.

◆ showmanyc()

std::streamsize Util::circularbuf::showmanyc ( )
overrideprivatevirtual

Refer to documentation of std::streambuf.

Definition at line 110 of file circularbuf.cpp.

◆ sync()

int Util::circularbuf::sync ( )
overrideprivatevirtual

Expands the get area to the size of the put area. Resets put_overflowed.

Returns
Returns 0.

Definition at line 139 of file circularbuf.cpp.

◆ to_pos_type()

template<typename T , std::enable_if_t< std::is_unsigned_v< T >, int > = 0>
pos_type Util::circularbuf::to_pos_type ( const T  value)
inlineprivate

Converts an unsigned, integral number type to pos_type.

Template Parameters
TType to convert from
Parameters
valueValue to convert
Returns
Returns the converted value.
Exceptions
std::overflow_erroris thrown if value is larger than the maximal number storable in pos_type.

Definition at line 143 of file circularbuf.h.

◆ underflow()

circularbuf::int_type Util::circularbuf::underflow ( )
overrideprivatevirtual

Refer to documentation of std::streambuf.

Definition at line 95 of file circularbuf.cpp.

Member Data Documentation

◆ buffer

std::vector<CharT> Util::circularbuf::buffer
private

The buffer itself.

Definition at line 154 of file circularbuf.h.

◆ put_overflowed

bool Util::circularbuf::put_overflowed
private

Indicates wheter an overflow occurred writing to the buffer. If this was the case, subsequent calls to sync() have to enlarge the get area to the put area's size regardless of the current put pointer's position.

Definition at line 161 of file circularbuf.h.


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