14 #ifndef DDL_STRUCT_ELEMENT_ACCESS_CLASS_HEADER
15 #define DDL_STRUCT_ELEMENT_ACCESS_CLASS_HEADER
22 #include <type_traits>
25 namespace access_element {
36 using is_factory = std::is_member_function_pointer<decltype(&T::getStaticElementCount)>;
38 template <
typename T,
typename Enable =
void>
40 static inline size_t getElementCount(
const T& access_type)
42 return access_type.getElementCount();
47 const StructElement*& element)
49 return access_type.getElement(element_idx, element);
54 struct Accessor<T, typename std::enable_if<is_factory<T>::value>::type> {
55 static inline size_t getElementCount(
const T& access_type)
57 return access_type.getStaticElementCount();
62 const StructElement*& element)
64 return access_type.getStaticElement(element_idx, element);
70 const std::string& struct_name,
74 if (struct_name.empty()) {
76 return a_util::result::SUCCESS;
79 size_t element_count = detail::Accessor<T>::getElementCount(decoder);
80 std::string prefix = struct_name + post_fix;
81 for (
size_t element_index = 0; element_index < element_count; ++element_index) {
82 const StructElement* element;
83 if (detail::Accessor<T>::getElement(decoder, element_index, element)) {
86 index = element_index;
87 return a_util::result::SUCCESS;
106 template <
typename T>
109 size_t element_count = detail::Accessor<T>::getElementCount(decoder);
110 for (
size_t element_index = 0; element_index < element_count; ++element_index) {
112 if (detail::Accessor<T>::getElement(decoder, element_index, element)) {
113 if (element->
name == element_name) {
114 index = element_index;
115 return a_util::result::SUCCESS;
120 return ERR_NOT_FOUND;
130 template <
typename T>
132 const std::string& struct_name,
135 return detail::findComplexIndex<T>(decoder, struct_name, index,
".");
145 template <
typename T>
147 const std::string& array_name,
150 return detail::findComplexIndex<T>(decoder, array_name, index,
"[");
160 template <
typename T>
162 const std::string& array_name,
165 size_t element_count = detail::Accessor<T>::getElementCount(decoder);
166 std::string prefix = array_name +
"[";
167 for (index += 1; index < element_count; ++index) {
169 if (!detail::Accessor<T>::getElement(decoder, index, element)) {
170 return a_util::result::SUCCESS;
175 return a_util::result::SUCCESS;
179 return a_util::result::SUCCESS;
189 template <
typename T>
192 auto element_index{
static_cast<std::size_t
>(-1)};
194 return res ? decoder.getElementValue(element_index, value) : res;
204 template <
typename T>
207 auto element_index{
static_cast<std::size_t
>(-1)};
209 return res ? codec.setElementValue(element_index, value) : res;
218 template <
typename T>
222 auto element_index{
static_cast<std::size_t
>(-1)};
223 if (
findIndex(decoder, element_name, element_index)) {
224 decoder.getElementValue(element_index, result);
236 template <
typename T>
238 const std::string& element_name,
241 auto element_index{
static_cast<std::size_t
>(-1)};
243 return res ? codec.setElementValue(element_index, value) : res;
252 template <
typename T>
256 if (decoder.getElementValue(element_index, result)) {
269 template <
typename T>
272 auto element_index{
static_cast<std::size_t
>(-1)};
273 if (
find_index(decoder, element_name, element_index)) {
274 return decoder.getElementAddress(element_index);
286 template <
typename T>
289 auto element_index{
static_cast<std::size_t
>(-1)};
290 if (
findIndex(codec, element_name, element_index)) {
291 return codec.getElementAddress(element_index);
303 template <
typename STRUCT,
typename T>
306 auto element_index{
static_cast<std::size_t
>(-1)};
308 return reinterpret_cast<const STRUCT*
>(decoder.getElementAddress(element_index));
320 template <
typename STRUCT,
typename T>
323 auto element_index{
static_cast<std::size_t
>(-1)};
325 return reinterpret_cast<STRUCT*
>(codec.getElementAddress(element_index));
338 template <
typename T,
typename CODEC>
340 const std::string& struct_name,
343 const T* address = getStructAddress<T>(decoder, struct_name);
345 return ERR_NOT_FOUND;
349 return a_util::result::SUCCESS;
359 template <
typename T,
typename CODEC>
361 const std::string& struct_name,
362 const T* struct_value)
364 T* address = getStructAddress<T>(codec, struct_name);
366 return ERR_NOT_FOUND;
370 return a_util::result::SUCCESS;
379 template <
typename ARRAY_TYPE,
typename T>
382 auto element_index{
static_cast<std::size_t
>(-1)};
384 return reinterpret_cast<const ARRAY_TYPE*
>(decoder.getElementAddress(element_index));
396 template <
typename ARRAY_TYPE,
typename T>
399 auto element_index{
static_cast<std::size_t
>(-1)};
401 return reinterpret_cast<ARRAY_TYPE*
>(codec.getElementAddress(element_index));
415 template <
typename CODEC>
417 const std::string& array_name,
418 const void*& start_address,
421 size_t start_index = 0;
426 size_t end_index = start_index;
431 start_address = decoder.getElementAddress(start_index);
432 if (!start_address) {
433 return ERR_UNEXPECTED;
436 const void* end_adress = decoder.getElementAddress(end_index);
438 size =
static_cast<const uint8_t*
>(end_adress) -
static_cast<const uint8_t*
>(start_address);
442 size_t start_offset =
static_cast<const uint8_t*
>(start_address) -
443 static_cast<const uint8_t*
>(decoder.getElementAddress(0));
444 size = decoder.getBufferSize(decoder.getRepresentation()) - start_offset;
447 return a_util::result::SUCCESS;
457 template <
typename T,
typename CODEC>
459 const std::string& array_name,
462 const void* start_address =
nullptr;
469 return a_util::result::SUCCESS;
478 template <
typename T>
481 auto element_index{
static_cast<std::size_t
>(-1)};
487 return codec.setElementValue(element_index, &
zero);
491 #define DDL_GET_ENUM_CASE(__variant_type, __data_type) \
492 case a_util::variant::VT_##__variant_type: { \
493 __data_type xValue = value.get##__variant_type(); \
494 for (AccessEnumType::const_iterator it = element->p_enum->begin(); \
495 it != element->p_enum->end(); \
497 if (xValue == it->second.as##__variant_type()) { \
510 template <
typename T>
515 if (decoder.getElement(element_index, element)) {
516 if (decoder.getElementValue(element_index, value)) {
517 if (element->p_enum) {
546 template <
typename T>
549 auto element_index{
static_cast<std::size_t
>(-1)};
550 if (
findIndex(decoder, element_name, element_index)) {
A common result class usable as return value throughout.
Container type, able to store any primitive data type (and arrays thereof)
std::string asString() const
Convert the value of the variant.
VariantType getType() const
Returns the current underlying data type of the instance.
Definition of old CodecFactory legacy header for old Codec API.
bool copy(void *dest, std::size_t dest_size, const void *source, std::size_t bytes_to_copy)
Portable safe memcopy.
bool zero(void *dest, std::size_t dest_size, std::size_t bytes_to_zero)
Portable safe memzero.
int compare(const char *left, const char *right)
Compares two 0-terminated C-strings.
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_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_util::result::Result findArrayEndIndex(const T &decoder, const std::string &array_name, size_t &index)
find the index of the first element after an array by name.
std::string getValueAsString(const T &decoder, size_t element_index)
Get the value of an element as a string, using enum value names if available.
a_util::result::Result reset(T &codec, const std::string &element_name)
Set the value of the requested element to zero.
const ARRAY_TYPE * getArrayAddress(const T &decoder, const std::string &array_name)
Get a pointer to an array by name.
a_util::result::Result getStructValue(const CODEC &decoder, const std::string &struct_name, T *struct_value)
Copy a sub-structure out of the structure.
const void * getValueAddress(const T &decoder, const std::string &element_name)
Get a pointer to an element by name.
const STRUCT * getStructAddress(const T &decoder, const std::string &struct_name)
Get a pointer to a sub-structure by name.
a_util::result::Result findArrayIndex(const T &decoder, const std::string &array_name, size_t &index)
find the index of the first element of an array by name.
a_util::result::Result setStructValue(CODEC &codec, const std::string &struct_name, const T *struct_value)
Copy a sub-structure into the structure.
a_util::result::Result getArrayValue(const CODEC &decoder, const std::string &array_name, T *array_value)
Copy an array out of the structure.
a_util::result::Result findIndex(const T &decoder, const std::string &element_name, size_t &index)
find the index of an element by name.
a_util::result::Result getValue(const T &decoder, const std::string &element_name, void *value)
Get the value of an element by name.
a_util::result::Result findStructIndex(const T &decoder, const std::string &struct_name, size_t &index)
find the index of the first element of a sub-structure by name.
a_util::result::Result setValue(T &codec, const std::string &element_name, const void *value)
Set the value of an element by name.
a_util::result::Result getArray(const CODEC &decoder, const std::string &array_name, const void *&start_address, size_t &size)
Get information about an array.
_MAKE_RESULT(-20, ERR_STRUCT_ELEMENT_NOT_FOUND)
Creates an a_util error ERR_AUTIL_NOT_FOUND.
definition of the ddl namespace
Common include for component a_util::strings.
Information about an element accessible with a decoder or codec.
std::string name
The full name of the element.
Definition of old StructElement legacy header for old Codec API.
Common include for component a_util::result.
#define DDL_GET_ENUM_CASE(__variant_type, __data_type)
For internal use only.