ADTF
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  {
294  tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf_util::cSystem::GetTime();
295  RETURN_IF_FAILED(pSample->Set(tmTime, &oSampleData, sizeof(DATA_TYPE)));
297  }
298 };
299 
309 template<typename DATA_TYPE>
310 ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
311 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
312 {
313  oSampleWriter.Transmit(oSampleData);
314  return oSampleWriter;
315 }
316 
324 inline cSampleReader& operator>>(cSampleReader& oSampleReader, cSampleWriter& oSampleWriter)
325 {
327  while (IS_OK(oSampleReader.GetNextSample(pSample)))
328  {
329  oSampleWriter << pSample;
330  };
331  return oSampleReader;
332 }
333 
341  const char* strNameOfWriter,
343 {
344  oWriter.SetName(strNameOfWriter);
345  return oWriter.SetType(pStreamType);
346 }
347 
348 } //namespace ant
349 
350 namespace flash
351 {
356 {
357 public:
358  cSampleWriter();
359  ~cSampleWriter() override;
360 
361  void SetName(const char* strName) override;
362  tResult GetName(base::ant::IString&& strName) override;
363 
366 
367  tResult BeginStreaming(ISampleStream& oSampleStream) override;
368  tResult EndStreaming() override;
369 
371 
373 
379 
381  tResult Flush() override;
382  tResult SetStreamError(tResult oError) override;
384 
385 protected:
386  class cImplementation;
387  std::unique_ptr<cImplementation> m_pImplementation;
388 };
389 
390 inline tResult make_sample_writer(ISampleWriter& oWriter,
391  const char* strNameOfWriter,
393 {
394  oWriter.SetName(strNameOfWriter);
395  return oWriter.SetType(pStreamType);
396 }
397 
407 {
408  THROW_IF_FAILED(oSampleWriter.Write(pSample));
409  return oSampleWriter;
410 }
411 
421 {
422  THROW_IF_FAILED(oSampleWriter.ChangeType(pType));
423  return oSampleWriter;
424 }
425 
434 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, ISampleWriter& (*pStreamfunc)(ISampleWriter&) )
435 {
436  return pStreamfunc(oSampleWriter);
437 }
438 
447 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const tResult& oError)
448 {
449  THROW_IF_FAILED(oSampleWriter.SetStreamError(oError));
450  return oSampleWriter;
451 }
452 
462 template<typename DATA_TYPE>
463 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, output_sample_data<DATA_TYPE>&& oOutputSampleData)
464 {
465  THROW_IF_FAILED(oSampleWriter.Write(oOutputSampleData.Release()));
466  return oSampleWriter;
467 }
468 
469 inline ISampleReader& operator>>(ISampleReader& oSampleReader, ISampleWriter& oSampleWriter)
470 {
472  while (IS_OK(oSampleReader.GetNextSample(pSample)))
473  {
474  oSampleWriter << pSample;
475  };
476  return oSampleReader;
477 }
478 
488 template<typename DATA_TYPE, typename BASE = cSampleWriter>
489 class sample_writer : public BASE
490 {
491 public:
497  {
498  _runtime->GetObject(m_pClock);
499  }
500 
501  using BASE::Write;
502  using BASE::Transmit;
503 
512  ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
513  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
514  tResult Write(const DATA_TYPE& oSampleData)
515  {
516  auto tmTime = m_pClock ? m_pClock->GetStreamTimeNs() : base::flash::duration_cast<base::flash::tNanoSeconds>(adtf_util::cSystem::GetTime());
517  return Write(tmTime, oSampleData);
518  }
519 
520  tResult Write(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
521  {
522  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
523  }
524 
525  tResult Write(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
526  {
527  static_assert(!std::is_pointer_v<DATA_TYPE>,
528  "pointer types are not allowed for sample writing. To support this you need to specialize the sample_writer template");
529 
532  ucom::ant::object_ptr<ISample> pFlashSample(pSample);
533  RETURN_IF_POINTER_NULL(pFlashSample);
534  RETURN_IF_FAILED(pFlashSample->SetTime(tmTime));
535  {
537  const adtf::base::adtf_memory<const DATA_TYPE> oMemory(&oSampleData);
538  RETURN_IF_FAILED(pFlashSample->WriteLock(pSampleBuffer, oMemory.GetSize()));
539  pSampleBuffer->Write(oMemory);
540  }
541  return BASE::Write(pSample);
542  }
543 
544  ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
545  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
546  tResult Transmit(const DATA_TYPE& oSampleData)
547  {
548  return Write(oSampleData);
549  }
550 
551  tResult Transmit(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
552  {
553  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
554  }
555 
556  tResult Transmit(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
557  {
558  return Write(tmTime, oSampleData);
559  }
560 
561 protected:
562  ucom::ant::object_ptr<services::flash::IReferenceClock> m_pClock;
563 };
564 
565 template<typename DATA_TYPE>
566 ADTF3_DEPRECATED("Writing data with no concrete sample time is deprecated. Use "
567  "sample_writer<DATA_TYPE>::Write(tmTime, oSampleData) instead.")
568 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter,
569  const DATA_TYPE& oSampleData)
570 {
571  THROW_IF_FAILED(oSampleWriter.Write(oSampleData));
572  return oSampleWriter;
573 }
574 
575 } // namespace flash
576 
577 using flash::cSampleWriter;
578 using flash::sample_writer;
579 using flash::make_sample_writer;
580 
581 } //namespace streaming
582 } //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:356
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:353
Specialized Sample Writer to write data of a given type DATA_TYPE (see Supported types for adtf_memor...
Definition: samplewriter.h:490
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:512
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:340
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:406
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