ADTF  3.18.2
builds/digitalwerk/solutions/adtf_content/adtf_base/adtf_core/extern/pkg_ddl/include/adtfddl/codec/access_element.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "struct_element.h"
9 #include <a_utils.h>
10 
12 
13 namespace adtf_ddl
14 {
15 
16 namespace access_element
17 {
18 
19 namespace detail
20 {
21 
23 template<typename T>
24 class is_factory : private T
25 {
26  typedef char one;
27  typedef long two;
28 
29  template<typename C>
30  static one test(decltype(static_cast<size_t (T::*)() const>(&is_factory<C>::GetStaticElementCount)));
31  template<typename C>
32  static two test(...);
33 
34 public:
35  enum
36  {
37  value = sizeof(test<T>(static_cast<size_t (T::*)() const>(nullptr))) == sizeof(char)
38  };
39 };
40 
42 template <typename T, typename Enable = void>
43 struct dispatcher;
44 
45 template<typename T>
46 struct dispatcher<T, typename std::enable_if<!is_factory<T>::value>::type>
47 {
48  typedef typename T::DefinedStructElementType StructElementType;
49  static inline size_t GetElementCount(const T& oType)
50  {
51  return oType.GetElementCount();
52  }
53 
54  static inline tResult GetElement(const T& oType, size_t nElement, const StructElementType*& pElement)
55  {
56  return oType.GetElement(nElement, pElement);
57  }
58 
59  static inline const char* GetElementName(const T& oType, const StructElementType* pElement)
60  {
61  return oType.GetStructElementName(pElement);
62  }
63 };
64 
66 template <typename T>
67 struct dispatcher<T, typename std::enable_if<is_factory<T>::value>::type>
68 {
69  typedef typename T::DefinedStructElementType StructElementType;
70  static inline size_t GetElementCount(const T& oType)
71  {
72  return oType.GetStaticElementCount();
73  }
74 
75  static inline tResult GetElement(const T& oType, size_t nElement, const StructElementType*& pElement)
76  {
77  return oType.GetStaticElement(nElement, pElement);
78  }
79 
80  static inline const char* GetElementName(const T& oType, const StructElementType* pElement)
81  {
82  return oType.GetStaticStructElementName(pElement);
83  }
84 };
85 
87 template <typename T>
88 tResult find_complex_index(const T& oDecoder, const A_UTILS_NS::cString& strStructName,
89  size_t& nIndex, const char* strPostfix)
90 {
91  if (strStructName.IsEmpty())
92  {
93  nIndex = 0;
95  }
96 
97  size_t nElementCount = dispatcher<T>::GetElementCount(oDecoder);
98  A_UTILS_NS::cString strPrefix = strStructName + strPostfix;
99  for (size_t nElement = 0; nElement < nElementCount; ++nElement)
100  {
101  const typename dispatcher<T>::StructElementType* pElement;
102  if (IS_OK(dispatcher<T>::GetElement(oDecoder, nElement, pElement)))
103  {
104  adtf_util::cString strElementName = detail::dispatcher<T>::GetElementName(oDecoder, pElement);
105  if (strElementName.StartsWith(strPrefix))
106  {
107  nIndex = nElement;
109  }
110  }
111  }
112 
113  RETURN_ERROR(ERR_NOT_FOUND);
114 }
115 
116 } //namespace detail
117 
125 template <typename T>
126 tResult find_index(const T& oDecoder, const A_UTILS_NS::cString& strElementName, size_t& nIndex)
127 {
128  size_t nElementCount = detail::dispatcher<T>::GetElementCount(oDecoder);
129  for (size_t nElement = 0; nElement < nElementCount; ++nElement)
130  {
131  const typename detail::dispatcher<T>::StructElementType* pElement;
132  if (IS_OK(detail::dispatcher<T>::GetElement(oDecoder, nElement, pElement)))
133  {
134  if (A_UTILS_NS::cStringUtil::IsEqual(strElementName.GetPtr(), detail::dispatcher<T>::GetElementName(oDecoder, pElement)))
135  {
136  nIndex = nElement;
138  }
139  }
140  }
141 
142  RETURN_ERROR(ERR_NOT_FOUND);
143 }
144 
152 template <typename T>
153 tResult find_struct_index(const T& oDecoder, const A_UTILS_NS::cString& strStructName, size_t& nIndex)
154 {
155  return detail::find_complex_index<T>(oDecoder, strStructName, nIndex, ".");
156 }
157 
165 template <typename T>
166 tResult find_array_index(const T& oDecoder, const A_UTILS_NS::cString& strArrayName, size_t& nIndex)
167 {
168  return detail::find_complex_index<T>(oDecoder, strArrayName, nIndex, "[");
169 }
170 
178 template <typename T>
179 tResult find_array_end_index(const T& oDecoder, const A_UTILS_NS::cString& strArrayName, size_t& nIndex)
180 {
181  size_t nElementCount = detail::dispatcher<T>::GetElementCount(oDecoder);
182  A_UTILS_NS::cString strPrefix = strArrayName + "[";
183  for (nIndex += 1; nIndex < nElementCount; ++nIndex)
184  {
185  const typename detail::dispatcher<T>::StructElementType* pElement;
186  if (IS_FAILED(detail::dispatcher<T>::GetElement(oDecoder, nIndex, pElement)))
187  {
189  }
190  adtf_util::cString strElementName = detail::dispatcher<T>::GetElementName(oDecoder, pElement);
191  if (!strElementName.StartsWith(strPrefix))
192  {
194  }
195  }
196 
198 }
199 
207 template <typename T>
208 tResult get_value(const T& oDecoder, const A_UTILS_NS::cString& strElementName, void* pValue)
209 {
210  size_t nElement = 0;
211  RETURN_IF_FAILED(find_index(oDecoder, strElementName, nElement));
212  return oDecoder.GetElementValue(nElement, pValue);
213 }
214 
222 template <typename T>
223 tResult set_value(T& oCodec, const A_UTILS_NS::cString& strElementName, const void* pValue)
224 {
225  size_t nElement = 0;
226  RETURN_IF_FAILED(find_index(oCodec, strElementName, nElement));
227  return oCodec.SetElementValue(nElement, pValue);
228 }
229 
236 template <typename T>
237 A_UTILS_NS::cVariant get_value(const T& oDecoder, const A_UTILS_NS::cString& strElementName)
238 {
239  A_UTILS_NS::cVariant oResult;
240  size_t nElement = 0;
241  if (IS_OK(find_index(oDecoder, strElementName, nElement)))
242  {
243  oDecoder.GetElementValue(nElement, oResult);
244  }
245  return oResult;
246 }
247 
255 template <typename T>
256 tResult set_value(T& oCodec, const A_UTILS_NS::cString& strElementName, const A_UTILS_NS::cVariant& oValue)
257 {
258  size_t nElement = 0;
259  RETURN_IF_FAILED(find_index(oCodec, strElementName, nElement));
260  return oCodec.SetElementValue(nElement, oValue);
261 }
262 
269 template <typename T>
270 A_UTILS_NS::cVariant get_value(const T& oDecoder, size_t nElement)
271 {
272  A_UTILS_NS::cVariant oResult;
273  if (IS_OK(oDecoder.GetElementValue(nElement, oResult)))
274  {
275  return oResult;
276  }
277 
278  return 0;
279 }
280 
287 template <typename T>
288 const void* get_value_address(const T& oDecoder, const A_UTILS_NS::cString& strElementName)
289 {
290  size_t nElement = 0;
291  if (IS_OK(find_index(oDecoder, strElementName, nElement)))
292  {
293  return oDecoder.GetElementAddress(nElement);
294  }
295 
296  return nullptr;
297 }
298 
305 template <typename T>
306 void* get_value_address(T& oCodec, const A_UTILS_NS::cString& strElementName)
307 {
308  size_t nElement = 0;
309  if (IS_OK(find_index(oCodec, strElementName, nElement)))
310  {
311  return oCodec.GetElementAddress(nElement);
312  }
313 
314  return nullptr;
315 }
316 
323 template <typename Struct, typename T>
324 const Struct* get_struct_address(const T& oDecoder, const A_UTILS_NS::cString& strStructName)
325 {
326  size_t nElement = 0;
327  if (IS_OK(find_struct_index(oDecoder, strStructName, nElement)))
328  {
329  return reinterpret_cast<const Struct*>(oDecoder.GetElementAddress(nElement));
330  }
331 
332  return nullptr;
333 }
334 
341 template <typename Struct, typename T>
342 Struct* get_struct_address(T& oCodec, const A_UTILS_NS::cString& strStructName)
343 {
344  size_t nElement = 0;
345  if (IS_OK(find_struct_index(oCodec, strStructName, nElement)))
346  {
347  return reinterpret_cast<Struct*>(oCodec.GetElementAddress(nElement));
348  }
349 
350  return nullptr;
351 }
352 
360 template <typename T, typename Codec>
361 tResult get_struct_value(const Codec& oDecoder, const A_UTILS_NS::cString& strStructName, T* pStructValue)
362 {
363  const T* pAddress = get_struct_address<T>(oDecoder, strStructName);
364  if (!pAddress)
365  {
366  RETURN_ERROR(ERR_NOT_FOUND);
367  }
368 
369  A_UTILS_NS::cMemoryBlock::MemCopy(pStructValue, pAddress, sizeof(T));
371 }
372 
380 template <typename T, typename Codec>
381 tResult set_struct_value(Codec& oCodec, const A_UTILS_NS::cString& strStructName, const T* pStructValue)
382 {
383  T* pAddress = get_struct_address<T>(oCodec, strStructName);
384  if (!pAddress)
385  {
386  RETURN_ERROR(ERR_NOT_FOUND);
387  }
388 
389  A_UTILS_NS::cMemoryBlock::MemCopy(pAddress, pStructValue, sizeof(T));
391 }
392 
399 template <typename ArrayType, typename T>
400 const ArrayType* get_array_address(const T& oDecoder, const A_UTILS_NS::cString& strArrayName)
401 {
402  size_t nElement = 0;
403  if (IS_OK(find_array_index(oDecoder, strArrayName, nElement)))
404  {
405  return reinterpret_cast<const ArrayType*>(oDecoder.GetElementAddress(nElement));
406  }
407 
408  return nullptr;
409 }
410 
417 template <typename ArrayType, typename T>
418 ArrayType* get_array_address(T& oCodec, const A_UTILS_NS::cString& strArrayName)
419 {
420  size_t nElement = 0;
421  if (IS_OK(find_array_index(oCodec, strArrayName, nElement)))
422  {
423  return reinterpret_cast<ArrayType*>(oCodec.GetElementAddress(nElement));
424  }
425 
426  return nullptr;
427 }
428 
437 template <typename Codec>
438 tResult get_array(const Codec& oDecoder, const A_UTILS_NS::cString& strArrayName,
439  const void*& pStartAddress, size_t& nSize)
440 {
441  size_t nStartIndex = 0;
442  RETURN_IF_FAILED(find_array_index(oDecoder, strArrayName, nStartIndex));
443  size_t nEndIndex = nStartIndex;
444  RETURN_IF_FAILED(find_array_end_index(oDecoder, strArrayName, nEndIndex));
445 
446  pStartAddress = oDecoder.GetElementAddress(nStartIndex);
447  if (!pStartAddress)
448  {
449  RETURN_ERROR(ERR_UNEXPECTED);
450  }
451 
452  const void* pEndAddress = oDecoder.GetElementAddress(nEndIndex);
453  if (pEndAddress)
454  {
455  nSize = static_cast<const uint8_t*>(pEndAddress) -
456  static_cast<const uint8_t*>(pStartAddress);
457  }
458  else
459  {
460  // it reaches til the end
461  size_t nStartOffset = static_cast<const uint8_t*>(pStartAddress) -
462  static_cast<const uint8_t*>(oDecoder.GetElementAddress(0));
463  nSize = oDecoder.GetBufferSize(oDecoder.GetRepresentation()) - nStartOffset;
464  }
465 
467 }
468 
476 template <typename T, typename Codec>
477 tResult get_array_value(const Codec& oDecoder, const A_UTILS_NS::cString& strArrayName, T* pArrayValue)
478 {
479  const void* pStartAddress = nullptr;
480  size_t nSize = 0;
481  RETURN_IF_FAILED(get_array(oDecoder, strArrayName, pStartAddress, nSize));
482  A_UTILS_NS::cMemoryBlock::MemCopy(pArrayValue, pStartAddress, nSize);
484 }
485 
492 template <typename T> tResult reset(T& oCodec, const A_UTILS_NS::cString& strElementName)
493 {
494  size_t nElement = 0;
495  RETURN_IF_FAILED(find_index(oCodec, strElementName, nElement));
496  uint64_t nZero = 0;
497  return oCodec.SetElementValue(nElement, &nZero);
498 }
499 
500 
507 template <typename T>
508 A_UTILS_NS::cString get_value_as_string(const T& oDecoder, size_t nElement)
509 {
510  return oDecoder.GetElementValueString(nElement);
511 }
512 
519 template <typename T>
520 A_UTILS_NS::cString get_value_as_string(const T& oDecoder, const A_UTILS_NS::cString& strElementName)
521 {
522  size_t nElement = 0;
523  if (IS_OK(find_index(oDecoder, strElementName, nElement)))
524  {
525  return get_value_as_string(oDecoder, nElement);
526  }
527  return "";
528 }
529 
530 }
531 
532 }
Copyright © Audi Electronics Venture GmbH.
#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.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
static tVoid MemCopy(tVoid *pDest, const tVoid *pSrc, tSize nCount)
Copies data from one buffer to another.
static tBool IsEqual(const tChar *str1, const tChar *str2, tSize nPos, tSize nLength)
This function checks if the two strings are equal (case sensitive)
const tChar * GetPtr() const
This function returns the current string as an array of characters (c-style)
Definition: string.h:467
tBool IsEmpty() const
This function checks if the string object is empty.
Definition: string.h:441
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
tResult find_complex_index(const T &oDecoder, const A_UTILS_NS::cString &strStructName, size_t &nIndex, const char *strPostfix)
For internal use only.
const Struct * get_struct_address(const T &oDecoder, const A_UTILS_NS::cString &strStructName)
Get a pointer to a sub-structure by name.
tResult get_struct_value(const Codec &oDecoder, const A_UTILS_NS::cString &strStructName, T *pStructValue)
Copy a sub-structure out of the structure.
tResult find_struct_index(const T &oDecoder, const A_UTILS_NS::cString &strStructName, size_t &nIndex)
Find the index of the first element of a sub-structure by name.
tResult get_array(const Codec &oDecoder, const A_UTILS_NS::cString &strArrayName, const void *&pStartAddress, size_t &nSize)
Get information about an array.
tResult find_index(const T &oDecoder, const A_UTILS_NS::cString &strElementName, size_t &nIndex)
Find the index of an element by name.
tResult find_array_end_index(const T &oDecoder, const A_UTILS_NS::cString &strArrayName, size_t &nIndex)
Find the index of the first element after an array by name.
tResult reset(T &oCodec, const A_UTILS_NS::cString &strElementName)
Set the value of the requested element to zero.
tResult set_value(T &oCodec, const A_UTILS_NS::cString &strElementName, const void *pValue)
Set the value of an element by name.
tResult get_value(const T &oDecoder, const A_UTILS_NS::cString &strElementName, void *pValue)
Get the value of an element by name.
tResult get_array_value(const Codec &oDecoder, const A_UTILS_NS::cString &strArrayName, T *pArrayValue)
Copy an array out of the structure.
tResult set_struct_value(Codec &oCodec, const A_UTILS_NS::cString &strStructName, const T *pStructValue)
Copy a sub-structure into the structure.
const ArrayType * get_array_address(const T &oDecoder, const A_UTILS_NS::cString &strArrayName)
Get a pointer to an array by name.
tResult find_array_index(const T &oDecoder, const A_UTILS_NS::cString &strArrayName, size_t &nIndex)
Find the index of the first element of an array by name.
A_UTILS_NS::cString get_value_as_string(const T &oDecoder, size_t nElement)
Get the value of an element as a string, using enum value names if available.
const void * get_value_address(const T &oDecoder, const A_UTILS_NS::cString &strElementName)
Get a pointer to an element by name.
Namespace for the mainpage_pkg_ddl.
Copyright © Audi Electronics Venture GmbH.
Forwarding legacy header.