ADTF
Loading...
Searching...
No Matches
samplewriter.h
Go to the documentation of this file.
1
7#pragma once
8#include "samplestream_intf.h"
10#include "sample_intf.h"
11#include "streamtype_intf.h"
12#include "samplestreamer_intf.h"
13#include "pin.h"
14#include "sample.h"
15#include "samplereader.h"
17
19
20namespace adtf
21{
22namespace streaming
23{
24namespace ant
25{
33class cSampleWriter : public sample_streamer<ISampleWriter, cOutPin>
34{
35protected:
38
41
42protected:
44
45public:
48 {
49 }
50
52 {
53 }
54
57 cSampleWriter(const char* strName, const ucom::ant::iobject_ptr<const IStreamType>& pStreamType):
58 base_type(strName, pStreamType)
59 {
60 }
61
62 tResult BeginStreaming(ISampleStream& oSampleStream) override
63 {
65
66 RETURN_IF_FAILED(oSampleStream.Open(m_strName, m_pOutStream, ISampleStreamAccess::PushRead, 0));
67
68 if (pConstType)
69 {
70 RETURN_IF_FAILED(m_pOutStream->Write(pConstType));
71 }
72
73 if (_runtime)
74 {
75 _runtime->GetObject(_clock);
76 }
77
79 }
80
82 {
83 _clock.Reset();
84 m_pOutStream.Reset();
86 }
87
88public: // Streaming Functions for
95 {
96 if (m_pOutStream)
97 {
98 RETURN_IF_FAILED(m_pOutStream->Write(pSample));
100 }
101 else
102 {
103 RETURN_ERROR_DESC(ERR_INVALID_STATE, "No valid output stream.");
104 }
105 }
106
113 {
114 RETURN_IF_FAILED(SetType(pType));
115 if (m_pOutStream)
116 {
117 RETURN_IF_FAILED(m_pOutStream->Write(pType));
118 }
120 }
121
126 virtual tResult Flush()
127 {
128 if (m_pOutStream)
129 {
130 RETURN_IF_FAILED(m_pOutStream->Flush());
132 }
133 else
134 {
135 RETURN_ERROR_DESC(ERR_INVALID_STATE, "No valid output stream.");
136 }
137 }
138
145 {
146 if (m_pOutStream)
147 {
148 RETURN_IF_FAILED(m_pOutStream->SetStreamError(oErr));
150 }
151 else
152 {
153 RETURN_ERROR_DESC(ERR_INVALID_STATE, "No valid output stream.");
154 }
155 }
156
165 {
166 if (m_poPin)
167 {
168 tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf::util::cSystem::GetTime();
169 RETURN_IF_FAILED(m_poPin->Run(tmTime, base::ant::IRunnable::RUN_TRIGGER, nullptr, 0));
171 }
172 RETURN_ERROR_DESC(ERR_NOT_INITIALIZED, "No connected pin.");
173 }
174};
175
187{
188 oSampleWriter.Transmit(pSample);
189 return oSampleWriter;
190}
191
201{
203 return oSampleWriter;
204}
205
214{
215 oSampleWriter.ChangeType(pType);
216 return oSampleWriter;
217}
218
226inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, cSampleWriter& (*pStreamfunc)(cSampleWriter&) )
227{
228 return pStreamfunc(oSampleWriter);
229}
230
238inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, const tResult& oError)
239{
240 oSampleWriter.SetStreamError(oError);
241 return oSampleWriter;
242}
243
248template<typename DATA_TYPE>
250{
251public:
293 tResult Transmit(const DATA_TYPE& oSampleData)
294 {
296 RETURN_IF_FAILED(adtf::streaming::alloc_sample(pSample));
297 tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf_util::cSystem::GetTime();
298 RETURN_IF_FAILED(pSample->Set(tmTime, &oSampleData, sizeof(DATA_TYPE)));
300 }
301};
302
313template<typename DATA_TYPE>
314ADTF3_DEPRECATED("Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
315sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
316{
317 oSampleWriter.Transmit(oSampleData);
318 return oSampleWriter;
319}
320
328inline cSampleReader& operator>>(cSampleReader& oSampleReader, cSampleWriter& oSampleWriter)
329{
331 while (IS_OK(oSampleReader.GetNextSample(pSample)))
332 {
333 oSampleWriter << pSample;
334 };
335 return oSampleReader;
336}
337
345 const char* strNameOfWriter,
347{
348 oWriter.SetName(strNameOfWriter);
349 return oWriter.SetType(pStreamType);
350}
351
352} // namespace ant
353
354namespace flash
355{
360class cSampleWriter : public ISampleWriter
361{
362public:
363 cSampleWriter();
364 ~cSampleWriter() override;
365
366 void SetName(const char* strName) override;
367 tResult GetName(base::ant::IString&& strName) override;
368
371
372 tResult BeginStreaming(ISampleStream& oSampleStream) override;
374
383
385
391
393 tResult Flush() override;
396
397protected:
399 class cImplementation;
400 std::unique_ptr<cImplementation> m_pImplementation;
402};
403
404inline tResult make_sample_writer(ISampleWriter& oWriter,
405 const char* strNameOfWriter,
407{
408 oWriter.SetName(strNameOfWriter);
409 return oWriter.SetType(pStreamType);
410}
411
421{
422 THROW_IF_FAILED(oSampleWriter.Write(pSample));
423 return oSampleWriter;
424}
425
435{
436 THROW_IF_FAILED(oSampleWriter.ChangeType(pType));
437 return oSampleWriter;
438}
439
447inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, ISampleWriter& (*pStreamfunc)(ISampleWriter&) )
448{
449 return pStreamfunc(oSampleWriter);
450}
451
459inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const tResult& oError)
460{
461 THROW_IF_FAILED(oSampleWriter.SetStreamError(oError));
462 return oSampleWriter;
463}
464
473template<typename DATA_TYPE>
474inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, output_sample_data<DATA_TYPE>&& oOutputSampleData)
475{
476 THROW_IF_FAILED(oSampleWriter.Write(oOutputSampleData.Release()));
477 return oSampleWriter;
478}
479
480inline ISampleReader& operator>>(ISampleReader& oSampleReader, ISampleWriter& oSampleWriter)
481{
483 while (IS_OK(oSampleReader.GetNextSample(pSample)))
484 {
485 oSampleWriter << pSample;
486 };
487 return oSampleReader;
488}
489
497template<typename DATA_TYPE, typename BASE = cSampleWriter>
498class sample_writer : public BASE
499{
500public:
506 {
507 _runtime->GetObject(m_pClock);
508 }
509
510 using BASE::Write;
511 using BASE::Transmit;
512
522 ADTF3_DEPRECATED("Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
523 tResult Write(const DATA_TYPE& oSampleData)
524 {
525 auto tmTime = m_pClock ? m_pClock->GetStreamTimeNs() :
526 base::flash::duration_cast<base::flash::tNanoSeconds>(adtf_util::cSystem::GetTime());
527 return Write(tmTime, oSampleData);
528 }
529
537 tResult Write(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
538 {
540 }
541
549 tResult Write(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
550 {
551 static_assert(!std::is_pointer_v<DATA_TYPE>, "pointer types are not allowed for sample writing. To support "
552 "this you need to specialize the sample_writer template");
553
555 RETURN_IF_FAILED(ant::alloc_sample(pSample));
556 ucom::ant::object_ptr<ISample> pFlashSample(pSample);
557 RETURN_IF_POINTER_NULL(pFlashSample);
558 RETURN_IF_FAILED(pFlashSample->SetTime(tmTime));
559 {
561 const adtf::base::adtf_memory<const DATA_TYPE> oMemory(&oSampleData);
562 RETURN_IF_FAILED(pFlashSample->WriteLock(pSampleBuffer, oMemory.GetSize()));
563 pSampleBuffer->Write(oMemory);
564 }
565 return BASE::Write(pSample);
566 }
567
568 ADTF3_DEPRECATED("Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
569 tResult Transmit(const DATA_TYPE& oSampleData)
570 {
571 return Write(oSampleData);
572 }
573
577 tResult Transmit(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
578 {
580 }
581
585 tResult Transmit(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
586 {
587 return Write(tmTime, oSampleData);
588 }
589
590protected:
597};
598
599template<typename DATA_TYPE>
600ADTF3_DEPRECATED("Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
601sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
602{
603 THROW_IF_FAILED(oSampleWriter.Write(oSampleData));
604 return oSampleWriter;
605}
606
607} // namespace flash
608
609using flash::cSampleWriter;
610using flash::sample_writer;
611using flash::make_sample_writer;
612
613} // namespace streaming
614} // namespace adtf
#define ADTF3_DEPRECATED(_depr_message_)
Definition adtf_base_deprecated.h:27
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_ERROR_DESC(_code,...)
Same as RETURN_ERROR(_error) using a printf like parameter list for detailed error description.
Definition result.h:44
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define IS_OK(s)
Check if result is OK.
Definition result.h:17
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult
Definition result.h:88
#define RETURN_IF_POINTER_NULL(_ptr)
Return ERR_POINTER if _ptr is nullptr, which requires the calling function's return type to be tResul...
Definition result.h:49
@ RUN_TRIGGER
Trigger run call.
Definition runnable_intf.h:63
Definition string_intf.h:29
Definition rawmemory_base.h:412
size_t GetSize() const override
Retrieves the size of the content of the assigned value of type T.
Definition rawmemory_base.h:479
@ PushRead
Definition samplestreamaccess_intf.h:31
Definition samplestream_intf.h:30
virtual tResult Open(const char *strName, adtf::ucom::ant::iobject_ptr< ISampleInStream > &pInStream, const adtf::ucom::ant::iobject_ptr< const IStreamType > &pInitialAcceptedStreamType, IPushReadEventSink *&pPushEventSink, ISampleStreamAccess::tMode ui32Mode, size_t szQueueSize)=0
The default Sample Reader will read the incomung Stream of a IInPin. This sample reader has no intern...
Definition samplereader.h:177
virtual tResult GetNextSample(ucom::ant::iobject_ptr< const ISample > &pSample)
Definition samplereader.h:340
Definition samplewriter.h:34
virtual tResult ChangeType(const ucom::ant::iobject_ptr< const IStreamType > &pType)
Definition samplewriter.h:112
cSampleWriter(const char *strName, const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
Definition samplewriter.h:57
sample_streamer< ISampleWriter, cOutPin > base_type
base type
Definition samplewriter.h:40
virtual tResult Transmit(const ucom::ant::iobject_ptr< const ISample > &pSample)
Definition samplewriter.h:94
virtual tResult SetStreamError(tResult oErr)
Definition samplewriter.h:144
virtual tResult Flush()
Definition samplewriter.h:126
tResult BeginStreaming(ISampleStream &oSampleStream) override
Definition samplewriter.h:62
tResult EndStreaming() override
Definition samplewriter.h:81
tResult ManualTrigger()
Definition samplewriter.h:164
ucom::object_ptr< ISampleOutStream > m_pOutStream
The SampleOutStream which is opened while BeginStreaming.
Definition samplewriter.h:37
virtual ~cSampleWriter()
DTOR.
Definition samplewriter.h:51
cSampleWriter()
CTOR.
Definition samplewriter.h:47
ucom::object_ptr< const IStreamType > m_pStreamType
Definition samplereader.h:49
adtf_util::cString m_strName
Definition samplereader.h:47
tResult SetType(const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
Definition samplereader.h:140
ucom::object_ptr< cOutPin > m_poPin
Definition samplereader.h:51
void SetName(const char *strName)
Definition samplereader.h:84
Definition samplewriter.h:250
tResult Transmit(const DATA_TYPE &oSampleData)
Definition samplewriter.h:293
Definition samplestreamer_intf.h:188
virtual tResult GetNextSample(ucom::ant::iobject_ptr< const ant::ISample > &pSample)=0
virtual void SetName(const char *strName)=0
virtual tResult SetType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType)=0
Definition samplestreamer_intf.h:234
virtual tResult ChangeType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pType)=0
virtual tResult SetStreamError(tResult oError)=0
virtual tResult Write(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)=0
tResult GetStreamerPin(ucom::ant::iobject_ptr< IStreamerPin > &pStreamerPin) const
void SetName(const char *strName) override
tResult GetType(ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType) const override
tResult Write(const ucom::ant::iobject_ptr< const ant::ISample > &pSample) override
tResult ChangeType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pType) override
tResult ManualTrigger(base::flash::tNanoSeconds tmActivationTime=base::flash::tNanoSeconds{-1}) override
tResult BeginStreaming(ISampleStream &oSampleStream) override
tResult GetName(base::ant::IString &&strName) override
virtual tResult Transmit(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
tResult SetType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType) override
tResult SetStreamerPin(const ucom::ant::iobject_ptr< IStreamerPin > &pStreamerPin) override
tResult SetStreamError(tResult oError) override
Definition samplewriter.h:499
sample_writer()
Default CTOR.
Definition samplewriter.h:505
ucom::ant::object_ptr< services::flash::IReferenceClock > m_pClock
Definition samplewriter.h:596
tResult Transmit(base::flash::tNanoSeconds tmTime, const DATA_TYPE &oSampleData)
Writes Data of the given DATA_TYPE. The Sample Time of the created Sample is retrieved from the IRefe...
Definition samplewriter.h:585
tResult Write(tTimeStamp tmTime, const DATA_TYPE &oSampleData)
Definition samplewriter.h:537
tResult Write(base::flash::tNanoSeconds tmTime, const DATA_TYPE &oSampleData)
Definition samplewriter.h:549
tResult Transmit(tTimeStamp tmTime, const DATA_TYPE &oSampleData)
Writes Data of the given DATA_TYPE. The Sample Time of the created Sample is retrieved from the IRefe...
Definition samplewriter.h:577
tResult Write(const DATA_TYPE &oSampleData)
Writes Data of the given DATA_TYPE. The Sample Time of the created Sample is retrieved from the IRefe...
Definition samplewriter.h:523
Base object pointer to realize binary compatible reference counting in interface methods.
Definition object_ptr_intf.h:112
Definition lockedobject_intf.h:207
Definition object_ptr.h:384
DestinationTimeStamp duration_cast(const SourceTimeStamp &) noexcept
Duration cast base template to converted between different time resolution.
Definition chrono.h:155
Namespace for all functionality of the ADTF Streaming SDK provided since v3.0.
Definition bindingproxyoutport.h:16
tResult make_sample_writer(cSampleWriter &oWriter, const char *strNameOfWriter, const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
Definition samplewriter.h:344
const ISampleInStream & operator>>(const ISampleInStream &oStreamReader, IStreamItem &oItem)
Definition sampleinstream_intf.h:80
ISampleOutStream & operator<<(ISampleOutStream &oStreamWriter, const adtf::ucom::ant::iobject_ptr< const ISample > &pSample)
Definition sampleoutstream_intf.h:97
tResult alloc_sample(ucom::ant::iobject_ptr< ucom::ant::IObject > &pSampleObject, const char *strSampleCID)
ISampleWriter & operator<<(ISampleWriter &oSampleWriter, const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
streaming operator to write a sample to the sample writer
Definition samplewriter.h:419
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14
tResult alloc_sample(ucom::ant::iobject_ptr< ucom::ant::IObject > &pSample)
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Alias always bringing the latest version of ant::ucom_object_ptr_cast() into scope.
Definition object_ptr_utilities.h:104
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
A timestamp with nanosecond precision.
Definition chrono.h:23