ADTF  3.18.2
samplewriter.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "samplestream_intf.h"
9 #include "sampleoutstream_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"
16 #include <adtf_base_deprecated.h>
17 
18 #include "reference_clock_intf.h"
19 
20 
21 namespace adtf
22 {
23 namespace streaming
24 {
25 namespace ant
26 {
34 class cSampleWriter : public sample_streamer<ISampleWriter, cOutPin>
35 {
36 protected:
39 
42 
43 protected:
45 
46 public:
49  {
50  }
52  virtual ~cSampleWriter()
53  {
54  }
58  cSampleWriter(const char* strName, const ucom::ant::iobject_ptr<const IStreamType>& pStreamType):
59  base_type(strName, pStreamType)
60  {
61  }
62 
63  tResult BeginStreaming(ISampleStream& oSampleStream) override
64  {
66 
68 
69  if (pConstType)
70  {
71  RETURN_IF_FAILED(m_pOutStream->Write(pConstType));
72  }
73 
74  if (_runtime)
75  {
76  _runtime->GetObject(_clock);
77  }
78 
80  }
81 
82  tResult EndStreaming() override
83  {
84  _clock.Reset();
85  m_pOutStream.Reset();
87  }
88 
89 public: //Streaming Functions for
96  {
97  if (m_pOutStream)
98  {
99  return m_pOutStream->Write(pSample);
100  }
101  else
102  {
103  RETURN_ERROR(ERR_INVALID_STATE);
104  }
105  }
106 
113  {
114  if (m_pOutStream)
115  {
116  return m_pOutStream->Write(pType);
117  }
118  else
119  {
120  return SetType(pType);
121  }
122  }
123 
128  virtual tResult Flush()
129  {
130  if (m_pOutStream)
131  {
132  return m_pOutStream->Flush();
133  }
134  else
135  {
136  RETURN_ERROR(ERR_INVALID_STATE);
137  }
138  }
139 
146  {
147  if (m_pOutStream)
148  {
149  return m_pOutStream->SetStreamError(oErr);
150  }
151  else
152  {
153  RETURN_ERROR(ERR_INVALID_STATE);
154  }
155  }
156 
165  {
166  if (m_poPin)
167  {
168  tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf::util::cSystem::GetTime();
169  return m_poPin->Run(tmTime, base::ant::IRunnable::RUN_TRIGGER, nullptr, 0);
170  }
171  RETURN_ERROR(ERR_NOT_INITIALIZED);
172  }
173 };
174 
185 {
186  oSampleWriter.Transmit(pSample);
187  return oSampleWriter;
188 }
189 
199 {
200  oSampleWriter.Transmit(ucom::ucom_object_ptr_cast<const ISample>(pSample));
201  return oSampleWriter;
202 }
212 {
213  oSampleWriter.ChangeType(pType);
214  return oSampleWriter;
215 }
224 inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, cSampleWriter& (*pStreamfunc)(cSampleWriter&) )
225 {
226  return pStreamfunc(oSampleWriter);
227 }
236 inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, const tResult& oError)
237 {
238  oSampleWriter.SetStreamError(oError);
239  return oSampleWriter;
240 }
241 
246 template<typename DATA_TYPE>
248 {
249 public:
290  tResult Transmit(const DATA_TYPE& oSampleData)
291  {
293  if (_runtime && IS_OK(_runtime->CreateInstance(ucom::ant::get_class_id<cSample>(), pSample)))
294  {
295  //IS OK
296  //this path is usually used if sample pool is used in system
297  }
298  else
299  {
300  pSample = ucom::ant::make_object_ptr<cSample>();
301  }
302  tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf_util::cSystem::GetTime();
303  RETURN_IF_FAILED(pSample->Set(tmTime, &oSampleData, sizeof(DATA_TYPE)));
305  }
306 };
307 
317 template<typename DATA_TYPE>
318 ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
319 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
320 {
321  oSampleWriter.Transmit(oSampleData);
322  return oSampleWriter;
323 }
324 
332 inline cSampleReader& operator>>(cSampleReader& oSampleReader, cSampleWriter& oSampleWriter)
333 {
335  while (IS_OK(oSampleReader.GetNextSample(pSample)))
336  {
337  oSampleWriter << pSample;
338  };
339  return oSampleReader;
340 }
341 
349  const char* strNameOfWriter,
351 {
352  oWriter.SetName(strNameOfWriter);
353  return oWriter.SetType(pStreamType);
354 }
355 
356 } //namespace ant
357 
358 namespace flash
359 {
364 {
365 public:
366  cSampleWriter();
367  ~cSampleWriter() override;
368 
369  void SetName(const char* strName) override;
370  tResult GetName(base::ant::IString&& strName) override;
371 
374 
375  tResult BeginStreaming(ISampleStream& oSampleStream) override;
376  tResult EndStreaming() override;
377 
379 
381 
387 
389  tResult Flush() override;
390  tResult SetStreamError(tResult oError) override;
392 
393 protected:
394  class cImplementation;
395  std::unique_ptr<cImplementation> m_pImplementation;
396 };
397 
398 inline tResult make_sample_writer(ISampleWriter& oWriter,
399  const char* strNameOfWriter,
401 {
402  oWriter.SetName(strNameOfWriter);
403  return oWriter.SetType(pStreamType);
404 }
405 
415 {
416  THROW_IF_FAILED(oSampleWriter.Write(pSample));
417  return oSampleWriter;
418 }
419 
429 {
430  THROW_IF_FAILED(oSampleWriter.ChangeType(pType));
431  return oSampleWriter;
432 }
433 
442 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, ISampleWriter& (*pStreamfunc)(ISampleWriter&) )
443 {
444  return pStreamfunc(oSampleWriter);
445 }
446 
455 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const tResult& oError)
456 {
457  THROW_IF_FAILED(oSampleWriter.SetStreamError(oError));
458  return oSampleWriter;
459 }
460 
470 template<typename DATA_TYPE>
471 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, output_sample_data<DATA_TYPE>&& oOutputSampleData)
472 {
473  THROW_IF_FAILED(oSampleWriter.Write(oOutputSampleData.Release()));
474  return oSampleWriter;
475 }
476 
477 inline ISampleReader& operator>>(ISampleReader& oSampleReader, ISampleWriter& oSampleWriter)
478 {
480  while (IS_OK(oSampleReader.GetNextSample(pSample)))
481  {
482  oSampleWriter << pSample;
483  };
484  return oSampleReader;
485 }
486 
496 template<typename DATA_TYPE, typename BASE = cSampleWriter>
497 class sample_writer : public BASE
498 {
499 public:
505  {
506  _runtime->GetObject(m_pClock);
507  }
508 
509  using BASE::Write;
510  using BASE::Transmit;
511 
520  ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
521  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
522  tResult Write(const DATA_TYPE& oSampleData)
523  {
524  auto tmTime = m_pClock ? m_pClock->GetStreamTimeNs() : base::flash::duration_cast<base::flash::tNanoSeconds>(adtf_util::cSystem::GetTime());
525  return Write(tmTime, oSampleData);
526  }
527 
528  tResult Write(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
529  {
530  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
531  }
532 
533  tResult Write(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
534  {
535  static_assert(!std::is_pointer_v<DATA_TYPE>,
536  "pointer types are not allowed for sample writing. To support this you need to specialize the sample_writer template");
537 
540  ucom::ant::object_ptr<ISample> pFlashSample(pSample);
541  RETURN_IF_POINTER_NULL(pFlashSample);
542  RETURN_IF_FAILED(pFlashSample->SetTime(tmTime));
544  RETURN_IF_FAILED(pFlashSample->WriteLock(pSampleBuffer, 0));
545  pSampleBuffer->Write(adtf::base::adtf_memory<const DATA_TYPE>(&oSampleData));
546  return BASE::Write(pSample);
547  }
548 
549  ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
550  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
551  tResult Transmit(const DATA_TYPE& oSampleData)
552  {
553  return Write(oSampleData);
554  }
555 
556  tResult Transmit(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
557  {
558  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
559  }
560 
561  tResult Transmit(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
562  {
563  return Write(tmTime, oSampleData);
564  }
565 
566 protected:
567  ucom::ant::object_ptr<services::flash::IReferenceClock> m_pClock;
568 };
569 
570 template<typename DATA_TYPE>
571 ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
572  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
573 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter,
574  const DATA_TYPE& oSampleData)
575 {
576  THROW_IF_FAILED(oSampleWriter.Write(oSampleData));
577  return oSampleWriter;
578 }
579 
580 } // namespace flash
581 
582 using flash::cSampleWriter;
583 using flash::sample_writer;
584 using flash::make_sample_writer;
585 
586 } //namespace streaming
587 } //namespace adtf
Copyright © Audi Electronics Venture GmbH.
#define ADTF3_DEPRECATED(_depr_message_)
Mark a function or variable as deprecated.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
#define RETURN_IF_POINTER_NULL(_ptr)
Return ERR_POINTER if _ptr is nullptr, which requires the calling function's return type to be tResul...
@ RUN_TRIGGER
Trigger run call.
Definition: runnable_intf.h:60
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition: string_intf.h:28
Template class implementation for the ant::IRawMemory interface (see Supported types for adtf_memory<...
@ PushRead
PushRead Operation mode for the ISampleStream::Open. See PushRead Mode - Synchronous Data Pipes.
Interface of the SampleStream.
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
Opens The SampleStream for reading access.
The default Sample Reader will read the incomung Stream of a IInPin.
Definition: samplereader.h:174
virtual tResult GetNextSample(ucom::ant::iobject_ptr< const ISample > &pSample)
Gets the next Sample within internal queue which not has been read.
Definition: samplereader.h:346
This base implementation of a sample writer is able to write to one Sample Stream which is connected ...
Definition: samplewriter.h:35
sample_streamer< ISampleWriter, cOutPin > base_type
base type
Definition: samplewriter.h:41
ucom::object_ptr< ISampleOutStream > m_pOutStream
The SampleOutStream which is opened while BeginStreaming.
Definition: samplewriter.h:38
cSampleWriter(const char *strName, const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
CTOR with name and type.
Definition: samplewriter.h:58
virtual tResult Transmit(const ucom::ant::iobject_ptr< const ISample > &pSample)
Transmits and writes a sample reference to the opened ISampleOutStream.
Definition: samplewriter.h:95
tResult EndStreaming() override
Sample Stream disconnected.
Definition: samplewriter.h:82
virtual tResult ChangeType(const ucom::ant::iobject_ptr< const IStreamType > &pType)
Changes and writes a stream type reference to the opened ISampleOutStream.
Definition: samplewriter.h:112
tResult ManualTrigger()
This is to Run a trigger call manually.
Definition: samplewriter.h:164
virtual tResult Flush()
Flushes a the opened ISampleOutStream.
Definition: samplewriter.h:128
tResult BeginStreaming(ISampleStream &oSampleStream) override
BeginStreaming will open the given Sample Stream for Writing while a connection is establishing.
Definition: samplewriter.h:63
virtual tResult SetStreamError(tResult oErr)
Writes an error to the opened ISampleOutStream.
Definition: samplewriter.h:145
Helper template can be used to implement ISampleStreamer.
Definition: samplereader.h:41
ucom::object_ptr< cOutPin > m_poPin
pin reference reading/writing from
Definition: samplereader.h:50
void SetName(const char *strName)
Sets the name of the streamer.
Definition: samplereader.h:84
tResult SetType(const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
Sets the StreamType of the streamer.
Definition: samplereader.h:140
ucom::object_ptr< const IStreamType > m_pStreamType
stream type of the streamer
Definition: samplereader.h:48
adtf_util::cString m_strName
name of the streamer (used i.e. to create the pins name)
Definition: samplereader.h:46
Specialized Sample Writer to write DATA of type DATA_TYPE.
Definition: samplewriter.h:248
tResult Transmit(const DATA_TYPE &oSampleData)
Definition: samplewriter.h:290
Interface for sample reads that read from sample streams via input pins.
virtual tResult GetNextSample(ucom::ant::iobject_ptr< const ant::ISample > &pSample)=0
Reads the next available sample from the associated sample stream.
virtual void SetName(const char *strName)=0
Sets the name of the streamer.
virtual tResult SetType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType)=0
Sets the initial stream type of a streamer.
Interface for sample writers that write to sample streams via output pins.
This base implementation of a sample writer is samples and types to the given pin set at adtf::stream...
Definition: samplewriter.h:364
virtual tResult Transmit(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
For compatibility purposes, call Write() instead.
tResult GetType(ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType) const override
Returns the initial stream type of the streamer.
tResult ManualTrigger(base::flash::tNanoSeconds tmActivationTime=base::flash::tNanoSeconds {-1}) override
Calls all connected runners synchronously in this call.
tResult ChangeType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pType) override
Communicates a type change for all following samples.
void SetName(const char *strName) override
Sets the name of the streamer.
tResult GetName(base::ant::IString &&strName) override
Retrieves the name of the streamer.
tResult EndStreaming() override
End streaming.
tResult SetStreamError(tResult oError) override
Reports and handles an error via the sample stream.
tResult Flush() override
Flushes the writer.
tResult SetStreamerPin(const ucom::ant::iobject_ptr< IStreamerPin > &pStreamerPin) override
Sets the pin that the streamer is associated with.
tResult SetType(const ucom::ant::iobject_ptr< const ant::IStreamType > &pStreamType) override
Sets the initial stream type of a streamer.
tResult BeginStreaming(ISampleStream &oSampleStream) override
Begin streaming on the given sample stream.
tResult Write(const ucom::ant::iobject_ptr< const ant::ISample > &pSample) override
Writes a sample to the connected sample stream.
Wrapper class that facilitates the handling of output samples.
Definition: sample.h:347
Specialized Sample Writer to write data of a given type DATA_TYPE (see Supported types for adtf_memor...
Definition: samplewriter.h:498
ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use " "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.") tResult Write(const DATA_TYPE &oSampleData)
Writes Data of the given DATA_TYPE.
Definition: samplewriter.h:520
virtual tResult CreateInstance(const char *strCID, iobject_ptr< IObject > &pObject, const tChar *strNameOfObject="") const =0
Creates a new instance of an object.
virtual tResult GetObject(iobject_ptr< IObject > &pObject, const char *strNameOID) const =0
Get registered object from object registry.
Base object pointer to realize binary compatible reference counting in interface methods.
Implementation for a exclusive lock guard.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
void Reset()
Reset this object_ptr.
Definition: object_ptr.h:361
ISampleOutStream & operator<<(ISampleOutStream &oStreamWriter, const adtf::ucom::ant::iobject_ptr< const ISample > &pSample)
Streaming Operator<< to write a sample to a writers queue of the ISampleStream.
tResult make_sample_writer(cSampleWriter &oWriter, const char *strNameOfWriter, const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
Definition: samplewriter.h:348
const ISampleInStream & operator>>(const ISampleInStream &oStreamReader, IStreamItem &oItem)
Streaming Operator>> to read a sample from the readers queue.
tResult alloc_sample(ucom::ant::iobject_ptr< ucom::ant::IObject > &pSampleObject, const char *strSampleCID)
Helper Function to get a Sample Instance through the adtf::ucom::ant::IRuntime.
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:414
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
adtf::ucom::IRuntime * _runtime
Global Runtime Pointer to reference to the current runtime.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult