Dev Essential
Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
1
14
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
24namespace ddl {
25namespace codec {
26
30_MAKE_RESULT(-3, ERR_AUTIL_UNEXPECTED);
31
35enum class TransformOption : uint32_t
36{
45};
46
56template <typename DECODER, typename ENCODER>
57a_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
105template <typename DECODER, typename ENCODER>
106a_util::result::Result transform(const DECODER& decoder, ENCODER& encoder)
107{
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
Definition result_type_decl.h:34
Definition codec.h:30
#define RETURN_ERROR_DESCRIPTION(_errcode,...)
Definition error_def.h:39
Namespace for the new faster CodecFactory/Decoder/Codec implementation.
Definition codec.h:22
a_util::result::Result transform(const DECODER &decoder, ENCODER &encoder, const TransformOption transform_option)
Definition serialization.h:57
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.
Definition codec_iterator.h:1475
TransformOption
Definition serialization.h:36
@ by_value
Definition serialization.h:44
@ by_memory
Definition serialization.h:40
a_util::result::Result transformToBuffer(const codec::Decoder &decoder, a_util::memory::MemoryBuffer &buffer, bool zero=false)
definition of the ddl namespace
Definition codec.h:21
#define _MAKE_RESULT(_no, _label)
Definition result_info_decl.h:36