ADTF
Loading...
Searching...
No Matches
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{
30template<typename T>
32{
33protected:
34 ucom::object_ptr<const ISample> m_pCurrentSample;
36
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
68 tTimeStamp GetTime() const
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
237 tTimeStamp GetTime() const
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 THROW_ERROR_DESC(ERR_INVALID_STATE, "No sample attached.");
331 }
332 }
333
337 const T& operator*() const
338 {
339 return GetData();
340 }
341
347 bool IsValid() const
348 {
349 return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= sizeof(T));
350 }
351
358 operator const T&() const
359 {
360 return GetData();
361 }
362
371 {
372 RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
373 m_pCurrentSample = pSample;
375 }
376
384 {
385 m_pCurrentSample.Reset();
386 m_pBuffer = nullptr;
388 }
389
397 adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey& oHash,
398 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{
427template<typename T, typename Enable = void>
429{
430private:
433 T m_oExtractedValue = {};
434
435public:
438 {
439 }
440
442 sample_data(const sample_data& oSampleData)
443 {
444 Reset(oSampleData.m_pCurrentSample);
445 }
446
448 sample_data(sample_data&& oSampleData):
449 m_pCurrentSample(std::move(oSampleData.m_pCurrentSample)),
450 m_pBuffer(std::move(oSampleData.m_pBuffer)),
451 m_oExtractedValue(std::move(oSampleData.m_oExtractedValue))
452 {
453 }
454
457 {
458 Reset(pSample);
459 }
460
462 {
464 }
465
474 tTimeStamp GetTime() const
475 {
476 if (m_pCurrentSample)
477 {
478 return m_pCurrentSample->GetTime();
479 }
480 return -1;
481 }
482
492 {
493 if (m_pCurrentSample)
494 {
495 return get_sample_time(m_pCurrentSample);
496 }
497 return adtf::base::tNanoSeconds{-1};
498 }
499
506 const T* GetDataPtr() const
507 {
508 if (m_pBuffer)
509 {
510 return &m_oExtractedValue;
511 }
512 else
513 {
514 return nullptr;
515 }
516 }
517
521 size_t GetDataSize() const
522 {
523 if (m_pBuffer)
524 {
525 return m_pBuffer->GetSize();
526 }
527 else
528 {
529 return 0;
530 }
531 }
532
536 const T* operator->()
537 {
538 return GetDataPtr();
539 }
540
544 const T* operator->() const
545 {
546 return GetDataPtr();
547 }
548
555 const T& GetData() const&
556 {
557 if (IsValid())
558 {
559 return m_oExtractedValue;
560 }
561 else
562 {
563 THROW_ERROR_DESC(ERR_INVALID_STATE, "No sample attached.");
564 }
565 }
566
570 T&& GetData() &&
571 {
572 if (IsValid())
573 {
574 return std::move(m_oExtractedValue);
575 }
576 else
577 {
578 THROW_ERROR_DESC(ERR_INVALID_STATE, "No sample attached.");
579 }
580 }
581
585 const T& operator*() const
586 {
587 return GetData();
588 }
589
595 bool IsValid() const
596 {
597 return (m_pCurrentSample && m_pBuffer && m_pBuffer->GetSize() >= 0);
598 }
599
607 operator const T&() const&
608 {
609 return GetData();
610 }
611
619 operator T&&() &&
620 {
621 return GetData();
622 }
623
632 {
633 RETURN_IF_FAILED(pSample->Lock(m_pBuffer));
634 m_pCurrentSample = pSample;
635 // We immediatelly read the value here if there is a specialization for T
636 RETURN_IF_FAILED(m_pBuffer->Read(adtf::base::adtf_memory<T>(&m_oExtractedValue)));
638 }
639
647 {
648 m_oExtractedValue = T{};
649 m_pCurrentSample.Reset();
650 m_pBuffer = nullptr;
652 }
653
661 adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey& oHash,
662 const adtf::util::cVariant oDefault = adtf::util::cVariant()) const
663 {
664 if (m_pCurrentSample)
665 {
666 return ant::get_sample_info(*m_pCurrentSample, oHash, oDefault);
667 }
668 return oDefault;
669 }
670};
671
685template<typename T>
686class sample_data<T, typename std::enable_if_t<std::is_trivially_copyable<T>::value>> : public flash::sample_data<T>
687{
688private:
689 using _base_type = flash::sample_data<T>;
690
691public:
693 sample_data(): _base_type()
694 {
695 }
696
698 sample_data(const sample_data& oSampleData): _base_type(oSampleData)
699 {
700 }
701
703 sample_data(sample_data&& oSampleData): _base_type(oSampleData)
704 {
705 }
706
708 sample_data(const ucom::iobject_ptr<const ant::ISample>& pSample): _base_type(pSample)
709 {
710 }
711
713 sample_data(const ucom::iobject_ptr<const flash::ISample>& pSample): _base_type(pSample)
714 {
715 }
716};
717
718} // namespace penguin
719
720using penguin::sample_data;
721
722} // namespace streaming
723} // namespace adtf
#define THROW_ERROR_DESC(_code,...)
throws a tResult exception
Definition result.h:85
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
adtf::util::cStringUtil::tHashKey32 tHashKey
Type for the HashKey of the Value.
Definition hashedvaluemap_intf.h:87
Definition rawmemory_base.h:412
sample_data(const ucom::iobject_ptr< const ISample > &pSample)
Definition sample_data.h:56
const T GetData() const
Definition sample_data.h:100
tResult Reset(const ucom::ant::iobject_ptr< const ISample > &pSample)
Definition sample_data.h:151
sample_data(const sample_data &oSampleData)
CTOR.
Definition sample_data.h:43
tResult Reset()
Definition sample_data.h:164
tTimeStamp GetTime() const
Definition sample_data.h:68
bool IsValid() const
Definition sample_data.h:116
sample_data()
CTOR.
Definition sample_data.h:39
sample_data(sample_data &&oSampleData)
Definition sample_data.h:49
const T * GetDataPtr() const
Definition sample_data.h:84
Definition sample_data.h:191
const T & operator*() const
Definition sample_data.h:337
const T * operator->()
Definition sample_data.h:301
sample_data(sample_data &&oSampleData)
Definition sample_data.h:212
bool IsValid() const
Definition sample_data.h:347
tResult Reset(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Definition sample_data.h:370
const T * GetDataPtr() const
Definition sample_data.h:271
sample_data()
CTOR.
Definition sample_data.h:201
const T & GetData() const
Definition sample_data.h:322
const T * operator->() const
Definition sample_data.h:309
tResult Reset()
Definition sample_data.h:383
sample_data(const sample_data &oSampleData)
CTOR.
Definition sample_data.h:206
size_t GetDataSize() const
Definition sample_data.h:286
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
Definition sample_data.h:219
adtf::base::flash::tNanoSeconds GetTimeNs() const
Definition sample_data.h:254
adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault=adtf::util::cVariant()) const
Definition sample_data.h:397
tTimeStamp GetTime() const
Definition sample_data.h:237
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
Definition sample_data.h:708
sample_data(const ucom::iobject_ptr< const flash::ISample > &pSample)
Definition sample_data.h:713
adtf::base::flash::tNanoSeconds GetTimeNs() const
Definition sample_data.h:491
const T & GetData() const &
Definition sample_data.h:555
const T * operator->() const
Definition sample_data.h:544
sample_data(sample_data &&oSampleData)
Definition sample_data.h:448
tResult Reset(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Definition sample_data.h:631
tResult Reset()
Definition sample_data.h:646
const T * GetDataPtr() const
Definition sample_data.h:506
sample_data()
CTOR.
Definition sample_data.h:437
T && GetData() &&
Definition sample_data.h:570
bool IsValid() const
Definition sample_data.h:595
sample_data(const ucom::iobject_ptr< const ant::ISample > &pSample)
Definition sample_data.h:456
const T & operator*() const
Definition sample_data.h:585
size_t GetDataSize() const
Definition sample_data.h:521
adtf_util::cVariant GetSampleInfo(const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault=adtf::util::cVariant()) const
Definition sample_data.h:661
const T * operator->()
Definition sample_data.h:536
sample_data(const sample_data &oSampleData)
CTOR.
Definition sample_data.h:442
tTimeStamp GetTime() const
Definition sample_data.h:474
Base object pointer to realize binary compatible reference counting in interface methods.
Definition object_ptr_intf.h:112
Definition lockedobject_intf.h:273
Definition object_ptr.h:384
DestinationTimeStamp duration_cast(const SourceTimeStamp &) noexcept
Duration cast base template to converted between different time resolution.
Definition chrono.h:155
Namespace for all functionality of the ADTF Streaming SDK provided since v3.0.
Definition bindingproxyoutport.h:16
adtf::util::cVariant get_sample_info(const ISampleInfo &oSampleInfo, const ISampleInfo::tHashKey &oHash, const adtf::util::cVariant oDefault)
base::flash::tNanoSeconds get_sample_time(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
Namespace for all functionality of the ADTF Streaming SDK provided since v3.15.
Definition sample_data.h:410
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14
base::flash::tNanoSeconds get_sample_time(const ucom::ant::iobject_ptr< const ant::ISample > &pSample)
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.
Definition object_ptr_utilities.h:104
ant::iobject_ptr< T > iobject_ptr
Alias always bringing the latest version of ant::iobject_ptr into scope.
Definition object_ptr_intf.h:437
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
A timestamp with nanosecond precision.
Definition chrono.h:23