ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 5/12/21.
4//
5
6#ifndef TTG_SERIALIZATION_STREAM_H
7#define TTG_SERIALIZATION_STREAM_H
8
9#include <streambuf>
10#include <cstring>
11
12namespace ttg::detail {
13
15 class counting_streambuf : public std::streambuf {
16 public:
17 using std::streambuf::streambuf;
18
20 size_t size() const { return size_; }
21
22 protected:
23 std::streamsize xsputn(const char_type* s, std::streamsize n) override {
24 this->size_ += n;
25 return n;
26 }
27
28 private:
29 size_t size_ = 0;
30 };
31
33 class iovec_ostreambuf : public std::streambuf {
34 public:
35 using std::streambuf::streambuf;
36
37 const auto& iovec() const { return iovec_; };
38
39 protected:
40 std::streamsize xsputn(const char_type* s, std::streamsize n) override {
41 iovec_.emplace_back(s, n);
42 return n;
43 }
44
45 private:
46 std::vector<std::pair<const void*, std::size_t>> iovec_ = {};
47 };
48
50 class iovec_istreambuf : public std::streambuf {
51 public:
52 using std::streambuf::streambuf;
53
54 iovec_istreambuf(const std::vector<std::pair<const void*, std::size_t>>& iovec) : iovec_(iovec) {}
55
56 protected:
57 std::streamsize xsgetn(char_type* s, std::streamsize max_n) override {
58 const auto n = iovec_[current_item_].second;
59 if (n > max_n)
60 throw std::out_of_range("iovec_istreambuf::xsgetn(dest, max_n): actual size of data exceeds max_n");
61 const char* ptr = static_cast<const char*>(iovec_[current_item_].first);
62 std::copy(ptr, ptr + n, s);
63 return n;
64 }
65
66 private:
67 std::size_t current_item_ = 0;
68 const std::vector<std::pair<const void*, std::size_t>>& iovec_;
69 };
70
72 class byte_ostreambuf : public std::streambuf {
73 public:
74 using std::streambuf::streambuf;
75
76 byte_ostreambuf(char_type* buffer, std::streamsize buffer_size = std::numeric_limits<std::streamsize>::max()) : buffer_(buffer), cursor_(buffer_), buffer_size_(buffer_size) {}
77
78 // hides basic_streambuf::sputn so can avoid the virtual function dispatch if the compiler is not aggressive enough
79 std::streamsize sputn(const char_type* s, std::streamsize n) noexcept {
80 return this->xsputn(s, n);
81 }
82
83 std::streamsize xsputn(const char_type* s, std::streamsize n) noexcept override final {
84 assert((cursor_ - buffer_) + n <= buffer_size_);
85 std::memcpy(cursor_, s, n * sizeof(char_type));
86 cursor_ += n;
87 return n;
88 }
89
91 std::streamsize size() const noexcept {
92 return cursor_ - buffer_;
93 }
94
95 private:
96 char_type* buffer_;
97 char_type* cursor_; // current location in buffer_
98 std::streamsize buffer_size_;
99 };
100
102 class byte_istreambuf : public std::streambuf {
103 public:
104 using std::streambuf::streambuf;
105
106 byte_istreambuf(const char_type* buffer, std::size_t buffer_size = std::numeric_limits<std::size_t>::max()) : buffer_(buffer), cursor_(buffer_), buffer_size_(buffer_size) {}
107
108 // hides basic_streambuf::sgetn so can avoid the virtual function dispatch if the compiler is not aggressive enough
109 std::streamsize sgetn(char_type* s, std::streamsize n) noexcept {
110 return this->xsgetn(s, n);
111 }
112
113 std::streamsize xsgetn(char_type* s, std::streamsize max_n) noexcept override final {
114 const auto n_to_read = std::min(buffer_size_ - (cursor_ - buffer_), max_n);
115 std::memcpy(s, cursor_, n_to_read * sizeof(char_type));
116 cursor_ += n_to_read;
117 return n_to_read;
118 }
119
121 std::streamsize size() const noexcept {
122 return cursor_ - buffer_;
123 }
124
125 private:
126 const char_type* buffer_;
127 const char_type* cursor_; // current location in buffer_
128 std::streamsize buffer_size_;
129 };
130
131} // namespace ttg::detail
132
133#endif // TTG_SERIALIZATION_STREAM_H
streambuf that writes bytes to a buffer in memory
Definition stream.h:102
byte_istreambuf(const char_type *buffer, std::size_t buffer_size=std::numeric_limits< std::size_t >::max())
Definition stream.h:106
std::streamsize xsgetn(char_type *s, std::streamsize max_n) noexcept override final
Definition stream.h:113
std::streamsize sgetn(char_type *s, std::streamsize n) noexcept
Definition stream.h:109
std::streamsize size() const noexcept
number of characters read from the buffer
Definition stream.h:121
streambuf that writes bytes to a buffer in memory
Definition stream.h:72
std::streamsize xsputn(const char_type *s, std::streamsize n) noexcept override final
Definition stream.h:83
std::streamsize sputn(const char_type *s, std::streamsize n) noexcept
Definition stream.h:79
byte_ostreambuf(char_type *buffer, std::streamsize buffer_size=std::numeric_limits< std::streamsize >::max())
Definition stream.h:76
std::streamsize size() const noexcept
number of characters written to the buffer
Definition stream.h:91
streambuf that counts bytes
Definition stream.h:15
std::streamsize xsputn(const char_type *s, std::streamsize n) override
Definition stream.h:23
streambuf that reads vector of address-size pairs
Definition stream.h:50
std::streamsize xsgetn(char_type *s, std::streamsize max_n) override
Definition stream.h:57
iovec_istreambuf(const std::vector< std::pair< const void *, std::size_t > > &iovec)
Definition stream.h:54
streambuf that records vector of address-size pairs
Definition stream.h:33
const auto & iovec() const
Definition stream.h:37
std::streamsize xsputn(const char_type *s, std::streamsize n) override
Definition stream.h:40