adtf_file_library  0.13.2
default_sample.h
Go to the documentation of this file.
1 
17 #ifndef ADTF_FILE_DEFAULT_SAMPLE
18 #define ADTF_FILE_DEFAULT_SAMPLE
19 
20 #include "stream_item.h"
21 #include "sample.h"
22 
23 #include <vector>
24 #include <chrono>
25 #include <cstring>
26 #include <functional>
27 #include <unordered_map>
28 #include <vector>
29 #include <stdexcept>
30 
31 namespace adtf_file
32 {
33 
37 class DefaultSample: public Sample, public ReadSample, public WriteSample
38 {
39  public:
40  void setTimeStamp(std::chrono::nanoseconds time_stamp) override;
41  void setSubStreamId(uint32_t substream_id) override;
42  void setFlags(uint32_t flags) override;
43  void* beginBufferWrite(size_t size) override;
44  void endBufferWrite() override;
45  void addInfo(uint32_t key, DataType type, uint64_t raw_bytes) override;
46 
47  public:
48  std::chrono::nanoseconds getTimeStamp() const override;
49  uint32_t getSubStreamId() const override;
50  uint32_t getFlags() const override;
51  std::pair<const void*, size_t> beginBufferRead() const override;
52  void endBufferRead() const override;
53  void iterateInfo(std::function<void(uint32_t key, DataType type, uint64_t raw_bytes)> functor) const override;
54 
55  public:
60  const std::unordered_map<uint32_t, std::pair<DataType, uint64_t>>& GetInfo() const;
61 
62  public:
68  template <typename T>
69  void setContent(const T& value)
70  {
71  static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types are allowed.");
72  _buffer.resize(sizeof(value));
73  memcpy(_buffer.data(), &value, sizeof(value));
74  }
75 
81  template <typename T>
82  const T& getContent() const
83  {
84  static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types are allowed.");
85  if (sizeof(T) > _buffer.size())
86  {
87  throw std::runtime_error("invalid sample size for requested type");
88  }
89  return *reinterpret_cast<const T*>(_buffer.data());
90  }
91 
92  private:
93  std::chrono::nanoseconds _time_stamp = std::chrono::nanoseconds(0);
94  uint32_t _substream_id = 0;
95  uint32_t _flags = 0;
96  std::vector<uint8_t> _buffer;
97  std::unordered_map<uint32_t, std::pair<DataType, uint64_t>> _info;
98 };
99 
100 }
101 
102 #endif
Default Sample implementation.
Definition: default_sample.h:38
const std::unordered_map< uint32_t, std::pair< DataType, uint64_t > > & GetInfo() const
Get the additional sample information.
const T & getContent() const
Get the Content of the sample for trivially copyable types.
Definition: default_sample.h:82
uint32_t getSubStreamId() const override
Get the Sub Stream Id if set.
std::chrono::nanoseconds getTimeStamp() const override
Get the Time Stamp.
void endBufferRead() const override
Ends reading from memory buffer retruned by beginBufferRead.
void setTimeStamp(std::chrono::nanoseconds time_stamp) override
Set the Time Stamp.
uint32_t getFlags() const override
Get the Flags.
void setContent(const T &value)
Set the Content of the sample for trivially copyable types.
Definition: default_sample.h:69
void endBufferWrite() override
ends writing to the memory allocated with beginBufferWrite
void addInfo(uint32_t key, DataType type, uint64_t raw_bytes) override
Adds a additional sample information.
void setSubStreamId(uint32_t substream_id) override
Set the Sub Stream Id, if needed.
void setFlags(uint32_t flags) override
Set the Flags.
std::pair< const void *, size_t > beginBufferRead() const override
Retrieve a pointer to the memory buffer for user data and size in bytes.
void iterateInfo(std::function< void(uint32_t key, DataType type, uint64_t raw_bytes)> functor) const override
iterates all additional data information (if any) by calling the functor
void * beginBufferWrite(size_t size) override
allocates the memory buffer for sample data
Interface class for samples that are read from a Reader.
Definition: sample.h:72
Sample base class.
Definition: stream_item.h:55
Interface class for samples that are written.
Definition: sample.h:114
namespace for ADTF File library
Definition: adtf2_adtf_core_media_sample_deserializer.h:25
DataType
data type id for the adtf_file::ReadSample::addInfo call.
Definition: sample.h:32

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