1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <limits>
#include <streambuf>
#include <type_traits>
#include <vector>
#include "caf/config.hpp"
#include "caf/detail/type_traits.hpp"
namespace caf {
/// The base class for all stream buffer implementations.
template <class CharT = char, class Traits = std::char_traits<CharT>>
class stream_buffer : public std::basic_streambuf<CharT, Traits> {
public:
using base = std::basic_streambuf<CharT, Traits>;
using pos_type = typename base::pos_type;
using off_type = typename base::off_type;
protected:
/// The standard only defines pbump(int), which can overflow on 64-bit
/// architectures. All stream buffer implementations should therefore use
/// these function instead. For a detailed discussion, see:
/// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47921
template <class T = int>
typename std::enable_if<sizeof(T) == 4>::type
safe_pbump(std::streamsize n) {
while (n > std::numeric_limits<int>::max()) {
this->pbump(std::numeric_limits<int>::max());
n -= std::numeric_limits<int>::max();
}
this->pbump(static_cast<int>(n));
}
template <class T = int>
typename std::enable_if<sizeof(T) == 8>::type
safe_pbump(std::streamsize n) {
this->pbump(static_cast<int>(n));
}
// As above, but for the get area.
template <class T = int>
typename std::enable_if<sizeof(T) == 4>::type
safe_gbump(std::streamsize n) {
while (n > std::numeric_limits<int>::max()) {
this->gbump(std::numeric_limits<int>::max());
n -= std::numeric_limits<int>::max();
}
this->gbump(static_cast<int>(n));
}
template <class T = int>
typename std::enable_if<sizeof(T) == 8>::type
safe_gbump(std::streamsize n) {
this->gbump(static_cast<int>(n));
}
pos_type default_seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which) {
auto new_off = pos_type(off_type(-1));
auto get = (which & std::ios_base::in) == std::ios_base::in;
auto put = (which & std::ios_base::out) == std::ios_base::out;
if (!(get || put))
return new_off; // nothing to do
if (get) {
switch (dir) {
default:
return pos_type(off_type(-1));
case std::ios_base::beg:
new_off = 0;
break;
case std::ios_base::cur:
new_off = this->gptr() - this->eback();
break;
case std::ios_base::end:
new_off = this->egptr() - this->eback();
break;
}
new_off += off;
this->setg(this->eback(), this->eback() + new_off, this->egptr());
}
if (put) {
switch (dir) {
default:
return pos_type(off_type(-1));
case std::ios_base::beg:
new_off = 0;
break;
case std::ios_base::cur:
new_off = this->pptr() - this->pbase();
break;
case std::ios_base::end:
new_off = this->egptr() - this->pbase();
break;
}
new_off += off;
this->setp(this->pbase(), this->epptr());
safe_pbump(new_off);
}
return new_off;
}
pos_type default_seekpos(pos_type pos, std::ios_base::openmode which) {
auto get = (which & std::ios_base::in) == std::ios_base::in;
auto put = (which & std::ios_base::out) == std::ios_base::out;
if (!(get || put))
return pos_type(off_type(-1)); // nothing to do
if (get)
this->setg(this->eback(), this->eback() + pos, this->egptr());
if (put) {
this->setp(this->pbase(), this->epptr());
safe_pbump(pos);
}
return pos;
}
};
/// A streambuffer abstraction over a fixed array of bytes. This streambuffer
/// cannot overflow/underflow. Once it has reached its end, attempts to read
/// characters will return `trait_type::eof`.
template <class CharT = char, class Traits = std::char_traits<CharT>>
class arraybuf : public stream_buffer<CharT, Traits> {
public:
using base = std::basic_streambuf<CharT, Traits>;
using char_type = typename base::char_type;
using traits_type = typename base::traits_type;
using int_type = typename base::int_type;
using pos_type = typename base::pos_type;
using off_type = typename base::off_type;
/// Constructs an array streambuffer from a container.
/// @param c A contiguous container.
/// @pre `c.data()` must point to a contiguous sequence of characters having
/// length `c.size()`.
template <
class Container,
class = typename std::enable_if<
detail::has_data_member<Container>::value
&& detail::has_size_member<Container>::value
>::type
>
arraybuf(Container& c)
: arraybuf(const_cast<char_type*>(c.data()), c.size()) {
// nop
}
/// Constructs an array streambuffer from a raw character sequence.
/// @param data A pointer to the first character.
/// @param size The length of the character sequence.
arraybuf(char_type* data, size_t size) {
setbuf(data, static_cast<std::streamsize>(size));
}
// There exists a bug in libstdc++ version < 5: the implementation does not
// provide the necessary move constructors, so we have to roll our own :-/.
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316 for details.
// TODO: remove after having raised the minimum GCC version to 5.
arraybuf(arraybuf&& other) {
this->setg(other.eback(), other.gptr(), other.egptr());
this->setp(other.pptr(), other.epptr());
other.setg(nullptr, nullptr, nullptr);
other.setp(nullptr, nullptr);
}
// TODO: remove after having raised the minimum GCC version to 5.
arraybuf& operator=(arraybuf&& other) {
this->setg(other.eback(), other.gptr(), other.egptr());
this->setp(other.pptr(), other.epptr());
other.setg(nullptr, nullptr, nullptr);
other.setp(nullptr, nullptr);
return *this;
}
protected:
// -- positioning ----------------------------------------------------------
std::basic_streambuf<char_type, Traits>*
setbuf(char_type* s, std::streamsize n) override {
this->setg(s, s, s + n);
this->setp(s, s + n);
return this;
}
pos_type seekpos(pos_type pos,
std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override {
return this->default_seekpos(pos, which);
}
pos_type seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which) override {
return this->default_seekoff(off, dir, which);
}
// -- put area -------------------------------------------------------------
std::streamsize xsputn(const char_type* s, std::streamsize n) override {
auto available = this->epptr() - this->pptr();
auto actual = std::min(n, static_cast<std::streamsize>(available));
std::memcpy(this->pptr(), s,
static_cast<size_t>(actual) * sizeof(char_type));
this->safe_pbump(actual);
return actual;
}
// -- get area -------------------------------------------------------------
std::streamsize xsgetn(char_type* s, std::streamsize n) override {
auto available = this->egptr() - this->gptr();
auto actual = std::min(n, static_cast<std::streamsize>(available));
std::memcpy(s, this->gptr(),
static_cast<size_t>(actual) * sizeof(char_type));
this->safe_gbump(actual);
return actual;
}
};
/// A streambuffer abstraction over a contiguous container. It supports
/// reading in the same style as `arraybuf`, but is unbounded for output.
template <class Container>
class containerbuf
: public stream_buffer<
typename Container::value_type,
std::char_traits<typename Container::value_type>
> {
public:
using base = std::basic_streambuf<
typename Container::value_type,
std::char_traits<typename Container::value_type>
>;
using char_type = typename base::char_type;
using traits_type = typename base::traits_type;
using int_type = typename base::int_type;
using pos_type = typename base::pos_type;
using off_type = typename base::off_type;
/// Constructs a container streambuf.
/// @param c A contiguous container.
template <
class C = Container,
class = typename std::enable_if<
detail::has_data_member<C>::value && detail::has_size_member<C>::value
>::type
>
containerbuf(Container& c) : container_(c) {
// We use a const_cast because C++11 doesn't provide a non-const data()
// overload. Using std::data(c) would be the right way to write this.
auto data = const_cast<char_type*>(c.data());
auto size = static_cast<std::streamsize>(c.size());
setbuf(data, size);
}
// See note in arraybuf(arraybuf&&).
// TODO: remove after having raised the minimum GCC version to 5.
containerbuf(containerbuf&& other)
: container_(other.container_) {
this->setg(other.eback(), other.gptr(), other.egptr());
other.setg(nullptr, nullptr, nullptr);
}
// TODO: remove after having raised the minimum GCC version to 5.
containerbuf& operator=(containerbuf&& other) {
this->setg(other.eback(), other.gptr(), other.egptr());
other.setg(nullptr, nullptr, nullptr);
return *this;
}
// Hides base-class implementation to simplify single-character lookup.
int_type sgetc() {
if (this->gptr() == this->egptr())
return traits_type::eof();
return traits_type::to_int_type(*this->gptr());
}
// Hides base-class implementation to simplify single-character insert
// without a put area.
int_type sputc(char_type c) {
container_.push_back(c);
return c;
}
// Hides base-class implementation to simplify multi-character insert without
// a put area.
std::streamsize sputn(const char_type* s, std::streamsize n) {
return xsputn(s, n);
}
protected:
// -- positioning ----------------------------------------------------------
base* setbuf(char_type* s, std::streamsize n) override {
this->setg(s, s, s + n);
return this;
}
pos_type seekpos(pos_type pos,
std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override {
// We only have a get area, so no put area (= out) operations.
if ((which & std::ios_base::out) == std::ios_base::out)
return pos_type(off_type(-1));
return this->default_seekpos(pos, which);
}
pos_type seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which) override {
// We only have a get area, so no put area (= out) operations.
if ((which & std::ios_base::out) == std::ios_base::out)
return pos_type(off_type(-1));
return this->default_seekoff(off, dir, which);
}
// Synchronizes the get area with the underlying buffer.
int sync() override {
// See note in ctor for const_cast
auto data = const_cast<char_type*>(container_.data());
auto size = static_cast<std::streamsize>(container_.size());
setbuf(data, size);
return 0;
}
// -- get area -------------------------------------------------------------
// We can't get obtain more characters on underflow, so we only optimize
// multi-character sequential reads.
std::streamsize xsgetn(char_type* s, std::streamsize n) override {
auto available = this->egptr() - this->gptr();
auto actual = std::min(n, static_cast<std::streamsize>(available));
std::memcpy(s, this->gptr(),
static_cast<size_t>(actual) * sizeof(char_type));
this->safe_gbump(actual);
return actual;
}
// -- put area -------------------------------------------------------------
// Should never get called, because there is always space in the buffer.
// (But just in case, it does the same thing as sputc.)
int_type overflow(int_type c = traits_type::eof()) final {
if (!traits_type::eq_int_type(c, traits_type::eof()))
container_.push_back(traits_type::to_char_type(c));
return c;
}
std::streamsize xsputn(const char_type* s, std::streamsize n) override {
container_.insert(container_.end(), s, s + n);
return n;
}
private:
Container& container_;
};
using charbuf = arraybuf<char>;
using vectorbuf = containerbuf<std::vector<char>>;
} // namespace caf
|