DNA Calib 1.1
Project brief
CharOutputStreamBuf.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#ifdef _MSC_VER
6 #pragma warning(push)
7 #pragma warning(disable : 4365 4987)
8#endif
9#include <array>
10#include <streambuf>
11#ifdef _MSC_VER
12 #pragma warning(pop)
13#endif
14
15namespace terse {
16
17template<class TStream, std::size_t BufferSize = 4096ul>
18class CharOutputStreamBuf : public std::streambuf {
19 public:
20 using PosType = std::streambuf::pos_type;
21 using OffType = std::streambuf::off_type;
22 using CharType = std::streambuf::char_type;
23 using IntType = std::streambuf::int_type;
24 using TraitsType = std::streambuf::traits_type;
25
26 public:
27 explicit CharOutputStreamBuf(TStream* stream_) : stream{stream_}, buffer{} {
28 setp(buffer.data(), buffer.data() + buffer.size() - 1ul);
29 }
30
32 sync();
33 }
34
35 std::streamsize xsputn(const CharType* source, std::streamsize size) override {
36 // Write a sequence of characters
37 if (size <= 0) {
38 return 0;
39 }
40
41 // If there's space, write data into buffer
42 const std::ptrdiff_t spaceLeft = epptr() - pptr();
43 if (size < spaceLeft) {
44 std::memcpy(pptr(), source, static_cast<std::size_t>(size));
45 pbump(static_cast<IntType>(size));
46 return size;
47 }
48
49 // No space in buffer, flush it first, and write directly into stream
50 if (sync()) {
51 return 0;
52 }
53 return static_cast<std::streamsize>(stream->write(source, static_cast<std::size_t>(size)));
54 }
55
56 IntType overflow(IntType value) override {
57 // Write a single character
58 if (value == TraitsType::eof()) {
59 return value;
60 }
61
62 const CharType data = static_cast<CharType>(value);
63 *pptr() = data;
64 pbump(1);
65 return (sync() ? TraitsType::eof() : value);
66 }
67
68 IntType sync() override {
69 const std::ptrdiff_t diff = pptr() - pbase();
70 if (diff > 0) {
71 const std::size_t bytesToWrite = static_cast<std::size_t>(diff);
72 const std::size_t bytesWritten = stream->write(buffer.data(), bytesToWrite);
73 pbump(static_cast<IntType>(-diff));
74 return (bytesToWrite == bytesWritten ? 0 : -1);
75 }
76 return 0;
77 }
78
79 private:
80 TStream* stream;
81 std::array<char, BufferSize> buffer;
82
83};
84
85} // namespace terse
Definition: CharOutputStreamBuf.h:18
~CharOutputStreamBuf()
Definition: CharOutputStreamBuf.h:31
std::streambuf::char_type CharType
Definition: CharOutputStreamBuf.h:22
std::streambuf::traits_type TraitsType
Definition: CharOutputStreamBuf.h:24
IntType overflow(IntType value) override
Definition: CharOutputStreamBuf.h:56
CharOutputStreamBuf(TStream *stream_)
Definition: CharOutputStreamBuf.h:27
std::array< char, BufferSize > buffer
Definition: CharOutputStreamBuf.h:81
std::streambuf::pos_type PosType
Definition: CharOutputStreamBuf.h:20
IntType sync() override
Definition: CharOutputStreamBuf.h:68
std::streambuf::int_type IntType
Definition: CharOutputStreamBuf.h:23
std::streamsize xsputn(const CharType *source, std::streamsize size) override
Definition: CharOutputStreamBuf.h:35
std::streambuf::off_type OffType
Definition: CharOutputStreamBuf.h:21
TStream * stream
Definition: CharOutputStreamBuf.h:80
Definition: Archive.h:14