DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
circularbuf.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
9#pragma once
10
11#include <streambuf>
12
13namespace Util
14{
19 class circularbuf : public std::streambuf
20 {
21 public:
25 using CharT = char;
26
32 circularbuf(size_t size);
33
39 bool empty();
40
46 size_t gsize();
47
52 size_t psize() const noexcept;
53
58 pos_type gtellp() const noexcept;
59
64 pos_type ptellp() const noexcept;
65
71 void clear();
72
82 void resize(size_t size);
83
84 private:
90 std::streamsize avail_get_count() const noexcept;
91
96 virtual int_type overflow(int_type c = traits_type::eof()) override;
97 virtual int_type underflow() override;
98 virtual std::streamsize showmanyc() override;
99 virtual int_type pbackfail(int_type c = traits_type::eof()) override;
100
105 virtual int sync() override;
107
119 virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir,
120 std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
121
129 virtual pos_type seekpos(pos_type pos,
130 std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) override;
131
140 template <typename T, std::enable_if_t<
141 std::is_unsigned_v<T>, int> = 0
142 >
143 inline pos_type to_pos_type(const T value)
144 {
145 if (value > static_cast<T>(std::numeric_limits<std::make_signed_t<T>>::max()))
146 throw std::overflow_error("Cannot convert a value into pos_type since this would cause an overflow in circularbuf::to_pos_type().");
147
148 return static_cast<std::make_signed_t<T>>(value);
149 }
150
154 std::vector<CharT> buffer;
155
162 };
163}
Circular stream buffer to be used with the standard library's stream classes. Reading from or writing...
Definition circularbuf.h:20
virtual int_type overflow(int_type c=traits_type::eof()) override
Refer to documentation of std::streambuf.
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.
bool put_overflowed
Indicates wheter an overflow occurred writing to the buffer. If this was the case,...
pos_type gtellp() const noexcept
Indicates the get pointer's position within the get area.
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....
std::streamsize avail_get_count() const noexcept
Returns the amount of characters available to be read from the current get pointer position to the ge...
void resize(size_t size)
Resizes the size of buffer to size. The put area will span the entire buffer. If the buffer is enlarg...
size_t gsize()
Returns the size of the get area. Not const since sync() needs to be called to get correct size after...
void clear()
Resets the put areas to the entire area of buffer. The get area will be empty. The get and put pointe...
virtual int_type underflow() override
Refer to documentation of std::streambuf.
virtual int_type pbackfail(int_type c=traits_type::eof()) override
Refer to documentation of std::streambuf.
size_t psize() const noexcept
Returns the size of the put area.
pos_type to_pos_type(const T value)
Converts an unsigned, integral number type to pos_type.
bool empty()
Indicates whether characters can be read from the get area. Not const since sync() needs to be called...
virtual std::streamsize showmanyc() override
Refer to documentation of std::streambuf.
pos_type ptellp() const noexcept
Indicates the put pointer's position within the put area.
char CharT
Data type of a single character stored in the buffer.
Definition circularbuf.h:25
std::vector< CharT > buffer
The buffer itself.
virtual int sync() override
Expands the get area to the size of the put area. Resets put_overflowed.
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...