ADTF
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>
14 #include <adtf_base_deprecated.h>
16 
17 #include <cstring>
18 #include <cinttypes>
19 
20 namespace adtf
21 {
22 namespace streaming
23 {
24 namespace ant
25 {
26 
52 class cSample :
53  public adtf::ucom::catwo::object<ant::ISample,
54  flash::ISample,
55  hollow::ISample,
56  ucom::ant::IClassInfo>
57 {
58 private:
61 
62 public:
64  ADTF_CLASS_ID_NAME(cSample, "sample.streaming.adtf.cid", "ADTF Default Sample");
67 
69 
70 
74  ADTF3_DEPRECATED("The class 'cSample' is deprecated. Please use 'alloc_sample()' instead.")
81  ADTF3_DEPRECATED("The class 'cSample' is deprecated. Please use 'alloc_sample()' instead.")
82  cSample(const ucom::ant::iobject_ptr<ISampleBufferFactory>& oSampleFactory);
83 
87  virtual ~cSample();
88 
89 private: //no copy, no assignment allowed - Samples will be treated only by ISample inferface !
91  cSample(const cSample&) = delete;
93  cSample(cSample&&) = delete;
95  cSample& operator=(const cSample&) = delete;
97  cSample& operator=(cSample&&) = delete;
98 
99 public: //implements ant::ISample
100  tResult Lock(ucom::ant::iobject_ptr_shared_locked<const adtf::streaming::ant::ISampleBuffer>& oSampleBuffer) const override;
101  tResult WriteLock(ucom::ant::iobject_ptr_locked<ISampleBuffer>& oSampleBuffer, size_t szPreAllocateSize) override;
102  tResult Set(const ant::ISample& oSample) override;
103  tResult Get(ant::ISample& oSample) const override;
104  tResult Reset() override;
105  tTimeStamp GetTime() const;
106  tResult SetTime(tTimeStamp tmTimeStamp);
107  uint32_t GetFlags() const override;
108  tResult SetFlags(uint32_t ui32Flags) override;
109  tResult Set(tTimeStamp tmTime,
110  const void* pData,
111  size_t nSize,
112  uint32_t ui32Flags=0) override;
113 
114  tResult GetSampleBufferFactory(ucom::ant::iobject_ptr<ISampleBufferFactory>& oFactory) const override;
115  tResult GetSampleInfo(ucom::ant::iobject_ptr<const ISampleInfo>& oSampleInfo) const override;
116  tResult GetSampleInfo(ucom::ant::iobject_ptr<ISampleInfo>& oSampleInfo) override;
117  tResult GetSampleLog(ucom::ant::iobject_ptr<ISampleLog>& oSampleLog) const override;
118 
119 public: //implements flash::ISample
120  base::flash::tNanoSeconds GetTimeNs() const override;
121  tResult SetTime(base::flash::tNanoSeconds tmTimeStamp) override;
122  tResult Set(base::flash::tNanoSeconds tmTimeStamp,
123  const void* pData,
124  size_t szSize,
125  uint32_t ui32Flags=0) override;
126 
127 public: //implements hollow::ISample
128  uint32_t GetSubStreamId() const override;
129  void SetSubStreamId(uint32_t nSubStreamId) override;
130 
131 public:
142 
143  tResult SetSampleInfo(const ucom::ant::iobject_ptr<ISampleInfo>& oInfo);
144  tResult SetSampleLog(const ucom::ant::iobject_ptr<ISampleLog>& oLog);
145 };
146 
154 {
155  public:
160  ADTF3_DEPRECATED("The class 'cReferenceSample' is deprecated because it is inherently unsafe. Please use 'alloc_sample()' in combination with 'ISample::SetSampleBuffer()' instead.")
162 };
163 
164 //helper functions for Sampleallocation
176 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSampleObject, const char* strSampleCID);
177 
190 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
191 
201 tResult alloc_sample(ucom::ant::iobject_ptr<ISample>& pSample);
202 
213 tResult alloc_sample(ucom::ant::iobject_ptr<ISample>& pSample, tTimeStamp nTimeStamp);
214 
215 } // namespace ant
216 
217 namespace hollow
218 {
219 
224 uint32_t get_sample_substream_id(const ant::ISample& oSample);
225 
231 {
232  return get_sample_substream_id(*pSample.Get());
233 }
234 
240 void set_sample_substream_id(ant::ISample& oSample, uint32_t nSubStreamId);
241 
247 inline void set_sample_substream_id(const ucom::ant::iobject_ptr<ant::ISample>& pSample, uint32_t nSubStreamId)
248 {
249  set_sample_substream_id(*pSample.Get(), nSubStreamId);
250 }
251 
252 }
253 
254 namespace flash
255 {
256 
267 
279 
291 
304 
318 
331 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
332 
341 
351 template <typename T>
353 {
354  static_assert(std::is_trivially_copyable<T>::value,
355  "You can only use plain types or structs without pointers or complex members.");
356 
357  public:
370  {
371  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
372  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
373  }
374 
380  explicit output_sample_data(tTimeStamp tmSampleTime)
381  {
382  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
383  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
384  }
385 
397  const T& oValue):
398  output_sample_data(tmSampleTime)
399  {
400  AssignValue(oValue);
401  }
402 
404  const T& oValue,
405  uint32_t nSubStreamId):
406  output_sample_data(tmSampleTime)
407  {
409  THROW_IF_FAILED(alloc_sample(pSample));
410  pSample->SetTime(tmSampleTime);
411  pSample->SetSubStreamId(nSubStreamId);
412  m_pSample = pSample;
413  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
414  AssignValue(oValue);
415  }
416 
424  const T& oValue):
425  output_sample_data(tmSampleTime)
426  {
427  AssignValue(oValue);
428  }
429 
439  explicit output_sample_data(const ant::ISample& oSampleToCopy)
440  {
441  THROW_IF_FAILED(alloc_sample(m_pSample));
442  THROW_IF_FAILED(m_pSample->Set(oSampleToCopy));
443  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, 0));
444  if (m_pBuffer->GetSize() != sizeof(T))
445  {
446  THROW_ERROR_DESC(ERR_INVALID_ARG, "The sample passed to output_sample_data (%zu), does not match the size of the requested type (%zu).",
447  m_pBuffer->GetSize(),
448  sizeof(T));
449  }
450  }
451 
458  output_sample_data(*pSampleToCopy.Get())
459  {
460  }
461 
464 
465  virtual ~output_sample_data() = default;
466 
471  void SetSubStreamId(uint32_t nSubStreamId)
472  {
473  hollow::set_sample_substream_id(m_pSample, nSubStreamId);
474  }
475 
481  {
482  return const_cast<T*>(const_cast<const output_sample_data*>(this)->GetDataPtr());
483  }
484 
489  const T* GetDataPtr() const
490  {
491  if (!m_pBuffer)
492  {
493  THROW_ERROR_DESC(ERR_INVALID_STATE,
494  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
495  }
496 
497  return reinterpret_cast<T*>(m_pBuffer->GetPtr());
498  }
499 
504  {
505  return GetDataPtr();
506  }
507 
511  const T* operator->() const
512  {
513  return GetDataPtr();
514  }
515 
520  T& GetData()
521  {
522  return *GetDataPtr();
523  }
524 
529  const T& GetData() const
530  {
531  return *GetDataPtr();
532  }
533 
538  {
539  return GetData();
540  }
541 
545  const T& operator*() const
546  {
547  return GetData();
548  }
549 
554  output_sample_data& operator=(const T& oValue)
555  {
556  AssignValue(oValue);
557  return *this;
558  }
559 
564  operator T&()
565  {
566  return GetData();
567  }
568 
573  operator const T&() const
574  {
575  return GetData();
576  }
577 
589  {
590  if (!m_pBuffer)
591  {
592  THROW_ERROR_DESC(ERR_INVALID_STATE,
593  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
594  }
595 
596  m_pBuffer = nullptr;
597  ucom::ant::object_ptr<ant::ISample> pSample(std::move(m_pSample));
598  m_pSample.Reset();
599  return pSample;
600  }
609  tResult SetSampleInfo(const ISampleInfo::tHashKey& oHash, const adtf::util::cVariant oValue)
610  {
611  return ant::set_sample_info(*m_pSample, oHash, oValue);
612  }
613 
614  protected:
617 
618  private:
619  inline void AssignValue(const T& oValue)
620  {
621  std::memcpy(this->GetDataPtr(), &oValue, sizeof(T));
622  }
623 
624  output_sample_data(const output_sample_data&) = delete;
626 };
627 
628 }
629 
630 namespace giant
631 {
632 
641 
650 
659 
660 }
661 
662 using ant::cSample;
665 using flash::alloc_sample;
671 
672 } // namespace streaming
673 } // namespace adtf
674 
Copyright © Audi Electronics Venture GmbH.
#define ADTF3_DEPRECATED(_depr_message_)
Mark a function or variable as deprecated.
#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:154
ADTF3_DEPRECATED("The class 'cReferenceSample' is deprecated because it is inherently unsafe. Please use 'alloc_sample()' in combination with 'ISample::SetSampleBuffer()' instead.") cReferenceSample()
CTOR.
The cSample class implements a basic sample which contains streaming data.
Definition: sample.h:57
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
cSample & operator=(const cSample &)=delete
delete copy operator (Samples will be treated only by ISample inferface)
Wrapper class that facilitates the handling of output samples.
Definition: sample.h:353
output_sample_data(const ant::ISample &oSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:439
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:609
void SetSubStreamId(uint32_t nSubStreamId)
Sets the Substream id of the generated sample.
Definition: sample.h:471
output_sample_data(base::flash::tNanoSeconds tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:396
output_sample_data & operator=(const T &oValue)
Assigns a new value to the data.
Definition: sample.h:554
output_sample_data(tTimeStamp tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:423
output_sample_data(base::flash::tNanoSeconds tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:369
output_sample_data(const ucom::ant::iobject_ptr< const ant::ISample > &pSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:457
output_sample_data(tTimeStamp tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:380
ucom::ant::object_ptr< ant::ISample > Release()
Returns a sample that contains the data and resets all internal data.
Definition: sample.h:588
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:247
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:230
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