ADTF  3.18.2
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 
15 #include <adtf_base_deprecated.h>
16 
17 namespace adtf
18 {
19 namespace mediadescription
20 {
21 namespace flash
22 {
23 
24 namespace detail
25 {
26 
27 template <typename Type>
29 
30 #define DDL_DEFINE_ARITHMETIC_TYPE_NAME(__type, __name) \
31 template <> \
32 struct arithmetic_type_name<__type> \
33 { \
34  static constexpr const char* name = __name; \
35 };
36 
37 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tBool, "tBool")
38 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tChar, "tChar")
39 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt8, "tUInt8")
40 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt16, "tUInt16")
41 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt32, "tUInt32")
42 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tUInt64, "tUInt64")
43 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt8, "tInt8")
44 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt16, "tInt16")
45 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt32, "tInt32")
46 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tInt64, "tInt64")
47 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tFloat32, "tFloat32")
48 DDL_DEFINE_ARITHMETIC_TYPE_NAME(tFloat64, "tFloat64")
49 
50 template<typename T, typename Enable = void>
52 {
53  static constexpr size_t value = 1;
54 };
55 
56 template<typename T>
57 struct 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 
62 inline 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 
73 template <typename T, typename MemberType>
74 size_t member_pointer_to_offset(MemberType T::* pMember)
75 {
76  T* pStart = 0;
77  return reinterpret_cast<size_t>(&(pStart->*pMember));
78 }
79 
80 }
81 
85 class cType
86 {
87  public:
88  virtual ~cType();
89 
93  virtual std::string GetName() const = 0;
94 
98  virtual size_t GetSize() const = 0;
99 
103  virtual size_t GetAlignment() const = 0;
104 };
105 
109 template <typename Type>
110 struct arithmetic_type: public cType
111 {
113  {
114  static_assert(std::is_arithmetic<Type>::value, "Type is not an arithmetic type");
115  }
116 
117  std::string GetName() const override
118  {
120  }
121 
122  size_t GetSize() const override
123  {
124  return sizeof(Type);
125  }
126 
127  size_t GetAlignment() const override
128  {
129  return alignof(Type);
130  }
131 };
132 
138 class cEnumerationType: public cType
139 {
140  public:
141  struct tValue
142  {
143  std::string strName;
144  int64_t nAssignedValue;
145  };
146 
147  public:
153  cEnumerationType(const std::string& strName, const std::shared_ptr<cType>& pUnderlyingType);
154  cEnumerationType(const cEnumerationType& oOther);
155  ~cEnumerationType() override = default;
156 
157  std::string GetName() const override;
158  size_t GetSize() const override;
159  size_t GetAlignment() const override;
160 
164  const std::shared_ptr<cType>& GetUnderlyingType() const;
165 
169  const std::vector<tValue>& GetValues() const;
170 
171  protected:
175  void Add(tValue&& sNewValue);
176 
177  private:
178  std::string m_strName;
179  std::shared_ptr<cType> m_pUnderlyingType;
180  std::vector<tValue> m_oValues;
181 };
182 
199 template <typename Type, typename Enable = void>
201 {
202  public:
206  enumeration(const std::string& strName):
207  cEnumerationType(strName,
208  std::make_shared<arithmetic_type<typename std::underlying_type<Type>::type>>())
209  {
210  }
211 
218  enumeration& Add(const std::string& strName, Type eValue)
219  {
220  cEnumerationType::Add({strName, static_cast<int64_t>(eValue)});
221  return *this;
222  }
223 };
224 
235 template <typename UnderlyingType>
236 class enumeration<UnderlyingType,
237  typename std::enable_if<std::is_arithmetic<UnderlyingType>::value>::type>: public cEnumerationType
238 {
239  public:
243  enumeration(const std::string& strName):
244  cEnumerationType(strName,
245  std::make_shared<arithmetic_type<UnderlyingType>>())
246  {
247  }
248 
255  enumeration& Add(const std::string& strName, int64_t nValue)
256  {
257  cEnumerationType::Add({strName, nValue});
258  return *this;
259  }
260 };
261 
267 class cStructureType: public cType
268 {
269  public:
273  struct member
274  {
275  size_t nOffset;
278  size_t nSize;
279  size_t nArrayCount;
280  std::string strName;
281  std::shared_ptr<cType> pTypeDefinition;
282  };
283 
284  public:
291  cStructureType(const std::string& strName,
292  size_t nSize,
293  size_t nAlignment);
294  cStructureType(const cStructureType& oOther);
295  ~cStructureType() override = default;
296 
297  std::string GetName() const override;
298  size_t GetSize() const override;
299  size_t GetAlignment() const override;
300 
304  const std::vector<member>& GetMembers() const;
305 
310  std::pair<std::string, std::string> GetAsStringPair() const;
311 
316  operator std::pair<std::string, std::string>() const
317  {
318  return GetAsStringPair();
319  }
320 
321  protected:
326  void Add(member&& sNewMember);
327 
328  private:
329  std::string m_strName;
330  size_t m_nSize;
331  size_t m_nAlignment;
332  std::vector<member> m_oMembers;
333 };
334 
364 template <typename T = void>
366 {
367  static_assert(std::is_trivially_copyable<T>::value,
368  "You can only use structs without pointers or complex members.");
369 
370  public:
375  structure(const std::string& strName):
376  cStructureType(strName, sizeof(T), alignof(T))
377  {
378  }
379 
386  template <typename MemberType>
387  structure& Add(const std::string& strName,
388  MemberType T::* pMemberOffset)
389  {
390  cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
391  m_nCurrentSerializedOffset,
392  detail::is_little_endian(),
393  sizeof(MemberType),
395  strName,
396  std::make_shared<arithmetic_type<typename std::remove_pointer<typename std::decay<MemberType>::type>::type>>()});
397  m_nCurrentSerializedOffset += sizeof(MemberType);
398  return *this;
399  }
400 
408  template <typename MemberType>
409  structure& Add(const std::string& strName, MemberType T::* pMemberOffset, const cEnumerationType& oTypeDefinition)
410  {
411  cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
412  m_nCurrentSerializedOffset,
413  detail::is_little_endian(),
414  sizeof(typename std::underlying_type<MemberType>::type),
416  strName,
417  std::make_shared<cEnumerationType>(oTypeDefinition)});
418  m_nCurrentSerializedOffset += sizeof(typename std::underlying_type<MemberType>::type);
419  return *this;
420  }
421 
429  template <typename MemberType>
430  structure& Add(const std::string& strName, MemberType T::* pMemberOffset, const cStructureType& oTypeDefinition)
431  {
432  cStructureType::Add({detail::member_pointer_to_offset(pMemberOffset),
433  m_nCurrentSerializedOffset,
434  detail::is_little_endian(),
435  sizeof(MemberType),
437  strName,
438  std::make_shared<cStructureType>(oTypeDefinition)});
439  m_nCurrentSerializedOffset += sizeof(MemberType);
440  return *this;
441  }
442 
443  private:
444  size_t m_nCurrentSerializedOffset = 0;
445 };
446 
461 template <>
462 class structure<void>: public cStructureType
463 {
464  public:
470  ADTF3_DEPRECATED("Use ddl::dd::DDStructure instead.")
471  structure(const std::string& strName, size_t nAlignment = 0);
472 
473  size_t GetSize() const override;
474  size_t GetAlignment() const override;
475 
485  template <typename ArithmeticType>
486  typename std::enable_if<std::is_arithmetic<ArithmeticType>::value, structure>::type&
487  Add(const std::string& strName,
488  size_t nArrayCount = 1,
489  size_t nAlignment = 0,
490  int32_t nBytePosition = -1,
491  bool bLittleEndian = true)
492  {
493  Add(strName, nArrayCount, nAlignment, nBytePosition, bLittleEndian, std::make_shared<arithmetic_type<ArithmeticType>>());
494  return *this;
495  }
496 
507  structure& Add(const std::string& strName,
508  const cEnumerationType& oTypeDefinition,
509  size_t nArrayCount = 1,
510  size_t nAlignment = 0,
511  int32_t nBytePosition = -1,
512  bool bLittleEndian = true);
513 
524  structure& Add(const std::string& strName,
525  const cStructureType& oTypeDefinition,
526  size_t nArrayCount = 1,
527  size_t nAlignment = 0,
528  int32_t nBytePosition = -1,
529  bool bLittleEndian = true);
530 
531  private:
532  void Add(const std::string& strName,
533  size_t nArrayCount,
534  size_t nAlignment,
535  int32_t nBytePosition,
536  bool bLittleEndian,
537  const std::shared_ptr<cType>& oTypeDefinition);
538 
539  size_t Align(size_t nOffset, size_t nAlignment) const;
540  void AlignOffset(size_t nAlignment);
541 
542  private:
543  size_t m_nCurrentOffset = 0;
544  size_t m_nCurrentSerializedOffset = 0;
545  size_t m_nMaxAlignment = 1;
546 };
547 
548 }
549 
550 using flash::cType;
551 }
552 }
Copyright © Audi Electronics Venture GmbH.
#define ADTF3_DEPRECATED(_depr_message_)
Mark a function or variable as deprecated.
Copyright © Audi Electronics Venture GmbH.
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
int64_t tInt64
type definition for signed integer values (64bit) (platform and compiler independent type).
int16_t tInt16
type definition for signed integer values (16bit) (platform and compiler independent type).
int32_t tInt32
type definition for signed integer values (32bit) (platform and compiler independent type).
float tFloat32
type definition for Float32 (32bit float values) (platform and compiler independent type).
uint16_t tUInt16
type definition for unsigned integer values (16bit) (platform and compiler independent type).
double tFloat64
type definition for Float64 (64bit double values) (platform and compiler independent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
uint32_t tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
int8_t tInt8
type definition for signed integer values (8bit) (platform and compiler independent type).
uint64_t tUInt64
type definition for unsigned integer values (64bit) (platform and compiler independent type).
cEnumerationType(const std::string &strName, const std::shared_ptr< cType > &pUnderlyingType)
Initializes the enumeration with a name and underlying type.
void Add(tValue &&sNewValue)
Adds a new mapped value to the enumeration.
const std::shared_ptr< cType > & GetUnderlyingType() const
const std::vector< tValue > & GetValues() const
std::pair< std::string, std::string > GetAsStringPair() const
Legacy conversion to a pair of (first) md_struct and (second)md_description.
std::string GetName() const override
const std::vector< member > & GetMembers() const
void Add(member &&sNewMember)
Adds a new member to the structure.
cStructureType(const std::string &strName, size_t nSize, size_t nAlignment)
Initializes the structure type.
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)
Adds a new mapped value to the enumeration.
This is used to create an enumeration ytpe from an existing c++ enum.
enumeration & Add(const std::string &strName, Type eValue)
Adds a new mapped value to the enumeration.
structure & Add(const std::string &strName, const cEnumerationType &oTypeDefinition, size_t nArrayCount=1, size_t nAlignment=0, int32_t nBytePosition=-1, bool bLittleEndian=true)
Adds a new member of an enumeration type.
structure & Add(const std::string &strName, const cStructureType &oTypeDefinition, size_t nArrayCount=1, size_t nAlignment=0, int32_t nBytePosition=-1, bool bLittleEndian=true)
Adds a new member of a structure type.
This is used to create a structure type from an existing c++ struct definition.
structure & Add(const std::string &strName, MemberType T::*pMemberOffset)
Adds a new member of arithmetic type (POD).
structure(const std::string &strName)
Initializes the structure type.
structure & Add(const std::string &strName, MemberType T::*pMemberOffset, const cStructureType &oTypeDefinition)
Adds a new member of a structure type.
structure & Add(const std::string &strName, MemberType T::*pMemberOffset, const cEnumerationType &oTypeDefinition)
Adds a new member of an enumeration type.
Namespace for entire ADTF SDK.
Type implementation for arithmetic (POD) types.
size_t nSerializedOffset
The offset in bytes in the serialized representation.
std::shared_ptr< cType > pTypeDefinition
The type of the member.
bool nSerializedLittleEndian
Whether or not the serialized value is in little endian or not.