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 * The implementation uses a custom stream buffer (VectorStreamBuffer) that stores data in a std::vector.
28 * @ingroup ioserialization
29 */
30class OCEAN_IO_SERIALIZATION_EXPORT VectorOutputStream : public std::ostream
31{
32 public:
33
34 /**
35 * This class implements a custom stream buffer that stores data in a vector.
36 */
37 class VectorStreamBuffer : public std::streambuf
38 {
39 public:
40
41 /// Definition of the buffer type storing the stream data.
42 using Buffer = std::vector<char>;
43
44 public:
45
46 /**
47 * Creates a new vector stream buffer with an empty buffer.
48 */
49 VectorStreamBuffer() = default;
50
51 /**
52 * Creates a new vector stream buffer by moving an existing buffer.
53 * @param buffer The buffer to be moved into this stream buffer
54 */
55 explicit VectorStreamBuffer(Buffer&& buffer);
56
57 /**
58 * Creates a new vector stream buffer with a specified initial capacity.
59 * @param capacity The initial reserved capacity of the buffer, in bytes, with range [0, infinity)
60 */
61 explicit VectorStreamBuffer(const size_t capacity);
62
63 /**
64 * Writes a single character to the buffer when the put area is full.
65 * @param character The character to write
66 * @return The character written, or EOF on failure
67 */
68 int_type overflow(int_type character) override;
69
70 /**
71 * Writes a sequence of characters to the buffer.
72 * @param data The pointer to the data to write, must be valid
73 * @param size The number of bytes to write, with range [0, infinity)
74 * @return The number of bytes actually written
75 */
76 std::streamsize xsputn(const char* data, std::streamsize size) override;
77
78 /**
79 * Repositions the stream position indicator using relative offsets.
80 * @param offset The offset to apply, can be positive or negative
81 * @param direction The reference position (beginning, current, or end)
82 * @param mode The open mode (input, output, or both)
83 * @return The new position, or pos_type(off_type(-1)) on failure
84 */
85 pos_type seekoff(off_type offset, std::ios_base::seekdir direction, std::ios_base::openmode mode = std::ios_base::out) override;
86
87 /**
88 * Repositions the stream position indicator to an absolute position.
89 * @param position The absolute position to seek to
90 * @param mode The open mode (input, output, or both)
91 * @return The new position, or pos_type(off_type(-1)) on failure
92 */
93 pos_type seekpos(pos_type position, std::ios_base::openmode mode = std::ios_base::out) override;
94
95 /**
96 * Returns the current size of the buffer.
97 * @return The number of bytes currently stored in the buffer, with range [0, infinity)
98 */
99 size_t size() const;
100
101 /**
102 * Reserves memory for the buffer to avoid reallocations during writing.
103 * @param capacity The desired capacity in bytes, with range [0, infinity)
104 */
105 void reserve(const size_t capacity);
106
107 /**
108 * Returns a const reference to the internal buffer.
109 * @return The const reference to the internal vector buffer
110 */
111 const Buffer& buffer() const;
112
113 /**
114 * Returns a reference to the internal buffer.
115 * @return The reference to the internal vector buffer
116 */
117 Buffer& buffer();
118
119 /**
120 * Clears the buffer content.
121 */
122 void clear();
123
124 protected:
125
126 /// The internal vector storing the stream data.
128
129 /// The current position in the buffer for writing, with range [0, infinity)
130 size_t currentPosition_ = 0;
131 };
132
133 public:
134
135 /**
136 * Creates a new vector output stream with an empty buffer.
137 */
139
140 /**
141 * Creates a new vector output stream with a specified initial buffer capacity.
142 * @param capacity The initial capacity of the buffer, in bytes, with range [0, infinity)
143 */
144 explicit VectorOutputStream(const size_t capacity);
145
146 /**
147 * Returns a pointer to the stream data.
148 * @return The pointer to the internal buffer data, nullptr if the buffer is empty
149 */
150 const void* data() const;
151
152 /**
153 * Returns the current size of the stream.
154 * @return The number of bytes currently stored in the stream, with range [0, infinity)
155 */
156 size_t size() const;
157
158 /**
159 * Clears the stream content.
160 */
161 void clear();
162
163 protected:
164
165 /// The internal stream buffer storing the data.
167};
168
170 buffer_(std::move(buffer))
171{
172 // nothing to do here
173}
174
176{
177 buffer_.reserve(capacity);
178}
179
181{
182 return buffer_.size();
183}
184
185inline void VectorOutputStream::VectorStreamBuffer::reserve(const size_t capacity)
186{
187 buffer_.reserve(capacity);
188}
189
194
199
201{
202 buffer_.clear();
203 currentPosition_ = 0;
204}
205
207 std::ostream(&streamBuffer_)
208{
209 // nothing to do here
210}
211
212inline VectorOutputStream::VectorOutputStream(const size_t capacity) :
213 std::ostream(&streamBuffer_)
214{
215 streamBuffer_.reserve(capacity);
216}
217
218inline const void* VectorOutputStream::data() const
219{
220 return streamBuffer_.buffer().data();
221}
222
223inline size_t VectorOutputStream::size() const
224{
225 return streamBuffer_.size();
226}
227
229{
231}
232
233}
234
235}
236
237}
238
239#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:38
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:185
size_t size() const
Returns the current size of the buffer.
Definition VectorOutputStream.h:180
const Buffer & buffer() const
Returns a const reference to the internal buffer.
Definition VectorOutputStream.h:190
void clear()
Clears the buffer content.
Definition VectorOutputStream.h:200
std::vector< char > Buffer
Definition of the buffer type storing the stream data.
Definition VectorOutputStream.h:42
Buffer buffer_
The internal vector storing the stream data.
Definition VectorOutputStream.h:127
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:31
VectorStreamBuffer streamBuffer_
The internal stream buffer storing the data.
Definition VectorOutputStream.h:166
size_t size() const
Returns the current size of the stream.
Definition VectorOutputStream.h:223
VectorOutputStream()
Creates a new vector output stream with an empty buffer.
Definition VectorOutputStream.h:206
void clear()
Clears the stream content.
Definition VectorOutputStream.h:228
const void * data() const
Returns a pointer to the stream data.
Definition VectorOutputStream.h:218
The namespace covering the entire Ocean framework.
Definition Accessor.h:15