ADTF  3.18.2
sample_data.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "sample_intf.h"
9 
10 #include <type_traits>
11 #include <cstring>
12 
13 namespace adtf
14 {
15 namespace streaming
16 {
17 namespace ant
18 {
19 
31 template <typename T>
33 {
34 protected:
35  ucom::object_ptr<const ISample> m_pCurrentSample;
37 public:
40  {
41  }
43  sample_data(const sample_data& oSampleData)
44  {
45  Reset(oSampleData.m_pCurrentSample);
46  }
49  sample_data(sample_data&& oSampleData)
50  {
51  std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
52  std::swap(m_pBuffer, oSampleData.m_pBuffer);
53  }
57  {
58  Reset(pSample);
59  }
69  {
70  if (m_pCurrentSample)
71  {
72  return m_pCurrentSample->GetTime();
73  }
74  return -1;
75  }
84  const T* GetDataPtr() const
85  {
86  if (m_pBuffer)
87  {
88  return reinterpret_cast<const T*>(m_pBuffer->GetPtr());
89  }
90  return nullptr;
91  }
100  const T GetData() const
101  {
102  const T* pData = GetDataPtr();
103  if (pData)
104  {
105  T sData;
106  sData = *pData;
107  return sData;
108  }
109  return T();
110  }
116  bool IsValid() const
117  {
118  return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
119  }
125  operator bool() const
126  {
127  return IsValid();
128  }
135  operator T() const
136  {
137  if (IsValid())
138  {
139  return *GetDataPtr();
140  }
141  return T();
142  }
143 
152  {
153  RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
154  m_pCurrentSample = pSample;
156  }
157 
165  {
166  m_pCurrentSample.Reset();
167  m_pBuffer = nullptr;
169  }
170 };
171 
172 } // namespace ant
173 
174 namespace flash
175 {
189 template<typename T>
191 {
192  static_assert(std::is_trivially_copyable<T>::value,
193  "You can only use plain types or structs without pointers or complex members.");
194 
195 private:
196  ucom::object_ptr<const ant::ISample> m_pCurrentSample;
198 
199 public:
202  {
203  }
204 
206  sample_data(const sample_data& oSampleData)
207  {
208  Reset(oSampleData.m_pCurrentSample);
209  }
212  sample_data(sample_data&& oSampleData)
213  {
214  std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
215  std::swap(m_pBuffer, oSampleData.m_pBuffer);
216  }
220  {
221  Reset(pSample);
222  }
223 
225  {
226  Reset(ucom::ucom_object_ptr_cast<const ant::ISample>(pSample));
227  }
228 
238  {
239  if (m_pCurrentSample)
240  {
241  return m_pCurrentSample->GetTime();
242  }
243  return -1;
244  }
245 
255  {
256  if (m_pCurrentSample)
257  {
258  return get_sample_time(m_pCurrentSample);
259  }
260  return adtf::base::tNanoSeconds {-1};
261  }
262 
271  const T* GetDataPtr() const
272  {
273  if (m_pBuffer)
274  {
275  return reinterpret_cast<const T*>(m_pBuffer->GetPtr());
276  }
277  else
278  {
279  return nullptr;
280  }
281  }
282 
286  size_t GetDataSize() const
287  {
288  if (m_pBuffer)
289  {
290  return m_pBuffer->GetSize();
291  }
292  else
293  {
294  return 0;
295  }
296  }
297 
301  const T* operator->()
302  {
303  return GetDataPtr();
304  }
305 
309  const T* operator->() const
310  {
311  return GetDataPtr();
312  }
313 
322  const T& GetData() const
323  {
324  if (IsValid())
325  {
326  return *GetDataPtr();
327  }
328  else
329  {
330  static T oDefaultValue{};
331  return oDefaultValue;
332  }
333  }
334 
338  const T& operator*() const
339  {
340  return GetData();
341  }
342 
348  bool IsValid() const
349  {
350  return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
351  }
352 
359  operator const T&() const
360  {
361  return GetData();
362  }
363 
372  {
373  RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
374  m_pCurrentSample = pSample;
376  }
377 
385  {
386  m_pCurrentSample.Reset();
387  m_pBuffer = nullptr;
389  }
390 
398  adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey& oHash, const adtf::util::cVariant oDefault = adtf::util::cVariant()) const
399  {
400  if (m_pCurrentSample)
401  {
402  return ant::get_sample_info(*m_pCurrentSample, oHash, oDefault);
403  }
404  return oDefault;
405  }
406 };
407 } // namespace flash
408 
409 namespace penguin
410 {
411 
426 template<typename T, typename Enable=void>
428 {
429 private:
430  ucom::object_ptr<const ant::ISample> m_pCurrentSample;
432  T m_oExtractedValue = {};
433 
434 public:
437  {
438  }
439 
441  sample_data(const sample_data& oSampleData)
442  {
443  Reset(oSampleData.m_pCurrentSample);
444  }
447  sample_data(sample_data&& oSampleData):
448  m_pCurrentSample(std::move(oSampleData.m_pCurrentSample)),
449  m_pBuffer(std::move(oSampleData.m_pBuffer)),
450  m_oExtractedValue(std::move(oSampleData.m_oExtractedValue))
451  {
452  }
456  {
457  Reset(pSample);
458  }
459 
461  {
462  Reset(ucom::ucom_object_ptr_cast<const ant::ISample>(pSample));
463  }
464 
474  {
475  if (m_pCurrentSample)
476  {
477  return m_pCurrentSample->GetTime();
478  }
479  return -1;
480  }
481 
491  {
492  if (m_pCurrentSample)
493  {
494  return get_sample_time(m_pCurrentSample);
495  }
496  return adtf::base::tNanoSeconds {-1};
497  }
498 
507  const T* GetDataPtr() const
508  {
509  if (m_pBuffer)
510  {
511  return &m_oExtractedValue;
512  }
513  else
514  {
515  return nullptr;
516  }
517  }
518 
522  size_t GetDataSize() const
523  {
524  if (m_pBuffer)
525  {
526  return m_pBuffer->GetSize();
527  }
528  else
529  {
530  return 0;
531  }
532  }
533 
537  const T* operator->()
538  {
539  return GetDataPtr();
540  }
541 
545  const T* operator->() const
546  {
547  return GetDataPtr();
548  }
549 
558  const T& GetData() const
559  {
560  if (IsValid())
561  {
562  return m_oExtractedValue;
563  }
564  else
565  {
566  static T oDefaultValue {};
567  return oDefaultValue;
568  }
569  }
570 
574  const T& operator*() const
575  {
576  return GetData();
577  }
578 
584  bool IsValid() const
585  {
586  return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= 0);
587  }
588 
596  operator const T&() const
597  {
598  return GetData();
599  }
600 
609  {
610  RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
611  m_pCurrentSample = pSample;
612  //We immediatelly read the value here if there is a specialization for T
613  RETURN_IF_FAILED(m_pBuffer->Read(adtf::base::adtf_memory<T>(&m_oExtractedValue)));
615  }
616 
624  {
625  m_oExtractedValue = T{};
626  m_pCurrentSample.Reset();
627  m_pBuffer = nullptr;
629  }
630 
638  adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey& oHash, const adtf::util::cVariant oDefault = adtf::util::cVariant()) const
639  {
640  if (m_pCurrentSample)
641  {
642  return ant::get_sample_info(*m_pCurrentSample, oHash, oDefault);
643  }
644  return oDefault;
645  }
646 };
647 
661 template<typename T>
662 class sample_data<T, typename std::enable_if_t<std::is_trivially_copyable<T>::value>> : public flash::sample_data<T>
663 {
664 private:
666 public:
669  _base_type()
670  {
671  }
672 
674  sample_data(const sample_data& oSampleData):
675  _base_type(oSampleData)
676  {
677  }
680  sample_data(sample_data&& oSampleData):
681  _base_type(oSampleData)
682  {
683  }
687  _base_type(pSample)
688  {
689  }
693  _base_type(pSample)
694  {
695  }
696 };
697 
698 } // namespace penguin
699 
700 using penguin::sample_data;
701 
702 } // namespace streaming
703 } // namespace adtf
704 
#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.
adtf::util::cStringUtil::tHashKey32 tHashKey
Type for the HashKey of the Value.
Template class implementation for the ant::IRawMemory interface (see Supported types for adtf_memory<...
Sample Data getter for an easy use of samples with samplebuffer set to the type T.
Definition: sample_data.h:33
tResult Reset()
Resets the sample the sample data reference to.
Definition: sample_data.h:164
sample_data(sample_data &&oSampleData)
move CTOR
Definition: sample_data.h:49
sample_data(const sample_data &oSampleData)
CTOR.
Definition: sample_data.h:43
const T GetData() const
Retrieves the pointer to the datas memory as type T*.
Definition: sample_data.h:100
tTimeStamp GetTime() const
retrieves the time of the data.
Definition: sample_data.h:68
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition: sample_data.h:84
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_data.h:151
bool IsValid() const
Validate if sample and sample buffer is set.
Definition: sample_data.h:116
sample_data(const ucom::iobject_ptr< const ISample > &pSample)
copy CTOR
Definition: sample_data.h:56
Easy data access for input samples.
Definition: sample_data.h:191
tResult Reset()
Resets the sample the sample data reference to.
Definition: sample_data.h:384
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
copy CTOR
Definition: sample_data.h:219
sample_data(sample_data &&oSampleData)
move CTOR
Definition: sample_data.h:212
sample_data(const sample_data &oSampleData)
CTOR.
Definition: sample_data.h:206
const T & operator*() const
Retrieves a reference to the data.
Definition: sample_data.h:338
const T * operator->() const
Retrieves the pointer to the datas memory.
Definition: sample_data.h:309
const T * operator->()
Retrieves the pointer to the datas memory.
Definition: sample_data.h:301
adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault=adtf::util::cVariant()) const
Retrieves a variant value for the hash key oHash out of the samples sample information .
Definition: sample_data.h:398
adtf::base::flash::tNanoSeconds GetTimeNs() const
retrieves the timestamp of the data.
Definition: sample_data.h:254
tTimeStamp GetTime() const
retrieves the timestamp of the data.
Definition: sample_data.h:237
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition: sample_data.h:271
bool IsValid() const
Validate if sample and sample buffer is set.
Definition: sample_data.h:348
const T & GetData() const
Retrieves a reference to the data.
Definition: sample_data.h:322
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_data.h:371
Easy data access for input samples of non trivial type @T (see Supported types for adtf_memory<T> for...
Definition: sample_data.h:428
tResult Reset()
Resets the sample the sample data reference to.
Definition: sample_data.h:623
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
copy CTOR
Definition: sample_data.h:455
sample_data(sample_data &&oSampleData)
move CTOR
Definition: sample_data.h:447
sample_data(const sample_data &oSampleData)
CTOR.
Definition: sample_data.h:441
const T & operator*() const
Retrieves a reference to the data.
Definition: sample_data.h:574
const T * operator->() const
Retrieves the pointer to the datas memory.
Definition: sample_data.h:545
const T * operator->()
Retrieves the pointer to the datas memory.
Definition: sample_data.h:537
adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault=adtf::util::cVariant()) const
Retrieves a variant value for the hash key oHash out of the samples sample information .
Definition: sample_data.h:638
adtf::base::flash::tNanoSeconds GetTimeNs() const
retrieves the timestamp of the data.
Definition: sample_data.h:490
tTimeStamp GetTime() const
retrieves the timestamp of the data.
Definition: sample_data.h:473
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition: sample_data.h:507
bool IsValid() const
Validate if sample and sample buffer is set.
Definition: sample_data.h:584
const T & GetData() const
Retrieves a reference to the data.
Definition: sample_data.h:558
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_data.h:608
Base object pointer to realize binary compatible reference counting in interface methods.
Implementation for a shared 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
adtf::util::cVariant get_sample_info(const ISampleInfo &oSampleInfo, const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault)
Retrieves a variant value for the hash key oHash out of the sample information oSampleInfo .
base::flash::tNanoSeconds get_sample_time(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Returns the sample time stamp with nanosecond precision.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.