ADTF  3.14.3
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 
17 #include "reference_clock_intf.h"
18 
19 
20 namespace adtf
21 {
22 namespace streaming
23 {
24 namespace ant
25 {
33 class cSampleWriter : public sample_streamer<ISampleWriter, cOutPin>
34 {
35 protected:
38 
41 
42 protected:
44 
45 public:
48  {
49  }
51  virtual ~cSampleWriter()
52  {
53  }
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 
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 
78  RETURN_NOERROR;
79  }
80 
81  tResult EndStreaming() override
82  {
83  _clock.Reset();
84  m_pOutStream.Reset();
85  RETURN_NOERROR;
86  }
87 
88 public: //Streaming Functions for
94  virtual tResult Transmit(const ucom::ant::iobject_ptr<const ISample>& pSample)
95  {
96  if (m_pOutStream)
97  {
98  return m_pOutStream->Write(pSample);
99  }
100  else
101  {
102  RETURN_ERROR(ERR_INVALID_STATE);
103  }
104  }
105 
112  {
113  if (m_pOutStream)
114  {
115  return m_pOutStream->Write(pType);
116  }
117  else
118  {
119  return SetType(pType);
120  }
121  }
122 
127  virtual tResult Flush()
128  {
129  if (m_pOutStream)
130  {
131  return m_pOutStream->Flush();
132  }
133  else
134  {
135  RETURN_ERROR(ERR_INVALID_STATE);
136  }
137  }
138 
144  virtual tResult SetStreamError(tResult oErr)
145  {
146  if (m_pOutStream)
147  {
148  return m_pOutStream->SetStreamError(oErr);
149  }
150  else
151  {
152  RETURN_ERROR(ERR_INVALID_STATE);
153  }
154  }
155 
163  tResult ManualTrigger()
164  {
165  if (m_poPin)
166  {
167  tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf::util::cSystem::GetTime();
168  return m_poPin->Run(tmTime, base::ant::IRunnable::RUN_TRIGGER, nullptr, 0);
169  }
170  RETURN_ERROR(ERR_NOT_INITIALIZED);
171  }
172 };
173 
184 {
185  oSampleWriter.Transmit(pSample);
186  return oSampleWriter;
187 }
188 
198 {
199  oSampleWriter.Transmit(ucom::ucom_object_ptr_cast<const ISample>(pSample));
200  return oSampleWriter;
201 }
211 {
212  oSampleWriter.ChangeType(pType);
213  return oSampleWriter;
214 }
223 inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, cSampleWriter& (*pStreamfunc)(cSampleWriter&) )
224 {
225  return pStreamfunc(oSampleWriter);
226 }
235 inline cSampleWriter& operator<<(cSampleWriter& oSampleWriter, const tResult& oError)
236 {
237  oSampleWriter.SetStreamError(oError);
238  return oSampleWriter;
239 }
240 
245 template<typename DATA_TYPE>
247 {
248 public:
289  tResult Transmit(const DATA_TYPE& oSampleData)
290  {
292  if (_runtime && IS_OK(_runtime->CreateInstance(ucom::ant::get_class_id<cSample>(), pSample)))
293  {
294  //IS OK
295  //this path is usually used if sample pool is used in system
296  }
297  else
298  {
299  pSample = ucom::ant::make_object_ptr<cSample>();
300  }
301  tTimeStamp tmTime = _clock ? _clock->GetStreamTime() : adtf_util::cSystem::GetTime();
302  RETURN_IF_FAILED(pSample->Set(tmTime, &oSampleData, sizeof(DATA_TYPE)));
304  }
305 };
306 
316 template<typename DATA_TYPE>
317 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
318 {
319  oSampleWriter.Transmit(oSampleData);
320  return oSampleWriter;
321 }
322 
330 inline cSampleReader& operator>>(cSampleReader& oSampleReader, cSampleWriter& oSampleWriter)
331 {
333  while (IS_OK(oSampleReader.GetNextSample(pSample)))
334  {
335  oSampleWriter << pSample;
336  };
337  return oSampleReader;
338 }
339 
346 inline tResult make_sample_writer(cSampleWriter& oWriter,
347  const char* strNameOfWriter,
349 {
350  oWriter.SetName(strNameOfWriter);
351  return oWriter.SetType(pStreamType);
352 }
353 
354 } //namespace ant
355 
356 namespace flash
357 {
362 {
363 public:
364  cSampleWriter();
365  ~cSampleWriter() override;
366 
367  void SetName(const char* strName) override;
368  tResult GetName(base::ant::IString&& strName) override;
369 
370  tResult SetType(const ucom::ant::iobject_ptr<const ant::IStreamType>& pStreamType) override;
371  tResult GetType(ucom::ant::iobject_ptr<const ant::IStreamType>& pStreamType) const override;
372 
373  tResult BeginStreaming(ISampleStream& oSampleStream) override;
374  tResult EndStreaming() override;
375 
376  tResult SetStreamerPin(const ucom::ant::iobject_ptr<IStreamerPin>& pStreamerPin) override;
377 
378  tResult Write(const ucom::ant::iobject_ptr<const ant::ISample>& pSample) override;
379 
384  virtual tResult Transmit(const ucom::ant::iobject_ptr<const ant::ISample>& pSample);
385 
387  tResult Flush() override;
388  tResult SetStreamError(tResult oError) override;
389  tResult ManualTrigger(base::flash::tNanoSeconds tmActivationTime = base::flash::tNanoSeconds {-1}) override;
390 
391 protected:
392  class cImplementation;
393  std::unique_ptr<cImplementation> m_pImplementation;
394 };
395 
396 inline tResult make_sample_writer(ISampleWriter& oWriter,
397  const char* strNameOfWriter,
399 {
400  oWriter.SetName(strNameOfWriter);
401  return oWriter.SetType(pStreamType);
402 }
403 
404 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const ucom::ant::iobject_ptr<const ant::ISample>& pSample)
405 {
406  oSampleWriter.Write(pSample);
407  return oSampleWriter;
408 }
409 
410 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const ucom::ant::iobject_ptr<const ant::IStreamType>& pType)
411 {
412  oSampleWriter.ChangeType(pType);
413  return oSampleWriter;
414 }
415 
416 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, ISampleWriter& (*pStreamfunc)(ISampleWriter&) )
417 {
418  return pStreamfunc(oSampleWriter);
419 }
420 
421 inline ISampleWriter& operator<<(ISampleWriter& oSampleWriter, const tResult& oError)
422 {
423  oSampleWriter.SetStreamError(oError);
424  return oSampleWriter;
425 }
426 
427 inline ISampleReader& operator>>(ISampleReader& oSampleReader, ISampleWriter& oSampleWriter)
428 {
429  ucom::ant::object_ptr<const ant::ISample> pSample;
430  while (IS_OK(oSampleReader.GetNextSample(pSample)))
431  {
432  oSampleWriter << pSample;
433  };
434  return oSampleReader;
435 }
436 
443 template<typename DATA_TYPE, typename BASE = cSampleWriter>
444 class sample_writer : public BASE
445 {
446 public:
452  {
453  _runtime->GetObject(m_pClock);
454  }
455 
463  tResult Write(const DATA_TYPE& oSampleData)
464  {
465  auto tmTime = m_pClock ? m_pClock->GetStreamTimeNs() : base::flash::duration_cast<base::flash::tNanoSeconds>(adtf_util::cSystem::GetTime());
466  return Write(tmTime, oSampleData);
467  }
468 
469  tResult Write(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
470  {
471  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
472  }
473 
474  tResult Write(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
475  {
478  ucom::ant::object_ptr<ISample> pFlashSample(pSample);
479  RETURN_IF_POINTER_NULL(pFlashSample);
480  RETURN_IF_FAILED(pFlashSample->Set(tmTime, &oSampleData, sizeof(DATA_TYPE)));
481  return cSampleWriter::Write(pSample);
482  }
483 
484  tResult Transmit(const DATA_TYPE& oSampleData)
485  {
486  return Write(oSampleData);
487  }
488 
489  tResult Transmit(tTimeStamp tmTime, const DATA_TYPE& oSampleData)
490  {
491  return Write(base::flash::duration_cast<base::flash::tNanoSeconds>(tmTime), oSampleData);
492  }
493 
494  tResult Transmit(base::flash::tNanoSeconds tmTime, const DATA_TYPE& oSampleData)
495  {
496  return Write(tmTime, oSampleData);
497  }
498 
499 protected:
500  ucom::ant::object_ptr<services::flash::IReferenceClock> m_pClock;
501 };
502 
503 template<typename DATA_TYPE>
504 sample_writer<DATA_TYPE>& operator<<(sample_writer<DATA_TYPE>& oSampleWriter, const DATA_TYPE& oSampleData)
505 {
506  oSampleWriter.Write(oSampleData);
507  return oSampleWriter;
508 }
509 
510 } // namespace flash
511 
512 using flash::cSampleWriter;
513 using flash::sample_writer;
514 using flash::make_sample_writer;
515 
516 } //namespace streaming
517 } //namespace adtf
@ 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:24
@ 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:364
This base implementation of a sample writer is able to write to one Sample Stream which is connected ...
Definition: samplewriter.h:34
sample_streamer< ISampleWriter, cOutPin > base_type
base type
Definition: samplewriter.h:40
ucom::object_ptr< ISampleOutStream > m_pOutStream
The SampleOutStream which is opened while BeginStreaming.
Definition: samplewriter.h:37
cSampleWriter(const char *strName, const ucom::ant::iobject_ptr< const IStreamType > &pStreamType)
CTOR with name and type.
Definition: samplewriter.h:57
virtual tResult Transmit(const ucom::ant::iobject_ptr< const ISample > &pSample)
Transmits and writes a sample reference to the opened ISampleOutStream.
Definition: samplewriter.h:94
tResult EndStreaming() override
Sample Stream disconnected.
Definition: samplewriter.h:81
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:111
tResult ManualTrigger()
This is to Run a trigger call manually.
Definition: samplewriter.h:163
virtual tResult Flush()
Flushes a the opened ISampleOutStream.
Definition: samplewriter.h:127
tResult BeginStreaming(ISampleStream &oSampleStream) override
BeginStreaming will open the given Sample Stream for Writing while a connection is establishing.
Definition: samplewriter.h:62
virtual tResult SetStreamError(tResult oErr)
Writes an error to the opened ISampleOutStream.
Definition: samplewriter.h:144
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:247
tResult Transmit(const DATA_TYPE &oSampleData)
Definition: samplewriter.h:289
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:362
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.
Specialized Sample Writer to write data of a given type DATA_TYPE.
Definition: samplewriter.h:445
tResult Write(const DATA_TYPE &oSampleData)
Writes Data of the given DATA_TYPE.
Definition: samplewriter.h:463
virtual tResult Write(const void *pvBuffer, size_t nBufferSize, size_t *pnBytesWritten=nullptr)=0
Write data to stream.
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.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:158
void Reset()
Reset this object_ptr.
Definition: object_ptr.h:356
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:346
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.
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 RETURN_IF_FAILED(s)
returns if the expression returns a failed tResult or throws an exception.

Copyright © Audi Electronics Venture GmbH. All rights reserved. (Generated on Thu Jun 9 2022 by doxygen 1.9.1)