Ocean
Loading...
Searching...
No Matches
OutputDataSerializer.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_OUTPUT_DATA_SERIALIZER_H
9#define META_OCEAN_IO_SERIALIZATION_OUTPUT_DATA_SERIALIZER_H
10
13
14namespace Ocean
15{
16
17namespace IO
18{
19
20namespace Serialization
21{
22
23/**
24 * This class implements an output data serializer.
25 * The output data serializer serializes data samples to a stream (e.g., file or network).
26 * @ingroup ioserialization
27 */
29{
30 protected:
31
32 /**
33 * This class implements an abstract stream for output data serializers.
34 */
35 class Stream
36 {
37 public:
38
39 /**
40 * Destructs the stream.
41 */
42 virtual ~Stream() = default;
43
44 /**
45 * Returns the output bitstream.
46 * @return The output bitstream
47 */
49
50 /**
51 * Returns whether this stream is valid.
52 * @return True, if so
53 */
54 virtual bool isValid() const = 0;
55 };
56
57 /// Definition of a unique pointer holding a stream.
58 using UniqueStream = std::unique_ptr<Stream>;
59
60 /// Definition of a FIFO queue holding sample pairs.
61 using SampleQueue = std::queue<SamplePair>;
62
63 public:
64
65 /**
66 * Adds a new channel to the serializer.
67 * @param sampleType The sample type, must be non-empty
68 * @param name The name of the channel, must be non-empty
69 * @param contentType The content type, must be non-empty
70 * @return The channel id, invalidChannelId() if the channel could not be added (e.g., if a channel with the same configuration already exists)
71 */
72 [[nodiscard]] DataSerializer::ChannelId addChannel(const std::string& sampleType, const std::string& name, const std::string& contentType);
73
74 /**
75 * Adds a new sample to the serializer.
76 * The sample will be added to the internal queue and will be written to the stream asynchronously.
77 * @param channelId The channel id
78 * @param sample The sample to add, will be moved
79 * @return True, if succeeded
80 */
81 bool addSample(const DataSerializer::ChannelId channelId, UniqueDataSample&& sample);
82
83 /**
84 * Starts the serializer.
85 * @return True, if succeeded
86 * @see DataSerializer::start().
87 */
88 bool start() override;
89
90 /**
91 * Stops the serializer.
92 * This function sets a stopping flag and returns immediately; it does not wait for the serializer to actually stop.
93 * The background thread will finish writing all pending samples in the queue before checking the stopping flag and terminating.
94 * This ensures that all queued samples are written to the output stream before the serializer stops.
95 * @return True, if the stop request was accepted; False, if the serializer was not started
96 * @see DataSerializer::stop(), stopAndWait(), hasStopped().
97 */
98 bool stop() override;
99
100 /**
101 * Returns whether the serializer has been started.
102 * @return True, if so
103 * @see DataSerializer::isStarted().
104 */
105 [[nodiscard]] bool isStarted() const override;
106
107 /**
108 * Returns whether the serializer has stopped and all remaining samples have been processed.
109 * @return True, if so
110 * @see DataSerializer::hasFinished().
111 */
112 [[nodiscard]] bool hasFinished() const override;
113
114 protected:
115
116 /**
117 * Creates the output stream.
118 * @return The output stream, nullptr if the stream could not be created
119 */
120 virtual UniqueStream createStream() const = 0;
121
122 /**
123 * Writes the header to the output bitstream.
124 * @param outputBitstream The output bitstream to which the header will be written
125 * @return True, if succeeded
126 */
127 virtual bool writeHeader(OutputBitstream& outputBitstream);
128
129 /**
130 * The thread run function.
131 * @see Thread::threadRun().
132 */
133 void threadRun() override;
134
135 protected:
136
137 /// The output stream.
139
140 /// The next channel id to be assigned.
142
143 /// The map mapping channel configurations to channel ids.
145
146 /// The queue holding samples which are pending to be written.
148};
149
150/**
151 * This class implements a file-based output data serializer.
152 * @ingroup ioserialization
153 */
155{
156 protected:
157
158 /**
159 * This class implements a file stream for file output data serializers.
160 */
161 class FileStream : public Stream
162 {
163 public:
164
165 /**
166 * Creates a new file stream with given filename.
167 * @param filename The filename of the file to write, must be valid
168 */
169 inline explicit FileStream(const std::string& filename);
170
171 /**
172 * Destructs the file stream.
173 */
174 inline ~FileStream() override;
175
176 /**
177 * Returns the output bitstream.
178 * @return The output bitstream
179 * @see Stream::outputBitstream().
180 */
181 inline OutputBitstream& outputBitstream() override;
182
183 /**
184 * Returns whether this stream is valid.
185 * @return True, if so
186 * @see Stream::isValid().
187 */
188 inline bool isValid() const override;
189
190 protected:
191
192 /// The file stream.
193 std::ofstream stream_;
194
195 /// The output bitstream.
197 };
198
199 public:
200
201 /**
202 * Sets the filename of the file to write.
203 * @param filename The filename of the file to write, must be non-empty
204 * @return True, if succeeded
205 */
206 virtual bool setFilename(const std::string& filename);
207
208 protected:
209
210 /**
211 * Creates the output stream.
212 * @return The output stream, nullptr if the stream could not be created
213 * @see OutputDataSerializer::createStream().
214 */
215 UniqueStream createStream() const override;
216
217 protected:
218
219 /// The filename of the file to write.
220 std::string filename_;
221};
222
223inline FileOutputDataSerializer::FileStream::FileStream(const std::string& filename) :
224 stream_(filename.c_str(), std::ios::binary),
225 outputBitstream_(stream_)
226
227{
228 // nothing to do here
229}
230
235
237{
238 return outputBitstream_;
239}
240
242{
243 return stream_.is_open() && !stream_.fail();
244}
245
246}
247
248}
249
250}
251
252#endif // META_OCEAN_IO_SERIALIZATION_OUTPUT_DATA_SERIALIZER_H
This class implements an output bitstream.
Definition Bitstream.h:215
This class implements the base class for data serializers.
Definition DataSerializer.h:36
std::unordered_map< ChannelConfiguration, ChannelId, ChannelConfiguration::Hash > ChannelConfigurationMap
Definition of a map mapping channel configurations to channel ids.
Definition DataSerializer.h:208
uint32_t ChannelId
Definition of a channel id.
Definition DataSerializer.h:40
This class implements a file stream for file output data serializers.
Definition OutputDataSerializer.h:162
OutputBitstream outputBitstream_
The output bitstream.
Definition OutputDataSerializer.h:196
bool isValid() const override
Returns whether this stream is valid.
Definition OutputDataSerializer.h:241
OutputBitstream & outputBitstream() override
Returns the output bitstream.
Definition OutputDataSerializer.h:236
~FileStream() override
Destructs the file stream.
Definition OutputDataSerializer.h:231
std::ofstream stream_
The file stream.
Definition OutputDataSerializer.h:193
FileStream(const std::string &filename)
Creates a new file stream with given filename.
Definition OutputDataSerializer.h:223
This class implements a file-based output data serializer.
Definition OutputDataSerializer.h:155
std::string filename_
The filename of the file to write.
Definition OutputDataSerializer.h:220
UniqueStream createStream() const override
Creates the output stream.
virtual bool setFilename(const std::string &filename)
Sets the filename of the file to write.
This class implements an abstract stream for output data serializers.
Definition OutputDataSerializer.h:36
virtual ~Stream()=default
Destructs the stream.
virtual OutputBitstream & outputBitstream()=0
Returns the output bitstream.
virtual bool isValid() const =0
Returns whether this stream is valid.
This class implements an output data serializer.
Definition OutputDataSerializer.h:29
void threadRun() override
The thread run function.
UniqueStream stream_
The output stream.
Definition OutputDataSerializer.h:138
bool addSample(const DataSerializer::ChannelId channelId, UniqueDataSample &&sample)
Adds a new sample to the serializer.
DataSerializer::ChannelId addChannel(const std::string &sampleType, const std::string &name, const std::string &contentType)
Adds a new channel to the serializer.
std::queue< SamplePair > SampleQueue
Definition of a FIFO queue holding sample pairs.
Definition OutputDataSerializer.h:61
SampleQueue sampleQueue_
The queue holding samples which are pending to be written.
Definition OutputDataSerializer.h:147
ChannelConfigurationMap channelConfigurationMap_
The map mapping channel configurations to channel ids.
Definition OutputDataSerializer.h:144
bool start() override
Starts the serializer.
bool hasFinished() const override
Returns whether the serializer has stopped and all remaining samples have been processed.
bool stop() override
Stops the serializer.
virtual UniqueStream createStream() const =0
Creates the output stream.
ChannelId nextChannelId_
The next channel id to be assigned.
Definition OutputDataSerializer.h:141
virtual bool writeHeader(OutputBitstream &outputBitstream)
Writes the header to the output bitstream.
bool isStarted() const override
Returns whether the serializer has been started.
std::unique_ptr< Stream > UniqueStream
Definition of a unique pointer holding a stream.
Definition OutputDataSerializer.h:58
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
The namespace covering the entire Ocean framework.
Definition Accessor.h:15