ADTF  3.18.3
sample.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "sample_intf.h"
9 
11 #include <adtfucom3/object.h>
12 #include <adtfbase/chrono.h>
15 
16 #include <cstring>
17 #include <cinttypes>
18 
19 namespace adtf
20 {
21 namespace streaming
22 {
23 namespace ant
24 {
25 
49 class cSample :
50  public adtf::ucom::catwo::object<ant::ISample,
51  flash::ISample,
52  hollow::ISample,
53  ucom::ant::IClassInfo>
54 {
55 private:
58 
59 public:
61  ADTF_CLASS_ID_NAME(cSample, "sample.streaming.adtf.cid", "ADTF Default Sample");
64 
66 
67 
77  cSample(const ucom::ant::iobject_ptr<ISampleBufferFactory>& oSampleFactory);
78 
82  virtual ~cSample();
83 
84 private: //no copy, no assignment allowed - Samples will be treated only by ISample inferface !
86  cSample(const cSample&) = delete;
88  cSample(cSample&&) = delete;
90  cSample& operator=(const cSample&) = delete;
92  cSample& operator=(cSample&&) = delete;
93 
94 public: //implements ant::ISample
95  tResult Lock(ucom::ant::iobject_ptr_shared_locked<const adtf::streaming::ant::ISampleBuffer>& oSampleBuffer) const override;
96  tResult WriteLock(ucom::ant::iobject_ptr_locked<ISampleBuffer>& oSampleBuffer, size_t szPreAllocateSize) override;
97  tResult Set(const ant::ISample& oSample) override;
98  tResult Get(ant::ISample& oSample) const override;
99  tResult Reset() override;
100  tTimeStamp GetTime() const;
101  tResult SetTime(tTimeStamp tmTimeStamp);
102  uint32_t GetFlags() const override;
103  tResult SetFlags(uint32_t ui32Flags) override;
104  tResult Set(tTimeStamp tmTime,
105  const void* pData,
106  size_t nSize,
107  uint32_t ui32Flags=0) override;
108 
109  tResult GetSampleBufferFactory(ucom::ant::iobject_ptr<ISampleBufferFactory>& oFactory) const override;
110  tResult GetSampleInfo(ucom::ant::iobject_ptr<const ISampleInfo>& oSampleInfo) const override;
111  tResult GetSampleInfo(ucom::ant::iobject_ptr<ISampleInfo>& oSampleInfo) override;
112  tResult GetSampleLog(ucom::ant::iobject_ptr<ISampleLog>& oSampleLog) const override;
113 
114 public: //implements flash::ISample
115  base::flash::tNanoSeconds GetTimeNs() const override;
116  tResult SetTime(base::flash::tNanoSeconds tmTimeStamp) override;
117  tResult Set(base::flash::tNanoSeconds tmTimeStamp,
118  const void* pData,
119  size_t szSize,
120  uint32_t ui32Flags=0) override;
121 
122 public: //implements hollow::ISample
123  uint32_t GetSubStreamId() const override;
124  void SetSubStreamId(uint32_t nSubStreamId) override;
125 
126 public:
137 
138  tResult SetSampleInfo(const ucom::ant::iobject_ptr<ISampleInfo>& oInfo);
139  tResult SetSampleLog(const ucom::ant::iobject_ptr<ISampleLog>& oLog);
140 };
141 
149 {
150  public:
156 };
157 
158 //helper functions for Sampleallocation
170 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSampleObject, const char* strSampleCID);
171 
184 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
185 
196 
208 
209 } // namespace ant
210 
211 namespace hollow
212 {
213 
218 uint32_t get_sample_substream_id(const ant::ISample& oSample);
219 
225 {
226  return get_sample_substream_id(*pSample.Get());
227 }
228 
234 void set_sample_substream_id(ant::ISample& oSample, uint32_t nSubStreamId);
235 
241 inline void set_sample_substream_id(const ucom::ant::iobject_ptr<ant::ISample>& pSample, uint32_t nSubStreamId)
242 {
243  set_sample_substream_id(*pSample.Get(), nSubStreamId);
244 }
245 
246 }
247 
248 namespace flash
249 {
250 
261 
273 
285 
298 
312 
325 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
326 
335 
345 template <typename T>
347 {
348  static_assert(std::is_trivially_copyable<T>::value,
349  "You can only use plain types or structs without pointers or complex members.");
350 
351  public:
364  {
365  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
366  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
367  }
368 
374  explicit output_sample_data(tTimeStamp tmSampleTime)
375  {
376  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
377  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
378  }
379 
391  const T& oValue):
392  output_sample_data(tmSampleTime)
393  {
394  AssignValue(oValue);
395  }
396 
398  const T& oValue,
399  uint32_t nSubStreamId):
400  output_sample_data(tmSampleTime)
401  {
403  THROW_IF_FAILED(alloc_sample(pSample));
404  pSample->SetTime(tmSampleTime);
405  pSample->SetSubStreamId(nSubStreamId);
406  m_pSample = pSample;
407  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
408  AssignValue(oValue);
409  }
410 
418  const T& oValue):
419  output_sample_data(tmSampleTime)
420  {
421  AssignValue(oValue);
422  }
423 
433  explicit output_sample_data(const ant::ISample& oSampleToCopy)
434  {
435  THROW_IF_FAILED(alloc_sample(m_pSample));
436  THROW_IF_FAILED(m_pSample->Set(oSampleToCopy));
437  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, 0));
438  if (m_pBuffer->GetSize() != sizeof(T))
439  {
440  THROW_ERROR_DESC(ERR_INVALID_ARG, "The sample passed to output_sample_data (%zu), does not match the size of the requested type (%zu).",
441  m_pBuffer->GetSize(),
442  sizeof(T));
443  }
444  }
445 
452  output_sample_data(*pSampleToCopy.Get())
453  {
454  }
455 
457  output_sample_data& operator=(output_sample_data&&) = default;
458 
459  virtual ~output_sample_data() = default;
460 
465  void SetSubStreamId(uint32_t nSubStreamId)
466  {
467  hollow::set_sample_substream_id(m_pSample, nSubStreamId);
468  }
469 
475  {
476  return const_cast<T*>(const_cast<const output_sample_data*>(this)->GetDataPtr());
477  }
478 
483  const T* GetDataPtr() const
484  {
485  if (!m_pBuffer)
486  {
487  THROW_ERROR_DESC(ERR_INVALID_STATE,
488  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
489  }
490 
491  return reinterpret_cast<T*>(m_pBuffer->GetPtr());
492  }
493 
498  {
499  return GetDataPtr();
500  }
501 
505  const T* operator->() const
506  {
507  return GetDataPtr();
508  }
509 
514  T& GetData()
515  {
516  return *GetDataPtr();
517  }
518 
523  const T& GetData() const
524  {
525  return *GetDataPtr();
526  }
527 
532  {
533  return GetData();
534  }
535 
539  const T& operator*() const
540  {
541  return GetData();
542  }
543 
548  output_sample_data& operator=(const T& oValue)
549  {
550  AssignValue(oValue);
551  return *this;
552  }
553 
558  operator T&()
559  {
560  return GetData();
561  }
562 
567  operator const T&() const
568  {
569  return GetData();
570  }
571 
583  {
584  if (!m_pBuffer)
585  {
586  THROW_ERROR_DESC(ERR_INVALID_STATE,
587  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
588  }
589 
590  m_pBuffer = nullptr;
591  ucom::ant::object_ptr<ant::ISample> pSample(std::move(m_pSample));
592  m_pSample.Reset();
593  return pSample;
594  }
603  tResult SetSampleInfo(const ISampleInfo::tHashKey& oHash, const adtf::util::cVariant oValue)
604  {
605  return ant::set_sample_info(*m_pSample, oHash, oValue);
606  }
607 
608  protected:
611 
612  private:
613  inline void AssignValue(const T& oValue)
614  {
615  std::memcpy(this->GetDataPtr(), &oValue, sizeof(T));
616  }
617 
618  output_sample_data(const output_sample_data&) = delete;
619  output_sample_data& operator=(const output_sample_data&) = delete;
620 };
621 
622 }
623 
624 namespace giant
625 {
626 
635 
644 
653 
654 }
655 
656 using ant::cSample;
659 using flash::alloc_sample;
665 
666 } // namespace streaming
667 } // namespace adtf
668 
#define UCOM_RESOLVE(...)
Resolve a path to a base class which is inherited multiple times.
Definition: adtf_iid.h:40
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
#define ADTF_CLASS_INFO_IMPL(_class)
Common macro to implement correct treatment of adtf::ucom::ant::IClassInfo.
Definition: class_id.h:57
adtf::util::cStringUtil::tHashKey32 tHashKey
Type for the HashKey of the Value.
The SampleBuffer factory manages and can create SampleBuffers.
The SampleBuffer is the memory block object for the data of a ISample.
The ISample interface sets and retrieves properties on samples .
Definition: sample_intf.h:35
Interface for additional sample information on a Additional Sample Information.
The ISampleLog interface defines an interface to trace and store Sample data- and timing flow.
Sample that uses samples buffers that do not copy the data, but only keep a reference to it.
Definition: sample.h:149
The cSample class implements a basic sample which contains streaming data.
Definition: sample.h:54
tResult SetSampleBufferFactory(const ucom::ant::iobject_ptr< ISampleBufferFactory > &pFactory)
Sets the reference to the buffer factory, which will be used to create the user data content in ISamp...
ADTF_CLASS_ID_NAME(cSample, "sample.streaming.adtf.cid", "ADTF Default Sample")
private d pointer
Wrapper class that facilitates the handling of output samples.
Definition: sample.h:347
output_sample_data(const ant::ISample &oSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:433
tResult SetSampleInfo(const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oValue)
Sets a variant value for the hash key oHash into the current samples sample information.
Definition: sample.h:603
void SetSubStreamId(uint32_t nSubStreamId)
Sets the Substream id of the generated sample.
Definition: sample.h:465
output_sample_data(base::flash::tNanoSeconds tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:390
output_sample_data & operator=(const T &oValue)
Assigns a new value to the data.
Definition: sample.h:548
output_sample_data(tTimeStamp tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:417
output_sample_data(base::flash::tNanoSeconds tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:363
output_sample_data(const ucom::ant::iobject_ptr< const ant::ISample > &pSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:451
output_sample_data(tTimeStamp tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:374
ucom::ant::object_ptr< ant::ISample > Release()
Returns a sample that contains the data and resets all internal data.
Definition: sample.h:582
virtual T * Get() const =0
Get raw pointer to shared object.
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
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:379
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
tResult set_sample_info(ISampleInfo &oSampleInfo, const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oValue)
Sets a variant value for the hash key oHash into the sample information oSampleInfo.
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.
tResult alloc_sample(ucom::ant::iobject_ptr< ucom::ant::IObject > &pSample)
Helper Function to get a ISample Instance through the adtf::ucom::ant::IRuntime.
base::flash::tNanoSeconds get_sample_time(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Returns the sample time stamp with nanosecond precision.
base::flash::tNanoSeconds get_sample_time(const ant::ISample &oSample)
Returns the sample timestamp with nanosecond precision.
void set_sample_time(ant::ISample &oSample, base::flash::tNanoSeconds tmTimeStamp)
Sets the sample timestamp with nanosecond precision.
void set_sample_time(const ucom::ant::iobject_ptr< ant::ISample > &pSample, base::flash::tNanoSeconds tmTimeStamp)
Sets the sample timestamp with nanosecond precision.
void set_sample_substream_id(const ucom::ant::iobject_ptr< ant::ISample > &pSample, uint32_t nSubStreamId)
Sets the substream id of a given sample.
Definition: sample.h:241
uint32_t get_sample_substream_id(const ant::ISample &oSample)
void set_sample_substream_id(ant::ISample &oSample, uint32_t nSubStreamId)
Sets the substream id of a given sample.
uint32_t get_sample_substream_id(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Definition: sample.h:224
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
ant::IObject IObject
Alias always bringing the latest version of ant::IObject into scope.
Definition: object_intf.h:102
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
#define THROW_ERROR_DESC(_code,...)
throws a tResult exception
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult