ADTF
workspace/conan/dev_essential/1.4.3/dw/stable/package/aef85909f792a5a628750d25016a24323084963f/include/ddl/serialization/serialization.h
Go to the documentation of this file.
1 
15 #ifndef DDL_SERIALIZER_CLASS_HEADER
16 #define DDL_SERIALIZER_CLASS_HEADER
17 
18 #include <a_util/result.h>
19 #include <ddl/codec/codec.h>
21 
22 #include <assert.h>
23 
24 namespace ddl {
25 namespace codec {
26 
30 _MAKE_RESULT(-3, ERR_AUTIL_UNEXPECTED);
31 
35 enum class TransformOption : uint32_t
36 {
40  by_memory = 0,
44  by_value = 1
45 };
46 
56 template <typename DECODER, typename ENCODER>
57 a_util::result::Result transform(const DECODER& decoder,
58  ENCODER& encoder,
59  const TransformOption transform_option)
60 {
61  size_t current_element_count = 0;
62  std::function<void(const typename DECODER::Element& element)> transform_value;
63  if (transform_option == TransformOption::by_memory) {
64  transform_value = std::function<void(const typename DECODER::Element& element)>(
65  [&current_element_count, &encoder](const typename DECODER::Element& element) {
66  uint64_t value_pointer = {}; // this is the max possible size of a data type at the
67  // moment! Usertypes are not allowed to be greater!
68  element.getRawValue(&value_pointer, sizeof(value_pointer));
69  encoder.setElementRawValue(encoder.resolve(current_element_count++),
70  &value_pointer,
71  sizeof(value_pointer));
72  });
73  }
74  else if (transform_option == TransformOption::by_value) {
75  transform_value = std::function<void(const typename DECODER::Element& element)>(
76  [&current_element_count, &encoder](const typename DECODER::Element& element) {
77  encoder.setElementVariantValue(encoder.resolve(current_element_count++),
78  element.getVariantValue());
79  });
80  }
81  else {
82  RETURN_ERROR_DESCRIPTION(ERR_AUTIL_UNEXPECTED,
83  std::string("Invalid transform_option given: " +
84  std::to_string(static_cast<uint32_t>(transform_option))).c_str());
85  }
86 
87  try {
88  forEachLeafElement(decoder.getElements(),
89  [&transform_value, &encoder](const auto& element) { transform_value(element); });
90  }
91  catch (const std::exception& oException) {
92  RETURN_ERROR_DESCRIPTION(ERR_AUTIL_UNEXPECTED, oException.what());
93  }
94  return {};
95 }
96 
105 template <typename DECODER, typename ENCODER>
106 a_util::result::Result transform(const DECODER& decoder, ENCODER& encoder)
107 {
108  return transform<DECODER, ENCODER>(decoder, encoder, TransformOption::by_value);
109 }
110 
121  bool zero = false);
122 
123 } // namespace codec
124 } // namespace ddl
125 
127 
128 #endif // DDL_SERIALIZER_CLASS_HEADER
Memory buffer class to encapsulate and manage raw contiguously memory.
Definition: memorybuffer.h:23
A common result class usable as return value throughout.
#define RETURN_ERROR_DESCRIPTION(_errcode,...)
Return an a_util::result::Result object with detailed error information.
Definition: error_def.h:39
cString to_string(const tResult &i_oResult, eResultFormatFlags i_eFormatFlags=eResultFormatFlags::RFF_DisableNone, const tChar *i_strFormat=nullptr)
Copy all information of an assigned result object to a (formatted) string.
bool zero(void *dest, std::size_t dest_size, std::size_t bytes_to_zero)
Portable safe memzero.
a_util::result::Result transform(const DECODER &decoder, ENCODER &encoder, const TransformOption transform_option)
Copies all elements memory content from decoder elements to codec elements The encoders element count...
_MAKE_RESULT(-3, ERR_AUTIL_UNEXPECTED)
Creates an a_util error ERR_AUTIL_UNEXPECTED.
void forEachLeafElement(ElementsType &elements, const std::function< void(std::conditional_t< std::is_const< ElementsType >::value, const typename ElementsType::element_type, typename ElementsType::element_type > &)> &func)
Iterates ALL leaf elements within ALL array elements.
a_util::result::Result transformToBuffer(const codec::Decoder &decoder, a_util::memory::MemoryBuffer &buffer, bool zero=false)
Tranforms the data from a given decoder into the opposite data representation.
@ by_value
Transform each element by value.
@ by_memory
Transform each element by memory.
Implementation of the ADTF default media description.
Utility for the Neutrino gcc5 compiler which has really no std::to_string implementation!
Common include for component a_util::result.