16 #ifndef DDL_PREVIEW_LEAF_VALUE_ACCESS_PRIVATE_CLASS_HEADER
17 #define DDL_PREVIEW_LEAF_VALUE_ACCESS_PRIVATE_CLASS_HEADER
87 static constexpr uint32_t
_invalid_pos =
static_cast<uint32_t
>(-1);
119 template <
bool ThrowError>
120 using throw_error = std::integral_constant<bool, ThrowError>;
157 template <
bool ThrowError>
162 if (!checkLeafLayout<ThrowError>(codec_index, rep)) {
173 throw_error<false>) noexcept
175 const auto& layout = codec_index.getLayout();
176 leaf_layout.element_type =
static_cast<LeafElementType>(layout.type_info->getType());
179 leaf_layout.byte_pos =
static_cast<uint32_t
>(layout.deserialized.bit_offset / 8);
180 if (layout.deserialized.type_bit_size > (std::numeric_limits<uint8_t>::max)()) {
184 leaf_layout.bit_size =
static_cast<uint8_t
>(layout.deserialized.type_bit_size);
187 leaf_layout.data_flags |=
188 (layout.byte_order == dd::ByteOrder::e_be) ?
191 leaf_layout.bit_pos = layout.serialized.bit_offset % 8;
192 leaf_layout.byte_pos =
static_cast<uint32_t
>(layout.serialized.bit_offset / 8 +
193 ((leaf_layout.bit_pos == 0) ? 0 : 1));
194 if (layout.serialized.type_bit_size_used > (std::numeric_limits<uint8_t>::max)()) {
197 leaf_layout.bit_size =
static_cast<uint8_t
>(layout.serialized.type_bit_size_used);
198 if (layout.serialized.type_bit_size_used == layout.deserialized.type_bit_size &&
199 leaf_layout.bit_pos == 0 &&
217 throw std::runtime_error(
218 "element size exceeds maximal bit size for small leaf layout of" +
225 template <
typename T>
228 bool exact_check_on_serialized,
229 throw_error<false>) noexcept
232 (codec_index.getLayout().serialized.type_bit_size / 8) :
233 (codec_index.getLayout().deserialized.type_bit_size / 8);
235 if (
sizeof(T) < size_to_check) {
239 return (
sizeof(T) == size_to_check);
243 template <
typename T>
249 if (!checkTypeSize<T>(codec_index, rep, exact_check, throw_error<false>{})) {
250 throw std::runtime_error(
251 "Standard datatype is only supported if bit size is not customized. Found "
252 "element type with different size to standard!");
258 template <
bool ThrowError>
261 switch (codec_index.
getType()) {
265 throw std::runtime_error(
"Small leaf layouts can only be used for leaf elements!");
271 return checkTypeSize<bool>(codec_index, rep,
false, throw_error<ThrowError>{});
276 return checkTypeSize<int8_t>(codec_index, rep,
false, throw_error<ThrowError>{});
281 return checkTypeSize<int16_t>(codec_index, rep,
false, throw_error<ThrowError>{});
286 return checkTypeSize<int32_t>(codec_index, rep,
false, throw_error<ThrowError>{});
290 return checkTypeSize<int32_t>(codec_index, rep,
true, throw_error<ThrowError>{});
295 return checkTypeSize<int64_t>(codec_index, rep,
false, throw_error<ThrowError>{});
299 return checkTypeSize<int64_t>(codec_index, rep,
true, throw_error<ThrowError>{});
304 throw std::runtime_error(
305 "user supported types are not supported in small leaf layout");
318 #if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
319 #pragma GCC diagnostic ignored \
325 template <
typename ValueType>
327 :
public std::is_arithmetic<a_util::underlying_type_or_type_t<ValueType>> {
330 template <
typename ElementValueTypeFrom,
typename TargetValueTypeTo>
332 :
public std::integral_constant<
334 (!std::is_enum<TargetValueTypeTo>::value &&
335 a_util::is_explicitly_convertible_to_v<ElementValueTypeFrom, TargetValueTypeTo>) ||
336 (std::is_enum<TargetValueTypeTo>::value &&
337 !std::is_floating_point<ElementValueTypeFrom>::value)> {
340 template <
typename ValueType>
342 :
public std::integral_constant<
344 a_util::is_explicitly_convertible_to_v<bool, ValueType> ||
345 a_util::is_explicitly_convertible_to_v<int8_t, ValueType> ||
346 a_util::is_explicitly_convertible_to_v<uint8_t, ValueType> ||
347 a_util::is_explicitly_convertible_to_v<int16_t, ValueType> ||
348 a_util::is_explicitly_convertible_to_v<uint16_t, ValueType> ||
349 a_util::is_explicitly_convertible_to_v<int32_t, ValueType> ||
350 a_util::is_explicitly_convertible_to_v<uint32_t, ValueType> ||
351 a_util::is_explicitly_convertible_to_v<int64_t, ValueType> ||
352 a_util::is_explicitly_convertible_to_v<uint64_t, ValueType> ||
353 a_util::is_explicitly_convertible_to_v<float, ValueType> ||
354 a_util::is_explicitly_convertible_to_v<double, ValueType>> {
364 template <
typename ElementValueType,
365 typename TargetValueType,
366 bool conversion_allowed =
375 static TargetValueType
convert(
const ElementValueType& element_value)
377 return static_cast<TargetValueType
>(element_value);
387 template <
typename ElementValueType,
typename TargetValueType>
395 static TargetValueType
convert(
const ElementValueType&)
398 "The used type must at least convertible from one fundamental type (bool, "
399 "any int, float, double) explicitly!");
400 throw std::runtime_error(
"unsupported type conversion requested");
404 template <
typename ElementValueTypeTo,
typename SourceValueTypeFrom>
406 :
public std::integral_constant<
408 (!std::is_enum<SourceValueTypeFrom>::value &&
409 a_util::is_explicitly_convertible_to_v<SourceValueTypeFrom, ElementValueTypeTo>) ||
410 (std::is_enum<SourceValueTypeFrom>::value &&
411 !std::is_floating_point<ElementValueTypeTo>::value)> {
414 template <
typename ValueType>
416 :
public std::integral_constant<
418 a_util::is_explicitly_convertible_to_v<ValueType, bool> ||
419 a_util::is_explicitly_convertible_to_v<ValueType, int8_t> ||
420 a_util::is_explicitly_convertible_to_v<ValueType, uint8_t> ||
421 a_util::is_explicitly_convertible_to_v<ValueType, int16_t> ||
422 a_util::is_explicitly_convertible_to_v<ValueType, uint16_t> ||
423 a_util::is_explicitly_convertible_to_v<ValueType, int32_t> ||
424 a_util::is_explicitly_convertible_to_v<ValueType, uint32_t> ||
425 a_util::is_explicitly_convertible_to_v<ValueType, int64_t> ||
426 a_util::is_explicitly_convertible_to_v<ValueType, uint64_t> ||
427 a_util::is_explicitly_convertible_to_v<ValueType, float> ||
428 a_util::is_explicitly_convertible_to_v<ValueType, double>> {
438 template <
typename ElementValueType,
439 typename SourceValueType,
440 bool conversion_allowed =
451 static bool convert(ElementValueType& element_value,
const SourceValueType& value)
453 element_value =
static_cast<ElementValueType
>(value);
464 template <
typename ElementValueType,
typename SourceValueType>
473 static bool convert(ElementValueType&,
const SourceValueType&)
476 "The used type must at least convertible to one fundamental type (bool, any "
477 "int, float, double) explicitly");
494 template <
typename ElementValueType,
typename TargetValueType>
495 TargetValueType
readBytes(
const void* data,
const size_t& data_size, uint32_t byte_pos)
497 ElementValueType value;
498 if (byte_pos +
sizeof(ElementValueType) > data_size) {
499 throw std::runtime_error(
"copy action exceeds buffersize");
501 std::memcpy(&value,
static_cast<const uint8_t*
>(data) + byte_pos,
sizeof(ElementValueType));
517 template <
typename ElementValueType,
typename SourceValueType>
519 const size_t& data_size,
521 const SourceValueType& source_value)
523 if (byte_pos +
sizeof(ElementValueType) > data_size) {
524 throw std::runtime_error(
"copy action exceeds buffersize");
527 ElementValueType value;
529 std::memcpy(
static_cast<uint8_t*
>(data) + byte_pos, &value,
sizeof(ElementValueType));
532 throw std::runtime_error(
"unsupported type conversion requested");
550 template <
typename ElementValueType,
typename TargetValueType>
557 ElementValueType value{};
559 auto res = bit_reader.
read<ElementValueType>(bit_offset, bit_size, &value, byte_order);
579 template <
typename ElementValueType,
typename SourceValueType>
585 const SourceValueType& source_value)
587 ElementValueType value{};
591 bit_reader.
write<ElementValueType>(bit_offset, bit_size, value, byte_order);
597 throw std::runtime_error(
"unsupported type conversion requested");
612 template <
typename ValueType>
617 return readBytes<bool, ValueType>(data, data_size, layout.
byte_pos);
620 return readBytes<uint8_t, ValueType>(data, data_size, layout.
byte_pos);
623 return readBytes<int8_t, ValueType>(data, data_size, layout.
byte_pos);
626 return readBytes<uint16_t, ValueType>(data, data_size, layout.
byte_pos);
629 return readBytes<int16_t, ValueType>(data, data_size, layout.
byte_pos);
632 return readBytes<uint32_t, ValueType>(data, data_size, layout.
byte_pos);
635 return readBytes<int32_t, ValueType>(data, data_size, layout.
byte_pos);
638 return readBytes<uint64_t, ValueType>(data, data_size, layout.
byte_pos);
641 return readBytes<int64_t, ValueType>(data, data_size, layout.
byte_pos);
644 return readBytes<float, ValueType>(data, data_size, layout.
byte_pos);
647 return readBytes<double, ValueType>(data, data_size, layout.
byte_pos);
654 throw std::runtime_error(
"invalid type");
667 template <
typename ValueType>
671 const size_t bit_size = layout.
bit_size;
675 a_util::memory::bit_big_endian :
676 a_util::memory::bit_little_endian;
679 return readBits<bool, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
682 return readBits<uint8_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
685 return readBits<int8_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
688 return readBits<uint16_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
691 return readBits<int16_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
694 return readBits<uint32_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
697 return readBits<int32_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
700 return readBits<uint64_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
703 return readBits<int64_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
706 return readBits<float, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
709 return readBits<double, ValueType>(data, data_size, bit_pos, bit_size, byte_order);
716 throw std::runtime_error(
"invalid type");
729 template <
typename ValueType>
731 const size_t& data_size,
733 const ValueType& value)
737 return writeBytes<bool, ValueType>(data, data_size, layout.
byte_pos, value);
740 return writeBytes<uint8_t, ValueType>(data, data_size, layout.
byte_pos, value);
743 return writeBytes<int8_t, ValueType>(data, data_size, layout.
byte_pos, value);
746 return writeBytes<uint16_t, ValueType>(data, data_size, layout.
byte_pos, value);
749 return writeBytes<int16_t, ValueType>(data, data_size, layout.
byte_pos, value);
752 return writeBytes<uint32_t, ValueType>(data, data_size, layout.
byte_pos, value);
755 return writeBytes<int32_t, ValueType>(data, data_size, layout.
byte_pos, value);
758 return writeBytes<uint64_t, ValueType>(data, data_size, layout.
byte_pos, value);
761 return writeBytes<int64_t, ValueType>(data, data_size, layout.
byte_pos, value);
764 return writeBytes<float, ValueType>(data, data_size, layout.
byte_pos, value);
767 return writeBytes<double, ValueType>(data, data_size, layout.
byte_pos, value);
774 throw std::runtime_error(
"invalid type");
788 template <
typename ValueType>
790 const size_t& data_size,
792 const ValueType& value)
795 const size_t bit_size = layout.
bit_size;
799 a_util::memory::bit_big_endian :
800 a_util::memory::bit_little_endian;
803 return writeBits<bool, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
806 return writeBits<uint8_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
809 return writeBits<int8_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
812 return writeBits<uint16_t, ValueType>(
813 data, data_size, bit_pos, bit_size, byte_order, value);
816 return writeBits<int16_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
819 return writeBits<uint32_t, ValueType>(
820 data, data_size, bit_pos, bit_size, byte_order, value);
823 return writeBits<int32_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
826 return writeBits<uint64_t, ValueType>(
827 data, data_size, bit_pos, bit_size, byte_order, value);
830 return writeBits<int64_t, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
833 return writeBits<float, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
836 return writeBits<double, ValueType>(data, data_size, bit_pos, bit_size, byte_order, value);
843 throw std::runtime_error(
"invalid type");
848 #if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
849 #pragma GCC diagnostic warning \
860 template <
typename ValueType>
871 const size_t data_size,
876 return detail::readSerializedBits<ValueType>(data, data_size, leaf_layout);
879 return detail::readDeserializedBytes<ValueType>(data, data_size, leaf_layout);
891 template <
typename ValueType>
902 const size_t data_size,
904 const ValueType& value)
908 detail::writeSerializedBits<ValueType>(data, data_size, leaf_layout, value);
911 detail::writeDeserializedBytes<ValueType>(data, data_size, leaf_layout, value);
918 #ifdef DDL_NO_SUPPORT_FOR_CONSTEXPR_CONDITION
919 #undef DDL_NO_SUPPORT_FOR_CONSTEXPR_CONDITION
a_util::result::Result write(size_t start_bit, size_t bit_length, T value, Endianess endianess=get_platform_endianess())
Write value to bitfield.
a_util::result::Result read(size_t start_bit, size_t bit_length, T *value, Endianess endianess=get_platform_endianess())
Read value from bitfield.
Fast Access Index Type for the coders.
ElementType getType() const
Get the elements type if CodecIndex is valid.
The Leaf codec index is a small layout information index to access the decoders/codecs data very fast...
static bool convertToLeafLayout(const CodecIndex &codec_index, LeafLayout &leaf_layout, ddl::DataRepresentation rep, throw_error< false >) noexcept
For internal use only.
const LeafLayout & getLayout() const noexcept
Get the leaf layout.
LeafLayout _leaf_layout
For internal use only.
static bool checkLeafLayout(const CodecIndex &codec_index, ddl::DataRepresentation rep)
For internal use only.
static bool convertToLeafLayout(const CodecIndex &codec_index, LeafLayout &leaf_layout, ddl::DataRepresentation rep, throw_error< true >)
For internal use only.
static bool checkTypeSize(const CodecIndex &codec_index, ddl::DataRepresentation rep, bool exact_check, throw_error< true >)
For internal use only.
static bool convertToLeafLayout(const CodecIndex &codec_index, LeafLayout &leaf_layout, ddl::DataRepresentation rep)
Retrieves a valid, small, 8-bytes LeafLayout from a CodecIndex.
LeafCodecIndex()=delete
no CTOR
LeafCodecIndex(const CodecIndex &codec_index, ddl::DataRepresentation rep=DataRepresentation::deserialized)
CTOR.
static bool checkTypeSize(const CodecIndex &codec_index, ddl::DataRepresentation rep, bool exact_check_on_serialized, throw_error< false >) noexcept
For internal use only.
static ByteOrder getPlatformDefault()
Get the current Platform Byteorder.
Implementation of the CodecIndex.
Definition of Data Representation header for Codec API.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Endianess
Enum describing the endianess.
std::string toString(const Result &result, ResultFormatFlags formatting_flags=DisableNone, const char *format=nullptr)
Copy all information of an assigned result object to a (formatted) string.
@ cet_float
Variant type is float.
@ cet_int16
Variant type is std::int16_t.
@ cet_int8
Variant type is std::int8_t.
@ cet_uint64
Variant type is std::uint64_t.
@ cet_uint8
Variant type is std::uint8_t.
@ cet_uint32
Variant type is std::uint32_t.
@ cet_sub_codec
type marks a subcodec/substructure with children
@ cet_bool
Variant type is bool.
@ cet_uint16
Variant type is std::uint16_t.
@ cet_int32
Variant type is std::int32_t.
@ cet_int64
Variant type is std::int64_t.
@ cet_empty
Variant type is empty.
@ cet_user_type
type marks a user defined type
@ cet_double
Variant type is double.
LeafDataRepresentation
LeafLayout data representation flags.
@ serialized_le
first bit is set second bit is not set for serialized data representation in little endianess of the ...
@ deserialized
first bit is zero for deserialized data representation of the LeafCodecIndex::data_flags
@ serialized_be
first and second bit is set for serialized data representation in big endianess of the LeafCodecIndex...
@ serialized
first bit is set for serialized data representation of the LeafCodecIndex::data_flags
LeafElementType
Valid ElementTypes for the LeafLayout.
@ let_uint64
LeafElementType type is std::uint64_t.
@ let_uint8
LeafElementType type is std::uint8_t.
@ let_double
LeafElementType type is double.
@ let_int8
LeafElementType type is std::int8_t.
@ let_int64
LeafElementType type is std::int64_t.
@ let_float
LeafElementType type is float.
@ let_int32
LeafElementType type is std::int32_t.
@ let_uint32
LeafElementType type is std::uint32_t.
@ let_bool
LeafElementType type is bool.
@ let_uint16
LeafElementType type is std::uint16_t.
@ let_int16
LeafElementType type is std::int16_t.
definition of the ddl namespace
DataRepresentation
Enumeration for the data representation.
@ serialized
serialized data, i.e network, on disks, can msgs, ...
@ deserialized
deserialized data, c-structs, arrays, ...
Utility for the Neutrino gcc5 compiler which has really no std::to_string implementation!
small leaf layout information to access codec data very fast.
static constexpr size_t _max_byte_size
max type byte size of an element (usually this can not be reached for standard data types)
LeafElementType element_type
type of the value. accept all valid leaf types, except user defined types
uint8_t bit_size
type bit size of an element for serialized or deserialized
uint8_t bit_pos
bit position of the element for serialized only
uint8_t data_flags
flags of the described layout
static constexpr size_t _max_bit_size
max type bit size of an element (usually this can not be reached for standard data types)
uint32_t byte_pos
byte position of the element for serialized or deserialized
static constexpr uint32_t _invalid_pos
invalid position for byte_pos
Value getter to retrieve the current value of the given element as a ValueType if supported from a da...
static ValueType getValue(const void *data, const size_t data_size, const LeafLayout &leaf_layout)
Returns the current value of the given element as a ValueType if supported.
Value setter to set the current value of the given element from a type ValueType if supported.
static void setValue(void *data, const size_t data_size, const LeafLayout &leaf_layout, const ValueType &value)
Sets the current value if the given element as a ValueType if supported.
static bool convert(ElementValueType &, const SourceValueType &)
Should convert a value of SourceValueType to a element_value of type ElementValueType,...
Converter to convert from a value of SourceValueType to a value of ElementValueType.
static bool convert(ElementValueType &element_value, const SourceValueType &value)
Converts a value of SourceValueType to a element_value of type ElementValueType.
static TargetValueType convert(const ElementValueType &)
Should convert a value of ElementValueType to a value of type ElementValueType, but this specialized ...
Converter to convert a value of ElementValueType to a value of TargetValueType.
static TargetValueType convert(const ElementValueType &element_value)
Converts a element_value to as value of type TargetValueType.
Class and function templates extending std type traits functionality.
Raw memory related functionality.