Honeycomb  0.1
Component-Model Framework
Stream.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 #include "Honey/Math/Alge/Alge.h"
6 
7 namespace honey { namespace net
8 {
9 
11 
35 template<class Alloc = std::allocator<byte>>
37 {
38 public:
39  StreamBuf_(szt maxSize = numeral<szt>().max(), const Alloc& alloc = Alloc()) :
40  _maxSize(maxSize),
41  _buf(alloc)
42  {
43  auto pend = Alge::min(_maxSize, buf_delta);
44  _buf.resize(Alge::max(pend, 1));
45  setg(&_buf[0], &_buf[0], &_buf[0]);
46  setp(&_buf[0], &_buf[0] + pend);
47  }
48 
50  szt size() const { return pptr() - gptr(); }
52  szt maxSize() const { return _maxSize; }
54  ByteBufConst data() const { return ByteBufConst(gptr(), size()); }
55 
57 
64  ByteBuf prepare(szt n) { reserve(n); return ByteBuf(pptr(), n); }
65 
67 
77  void commit(szt n)
78  {
79  if (pptr() + n > epptr()) n = epptr() - pptr();
80  pbump(n);
81  setg(eback(), gptr(), pptr());
82  }
83 
85 
91  void consume(szt n)
92  {
93  if (egptr() < pptr()) setg(&_buf[0], gptr(), pptr());
94  if (gptr() + n > pptr()) n = pptr() - gptr();
95  gbump(n);
96  }
97 
98 protected:
99  static const int buf_delta = 128;
100 
102 
105  int_type underflow()
106  {
107  if (gptr() < pptr())
108  {
109  setg(&_buf[0], gptr(), pptr());
110  return traits_type::to_int_type(*gptr());
111  }
112  else
113  return traits_type::eof();
114  }
115 
117 
123  int_type overflow(int_type c)
124  {
125  if (!traits_type::eq_int_type(c, traits_type::eof()))
126  {
127  if (pptr() == epptr())
128  {
129  szt _bufsize = pptr() - gptr();
130  if (_bufsize < _maxSize && _maxSize - _bufsize < buf_delta)
131  reserve(_maxSize - _bufsize);
132  else
133  reserve(buf_delta);
134  }
135 
136  *pptr() = traits_type::to_char_type(c);
137  pbump(1);
138  return c;
139  }
140 
141  return traits_type::not_eof(c);
142  }
143 
144  void reserve(szt n)
145  {
146  //get current stream positions as offsets
147  szt gnext = gptr() - &_buf[0];
148  szt pnext = pptr() - &_buf[0];
149  szt pend = epptr() - &_buf[0];
150 
151  //check if there is already enough space in the put area
152  if (n <= pend - pnext) return;
153 
154  //shift existing contents of get area to start of buffer
155  if (gnext > 0)
156  {
157  pnext -= gnext;
158  std::memmove(&_buf[0], &_buf[0] + gnext, pnext);
159  }
160 
161  //ensure buffer is large enough to hold at least the specified size
162  if (n > pend - pnext)
163  {
164  if (n <= _maxSize && pnext <= _maxSize - n)
165  {
166  pend = pnext + n;
167  _buf.resize(Alge::max(pend, 1));
168  }
169  else
170  throw std::length_error("buffer too large");
171  }
172 
173  //update stream positions
174  setg(&_buf[0], &_buf[0], &_buf[0] + pnext);
175  setp(&_buf[0] + pnext, &_buf[0] + pend);
176  }
177 
178 private:
179  szt _maxSize;
180  vector<byte, Alloc> _buf;
181 };
182 
184 
185 } }
automatically resizable buffer class based on ByteStreamBuf
Definition: Stream.h:36
int_type overflow(int_type c)
Override std::streambuf behaviour.
Definition: Stream.h:123
byte * egptr() const
Definition: ByteStream.h:46
A contiguous region of referenced (not owned by object) memory.
Definition: Buffer.h:17
static const int buf_delta
Definition: Stream.h:99
byte * epptr() const
Definition: ByteStream.h:51
StreamBuf_(szt maxSize=numeral< szt >().max(), const Alloc &alloc=Alloc())
Definition: Stream.h:39
Buffer< const byte > ByteBufConst
Definition: Bytes.h:21
StreamBuf_ StreamBuf
Definition: Stream.h:183
Inherit to declare that class is not copyable.
Definition: Meta.h:286
void reserve(szt n)
Definition: Stream.h:144
byte * gptr() const
Definition: ByteStream.h:45
static std::common_type< Num, Num2 >::type max(Num a, Num2 b)
Get the maximum of two numbers.
Definition: Alge.h:94
static std::common_type< Num, Num2 >::type min(Num a, Num2 b)
Get the minimum of two numbers.
Definition: Alge.h:89
void setg(byte *gbeg, byte *gnext, byte *gend)
Definition: ByteStream.h:47
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
T * alloc(szt count=1)
Allocate memory for count number of T objects. Objects are not constructed.
Definition: Allocator.h:31
Buffer< byte > ByteBuf
A buffer of bytes.
Definition: Bytes.h:20
void setp(byte *new_pbase, byte *new_epptr)
Definition: ByteStream.h:52
ByteBuf prepare(szt n)
Get a buffer that represents the output sequence with the given size.
Definition: Stream.h:64
int_type underflow()
Override std::streambuf behaviour.
Definition: Stream.h:105
ByteBufConst data() const
Get the data that represents the input sequence.
Definition: Stream.h:54
void commit(szt n)
Move characters from the output sequence to the input sequence.
Definition: Stream.h:77
szt size() const
Get the size of the input sequence.
Definition: Stream.h:50
byte * pptr() const
Definition: ByteStream.h:50
Global Honeycomb namespace.
void consume(szt n)
Remove characters from the input sequence.
Definition: Stream.h:91
byte * eback() const
Definition: ByteStream.h:44
A stream I/O buffer of bytes, to be passed into ByteStream.
Definition: ByteStream.h:17
szt maxSize() const
Get the max sum of sizes of the input and output sequences.
Definition: Stream.h:52