ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
13namespace adtf
14{
15namespace streaming
16{
17namespace ant
18{
19
31template <typename T>
33{
34protected:
35 ucom::object_ptr<const ISample> m_pCurrentSample;
37public:
40 {
41 }
42
43 sample_data(const sample_data& oSampleData)
44 {
45 Reset(oSampleData.m_pCurrentSample);
46 }
47
49 sample_data(sample_data&& oSampleData)
50 {
51 std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
52 std::swap(m_pBuffer, oSampleData.m_pBuffer);
53 }
54
57 {
58 Reset(pSample);
59 }
60
69 {
70 if (m_pCurrentSample)
71 {
72 return m_pCurrentSample->GetTime();
73 }
74 return -1;
75 }
76
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 }
92
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 }
111
116 bool IsValid() const
117 {
118 return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
119 }
120
125 operator bool() const
126 {
127 return IsValid();
128 }
129
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
174namespace flash
175{
189template<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
195private:
198
199public:
202 {
203 }
204
206 sample_data(const sample_data& oSampleData)
207 {
208 Reset(oSampleData.m_pCurrentSample);
209 }
210
213 {
214 std::swap(m_pCurrentSample, oSampleData.m_pCurrentSample);
215 std::swap(m_pBuffer, oSampleData.m_pBuffer);
216 }
217
220 {
221 Reset(pSample);
222 }
223
225 {
227 }
228
238 {
239 if (m_pCurrentSample)
240 {
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
409namespace penguin
410{
411
426template<typename T, typename Enable=void>
428{
429private:
432 T m_oExtractedValue = {};
433
434public:
437 {
438 }
439
441 sample_data(const sample_data& oSampleData)
442 {
443 Reset(oSampleData.m_pCurrentSample);
444 }
445
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 }
453
456 {
457 Reset(pSample);
458 }
459
461 {
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
661template<typename T>
662class sample_data<T, typename std::enable_if_t<std::is_trivially_copyable<T>::value>> : public flash::sample_data<T>
663{
664private:
665 using _base_type = flash::sample_data<T>;
666public:
669 _base_type()
670 {
671 }
672
674 sample_data(const sample_data& oSampleData):
675 _base_type(oSampleData)
676 {
677 }
678
680 sample_data(sample_data&& oSampleData):
681 _base_type(oSampleData)
682 {
683 }
684
687 _base_type(pSample)
688 {
689 }
690
693 _base_type(pSample)
694 {
695 }
696};
697
698} // namespace penguin
699
700using penguin::sample_data;
701
702} // namespace streaming
703} // namespace adtf
704
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#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 IRawMemory interface (see Supported types for adtf_memory<T> fo...
tResult Reset()
Resets the sample the sample data reference to.
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 * GetDataPtr() const
Retrieves the pointer to the datas memory.
Definition sample_data.h:84
const T GetData() const
Retrieves the pointer to the datas memory as type T*.
tTimeStamp GetTime() const
retrieves the time of the data.
Definition sample_data.h:68
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.
bool IsValid() const
Validate if sample and sample buffer is set.
sample_data(const ucom::iobject_ptr< const ISample > &pSample)
copy CTOR
Definition sample_data.h:56
Easy data access for input samples.
tResult Reset()
Resets the sample the sample data reference to.
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
copy CTOR
const T & operator*() const
Retrieves a reference to the data.
sample_data(sample_data &&oSampleData)
move CTOR
const T * operator->()
Retrieves the pointer to the datas memory.
sample_data(const sample_data &oSampleData)
CTOR.
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
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 .
adtf::base::flash::tNanoSeconds GetTimeNs() const
retrieves the timestamp of the data.
tTimeStamp GetTime() const
retrieves the timestamp of the data.
const T * operator->() const
Retrieves the pointer to the datas memory.
bool IsValid() const
Validate if sample and sample buffer is set.
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.
const T & GetData() const
Retrieves a reference to the data.
tResult Reset()
Resets the sample the sample data reference to.
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
copy CTOR
const T & operator*() const
Retrieves a reference to the data.
sample_data(sample_data &&oSampleData)
move CTOR
const T * operator->()
Retrieves the pointer to the datas memory.
sample_data(const sample_data &oSampleData)
CTOR.
const T * GetDataPtr() const
Retrieves the pointer to the datas memory.
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 .
adtf::base::flash::tNanoSeconds GetTimeNs() const
retrieves the timestamp of the data.
tTimeStamp GetTime() const
retrieves the timestamp of the data.
const T * operator->() const
Retrieves the pointer to the datas memory.
bool IsValid() const
Validate if sample and sample buffer is set.
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.
const T & GetData() const
Retrieves a reference to the data.
Base object pointer to realize binary compatible reference counting in interface methods.
Implementation for a shared lock guard.
tVoid swap(A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oLHS, A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oRHS)
std::swap specialization for AUTILSDPtr for perfect fit on ADL
Definition d_ptr.h:237
DestinationTimeStamp duration_cast(const SourceTimeStamp &)
Duration cast base template to converted between different time resolution.
Namespace for all functionality of the ADTF Streaming SDK provided since v3.0.
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 the ADTF Streaming SDK.
base::flash::tNanoSeconds get_sample_time(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Returns the sample time stamp with nanosecond precision.
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Alias always bringing the latest version of ant::ucom_object_ptr_cast() into scope.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.