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 public:
61
62 /**
63 * Adds a new channel to the serializer.
64 * @param sampleType The sample type, must be non-empty
65 * @param name The name of the channel, must be non-empty
66 * @param contentType The content type, must be non-empty
67 * @return The channel id, invalidChannelId() if the channel could not be added (e.g., if a channel with the same configuration already exists)
68 */
69 [[nodiscard]] DataSerializer::ChannelId addChannel(const std::string& sampleType, const std::string& name, const std::string& contentType);
70
71 /**
72 * Adds a new sample to the serializer.
73 * The sample will be added to the internal queue and will be written to the stream asynchronously.
74 * @param channelId The channel id
75 * @param sample The sample to add, will be moved
76 * @return True, if succeeded
77 */
78 bool addSample(const DataSerializer::ChannelId channelId, UniqueDataSample&& sample);
79
80 /**
81 * Starts the serializer.
82 * @return True, if succeeded
83 * @see DataSerializer::start().
84 */
85 bool start() override;
86
87 /**
88 * Stops the serializer.
89 * This function sets a stopping flag and returns immediately; it does not wait for the serializer to actually stop.
90 * The background thread will finish writing all pending samples in the queue before checking the stopping flag and terminating.
91 * This ensures that all queued samples are written to the output stream before the serializer stops.
92 * @return True, if the stop request was accepted; False, if the serializer was not started
93 * @see DataSerializer::stop(), stopAndWait(), hasStopped().
94 */
95 bool stop() override;
96
97 /**
98 * Returns whether the serializer has been started.
99 * @return True, if so
100 * @see DataSerializer::isStarted().
101 */
102 [[nodiscard]] bool isStarted() const override;
103
104 /**
105 * Returns whether the serializer has stopped.
106 * @return True, if so
107 * @see DataSerializer::hasStopped().
108 */
109 [[nodiscard]] bool hasStopped() const override;
110
111 protected:
112
113 /**
114 * Creates the output stream.
115 * @return The output stream, nullptr if the stream could not be created
116 */
117 virtual UniqueStream createStream() const = 0;
118
119 /**
120 * Writes the header to the output bitstream.
121 * @param outputBitstream The output bitstream to which the header will be written
122 * @return True, if succeeded
123 */
124 virtual bool writeHeader(OutputBitstream& outputBitstream);
125
126 /**
127 * The thread run function.
128 * @see Thread::threadRun().
129 */
130 void threadRun() override;
131
132 protected:
133
134 /// The output stream.
136
137 /// The next channel id to be assigned.
139
140 /// The map mapping channel configurations to channel ids.
142};
143
144/**
145 * This class implements a file-based output data serializer.
146 * @ingroup ioserialization
147 */
149{
150 protected:
151
152 /**
153 * This class implements a file stream for file output data serializers.
154 */
155 class FileStream : public Stream
156 {
157 public:
158
159 /**
160 * Creates a new file stream with given filename.
161 * @param filename The filename of the file to write, must be valid
162 */
163 inline explicit FileStream(const std::string& filename);
164
165 /**
166 * Destructs the file stream.
167 */
168 inline ~FileStream() override;
169
170 /**
171 * Returns the output bitstream.
172 * @return The output bitstream
173 * @see Stream::outputBitstream().
174 */
175 inline OutputBitstream& outputBitstream() override;
176
177 /**
178 * Returns whether this stream is valid.
179 * @return True, if so
180 * @see Stream::isValid().
181 */
182 inline bool isValid() const override;
183
184 protected:
185
186 /// The file stream.
187 std::ofstream stream_;
188
189 /// The output bitstream.
191 };
192
193 public:
194
195 /**
196 * Sets the filename of the file to write.
197 * @param filename The filename of the file to write, must be non-empty
198 * @return True, if succeeded
199 */
200 virtual bool setFilename(const std::string& filename);
201
202 protected:
203
204 /**
205 * Creates the output stream.
206 * @return The output stream, nullptr if the stream could not be created
207 * @see OutputDataSerializer::createStream().
208 */
209 UniqueStream createStream() const override;
210
211 protected:
212
213 /// The filename of the file to write.
214 std::string filename_;
215};
216
217inline FileOutputDataSerializer::FileStream::FileStream(const std::string& filename) :
218 stream_(filename.c_str(), std::ios::binary),
219 outputBitstream_(stream_)
220
221{
222 // nothing to do here
223}
224
229
231{
232 return outputBitstream_;
233}
234
236{
237 return stream_.is_open() && !stream_.fail();
238}
239
240}
241
242}
243
244}
245
246#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:156
OutputBitstream outputBitstream_
The output bitstream.
Definition OutputDataSerializer.h:190
bool isValid() const override
Returns whether this stream is valid.
Definition OutputDataSerializer.h:235
OutputBitstream & outputBitstream() override
Returns the output bitstream.
Definition OutputDataSerializer.h:230
~FileStream() override
Destructs the file stream.
Definition OutputDataSerializer.h:225
std::ofstream stream_
The file stream.
Definition OutputDataSerializer.h:187
FileStream(const std::string &filename)
Creates a new file stream with given filename.
Definition OutputDataSerializer.h:217
This class implements a file-based output data serializer.
Definition OutputDataSerializer.h:149
std::string filename_
The filename of the file to write.
Definition OutputDataSerializer.h:214
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
bool hasStopped() const override
Returns whether the serializer has stopped.
void threadRun() override
The thread run function.
UniqueStream stream_
The output stream.
Definition OutputDataSerializer.h:135
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.
ChannelConfigurationMap channelConfigurationMap_
The map mapping channel configurations to channel ids.
Definition OutputDataSerializer.h:141
bool start() override
Starts the serializer.
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:138
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