ADTF
Loading...
Searching...
No Matches
type_reflection_legacy.h
Go to the documentation of this file.
1
7#pragma once
8#include <adtf_utils.h>
9#include <type_traits>
10#include <memory>
11#include <vector>
12#include <string>
13#include <utility>
14
16
17namespace adtf
18{
19namespace mediadescription
20{
21namespace flash
22{
24namespace detail
25{
26
27template <typename Type>
28struct arithmetic_type_name;
29
30#define DDL_DEFINE_ARITHMETIC_TYPE_NAME(__type, __name) \
31template <> \
32struct arithmetic_type_name<__type> \
33{ \
34 static constexpr const char* name = __name; \
35};
36
37DDL_DEFINE_ARITHMETIC_TYPE_NAME(tBool, "tBool")
38DDL_DEFINE_ARITHMETIC_TYPE_NAME(tChar, "tChar")
39DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt8, "tUInt8")
40DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt16, "tUInt16")
41DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt32, "tUInt32")
42DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt64, "tUInt64")
43DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt8, "tInt8")
44DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt16, "tInt16")
45DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt32, "tInt32")
46DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt64, "tInt64")
47DDL_DEFINE_ARITHMETIC_TYPE_NAME(tFloat32, "tFloat32")
48DDL_DEFINE_ARITHMETIC_TYPE_NAME(tFloat64, "tFloat64")
49
50template<typename T, typename Enable = void>
51struct array_count
52{
53 static constexpr size_t value = 1;
54};
55
56template<typename T>
57struct array_count<T, typename std::enable_if<std::is_array<T>::value>::type>
58{
59 static constexpr size_t value = sizeof(T) / sizeof(typename std::remove_pointer<typename std::decay<T>::type>::type);
60};
61
62inline bool is_little_endian()
63{
64 union
65 {
66 uint16_t nNumber;
67 uint8_t aBytes[2];
68 } sTest;
69 sTest.nNumber = 0x1;
70 return sTest.aBytes[0] == 1;
71}
72
73template <typename T, typename MemberType>
74size_t member_pointer_to_offset(MemberType T::* pMember)
75{
76 T* pStart = 0;
77 return reinterpret_cast<size_t>(&(pStart->*pMember));
78}
79
80} // namespace detail
82
86class cType
87{
88 public:
89 virtual ~cType();
90
94 virtual std::string GetName() const = 0;
95
99 virtual size_t GetSize() const = 0;
100
104 virtual size_t GetAlignment() const = 0;
105};
106
110template <typename Type>
111struct arithmetic_type: public cType
112{
113 arithmetic_type()
114 {
115 static_assert(std::is_arithmetic<Type>::value, "Type is not an arithmetic type");
116 }
117
118 std::string GetName() const override
119 {
120 return detail::arithmetic_type_name<Type>::name;
121 }
122
123 size_t GetSize() const override
124 {
125 return sizeof(Type);
126 }
127
128 size_t GetAlignment() const override
129 {
130 return alignof(Type);
131 }
132};
133
140{
141 public:
145 struct tValue
146 {
148 std::string strName;
151 };
152
153 public:
159 cEnumerationType(const std::string& strName, const std::shared_ptr<cType>& pUnderlyingType);
168 ~cEnumerationType() override = default;
172 std::string GetName() const override;
176 size_t GetSize() const override;
180 size_t GetAlignment() const override;
181
185 const std::shared_ptr<cType>& GetUnderlyingType() const;
186
190 const std::vector<tValue>& GetValues() const;
191
192 protected:
196 void Add(tValue&& sNewValue);
197
198 private:
199 std::string m_strName;
200 std::shared_ptr<cType> m_pUnderlyingType;
201 std::vector<tValue> m_oValues;
202};
203
220template <typename Type, typename Enable = void>
222{
223 public:
227 enumeration(const std::string& strName):
228 cEnumerationType(strName,
229 std::make_shared<arithmetic_type<typename std::underlying_type<Type>::type>>())
230 {
231 }
232
239 enumeration& Add(const std::string& strName, Type eValue)
240 {
241 cEnumerationType::Add({strName, static_cast<int64_t>(eValue)});
242 return *this;
243 }
244};
245
256template <typename UnderlyingType>
257class enumeration<UnderlyingType,
258 typename std::enable_if<std::is_arithmetic<UnderlyingType>::value>::type>: public cEnumerationType
259{
260 public:
264 enumeration(const std::string& strName):
265 cEnumerationType(strName,
266 std::make_shared<arithmetic_type<UnderlyingType>>())
267 {
268 }
269
276 enumeration& Add(const std::string& strName, int64_t nValue)
277 {
278 cEnumerationType::Add({strName, nValue});
279 return *this;
280 }
281};
282
289{
290 public:
294 struct member
295 {
296 size_t nOffset;
299 size_t nSize;
300 size_t nArrayCount;
301 std::string strName;
302 std::shared_ptr<cType> pTypeDefinition;
303 };
304
305 public:
312 cStructureType(const std::string& strName,
313 size_t nSize,
314 size_t nAlignment);
315 cStructureType(const cStructureType& oOther);
316 ~cStructureType() override = default;
317
318 std::string GetName() const override;
319 size_t GetSize() const override;
320 size_t GetAlignment() const override;
321
325 const std::vector<member>& GetMembers() const;
326
331 std::pair<std::string, std::string> GetAsStringPair() const;
332
337 operator std::pair<std::string, std::string>() const
338 {
339 return GetAsStringPair();
340 }
341
342 protected:
347 void Add(member&& sNewMember);
348
349 private:
350 std::string m_strName;
351 size_t m_nSize;
352 size_t m_nAlignment;
353 std::vector<member> m_oMembers;
354};
355
385template <typename T = void>
387{
388 static_assert(std::is_trivially_copyable<T>::value,
389 "You can only use structs without pointers or complex members.");
390
391 public:
396 structure(const std::string& strName):
397 cStructureType(strName, sizeof(T), alignof(T))
398 {
399 }
400
407 template <typename MemberType>
408 structure& Add(const std::string& strName,
409 MemberType T::* pMemberOffset)
410 {
411 cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
412 m_nCurrentSerializedOffset,
413 detail::is_little_endian(),
414 sizeof(MemberType),
415 detail::array_count<MemberType>::value,
416 strName,
417 std::make_shared<arithmetic_type<typename std::remove_pointer<typename std::decay<MemberType>::type>::type>>()});
418 m_nCurrentSerializedOffset += sizeof(MemberType);
419 return *this;
420 }
421
429 template <typename MemberType>
430 structure& Add(const std::string& strName, MemberType T::* pMemberOffset, const cEnumerationType& oTypeDefinition)
431 {
432 cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
433 m_nCurrentSerializedOffset,
434 detail::is_little_endian(),
435 sizeof(typename std::underlying_type<MemberType>::type),
436 detail::array_count<MemberType>::value,
437 strName,
438 std::make_shared<cEnumerationType>(oTypeDefinition)});
439 m_nCurrentSerializedOffset += sizeof(typename std::underlying_type<MemberType>::type);
440 return *this;
441 }
442
450 template <typename MemberType>
451 structure& Add(const std::string& strName, MemberType T::* pMemberOffset, const cStructureType& oTypeDefinition)
452 {
453 cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
454 m_nCurrentSerializedOffset,
455 detail::is_little_endian(),
456 sizeof(MemberType),
457 detail::array_count<MemberType>::value,
458 strName,
459 std::make_shared<cStructureType>(oTypeDefinition)});
460 m_nCurrentSerializedOffset += sizeof(MemberType);
461 return *this;
462 }
463
464 private:
465 size_t m_nCurrentSerializedOffset = 0;
466};
467
482template <>
483class structure<void>: public cStructureType
484{
485 public:
492 ADTF3_DEPRECATED("Use ddl::dd::DDStructure instead.")
493 structure(const std::string& strName, size_t nAlignment = 0);
494
495 size_t GetSize() const override;
496 size_t GetAlignment() const override;
497
507 template <typename ArithmeticType>
508 typename std::enable_if<std::is_arithmetic<ArithmeticType>::value, structure>::type&
509 Add(const std::string& strName,
510 size_t nArrayCount = 1,
511 size_t nAlignment = 0,
512 int32_t nBytePosition = -1,
513 bool bLittleEndian = true)
514 {
515 Add(strName, nArrayCount, nAlignment, nBytePosition, bLittleEndian, std::make_shared<arithmetic_type<ArithmeticType>>());
516 return *this;
517 }
518
529 structure& Add(const std::string& strName,
530 const cEnumerationType& oTypeDefinition,
531 size_t nArrayCount = 1,
532 size_t nAlignment = 0,
533 int32_t nBytePosition = -1,
534 bool bLittleEndian = true);
535
546 structure& Add(const std::string& strName,
547 const cStructureType& oTypeDefinition,
548 size_t nArrayCount = 1,
549 size_t nAlignment = 0,
550 int32_t nBytePosition = -1,
551 bool bLittleEndian = true);
552
553 private:
554 void Add(const std::string& strName,
555 size_t nArrayCount,
556 size_t nAlignment,
557 int32_t nBytePosition,
558 bool bLittleEndian,
559 const std::shared_ptr<cType>& oTypeDefinition);
560
561 size_t Align(size_t nOffset, size_t nAlignment) const;
562 void AlignOffset(size_t nAlignment);
563
564 private:
565 size_t m_nCurrentOffset = 0;
566 size_t m_nCurrentSerializedOffset = 0;
567 size_t m_nMaxAlignment = 1;
568};
569
570}
571
572using flash::cType;
573}
574}
#define ADTF3_DEPRECATED(_depr_message_)
Definition adtf_base_deprecated.h:27
cEnumerationType(const std::string &strName, const std::shared_ptr< cType > &pUnderlyingType)
cStructureType(const std::string &strName, size_t nSize, size_t nAlignment)
enumeration(const std::string &strName)
Definition type_reflection_legacy.h:227
structure(const std::string &strName)
Definition type_reflection_legacy.h:396
Definition type_reflection_legacy.h:140
cEnumerationType(const cEnumerationType &oOther)
const std::shared_ptr< cType > & GetUnderlyingType() const
const std::vector< tValue > & GetValues() const
cEnumerationType(const std::string &strName, const std::shared_ptr< cType > &pUnderlyingType)
cStructureType(const std::string &strName, size_t nSize, size_t nAlignment)
const std::vector< member > & GetMembers() const
std::string GetName() const override
std::pair< std::string, std::string > GetAsStringPair() const
Definition type_reflection_legacy.h:87
virtual size_t GetAlignment() const =0
virtual std::string GetName() const =0
virtual size_t GetSize() const =0
enumeration & Add(const std::string &strName, int64_t nValue)
Definition type_reflection_legacy.h:276
enumeration(const std::string &strName)
Definition type_reflection_legacy.h:227
enumeration & Add(const std::string &strName, Type eValue)
Definition type_reflection_legacy.h:239
structure & Add(const std::string &strName, const cEnumerationType &oTypeDefinition, size_t nArrayCount=1, size_t nAlignment=0, int32_t nBytePosition=-1, bool bLittleEndian=true)
structure(const std::string &strName, size_t nAlignment=0)
structure & Add(const std::string &strName, const cStructureType &oTypeDefinition, size_t nArrayCount=1, size_t nAlignment=0, int32_t nBytePosition=-1, bool bLittleEndian=true)
std::enable_if< std::is_arithmetic< ArithmeticType >::value, structure >::type & Add(const std::string &strName, size_t nArrayCount=1, size_t nAlignment=0, int32_t nBytePosition=-1, bool bLittleEndian=true)
Definition type_reflection_legacy.h:509
structure & Add(const std::string &strName, MemberType T::*pMemberOffset, const cEnumerationType &oTypeDefinition)
Definition type_reflection_legacy.h:430
structure & Add(const std::string &strName, MemberType T::*pMemberOffset)
Definition type_reflection_legacy.h:408
structure & Add(const std::string &strName, MemberType T::*pMemberOffset, const cStructureType &oTypeDefinition)
Definition type_reflection_legacy.h:451
structure(const std::string &strName)
Definition type_reflection_legacy.h:396
Namespace for all internally used functionality implemented.
Definition adtf_mediadescription_version_namespaces.dox:34
Namespace for all functionality of the ADTF Mediadescription SDK provided since v3....
Definition codec_sample_streamer_legacy.h:20
Namespace for the ADTF Mediadescription SDK.
Definition codec_sample_streamer.h:22
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
Definition type_reflection_legacy.h:112
size_t GetSize() const override
Definition type_reflection_legacy.h:123
size_t GetAlignment() const override
Definition type_reflection_legacy.h:128
std::string GetName() const override
Definition type_reflection_legacy.h:118
Definition type_reflection_legacy.h:146
std::string strName
The name of the value.
Definition type_reflection_legacy.h:148
int64_t nAssignedValue
The value of the enumeration.
Definition type_reflection_legacy.h:150
Definition type_reflection_legacy.h:295
std::shared_ptr< cType > pTypeDefinition
The type of the member.
Definition type_reflection_legacy.h:302
std::string strName
The name of the member.
Definition type_reflection_legacy.h:301
bool nSerializedLittleEndian
Whether or not the serialized value is in little endian or not.
Definition type_reflection_legacy.h:298
size_t nSerializedOffset
The offset in bytes in the serialized representation.
Definition type_reflection_legacy.h:297
size_t nOffset
The offset in bytes.
Definition type_reflection_legacy.h:296
size_t nArrayCount
The array size, 1 -> no array.
Definition type_reflection_legacy.h:300
size_t nSize
The size in bytes.
Definition type_reflection_legacy.h:299