DynExp
Highly flexible laboratory automation for dynamically changing experiments.
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 
13 namespace 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.
Definition: circularbuf.cpp:76
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,...
Definition: circularbuf.h:161
circularbuf(size_t size)
Constructs a new circularbuf instance with a buffer of size size. The put area will span the entire b...
Definition: circularbuf.cpp:8
pos_type gtellp() const noexcept
Indicates the get pointer's position within the get area.
Definition: circularbuf.cpp:34
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...
Definition: circularbuf.cpp:71
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...
Definition: circularbuf.cpp:52
size_t gsize()
Returns the size of the get area. Not const since sync() needs to be called to get correct size after...
Definition: circularbuf.cpp:22
void clear()
Resets the put areas to the entire area of buffer. The get area will be empty. The get and put pointe...
Definition: circularbuf.cpp:44
virtual int_type underflow() override
Refer to documentation of std::streambuf.
Definition: circularbuf.cpp:95
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.
Definition: circularbuf.cpp:29
pos_type to_pos_type(const T value)
Converts an unsigned, integral number type to pos_type.
Definition: circularbuf.h:143
bool empty()
Indicates whether characters can be read from the get area. Not const since sync() needs to be called...
Definition: circularbuf.cpp:15
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.
Definition: circularbuf.cpp:39
char CharT
Data type of a single character stored in the buffer.
Definition: circularbuf.h:25
std::vector< CharT > buffer
The buffer itself.
Definition: circularbuf.h:154
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...
Definition: circularbuf.cpp:7