Ocean
Loading...
Searching...
No Matches
VectorOutputStream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_SERIALIZATION_VECTOR_OUTPUT_STREAM_H
9#define META_OCEAN_IO_SERIALIZATION_VECTOR_OUTPUT_STREAM_H
10
12
13#include <streambuf>
14
15namespace Ocean
16{
17
18namespace IO
19{
20
21namespace Serialization
22{
23
24/**
25 * This class implements an output stream that writes to an internal vector buffer.
26 * The class provides a memory-based alternative to file or network streams, allowing efficient in-memory buffering of stream data.<br>
27 * This is particularly useful for scenarios where data needs to be serialized to memory before being processed or transmitted, rather than written directly to a file.<br>
28 * The implementation uses a custom stream buffer (VectorStreamBuffer) that stores data in a std::vector, providing automatic memory management and resizing.
29 * @ingroup ioserialization
30 */
31class OCEAN_IO_SERIALIZATION_EXPORT VectorOutputStream : public std::ostream
32{
33 public:
34
35 /**
36 * This class implements a custom stream buffer that stores data in a vector.
37 * The class extends std::streambuf to provide a memory-backed buffer that grows dynamically as data is written.<br>
38 * It supports standard streambuf operations including overflow handling, batch writes via xsputn, and seeking within the buffer.
39 */
40 class OCEAN_IO_SERIALIZATION_EXPORT VectorStreamBuffer : public std::streambuf
41 {
42 public:
43
44 /// Definition of the buffer type storing the stream data.
45 using Buffer = std::vector<char>;
46
47 public:
48
49 /**
50 * Creates a new vector stream buffer with an empty buffer.
51 */
52 VectorStreamBuffer() = default;
53
54 /**
55 * Creates a new vector stream buffer by moving an existing buffer.
56 * @param buffer The buffer to be moved into this stream buffer
57 */
58 explicit VectorStreamBuffer(Buffer&& buffer);
59
60 /**
61 * Creates a new vector stream buffer with a specified initial capacity.
62 * @param capacity The initial reserved capacity of the buffer, in bytes, with range [0, infinity)
63 */
64 explicit VectorStreamBuffer(const size_t capacity);
65
66 /**
67 * Writes a single character to the buffer when the put area is full.
68 * @param character The character to write
69 * @return The character written, or EOF on failure
70 */
71 int_type overflow(int_type character) override;
72
73 /**
74 * Writes a sequence of characters to the buffer.
75 * @param data The pointer to the data to write, must be valid
76 * @param size The number of bytes to write, with range [0, infinity)
77 * @return The number of bytes actually written
78 */
79 std::streamsize xsputn(const char* data, std::streamsize size) override;
80
81 /**
82 * Repositions the stream position indicator using relative offsets.
83 * @param offset The offset to apply, can be positive or negative
84 * @param direction The reference position (beginning, current, or end)
85 * @param mode The open mode (input, output, or both)
86 * @return The new position, or pos_type(off_type(-1)) on failure
87 */
88 pos_type seekoff(off_type offset, std::ios_base::seekdir direction, std::ios_base::openmode mode = std::ios_base::out) override;
89
90 /**
91 * Repositions the stream position indicator to an absolute position.
92 * @param position The absolute position to seek to
93 * @param mode The open mode (input, output, or both)
94 * @return The new position, or pos_type(off_type(-1)) on failure
95 */
96 pos_type seekpos(pos_type position, std::ios_base::openmode mode = std::ios_base::out) override;
97
98 /**
99 * Returns the current size of the buffer.
100 * @return The number of bytes currently stored in the buffer, with range [0, infinity)
101 */
102 size_t size() const;
103
104 /**
105 * Reserves memory for the buffer to avoid reallocations during writing.
106 * @param capacity The desired capacity in bytes, with range [0, infinity)
107 */
108 void reserve(const size_t capacity);
109
110 /**
111 * Returns a const reference to the internal buffer.
112 * @return The const reference to the internal vector buffer
113 */
114 const Buffer& buffer() const;
115
116 /**
117 * Returns a reference to the internal buffer.
118 * @return The reference to the internal vector buffer
119 */
120 Buffer& buffer();
121
122 /**
123 * Clears the buffer content.
124 */
125 void clear();
126
127 protected:
128
129 /// The internal vector storing the stream data.
131
132 /// The current position in the buffer for writing, with range [0, infinity)
133 size_t currentPosition_ = 0;
134 };
135
136 public:
137
138 /**
139 * Creates a new vector output stream with an empty buffer.
140 */
142
143 /**
144 * Creates a new vector output stream with a specified initial buffer capacity.
145 * @param capacity The initial capacity of the buffer, in bytes, with range [0, infinity)
146 */
147 explicit VectorOutputStream(const size_t capacity);
148
149 /**
150 * Returns a pointer to the stream data.
151 * @return The pointer to the internal buffer data, nullptr if the buffer is empty
152 */
153 const void* data() const;
154
155 /**
156 * Returns the current size of the stream.
157 * @return The number of bytes currently stored in the stream, with range [0, infinity)
158 */
159 size_t size() const;
160
161 /**
162 * Clears the stream content.
163 */
164 void clear();
165
166 protected:
167
168 /// The internal stream buffer storing the data.
170};
171
173 buffer_(std::move(buffer))
174{
175 // nothing to do here
176}
177
179{
180 buffer_.reserve(capacity);
181}
182
184{
185 return buffer_.size();
186}
187
188inline void VectorOutputStream::VectorStreamBuffer::reserve(const size_t capacity)
189{
190 buffer_.reserve(capacity);
191}
192
197
202
204{
205 buffer_.clear();
206 currentPosition_ = 0;
207}
208
210 std::ostream(&streamBuffer_)
211{
212 // nothing to do here
213}
214
215inline VectorOutputStream::VectorOutputStream(const size_t capacity) :
216 std::ostream(&streamBuffer_)
217{
218 streamBuffer_.reserve(capacity);
219}
220
221inline const void* VectorOutputStream::data() const
222{
223 return streamBuffer_.buffer().data();
224}
225
226inline size_t VectorOutputStream::size() const
227{
228 return streamBuffer_.size();
229}
230
232{
234}
235
236}
237
238}
239
240}
241
242#endif // META_OCEAN_IO_SERIALIZATION_VECTOR_OUTPUT_STREAM_H
This class implements a custom stream buffer that stores data in a vector.
Definition VectorOutputStream.h:41
pos_type seekoff(off_type offset, std::ios_base::seekdir direction, std::ios_base::openmode mode=std::ios_base::out) override
Repositions the stream position indicator using relative offsets.
int_type overflow(int_type character) override
Writes a single character to the buffer when the put area is full.
void reserve(const size_t capacity)
Reserves memory for the buffer to avoid reallocations during writing.
Definition VectorOutputStream.h:188
size_t size() const
Returns the current size of the buffer.
Definition VectorOutputStream.h:183
const Buffer & buffer() const
Returns a const reference to the internal buffer.
Definition VectorOutputStream.h:193
void clear()
Clears the buffer content.
Definition VectorOutputStream.h:203
std::vector< char > Buffer
Definition of the buffer type storing the stream data.
Definition VectorOutputStream.h:45
Buffer buffer_
The internal vector storing the stream data.
Definition VectorOutputStream.h:130
VectorStreamBuffer()=default
Creates a new vector stream buffer with an empty buffer.
std::streamsize xsputn(const char *data, std::streamsize size) override
Writes a sequence of characters to the buffer.
pos_type seekpos(pos_type position, std::ios_base::openmode mode=std::ios_base::out) override
Repositions the stream position indicator to an absolute position.
This class implements an output stream that writes to an internal vector buffer.
Definition VectorOutputStream.h:32
VectorStreamBuffer streamBuffer_
The internal stream buffer storing the data.
Definition VectorOutputStream.h:169
size_t size() const
Returns the current size of the stream.
Definition VectorOutputStream.h:226
VectorOutputStream()
Creates a new vector output stream with an empty buffer.
Definition VectorOutputStream.h:209
void clear()
Clears the stream content.
Definition VectorOutputStream.h:231
const void * data() const
Returns a pointer to the stream data.
Definition VectorOutputStream.h:221
The namespace covering the entire Ocean framework.
Definition Accessor.h:15