16#ifndef DDL_LEAF_VALUE_ACCESS_CLASS_HEADER
17#define DDL_LEAF_VALUE_ACCESS_CLASS_HEADER
45 template <
bool ThrowError>
46 using throw_error = std::integral_constant<bool, ThrowError>;
82 template <
bool ThrowError>
87 if (!checkLeafLayout<ThrowError>(codec_index, rep)) {
98 throw_error<false>)
noexcept
100 const auto& layout = codec_index.getLayout();
101 leaf_layout.element_type =
static_cast<LeafElementType>(layout.type_info->getType());
104 leaf_layout.byte_pos =
static_cast<uint32_t
>(layout.deserialized.bit_offset / 8);
105 if (layout.deserialized.type_bit_size > (std::numeric_limits<uint8_t>::max)()) {
109 leaf_layout.bit_size =
static_cast<uint8_t
>(layout.deserialized.type_bit_size);
112 leaf_layout.data_flags |=
113 (layout.byte_order == dd::ByteOrder::e_be) ?
116 leaf_layout.bit_pos = layout.serialized.bit_offset % 8;
117 leaf_layout.byte_pos =
static_cast<uint32_t
>(layout.serialized.bit_offset / 8);
118 if (layout.serialized.type_bit_size_used > (std::numeric_limits<uint8_t>::max)()) {
121 leaf_layout.bit_size =
static_cast<uint8_t
>(layout.serialized.type_bit_size_used);
122 if (layout.serialized.type_bit_size_used == layout.deserialized.type_bit_size &&
123 leaf_layout.bit_pos == 0 &&
136 LeafLayout& leaf_layout,
141 throw std::runtime_error(
142 "element size exceeds maximal bit size for small leaf layout of" +
143 std::to_string((std::numeric_limits<uint8_t>::max)()));
149 template <
typename T>
150 static bool checkTypeSize(
const CodecIndex& codec_index,
152 bool exact_check_on_serialized,
153 throw_error<false>)
noexcept
156 (codec_index.getLayout().serialized.type_bit_size / 8) :
157 (codec_index.getLayout().
deserialized.type_bit_size / 8);
159 if (
sizeof(T) < size_to_check) {
163 return (
sizeof(T) == size_to_check);
167 template <
typename T>
168 static bool checkTypeSize(
const CodecIndex& codec_index,
173 if (!checkTypeSize<T>(codec_index, rep, exact_check, throw_error<false>{})) {
174 throw std::runtime_error(
175 "Standard datatype is only supported if bit size is not customized. Found "
176 "element type with different size to standard!");
182 template <
bool ThrowError>
185 switch (codec_index.getType()) {
189 throw std::runtime_error(
"Small leaf layouts can only be used for leaf elements!");
195 return checkTypeSize<bool>(codec_index, rep,
false, throw_error<ThrowError>{});
200 return checkTypeSize<int8_t>(codec_index, rep,
false, throw_error<ThrowError>{});
205 return checkTypeSize<int16_t>(codec_index, rep,
false, throw_error<ThrowError>{});
210 return checkTypeSize<int32_t>(codec_index, rep,
false, throw_error<ThrowError>{});
214 return checkTypeSize<int32_t>(codec_index, rep,
true, throw_error<ThrowError>{});
219 return checkTypeSize<int64_t>(codec_index, rep,
false, throw_error<ThrowError>{});
223 return checkTypeSize<int64_t>(codec_index, rep,
true, throw_error<ThrowError>{});
228 throw std::runtime_error(
229 "user supported types are not supported in small leaf layout");
239 LeafLayout _leaf_layout;
242#if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
243#pragma GCC diagnostic ignored \
249template <
typename ValueType>
251 :
public std::is_arithmetic<a_util::underlying_type_or_type_t<ValueType>> {};
253template <
typename ElementValueTypeFrom,
typename TargetValueTypeTo>
255 :
public std::integral_constant<
257 (!std::is_enum<TargetValueTypeTo>::value &&
258 a_util::is_explicitly_convertible_to_v<ElementValueTypeFrom, TargetValueTypeTo>) ||
259 (std::is_enum<TargetValueTypeTo>::value &&
260 !std::is_floating_point<ElementValueTypeFrom>::value)> {};
262template <
typename ValueType>
264 :
public std::integral_constant<
266 a_util::is_explicitly_convertible_to_v<bool, ValueType> ||
267 a_util::is_explicitly_convertible_to_v<int8_t, ValueType> ||
268 a_util::is_explicitly_convertible_to_v<uint8_t, ValueType> ||
269 a_util::is_explicitly_convertible_to_v<int16_t, ValueType> ||
270 a_util::is_explicitly_convertible_to_v<uint16_t, ValueType> ||
271 a_util::is_explicitly_convertible_to_v<int32_t, ValueType> ||
272 a_util::is_explicitly_convertible_to_v<uint32_t, ValueType> ||
273 a_util::is_explicitly_convertible_to_v<int64_t, ValueType> ||
274 a_util::is_explicitly_convertible_to_v<uint64_t, ValueType> ||
275 a_util::is_explicitly_convertible_to_v<float, ValueType> ||
276 a_util::is_explicitly_convertible_to_v<double, ValueType>> {};
285template <
typename ElementValueType,
286 typename TargetValueType,
287 bool conversion_allowed =
296 static TargetValueType
convert(
const ElementValueType& element_value)
298 return static_cast<TargetValueType
>(element_value);
308template <
typename ElementValueType,
typename TargetValueType>
316 static TargetValueType
convert(
const ElementValueType&)
319 "The used type must at least convertible from one fundamental type (bool, "
320 "any int, float, double) explicitly!");
321 throw std::runtime_error(
"unsupported type conversion requested");
325template <
typename ElementValueTypeTo,
typename SourceValueTypeFrom>
327 :
public std::integral_constant<
329 (!std::is_enum<SourceValueTypeFrom>::value &&
330 a_util::is_explicitly_convertible_to_v<SourceValueTypeFrom, ElementValueTypeTo>) ||
331 (std::is_enum<SourceValueTypeFrom>::value &&
332 !std::is_floating_point<ElementValueTypeTo>::value)> {};
334template <
typename ValueType>
336 :
public std::integral_constant<
338 a_util::is_explicitly_convertible_to_v<ValueType, bool> ||
339 a_util::is_explicitly_convertible_to_v<ValueType, int8_t> ||
340 a_util::is_explicitly_convertible_to_v<ValueType, uint8_t> ||
341 a_util::is_explicitly_convertible_to_v<ValueType, int16_t> ||
342 a_util::is_explicitly_convertible_to_v<ValueType, uint16_t> ||
343 a_util::is_explicitly_convertible_to_v<ValueType, int32_t> ||
344 a_util::is_explicitly_convertible_to_v<ValueType, uint32_t> ||
345 a_util::is_explicitly_convertible_to_v<ValueType, int64_t> ||
346 a_util::is_explicitly_convertible_to_v<ValueType, uint64_t> ||
347 a_util::is_explicitly_convertible_to_v<ValueType, float> ||
348 a_util::is_explicitly_convertible_to_v<ValueType, double>> {};
357template <
typename ElementValueType,
358 typename SourceValueType,
359 bool conversion_allowed =
370 static bool convert(ElementValueType& element_value,
const SourceValueType& value)
372 element_value =
static_cast<ElementValueType
>(value);
383template <
typename ElementValueType,
typename SourceValueType>
392 static bool convert(ElementValueType&,
const SourceValueType&)
395 "The used type must at least convertible to one fundamental type (bool, any "
396 "int, float, double) explicitly");
413template <
typename ElementValueType,
typename TargetValueType>
414TargetValueType
readBytes(
const void* data,
const size_t& data_size, uint32_t byte_pos)
416 ElementValueType value;
417 if (byte_pos +
sizeof(ElementValueType) > data_size) {
418 throw std::runtime_error(
"copy action exceeds buffersize");
420 std::memcpy(&value,
static_cast<const uint8_t*
>(data) + byte_pos,
sizeof(ElementValueType));
436template <
typename ElementValueType,
typename SourceValueType>
438 const size_t& data_size,
440 const SourceValueType& source_value)
442 if (byte_pos +
sizeof(ElementValueType) > data_size) {
443 throw std::runtime_error(
"copy action exceeds buffersize");
446 ElementValueType value;
448 std::memcpy(
static_cast<uint8_t*
>(data) + byte_pos, &value,
sizeof(ElementValueType));
451 throw std::runtime_error(
"unsupported type conversion requested");
469template <
typename ElementValueType,
typename TargetValueType>
476 ElementValueType value{};
478 auto res = bit_reader.
read<ElementValueType>(bit_offset, bit_size, &value, byte_order);
498template <
typename ElementValueType,
typename SourceValueType>
504 const SourceValueType& source_value)
506 ElementValueType value{};
510 bit_reader.
write<ElementValueType>(bit_offset, bit_size, value, byte_order);
516 throw std::runtime_error(
"unsupported type conversion requested");
531template <
typename ValueType>
573 throw std::runtime_error(
"invalid type");
586template <
typename ValueType>
590 const size_t bit_size = layout.
bit_size;
594 a_util::memory::bit_big_endian :
595 a_util::memory::bit_little_endian;
635 throw std::runtime_error(
"invalid type");
648template <
typename ValueType>
650 const size_t& data_size,
652 const ValueType& value)
693 throw std::runtime_error(
"invalid type");
707template <
typename ValueType>
709 const size_t& data_size,
711 const ValueType& value)
714 const size_t bit_size = layout.
bit_size;
718 a_util::memory::bit_big_endian :
719 a_util::memory::bit_little_endian;
732 data, data_size, bit_pos, bit_size, byte_order, value);
739 data, data_size, bit_pos, bit_size, byte_order, value);
746 data, data_size, bit_pos, bit_size, byte_order, value);
762 throw std::runtime_error(
"invalid type");
767#if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
768#pragma GCC diagnostic warning \
779template <
typename ValueType>
790 const size_t data_size,
810template <
typename ValueType>
821 const size_t data_size,
823 const ValueType& value)
837#ifdef DDL_NO_SUPPORT_FOR_CONSTEXPR_CONDITION
838#undef DDL_NO_SUPPORT_FOR_CONSTEXPR_CONDITION
Bit Serializer Class.
Definition bitserializer.h:607
a_util::result::Result read(size_t start_bit, size_t bit_length, T *value, Endianess endianess=get_platform_endianess())
Definition bitserializer.h:644
a_util::result::Result write(size_t start_bit, size_t bit_length, T value, Endianess endianess=get_platform_endianess())
Definition bitserializer.h:675
Definition codec_index.h:276
LeafCodecIndex(const CodecIndex &codec_index, ddl::DataRepresentation rep)
CTOR.
Definition leaf_value_access.h:58
LeafCodecIndex()=delete
no CTOR
const LeafLayout & getLayout() const noexcept
Get the leaf layout.
Definition leaf_value_access.h:66
static bool convertToLeafLayout(const CodecIndex &codec_index, LeafLayout &leaf_layout, ddl::DataRepresentation rep)
Retrieves a valid, small, 8-bytes LeafLayout from a CodecIndex.
Definition leaf_value_access.h:83
static ByteOrder getPlatformDefault()
Get the current Platform Byteorder.
void writeBits(void *data, size_t data_size, size_t bit_offset, size_t bit_size, a_util::memory::Endianess byte_order, const SourceValueType &source_value)
Write the bits of the element to the data area after conversion from source_value.
Definition leaf_value_access.h:499
TargetValueType readBits(const void *data, size_t data_size, size_t bit_offset, size_t bit_size, a_util::memory::Endianess byte_order)
Read the bits from a data area and converts this value to the TargetValueType.
Definition leaf_value_access.h:470
ValueType readDeserializedBytes(const void *data, const size_t &data_size, const LeafLayout &layout)
Retrieves a value from the given data area with the layout information of the layout.
Definition leaf_value_access.h:532
void writeBytes(void *data, const size_t &data_size, uint32_t byte_pos, const SourceValueType &source_value)
Writes the value of source_value to the data area after conversion to the elements ElementValueType.
Definition leaf_value_access.h:437
TargetValueType readBytes(const void *data, const size_t &data_size, uint32_t byte_pos)
Read the bytes from a data area and converts this value to the TargetValueType.
Definition leaf_value_access.h:414
void writeSerializedBits(void *data, const size_t &data_size, const LeafLayout &layout, const ValueType &value)
Writes a value to the given data area with the layout information of the layout.
Definition leaf_value_access.h:708
ValueType readSerializedBits(const void *data, const size_t &data_size, const LeafLayout &layout)
Retrieves a value from the given data area with the layout information of the layout.
Definition leaf_value_access.h:587
void writeDeserializedBytes(void *data, const size_t &data_size, const LeafLayout &layout, const ValueType &value)
Writes a value to the given data area with the layout information of the layout.
Definition leaf_value_access.h:649
Endianess
Enum describing the endianess.
Definition bitserializer.h:31
std::string toString(const Result &result, ResultFormatFlags formatting_flags=DisableNone, const char *format=nullptr)
Namespace for the new faster CodecFactory/Decoder/Codec implementation.
Definition codec.h:22
LeafElementType
Definition leaf_layout.h:51
@ let_uint64
LeafElementType type is std::uint64_t.
Definition leaf_layout.h:66
@ let_uint8
LeafElementType type is std::uint8_t.
Definition leaf_layout.h:54
@ let_double
LeafElementType type is double.
Definition leaf_layout.h:69
@ let_int8
LeafElementType type is std::int8_t.
Definition leaf_layout.h:53
@ let_int64
LeafElementType type is std::int64_t.
Definition leaf_layout.h:64
@ let_float
LeafElementType type is float.
Definition leaf_layout.h:68
@ let_int32
LeafElementType type is std::int32_t.
Definition leaf_layout.h:60
@ let_uint32
LeafElementType type is std::uint32_t.
Definition leaf_layout.h:62
@ let_bool
LeafElementType type is bool.
Definition leaf_layout.h:52
@ let_uint16
LeafElementType type is std::uint16_t.
Definition leaf_layout.h:58
@ let_int16
LeafElementType type is std::int16_t.
Definition leaf_layout.h:56
@ serialized_le
first bit is set second bit is not set for serialized data representation in little endianess of the ...
Definition leaf_layout.h:45
@ deserialized
first bit is zero for deserialized data representation of the LeafCodecIndex::data_flags
Definition leaf_layout.h:30
@ serialized_be
first and second bit is set for serialized data representation in big endianess of the LeafCodecIndex...
Definition leaf_layout.h:40
@ serialized
first bit is set for serialized data representation of the LeafCodecIndex::data_flags
Definition leaf_layout.h:35
@ cet_float
Variant type is float.
Definition codec_type_info.h:41
@ cet_int16
Variant type is std::int16_t.
Definition codec_type_info.h:35
@ cet_int8
Variant type is std::int8_t.
Definition codec_type_info.h:33
@ cet_uint64
Variant type is std::uint64_t.
Definition codec_type_info.h:40
@ cet_uint8
Variant type is std::uint8_t.
Definition codec_type_info.h:34
@ cet_uint32
Variant type is std::uint32_t.
Definition codec_type_info.h:38
@ cet_sub_codec
type marks a subcodec/substructure with children
Definition codec_type_info.h:43
@ cet_bool
Variant type is bool.
Definition codec_type_info.h:32
@ cet_uint16
Variant type is std::uint16_t.
Definition codec_type_info.h:36
@ cet_int32
Variant type is std::int32_t.
Definition codec_type_info.h:37
@ cet_int64
Variant type is std::int64_t.
Definition codec_type_info.h:39
@ cet_empty
Variant type is empty.
Definition codec_type_info.h:31
@ cet_user_type
type marks a user defined type
Definition codec_type_info.h:44
@ cet_double
Variant type is double.
Definition codec_type_info.h:42
definition of the ddl namespace
Definition codec.h:21
DataRepresentation
Definition data_representation.h:25
@ serialized
serialized data, i.e network, on disks, can msgs, ...
Definition data_representation.h:26
@ deserialized
deserialized data, c-structs, arrays, ...
Definition data_representation.h:27
small leaf layout information to access codec data very fast.
Definition leaf_layout.h:76
uint8_t bit_size
type bit size of an element for serialized or deserialized
Definition leaf_layout.h:92
uint32_t byte_pos
byte position of the element for serialized or deserialized
Definition leaf_layout.h:85
LeafElementType element_type
type of the value. accept all valid leaf types, except user defined types
Definition leaf_layout.h:90
uint8_t bit_pos
bit position of the element for serialized only
Definition leaf_layout.h:88
uint8_t data_flags
flags of the described layout
Definition leaf_layout.h:94
Definition leaf_value_access.h:780
static ValueType getValue(const void *data, const size_t data_size, const LeafLayout &leaf_layout)
Definition leaf_value_access.h:789
Definition leaf_value_access.h:811
static void setValue(void *data, const size_t data_size, const LeafLayout &leaf_layout, const ValueType &value)
Definition leaf_value_access.h:820
static bool convert(ElementValueType &, const SourceValueType &)
Should convert a value of SourceValueType to a element_value of type ElementValueType,...
Definition leaf_value_access.h:392
Converter to convert from a value of SourceValueType to a value of ElementValueType.
Definition leaf_value_access.h:361
static bool convert(ElementValueType &element_value, const SourceValueType &value)
Converts a value of SourceValueType to a element_value of type ElementValueType.
Definition leaf_value_access.h:370
Definition leaf_value_access.h:276
Definition leaf_value_access.h:348
Definition leaf_value_access.h:332
Definition leaf_value_access.h:260
Definition leaf_value_access.h:251
static TargetValueType convert(const ElementValueType &)
Should convert a value of ElementValueType to a value of type ElementValueType, but this specialized ...
Definition leaf_value_access.h:316
Converter to convert a value of ElementValueType to a value of TargetValueType.
Definition leaf_value_access.h:289
static TargetValueType convert(const ElementValueType &element_value)
Converts a element_value to as value of type TargetValueType.
Definition leaf_value_access.h:296