ADTF  3.14.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>
14 
15 namespace adtf
16 {
17 namespace streaming
18 {
19 namespace ant
20 {
21 
45 class cSample :
46  public adtf::ucom::catwo::object<ant::ISample,
47  flash::ISample,
48  hollow::ISample,
49  ucom::ant::IClassInfo>
50 {
51 private:
53  A_UTILS_D(cSample)
54 
55 public:
57  ADTF_CLASS_ID_NAME(cSample, "sample.streaming.adtf.cid", "ADTF Default Sample");
60 
62 
63 
73  cSample(const ucom::ant::iobject_ptr<ISampleBufferFactory>& oSampleFactory);
74 
78  virtual ~cSample();
79 
80 private: //no copy, no assignment allowed - Samples will be treated only by ISample inferface !
82  cSample(const cSample&) = delete;
84  cSample(cSample&&) = delete;
86  cSample& operator=(const cSample&) = delete;
88  cSample& operator=(cSample&&) = delete;
89 
90 public: //implements ant::ISample
91  tResult Lock(ucom::ant::iobject_ptr_shared_locked<const adtf::streaming::ant::ISampleBuffer>& oSampleBuffer) const override;
92  tResult WriteLock(ucom::ant::iobject_ptr_locked<ISampleBuffer>& oSampleBuffer, size_t szPreAllocateSize) override;
93  tResult Set(const ant::ISample& oSample) override;
94  tResult Get(ant::ISample& oSample) const override;
95  tResult Reset() override;
96  tTimeStamp GetTime() const;
97  tResult SetTime(tTimeStamp tmTimeStamp);
98  uint32_t GetFlags() const override;
99  tResult SetFlags(uint32_t ui32Flags) override;
100  tResult Set(tTimeStamp tmTime,
101  const void* pData,
102  size_t nSize,
103  uint32_t ui32Flags=0) override;
104 
105  tResult GetSampleBufferFactory(ucom::ant::iobject_ptr<ISampleBufferFactory>& oFactory) const override;
106  tResult GetSampleInfo(ucom::ant::iobject_ptr<const ISampleInfo>& oSampleInfo) const override;
107  tResult GetSampleInfo(ucom::ant::iobject_ptr<ISampleInfo>& oSampleInfo) override;
108  tResult GetSampleLog(ucom::ant::iobject_ptr<ISampleLog>& oSampleLog) const override;
109 
110 public: //implements flash::ISample
111  base::flash::tNanoSeconds GetTimeNs() const override;
112  tResult SetTime(base::flash::tNanoSeconds tmTimeStamp) override;
113  tResult Set(base::flash::tNanoSeconds tmTimeStamp,
114  const void* pData,
115  size_t szSize,
116  uint32_t ui32Flags=0) override;
117 
118 public: //implements hollow::ISample
119  uint32_t GetSubStreamId() const override;
120  void SetSubStreamId(uint32_t nSubStreamId) override;
121 
122 public:
132  tResult SetSampleBufferFactory(const ucom::ant::iobject_ptr<ISampleBufferFactory>& pFactory);
133 
134  tResult SetSampleInfo(const ucom::ant::iobject_ptr<ISampleInfo>& oInfo);
135  tResult SetSampleLog(const ucom::ant::iobject_ptr<ISampleLog>& oLog);
136 };
137 
145 {
146  public:
152 };
153 
165 template <typename T>
167 {
168 protected:
169  ucom::object_ptr<const ISample> m_pCurrentSample;
171 public:
174  {
175  }
177  sample_data(const sample_data& oSampleData)
178  {
179  Reset(oSampleData.m_pCurrentSample);
180  }
183  sample_data(sample_data&& oSampleData)
184  {
185  std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
186  std::swap(m_pBuffer, oSampleData.m_pBuffer);
187  }
191  {
192  Reset(pSample);
193  }
202  tTimeStamp GetTime() const
203  {
204  if (m_pCurrentSample)
205  {
206  return m_pCurrentSample->GetTime();
207  }
208  return -1;
209  }
218  const T* GetDataPtr() const
219  {
220  if (m_pBuffer)
221  {
222  return reinterpret_cast<const T*>(m_pBuffer->GetPtr());
223  }
224  return nullptr;
225  }
234  const T GetData() const
235  {
236  const T* pData = GetDataPtr();
237  if (pData)
238  {
239  T sData;
240  sData = *pData;
241  return sData;
242  }
243  return T();
244  }
250  bool IsValid() const
251  {
252  return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
253  }
259  operator bool() const
260  {
261  return IsValid();
262  }
269  operator T() const
270  {
271  if (IsValid())
272  {
273  return *GetDataPtr();
274  }
275  return T();
276  }
277 
286  {
287  RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
288  m_pCurrentSample = pSample;
289  RETURN_NOERROR;
290  }
291 
298  tResult Reset()
299  {
300  m_pCurrentSample.Reset();
301  m_pBuffer = nullptr;
302  RETURN_NOERROR;
303  }
304 };
305 
306 //helper functions for Sampleallocation
318 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSampleObject, const char* strSampleCID);
319 
332 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
333 
344 
355 tResult alloc_sample(ucom::ant::iobject_ptr<ISample>& pSample, tTimeStamp nTimeStamp);
356 
357 } // namespace ant
358 
359 namespace hollow
360 {
361 
366 uint32_t get_sample_substream_id(const ant::ISample& oSample);
367 
373 {
374  return get_sample_substream_id(*pSample.Get());
375 }
376 
382 void set_sample_substream_id(ant::ISample& oSample, uint32_t nSubStreamId);
383 
389 inline void set_sample_substream_id(const ucom::ant::iobject_ptr<ant::ISample>& pSample, uint32_t nSubStreamId)
390 {
391  set_sample_substream_id(*pSample.Get(), nSubStreamId);
392 }
393 
394 }
395 
396 namespace flash
397 {
398 
409 
421 
432 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, tTimeStamp nTimeStamp);
433 
445 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID);
446 
459 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, base::flash::tNanoSeconds nTimeStamp);
460 
473 tResult alloc_sample(ucom::ant::iobject_ptr<ucom::ant::IObject>& pSample, const char* strSampleCID, tTimeStamp nTimeStamp);
474 
483 
497 template <typename T>
499 {
500  static_assert(std::is_trivially_copyable<T>::value,
501  "You can only use plain types or structs without pointers or complex members.");
502 
503 private:
504  ucom::object_ptr<const ant::ISample> m_pCurrentSample;
506 public:
509  {
510  }
511 
513  sample_data(const sample_data& oSampleData)
514  {
515  Reset(oSampleData.m_pCurrentSample);
516  }
519  sample_data(sample_data&& oSampleData)
520  {
521  std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
522  std::swap(m_pBuffer, oSampleData.m_pBuffer);
523  }
527  {
528  Reset(pSample);
529  }
530 
532  {
533  Reset(ucom::ucom_object_ptr_cast<const ant::ISample>(pSample));
534  }
535 
544  tTimeStamp GetTime() const
545  {
546  if (m_pCurrentSample)
547  {
548  return m_pCurrentSample->GetTime();
549  }
550  return -1;
551  }
552 
562  {
563  if (m_pCurrentSample)
564  {
565  return get_sample_time(m_pCurrentSample);
566  }
567  return adtf::base::tNanoSeconds{-1};
568  }
569 
578  const T* GetDataPtr() const
579  {
580  if (m_pBuffer)
581  {
582  return reinterpret_cast<const T*>(m_pBuffer->GetPtr());
583  }
584  else
585  {
586  return nullptr;
587  }
588  }
589 
593  size_t GetDataSize() const
594  {
595  if (m_pBuffer)
596  {
597  return m_pBuffer->GetSize();
598  }
599  else
600  {
601  return 0;
602  }
603  }
604 
608  const T* operator->()
609  {
610  return GetDataPtr();
611  }
612 
616  const T* operator->() const
617  {
618  return GetDataPtr();
619  }
620 
629  const T& GetData() const
630  {
631  if (IsValid())
632  {
633  return *GetDataPtr();
634  }
635  else
636  {
637  static T oDefaultValue;
638  return oDefaultValue;
639  }
640  }
641 
645  const T& operator*() const
646  {
647  return GetData();
648  }
649 
655  bool IsValid() const
656  {
657  return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
658  }
659 
666  operator const T&() const
667  {
668  return GetData();
669  }
670 
679  {
680  RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
681  m_pCurrentSample = pSample;
682  RETURN_NOERROR;
683  }
684 
691  tResult Reset()
692  {
693  m_pCurrentSample.Reset();
694  m_pBuffer = nullptr;
695  RETURN_NOERROR;
696  }
697 };
698 
708 template <typename T>
710 {
711  static_assert(std::is_trivially_copyable<T>::value,
712  "You can only use plain types or structs without pointers or complex members.");
713 
714  public:
727  {
728  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
729  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
730  }
731 
737  explicit output_sample_data(tTimeStamp tmSampleTime)
738  {
739  THROW_IF_FAILED(alloc_sample(m_pSample, tmSampleTime));
740  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
741  }
742 
754  const T& oValue):
755  output_sample_data(tmSampleTime)
756  {
757  GetData() = oValue;
758  }
759 
761  const T& oValue,
762  uint32_t nSubStreamId):
763  output_sample_data(tmSampleTime)
764  {
766  THROW_IF_FAILED(alloc_sample(pSample));
767  pSample->SetTime(tmSampleTime);
768  pSample->SetSubStreamId(nSubStreamId);
769  m_pSample = pSample;
770  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, sizeof(T)));
771  GetData() = oValue;
772  }
773 
780  output_sample_data(tTimeStamp tmSampleTime,
781  const T& oValue):
782  output_sample_data(tmSampleTime)
783  {
784  GetData() = oValue;
785  }
786 
796  explicit output_sample_data(const ant::ISample& oSampleToCopy)
797  {
798  THROW_IF_FAILED(alloc_sample(m_pSample));
799  THROW_IF_FAILED(m_pSample->Set(oSampleToCopy));
800  THROW_IF_FAILED(m_pSample->WriteLock(m_pBuffer, 0));
801  if (m_pBuffer->GetSize() != sizeof(T))
802  {
803  THROW_ERROR_DESC(ERR_INVALID_ARG, "The sample passed to output_sample_data (%d), does not match the size of the requested type (%d).",
804  m_pBuffer->GetSize(),
805  sizeof(T));
806  }
807  }
808 
815  output_sample_data(*pSampleToCopy.Get())
816  {
817  }
818 
820  output_sample_data& operator=(output_sample_data&&) = default;
821 
822  virtual ~output_sample_data() = default;
823 
828  void SetSubStreamId(uint32_t nSubStreamId)
829  {
830  hollow::set_sample_substream_id(m_pSample, nSubStreamId);
831  }
832 
838  {
839  return const_cast<T*>(const_cast<const output_sample_data*>(this)->GetDataPtr());
840  }
841 
846  const T* GetDataPtr() const
847  {
848  if (!m_pBuffer)
849  {
850  THROW_ERROR_DESC(ERR_INVALID_STATE,
851  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
852  }
853 
854  return reinterpret_cast<T*>(m_pBuffer->GetPtr());
855  }
856 
861  {
862  return GetDataPtr();
863  }
864 
868  const T* operator->() const
869  {
870  return GetDataPtr();
871  }
872 
877  T& GetData()
878  {
879  return *GetDataPtr();
880  }
881 
886  const T& GetData() const
887  {
888  return *GetDataPtr();
889  }
890 
895  {
896  return GetData();
897  }
898 
902  const T& operator*() const
903  {
904  return GetData();
905  }
906 
911  output_sample_data& operator=(const T& oValue)
912  {
913  GetData() = oValue;
914  return *this;
915  }
916 
921  operator T&()
922  {
923  return GetData();
924  }
925 
930  operator const T&() const
931  {
932  return GetData();
933  }
934 
946  {
947  if (!m_pBuffer)
948  {
949  THROW_ERROR_DESC(ERR_INVALID_STATE,
950  "This output_sample_data instance is not valid. Maybe you tried to access it after you called Release()");
951  }
952 
953  m_pBuffer = nullptr;
954  ucom::ant::object_ptr<ant::ISample> pSample(std::move(m_pSample));
955  m_pSample.Reset();
956  return pSample;
957  }
958 
959  protected:
962 
963  private:
964  output_sample_data(const output_sample_data&) = delete;
965  output_sample_data& operator=(const output_sample_data&) = delete;
966 };
967 
968 }
969 
970 namespace giant
971 {
972 
981 
990 
999 
1000 }
1001 
1002 using ant::cSample;
1003 using ant::cReferenceSample;
1004 using flash::sample_data;
1006 using flash::alloc_sample;
1012 
1013 } // namespace streaming
1014 } // namespace adtf
#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.
#define ADTF_CLASS_INFO_IMPL(_class)
Common macro to implement correct treatment of adtf::ucom::ant::IClassInfo.
Definition: class_id.h:57
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:145
The cSample class implements a basic sample which contains streaming data.
Definition: sample.h:50
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
Sample Data getter for an easy use of samples with samplebuffer set to the type T.
Definition: sample.h:167
tResult Reset()
Resets the sample the sample data reference to.
Definition: sample.h:298
sample_data(sample_data &&oSampleData)
move CTOR
Definition: sample.h:183
sample_data(const sample_data &oSampleData)
CTOR.
Definition: sample.h:177
const T GetData() const
Retrieves the pointer to the datas memory as type T*.
Definition: sample.h:234
tTimeStamp GetTime() const
retrieves the time of the data.
Definition: sample.h:202
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition: sample.h:218
tResult Reset(const ucom::ant::iobject_ptr< const ISample > &pSample)
Resets the sample the sample data reference to with a new reference to a sample.
Definition: sample.h:285
bool IsValid() const
Validate if sample and sample buffer is set.
Definition: sample.h:250
sample_data(const ucom::iobject_ptr< const ISample > &pSample)
copy CTOR
Definition: sample.h:190
Wrapper class that facilitates the handling of output samples.
Definition: sample.h:710
output_sample_data(const ant::ISample &oSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:796
void SetSubStreamId(uint32_t nSubStreamId)
Sets the Substream id of the generated sample.
Definition: sample.h:828
output_sample_data(base::flash::tNanoSeconds tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:753
output_sample_data & operator=(const T &oValue)
Assigns a new value to the data.
Definition: sample.h:911
output_sample_data(tTimeStamp tmSampleTime, const T &oValue)
Initializes the data timestamp and value.
Definition: sample.h:780
output_sample_data(base::flash::tNanoSeconds tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:726
output_sample_data(const ucom::ant::iobject_ptr< const ant::ISample > &pSampleToCopy)
Initializes the data from an existing sample.
Definition: sample.h:814
output_sample_data(tTimeStamp tmSampleTime)
Initializes the data timestamp.
Definition: sample.h:737
ucom::ant::object_ptr< ant::ISample > Release()
Returns a sample that contains the data and resets all internal data.
Definition: sample.h:945
Easy data access for input samples.
Definition: sample.h:499
tResult Reset()
Resets the sample the sample data reference to.
Definition: sample.h:691
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
copy CTOR
Definition: sample.h:526
sample_data(sample_data &&oSampleData)
move CTOR
Definition: sample.h:519
sample_data(const sample_data &oSampleData)
CTOR.
Definition: sample.h:513
const T & operator*() const
Retrieves a reference to the data.
Definition: sample.h:645
const T * operator->() const
Retrieves the pointer to the datas memory.
Definition: sample.h:616
const T * operator->()
Retrieves the pointer to the datas memory.
Definition: sample.h:608
adtf::base::flash::tNanoSeconds GetTimeNs() const
retrieves the timestamp of the data.
Definition: sample.h:561
tTimeStamp GetTime() const
retrieves the timestamp of the data.
Definition: sample.h:544
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition: sample.h:578
bool IsValid() const
Validate if sample and sample buffer is set.
Definition: sample.h:655
const T & GetData() const
Retrieves a reference to the data.
Definition: sample.h:629
tResult Reset(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Resets the sample the sample data reference to with a new reference to a sample.
Definition: sample.h:678
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.
Implementation for a shared lock guard.
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
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:359
void swap(StackPtr< T, StackSize, Alignment > &lhs, StackPtr< T, StackSize, Alignment > &rhs)
Swap storages of two objects of type StackPtr.
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:389
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:372
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.
#define THROW_ERROR_DESC(_code,...)
throws a tResult exception
#define RETURN_IF_FAILED(s)
returns if the expression returns a failed tResult or throws an exception.
#define THROW_IF_FAILED(s)
throws if the expression returns a failed tResult

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