ADTF File Library
Loading...
Searching...
No Matches
adtf_file_reader.h
Go to the documentation of this file.
1
16
17#pragma once
18
19#include <chrono>
20#include <memory>
21#include <vector>
22#include <unordered_map>
23#include <istream>
24#include <string>
25#include <optional>
26#include <ifhd/ifhd.h>
27#include "stream_item.h"
28#include "object.h"
29#include "default_sample.h"
30#include "stream_type.h"
31#include "reader.h"
32
39namespace adtf_file
40{
41
46{
47 public:
53 virtual void read(void* destination, size_t count) = 0;
54
61 template <typename T>
63 {
64 static_assert(std::is_trivially_copyable<T>::value, "Only trivially copyable types and std::string are allowed. Alternatively, provide a specialization for adtf_file::InputStream& operator>>(T& value).");
65 read(&value, sizeof(T));
66 return *this;
67 }
68};
69
75template <>
77
82{
83 public:
88 virtual std::string getId() const = 0;
94 virtual void deserialize(InputStream& stream, PropertyStreamType& stream_type) const = 0;
95};
96
100class StreamTypeDeserializers: private std::unordered_map<std::string, std::shared_ptr<StreamTypeDeserializer>>
101{
102 public:
113 StreamTypeDeserializers& add(const std::shared_ptr<StreamTypeDeserializer>& deserializer)
114 {
115 (*this)[deserializer->getId()] = deserializer;
116 return *this;
117 }
118
125 void Deserialize(const std::string& id, InputStream& stream, PropertyStreamType& stream_type) const
126 {
127 auto deserializer = find(id);
128 if (deserializer == end())
129 {
130 throw std::runtime_error("no type deserializer for '" + id + "' available");
131 }
132
133 deserializer->second->deserialize(stream, stream_type);
134 }
135};
136
141{
142 public:
147 virtual void setStreamType(const StreamType& type) = 0;
153 virtual void deserialize(ReadSample& sample, InputStream& stream) = 0;
154};
155
160{
161 public:
166 virtual std::string getId() const = 0;
171 virtual std::shared_ptr<SampleDeserializer> build() const = 0;
172};
173
178template <typename FACTORY>
180{
181 public:
182 std::string getId() const override
183 {
184 return FACTORY::id;
185 }
186
187 std::shared_ptr<SampleDeserializer> build() const override
188 {
189 return std::make_shared<FACTORY>();
190 }
191};
192
197 private std::unordered_map<std::string, std::shared_ptr<SampleDeserializerFactory>>
198{
199 public:
205 SampleDeserializerFactories& add(const std::shared_ptr<SampleDeserializerFactory>& factory)
206 {
207 (*this)[factory->getId()] = factory;
208 return *this;
209 }
210
217 std::shared_ptr<SampleDeserializer> build(const std::string& id) const
218 {
219 try
220 {
221 return at(id)->build();
222 }
223 catch (...)
224 {
225 throw std::runtime_error("no sample deserializer factory for '" + id + "' available.");
226 }
227 }
228};
229
235{
236 public:
240 static constexpr int64_t read_cache_default = -1;
244 static constexpr int64_t read_cache_deactivated = 0;
245
250
256 ADTFDatFileReader(std::optional<StreamTypeDeserializers> type_factories,
257 std::optional<SampleDeserializerFactories> sample_deserializer_factories);
258
259 ADTFDatFileReader(const ADTFDatFileReader&) = delete;
260
265
266 // implement Reader interface
267 std::string getReaderIdentifier() const override
268 {
269 return "adtfdat";
270 }
271
288 void open(const std::string& filename,
289 std::shared_ptr<SampleFactory> sample_factory,
290 std::shared_ptr<StreamTypeFactory> stream_type_factory) override;
291
294 void open(const std::string& filename);
295
297 uint32_t getFileVersion() const override;
298 std::string getDescription() const override;
299 std::vector<Extension> getExtensions() const override;
300 std::vector<Stream> getStreams() const override;
301 std::optional<uint64_t> getItemCount() const override;
302 std::optional<double> getProgress() const override;
303
304 // implement SeekableReader interface
305 uint64_t getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp) override;
306 uint64_t getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index) override;
307 std::shared_ptr<const StreamType> getStreamTypeBefore(uint64_t item_index, uint16_t stream_id) override;
308 void seekTo(uint64_t item_index) override;
309
313 int64_t getNextItemIndex() const;
314
319 a_util::datetime::DateTime getDateTime() const;
320
325 std::chrono::nanoseconds getFirstTime() const;
330 std::chrono::nanoseconds getLastTime() const;
331
332 private:
333 void openDescription(const std::string& description_file_name, bool ignore_unsupported_streams);
334 std::shared_ptr<const StreamType> buildType(const std::string& id, InputStream& stream);
335 std::pair<std::shared_ptr<const StreamType>, std::shared_ptr<SampleDeserializer>> getInitialTypeAndSampleDeserializer(uint16_t stream_id);
336 std::pair<std::shared_ptr<const StreamType>, std::shared_ptr<SampleDeserializer> > getInitialTypeAndSampleFactoryAdtf2(InputStream& stream);
337 std::pair<std::shared_ptr<const StreamType>, std::shared_ptr<SampleDeserializer>> getInitialTypeAndSampleFactoryAdtf3(InputStream& stream);
338 std::chrono::nanoseconds fromFileTimeStamp(timestamp_t time_stamp);
339 timestamp_t toFileTimeStamp(std::chrono::nanoseconds time_stamp);
340
341 private:
342 std::unique_ptr<ifhd::v500::IndexedFileReader> _file;
343 std::vector<Extension> _extensions;
344 std::vector<Stream> _streams;
345 StreamTypeDeserializers _type_factories;
346 SampleDeserializerFactories _sample_deserializer_factories;
347 std::unordered_map<size_t, std::shared_ptr<SampleDeserializer>> _stream_sample_deserializers;
348 std::shared_ptr<SampleFactory> _sample_factory;
349 std::shared_ptr<StreamTypeFactory> _stream_type_factory;
350};
351
356{
357public:
367 ADTFDatFileReaderFactory(std::optional<StreamTypeDeserializers> type_factories = {},
368 std::optional<SampleDeserializerFactories> sample_deserializer_factories = {}) :
369 _type_factories(type_factories),
370 _sample_deserializer_factories(sample_deserializer_factories)
371 {
372 }
373
374 std::shared_ptr<Reader> makeReader() const override
375 {
376 return std::make_unique<ADTFDatFileReader>(_type_factories,
377 _sample_deserializer_factories);
378 }
379
380private:
381 std::optional<StreamTypeDeserializers> _type_factories;
382 std::optional<SampleDeserializerFactories> _sample_deserializer_factories;
383};
384
390inline std::string getShortDescription(const std::string& description)
391{
392 return description.substr(0, description.find_first_of("\n"));
393}
394
400inline std::string getLongDescription(const std::string& description)
401{
402 size_t pos_found = description.find_first_of("\n");
403 if (pos_found != std::string::npos)
404 {
405 return description.substr(pos_found);
406 }
407 else
408 {
409 return std::string("");
410 }
411}
412
413}
414
ADTFDatFileReaderFactory(std::optional< StreamTypeDeserializers > type_factories={}, std::optional< SampleDeserializerFactories > sample_deserializer_factories={})
CTOR.
Definition adtf_file_reader.h:367
std::shared_ptr< Reader > makeReader() const override
Creates a reader.
Definition adtf_file_reader.h:374
void open(const std::string &filename)
a file and uses default sample and stream type factories.
FileItem getNextItem() override
uint64_t getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index) override
static constexpr int64_t read_cache_deactivated
Definition adtf_file_reader.h:244
std::string getDescription() const override
std::optional< uint64_t > getItemCount() const override
Get the Item Count. This gets the overall count of all items (samples, stream types and triggers) of ...
std::chrono::nanoseconds getFirstTime() const
Get the timestamp of the very first file item.
a_util::datetime::DateTime getDateTime() const
Get the File Date Time of creating the adtfdat file from fileheader.
uint64_t getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp) override
std::shared_ptr< const StreamType > getStreamTypeBefore(uint64_t item_index, uint16_t stream_id) override
std::string getReaderIdentifier() const override
Get the Reader Identifier of the Reader.
Definition adtf_file_reader.h:267
std::vector< Stream > getStreams() const override
Get the Streams.
std::chrono::nanoseconds getLastTime() const
Get the timestamp of the very last file item.
int64_t getNextItemIndex() const
uint32_t getFileVersion() const override
void seekTo(uint64_t item_index) override
void open(const std::string &filename, std::shared_ptr< SampleFactory > sample_factory, std::shared_ptr< StreamTypeFactory > stream_type_factory) override
CTOR for a new Reader.
std::vector< Extension > getExtensions() const override
Get the Extensions if any.
static constexpr int64_t read_cache_default
Definition adtf_file_reader.h:240
ADTFDatFileReader(std::optional< StreamTypeDeserializers > type_factories, std::optional< SampleDeserializerFactories > sample_deserializer_factories)
CTOR for a new Reader.
std::optional< double > getProgress() const override
Get the Progress, a relative file position between 0.0 and 1.0.
ADTFDatFileReader(ADTFDatFileReader &&)=default
Move CTOR.
class to create or read a file item. This file item is either a sample, streamtype or trigger.
Definition reader.h:172
interface class for input streams
Definition adtf_file_reader.h:46
InputStream & operator>>(T &value)
Reads a trivially copyable value from the stream.
Definition adtf_file_reader.h:62
virtual void read(void *destination, size_t count)=0
Reads the count bytes from the stream into destination.
base interface class for all available objects of the ADTF File Library that can be added to the obje...
Definition object.h:38
The default interface class for stream types.
Definition stream_type.h:32
Interface class for samples that are read from a Reader.
Definition sample.h:74
Reader Factory interface to create a reader.
Definition reader.h:352
container class for SampleDeserializerFactory
Definition adtf_file_reader.h:198
SampleDeserializerFactories & add(const std::shared_ptr< SampleDeserializerFactory > &factory)
Adds a deserializer factory implemention to the container.
Definition adtf_file_reader.h:205
std::shared_ptr< SampleDeserializer > build(const std::string &id) const
creates the SampleDeserializer for the given id
Definition adtf_file_reader.h:217
SampleDeserializerFactory interface class.
Definition adtf_file_reader.h:160
virtual std::string getId() const =0
Get the identifier if the factory.
virtual std::shared_ptr< SampleDeserializer > build() const =0
create a SampleDeserializer instance
Sample deserializer interface to deserialize a sample.
Definition adtf_file_reader.h:141
virtual void setStreamType(const StreamType &type)=0
StreamType infomation to initialize deserializer if needed.
virtual void deserialize(ReadSample &sample, InputStream &stream)=0
Deserialize a sample from stream.
Interface for seekable Reader, where the file position can be adapted.
Definition reader.h:288
StreamType deserializer interface to deserialize a stream type.
Definition adtf_file_reader.h:82
virtual void deserialize(InputStream &stream, PropertyStreamType &stream_type) const =0
Derserialize a stream_type from stream.
virtual std::string getId() const =0
Get the Id of the serializer i.e. "adtf/default".
container class for StreamTypeDeserializers
Definition adtf_file_reader.h:101
void Deserialize(const std::string &id, InputStream &stream, PropertyStreamType &stream_type) const
deserializes the given stream into a stream_type with the StreamTypeDeserializer with id
Definition adtf_file_reader.h:125
StreamTypeDeserializers & add(const std::shared_ptr< StreamTypeDeserializer > &deserializer)
Adds a deserializer implemention to the container.
Definition adtf_file_reader.h:113
Stream Type base class.
Definition stream_item.h:43
default implementation template for a SampleDeserializerFactory creating
Definition adtf_file_reader.h:180
std::string getId() const override
Get the identifier if the factory.
Definition adtf_file_reader.h:182
std::shared_ptr< SampleDeserializer > build() const override
create a SampleDeserializer instance
Definition adtf_file_reader.h:187
template class for a sample factory to create sample with implementation type SAMPLE_CLASS
Definition reader.h:136
template class for a streamtype factory to create streamtypes with implementation type STREAM_TYPE_CL...
Definition reader.h:109
namespace for ADTF File library
Definition adtf2_adtf_core_media_sample_deserializer.h:25
std::string getLongDescription(const std::string &description)
helper fuction to retrieve long description out of description
Definition adtf_file_reader.h:400
std::string getShortDescription(const std::string &description)
helper fuction to retrieve short description out of description
Definition adtf_file_reader.h:390