1 #ifndef ZSERIO_BIT_STREAM_READER_H_INC
2 #define ZSERIO_BIT_STREAM_READER_H_INC
42 operator size_t()
const
110 const uint8_t* buffer,
size_t bufferByteSize,
ArrayPreallocation maxArrayPrealloc = {});
128 Span<const uint8_t> buffer,
size_t bufferBitSize, ArrayPreallocation maxArrayPrealloc = {});
138 const uint8_t* buffer,
size_t bufferBitSize, BitsTag, ArrayPreallocation maxArrayPrealloc = {});
146 template <
typename ALLOC>
290 template <
typename ALLOC = std::allocator<u
int8_t>>
293 const size_t len =
static_cast<size_t>(
readVarSize());
299 if ((beginBitPosition & 0x07U) != 0)
304 for (
size_t i = 0; i < len; ++i)
306 value.push_back(readByte());
326 template <
typename ALLOC = std::allocator<
char>>
329 const size_t len =
static_cast<size_t>(
readVarSize());
335 if ((beginBitPosition & 0x07U) != 0)
340 for (
size_t i = 0; i < len; ++i)
342 const char readCharacter = std::char_traits<char>::to_char_type(
343 static_cast<std::char_traits<char>::int_type
>(readByte()));
344 value.push_back(readCharacter);
364 template <
typename ALLOC = std::allocator<u
int8_t>>
367 const size_t bitSize =
static_cast<size_t>(
readVarSize());
373 const size_t numBytesToRead = bitSize / 8;
374 const uint8_t numRestBits =
static_cast<uint8_t
>(bitSize - numBytesToRead * 8);
378 if ((beginBitPosition & 0x07U) != 0)
391 (void)std::copy(sourceIt, sourceIt + numBytesToRead, buffer.
begin());
424 void alignTo(
size_t alignment);
449 ReaderContext m_context;
455 inline void read(BitStreamReader& reader,
Bool& value)
457 value = reader.readBool();
460 template <BitSize BIT_SIZE,
bool IS_SIGNED>
461 void read(BitStreamReader& reader, FixedIntWrapper<BIT_SIZE, IS_SIGNED>& value)
463 using ValueType =
typename FixedIntWrapper<BIT_SIZE, IS_SIGNED>::ValueType;
465 if constexpr (
sizeof(ValueType) <= 4)
467 if constexpr (std::is_signed_v<ValueType>)
469 value =
static_cast<ValueType
>(reader.readSignedBits32(BIT_SIZE));
473 value =
static_cast<ValueType
>(reader.readUnsignedBits32(BIT_SIZE));
478 if constexpr (std::is_signed_v<ValueType>)
480 value =
static_cast<ValueType
>(reader.readSignedBits64(BIT_SIZE));
484 value =
static_cast<ValueType
>(reader.readUnsignedBits64(BIT_SIZE));
489 template <
typename T>
490 void read(BitStreamReader& reader, DynIntWrapper<T>& value, uint8_t
numBits)
492 if constexpr (
sizeof(T) <= 4)
494 if constexpr (std::is_signed_v<T>)
496 value =
static_cast<T
>(reader.readSignedBits32(
numBits));
500 value =
static_cast<T
>(reader.readUnsignedBits32(
numBits));
505 if constexpr (std::is_signed_v<T>)
507 value =
static_cast<T
>(reader.readSignedBits64(
numBits));
511 value =
static_cast<T
>(reader.readUnsignedBits64(
numBits));
516 inline void read(BitStreamReader& reader,
VarInt16& value)
518 value = reader.readVarInt16();
521 inline void read(BitStreamReader& reader,
VarInt32& value)
523 value = reader.readVarInt32();
526 inline void read(BitStreamReader& reader,
VarInt64& value)
528 value = reader.readVarInt64();
531 inline void read(BitStreamReader& reader,
VarInt& value)
533 value = reader.readVarInt();
536 inline void read(BitStreamReader& reader,
VarUInt16& value)
538 value = reader.readVarUInt16();
541 inline void read(BitStreamReader& reader,
VarUInt32& value)
543 value = reader.readVarUInt32();
546 inline void read(BitStreamReader& reader,
VarUInt64& value)
548 value = reader.readVarUInt64();
551 inline void read(BitStreamReader& reader,
VarUInt& value)
553 value = reader.readVarUInt();
556 inline void read(BitStreamReader& reader,
VarSize& value)
558 value = reader.readVarSize();
561 inline void read(BitStreamReader& reader,
Float16& value)
563 value = reader.readFloat16();
566 inline void read(BitStreamReader& reader,
Float32& value)
568 value = reader.readFloat32();
571 inline void read(BitStreamReader& reader,
Float64& value)
573 value = reader.readFloat64();
576 template <
typename ALLOC>
577 inline void read(BitStreamReader& reader, BasicBytes<ALLOC>& value)
579 value = reader.readBytes(value.get_allocator());
582 template <
typename ALLOC>
585 value = reader.readString(value.get_allocator());
588 template <
typename ALLOC>
589 inline void read(BitStreamReader& reader, BasicBitBuffer<ALLOC>& value)
591 value = reader.readBitBuffer(value.get_allocator());
static constexpr size_t MAX_INITIAL_ARRAY_ALLOCATION
ArrayPreallocation(size_t alloc)
Span< const uint8_t > getData() const
size_t getBitSize() const
void setBitPosition(BitPosType position)
void alignTo(size_t alignment)
uint32_t readUnsignedBits32(uint8_t numBits=32)
size_t getMaxArrayPreallocation() const
BasicBitBuffer< RebindAlloc< ALLOC, uint8_t > > readBitBuffer(const ALLOC &allocator=ALLOC())
VarUInt16 readVarUInt16()
uint64_t readUnsignedBits64(uint8_t numBits=64)
VarUInt32 readVarUInt32()
BitStreamReader(const BasicBitBuffer< ALLOC > &bitBuffer, ArrayPreallocation maxArrayPrealloc={})
VarUInt64 readVarUInt64()
size_t getBufferBitSize() const
int64_t readSignedBits64(uint8_t numBits=64)
int32_t readSignedBits32(uint8_t numBits=32)
BitStreamReader(const uint8_t *buffer, size_t bufferByteSize, ArrayPreallocation maxArrayPrealloc={})
~BitStreamReader()=default
BasicString< ALLOC > readString(const ALLOC &alloc=ALLOC())
BasicBytes< ALLOC > readBytes(const ALLOC &alloc=ALLOC())
BitPosType getBitPosition() const
constexpr iterator begin() const noexcept
uint8_t numBits(uint64_t numValues)
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
std::vector< T, ALLOC > Vector
std::vector< uint8_t, ALLOC > BasicBytes
detail::FloatWrapper< float, detail::FloatType::FLOAT32 > Float32
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
detail::FloatWrapper< double, detail::FloatType::FLOAT64 > Float64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize
ReaderContext(Span< const uint8_t > readBuffer, size_t readBufferBitSize, size_t maxArrayPrealloc)
Span< const uint8_t > buffer
ReaderContext & operator=(const ReaderContext &&)=delete
const BitPosType bufferBitSize
ReaderContext(const ReaderContext &&)=delete
ReaderContext & operator=(const ReaderContext &)=delete
ReaderContext(const ReaderContext &)=delete
const size_t maxArrayPreallocation