ADTF File Library
Loading...
Searching...
No Matches
default_sample.h
Go to the documentation of this file.
1
16
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 <variant>
30#include <stdexcept>
31
32namespace adtf_file
33{
34
38class DefaultSample: public Sample, public ReadSample, public WriteSample, public SampleCopy
39{
40 public:
41 void setTimeStamp(std::chrono::nanoseconds time_stamp) override;
42 void setSubStreamId(uint32_t substream_id) override;
43 void setFlags(uint32_t flags) override;
44 void* beginBufferWrite(size_t size) override;
45 void endBufferWrite() override;
46 void addInfo(uint32_t key, DataType type, uint64_t raw_bytes) override;
47
48 public:
49 std::chrono::nanoseconds getTimeStamp() const override;
50 uint32_t getSubStreamId() const override;
51 uint32_t getFlags() const override;
52 std::pair<const void*, size_t> beginBufferRead() const override;
53 void endBufferRead() const override;
54 void iterateInfo(std::function<void(uint32_t key, DataType type, uint64_t raw_bytes)> functor) const override;
55
56 public:
57 void CopyFrom(const std::shared_ptr<const Sample>& sample) override;
58
59 public:
64 const std::unordered_map<uint32_t, std::pair<DataType, uint64_t>>& GetInfo() const;
65
66 public:
72 template <typename T>
73 void setContent(const T& value)
74 {
75 static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types are allowed.");
76 const auto buffer = beginBufferWrite(sizeof(value));
77 std::memcpy(buffer, &value, sizeof(value));
79 }
80
86 template <typename T>
87 T getContent() const
88 {
89 static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types are allowed.");
90 const auto [buffer, buffer_size] = beginBufferRead();
91 if (sizeof(T) > buffer_size)
92 {
94 throw std::runtime_error("invalid sample size for requested type");
95 }
96 const auto value = *reinterpret_cast<const T*>(buffer);
98 return value;
99 }
100
101 private:
102 std::chrono::nanoseconds _time_stamp = std::chrono::nanoseconds(0);
103 uint32_t _substream_id = 0;
104 uint32_t _flags = 0;
105 std::variant<std::monostate, std::vector<uint8_t>, std::shared_ptr<const WriteSample>> _buffer;
106 std::unordered_map<uint32_t, std::pair<DataType, uint64_t>> _info;
107};
108
109}
110
111#endif
Default Sample implementation.
Definition default_sample.h:39
uint32_t getSubStreamId() const override
Get the Sub Stream Id if set.
std::chrono::nanoseconds getTimeStamp() const override
Get the Time Stamp.
void CopyFrom(const std::shared_ptr< const Sample > &sample) override
void endBufferRead() const override
Ends reading from memory buffer retruned by beginBufferRead.
void setTimeStamp(std::chrono::nanoseconds time_stamp) override
Set the Time Stamp.
const std::unordered_map< uint32_t, std::pair< DataType, uint64_t > > & GetInfo() const
Get the additional sample information.
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:73
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.
T getContent() const
Get the Content of the sample for trivially copyable types.
Definition default_sample.h:87
void setSubStreamId(uint32_t substream_id) override
Set the Sub Stream Id, if needed.
void setFlags(uint32_t flags) override
Set the Flags.
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
std::pair< const void *, size_t > beginBufferRead() const override
Retrieve a pointer to the memory buffer for user data and size in bytes.
Interface class for samples that are read from a Reader.
Definition sample.h:74
interface for copying the contents of one sample into another, probably sharing resources (such as th...
Definition sample.h:183
Sample base class.
Definition stream_item.h:55
Interface class for samples that are written.
Definition sample.h:116
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:34