adtf_file_library  0.13.2
adtf_file_writer.h
Go to the documentation of this file.
1 
17 #ifndef ADTF_FILE_FILE_WRITER_INCLUDED
18 #define ADTF_FILE_FILE_WRITER_INCLUDED
19 
20 #include <memory>
21 #include <string>
22 #include <chrono>
23 #include <unordered_map>
24 #include <queue>
25 #include <list>
26 
27 #include <ifhd/ifhd.h>
28 #include "sample.h"
29 #include "stream_type.h"
30 #include "object.h"
31 
32 namespace adtf_file
33 {
34 
39 {
40  public:
46  virtual void write(const void* data, size_t data_size) = 0;
47 
54  template <typename T>
55  OutputStream& operator<<(const T& value)
56  {
57  static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types and std::string are allowed. Alternatively, provide a specialization for adtf_file::OutputStream& operator<<(T& value).");
58  write(&value, sizeof(T));
59  return *this;
60  }
61 };
62 
68 template <>
69 OutputStream& OutputStream::operator<<(const std::string& value);
70 
75 {
76  public:
81  virtual std::string getMetaType() const = 0;
87  virtual void serialize(const StreamType& stream_type, OutputStream& stream) const = 0;
88 };
89 
94 {
95  public:
100  virtual std::string getTypeId() const = 0;
101 };
102 
106 class StreamTypeSerializers: private std::unordered_map<std::string, std::shared_ptr<StreamTypeSerializer>>
107 {
108  public:
114  void add(const std::shared_ptr<StreamTypeSerializer>& serializer)
115  {
116  emplace(serializer->getMetaType(), serializer);
117  }
126  void serialize(const StreamType& stream_type, OutputStream& stream) const
127  {
128  auto& property_type = dynamic_cast<const PropertyStreamType&>(stream_type);
129  auto serializer = find(property_type.getMetaType());
130  if (serializer == end())
131  {
132  // try to find a default serializer
133  serializer = find("");
134  if (serializer == end())
135  {
136  throw std::runtime_error("No serializer for stream type " + property_type.getMetaType());
137  }
138  }
139 
140  serializer->second->serialize(stream_type, stream);
141  }
142 
149  std::string getAdtf2TypeId(const StreamType& stream_type)
150  {
151  auto& property_type = dynamic_cast<const PropertyStreamType&>(stream_type);
152  auto serializer = find(property_type.getMetaType());
153  if (serializer != end())
154  {
155  auto adtf2_serializer = std::dynamic_pointer_cast<ADTF2StreamTypeSerializer>(serializer->second);
156  if (adtf2_serializer)
157  {
158  return adtf2_serializer->getTypeId();
159  }
160  }
161 
162  return "";
163  }
164 };
165 
171 {
172  public:
177  virtual std::string getId() const = 0;
182  virtual void setStreamType(const StreamType& stream_type) = 0;
189  virtual void serialize(const WriteSample& sample, OutputStream& stream) = 0;
190 };
191 
196 {
197  public:
202  virtual std::string getId() const = 0;
207  virtual std::shared_ptr<SampleSerializer> build() const = 0;
208 };
209 
214 template <typename SERIALIZER>
216 {
217  public:
218  std::string getId() const override
219  {
220  return SERIALIZER::id;
221  }
222 
223  std::shared_ptr<SampleSerializer> build() const override
224  {
225  return std::make_shared<SERIALIZER>();
226  }
227 };
228 
232 class SampleSerializerFactories: private std::unordered_map<std::string, std::shared_ptr<SampleSerializerFactory>>
233 {
234  public:
240  void add(const std::shared_ptr<SampleSerializerFactory>& serializer_factory)
241  {
242  emplace(serializer_factory->getId(), serializer_factory);
243  }
244 
251  std::shared_ptr<SampleSerializer> build(const std::string& id) const
252  {
253  try
254  {
255  return at(id)->build();
256  }
257  catch (...)
258  {
259  throw std::runtime_error("no sample serializer factory with id '" + id + "' available");
260  }
261  }
262 };
263 
264 // @cond adtf_file_no_doc
265 class CompatIndexedFileWriter;
266 // @endcond
267 
271 class ADTFDatFileWriter: private ifhd::v400::IndexedFileWriter::ChunkDroppedCallback
272 {
273  private:
274  class Buffer: public std::vector<uint8_t>, public OutputStream
275  {
276 
277  public:
278  void write(const void* data, size_t data_size) override
279  {
280  this->insert(end(), static_cast<const uint8_t*>(data), static_cast<const uint8_t*>(data) + data_size);
281  }
282  };
283 
284  class Chunk: public Buffer
285  {
286  public:
287  size_t stream_id;
288  std::chrono::nanoseconds time_stamp;
289  uint32_t flags;
290 
291  public:
292  void reset(size_t stream_id_to_set,
293  std::chrono::nanoseconds time_stamp_to_set,
294  uint32_t flags_to_set)
295  {
296  this->stream_id = stream_id_to_set;
297  this->time_stamp = time_stamp_to_set;
298  this->flags = flags_to_set;
299  clear();
300  }
301  };
302 
303  public:
309  {
314  adtf2 = 2,
319  adtf3 = 3,
324  adtf3ns = 4
325  };
326 
327  public:
328  ADTFDatFileWriter() = delete;
342  ADTFDatFileWriter(const std::string& file_name,
343  std::chrono::nanoseconds history_duration,
344  StreamTypeSerializers type_serializers,
345  TargetADTFVersion target_adtf_version = adtf3ns,
346  uint32_t writer_flags = 0);
348 
349  ADTFDatFileWriter(const ADTFDatFileWriter&) = delete;
354 
362  size_t createStream(const std::string& name,
363  const StreamType& initial_type,
364  const std::shared_ptr<SampleSerializer>& serializer);
371  void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType& type);
378  void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample& sample);
384  void writeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp);
385 
389  void quitHistory();
390 
399  std::shared_ptr<OutputStream> getExtensionStream(const std::string& name,
400  uint32_t user_id,
401  uint32_t type_id,
402  uint32_t file_version_id);
407  void setFileDescription(const std::string& description);
408  private:
409  void onChunkDropped(uint64_t index, uint16_t stream_id, uint16_t flags, timestamp_t time_stamp) override;
410 
411  const Chunk& serialize(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType& type);
412  const Chunk& serialize(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample& sample);
413  const Chunk& serializeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp);
414 
415  void write(const Chunk& chunk);
416 
417  void closeAdtf2();
418  void closeAdtf3();
419 
420  private:
421  std::unique_ptr<CompatIndexedFileWriter> _file;
422  StreamTypeSerializers _type_serializers;
423  TargetADTFVersion _target_adtf_version;
424  size_t _stream_id_counter = 0;
425  bool _history_active;
426  bool _lock_chunk_write;
427 
428  struct Stream
429  {
430  std::string name;
431  Buffer initial_type;
432  std::string adtf2_initial_type_id;
433  std::shared_ptr<SampleSerializer> sample_serializer;
434  std::list<Buffer> type_queue;
435  Chunk chunk_buffer;
436  bool has_samples = false;
437  };
438 
439  std::vector<Stream> _streams;
440  std::weak_ptr<OutputStream> _last_extension_stream;
441 };
442 
443 }
444 
445 #endif
ADTF2 stream type serializer interface class to write ADTF 2 stream types.
Definition: adtf_file_writer.h:94
virtual std::string getTypeId() const =0
Get the Type Id of the serializer.
Default ADTF DAT File Writer to write an ADTF DAT File (IFHD file with ADTF type and sample content).
Definition: adtf_file_writer.h:272
size_t createStream(const std::string &name, const StreamType &initial_type, const std::shared_ptr< SampleSerializer > &serializer)
Create a Stream and retrieve the stream id for it.
void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const WriteSample &sample)
Writes a sample to the file or the history.
void setFileDescription(const std::string &description)
Set the File Description.
void quitHistory()
quits the history.
ADTFDatFileWriter(const std::string &file_name, std::chrono::nanoseconds history_duration, StreamTypeSerializers type_serializers, TargetADTFVersion target_adtf_version=adtf3ns, uint32_t writer_flags=0)
CTOR for a new ADTF file for writing.
void writeTrigger(size_t stream_id, std::chrono::nanoseconds time_stamp)
Writes a trigger to the file or the history.
TargetADTFVersion
Target Verion of the ADTF file to write.
Definition: adtf_file_writer.h:309
@ adtf3ns
target version is ADTF 3 file, ontaining samples, triggers and stream types with nanosecond resolutio...
Definition: adtf_file_writer.h:324
@ adtf3
target version is ADTF 3 file, containing samples, triggers and stream types.
Definition: adtf_file_writer.h:319
@ adtf2
target version is ADTF 2 file, containing samples only.
Definition: adtf_file_writer.h:314
void write(size_t stream_id, std::chrono::nanoseconds time_stamp, const StreamType &type)
Writes a type to the file or the history.
std::shared_ptr< OutputStream > getExtensionStream(const std::string &name, uint32_t user_id, uint32_t type_id, uint32_t file_version_id)
Creates and gets a extension stream to write information into an extension.
ADTFDatFileWriter(ADTFDatFileWriter &&)=default
move CTOR
base interface class for all available objects of the ADTF File Library that can be added to the obje...
Definition: object.h:33
Interface class for an output stream.
Definition: adtf_file_writer.h:39
virtual void write(const void *data, size_t data_size)=0
Writes the data to the output stream.
OutputStream & operator<<(const T &value)
template operator to write a standard layout type to the output stream
Definition: adtf_file_writer.h:55
The default interface class for stream types.
Definition: stream_type.h:32
Container class for SampleSerializerFactory.
Definition: adtf_file_writer.h:233
void add(const std::shared_ptr< SampleSerializerFactory > &serializer_factory)
Adds a SampleSerializerFactory instance to the container.
Definition: adtf_file_writer.h:240
std::shared_ptr< SampleSerializer > build(const std::string &id) const
Create an instance of a SampleSerializer for the given id.
Definition: adtf_file_writer.h:251
interface class for sample serializer
Definition: adtf_file_writer.h:196
virtual std::string getId() const =0
Get the id of the serializer.
virtual std::shared_ptr< SampleSerializer > build() const =0
Create an instance of a SampleSerializer.
Interface class for a sample serializer. Usually the implementaion has a serialization state dependin...
Definition: adtf_file_writer.h:171
virtual void serialize(const WriteSample &sample, OutputStream &stream)=0
Serializes the sample into the given stream. These samples usually are described by the previous set ...
virtual void setStreamType(const StreamType &stream_type)=0
Set the Stream Type and the serialization state of the serializer.
virtual std::string getId() const =0
Get the Id of the sample serializer.
interface class for stream type serializers.
Definition: adtf_file_writer.h:75
virtual std::string getMetaType() const =0
Get the Meta Type name for which the serializer serializes stream types.
virtual void serialize(const StreamType &stream_type, OutputStream &stream) const =0
serializes the given stream type into the output stream
Container class for StreamTypeSerializer.
Definition: adtf_file_writer.h:107
void add(const std::shared_ptr< StreamTypeSerializer > &serializer)
Adds a StreamTypeSerializer instance to the container.
Definition: adtf_file_writer.h:114
std::string getAdtf2TypeId(const StreamType &stream_type)
Get the ADTF 2 type id of the stream_type.
Definition: adtf_file_writer.h:149
void serialize(const StreamType &stream_type, OutputStream &stream) const
Serialzes the given stream type into the given stream. It will use the StreamTypeSerializer supportin...
Definition: adtf_file_writer.h:126
Stream Type base class.
Definition: stream_item.h:43
Interface class for samples that are written.
Definition: sample.h:114
default implementation template for SampleSerializerFactory to create an instance of the serialzer ty...
Definition: adtf_file_writer.h:216
std::string getId() const override
Get the id of the serializer.
Definition: adtf_file_writer.h:218
std::shared_ptr< SampleSerializer > build() const override
Create an instance of a SampleSerializer.
Definition: adtf_file_writer.h:223
namespace for ADTF File library
Definition: adtf2_adtf_core_media_sample_deserializer.h:25

Copyright © CARIAD SE.
Generated on Mon Jun 10 2024 by doxygen 1.9.1
GIT Commit Hash: eb3af397a6b49ad6fcad9a60d8277d909b458b48