1 #ifndef ZSERIO_BIT_STREAM_WRITER_H_INC
2 #define ZSERIO_BIT_STREAM_WRITER_H_INC
71 template <
typename ALLOC>
236 template <
typename ALLOC>
243 size_t numBytesToWrite = bitSize / 8;
244 const uint8_t numRestBits =
static_cast<uint8_t
>(bitSize - numBytesToWrite * 8);
247 if ((beginBitPosition & 0x07U) != 0)
252 writeUnsignedBits32Impl(*it, 8);
261 (void)std::copy(buffer.
begin(), buffer.
begin() + numBytesToWrite,
262 m_buffer.
data() + beginBitPosition / 8);
268 writeUnsignedBits32Impl(
static_cast<uint32_t
>(*itEnd) >> (8U - numRestBits), numRestBits);
294 void alignTo(
size_t alignment);
303 return m_buffer.
data() !=
nullptr;
327 return m_bufferBitSize;
331 void writeUnsignedBits32Impl(uint32_t data, uint8_t
numBits);
332 void writeUnsignedBits64Impl(uint64_t data, uint8_t
numBits);
333 void writeSignedVarNum(int64_t value,
size_t maxVarBytes,
size_t numVarBytes);
334 void writeUnsignedVarNum(uint64_t value,
size_t maxVarBytes,
size_t numVarBytes);
335 void writeVarNum(uint64_t value,
bool hasSign,
bool isNegative,
size_t maxVarBytes,
size_t numVarBytes);
337 void checkCapacity(
size_t bitSize)
const;
338 void throwInsufficientCapacityException()
const;
342 size_t m_bufferBitSize;
348 inline void write(BitStreamWriter& writer,
Bool value)
350 writer.writeBool(value);
353 template <BitSize BIT_SIZE,
bool IS_SIGNED>
354 void write(BitStreamWriter& writer, FixedIntWrapper<BIT_SIZE, IS_SIGNED> value)
356 using ValueType =
typename FixedIntWrapper<BIT_SIZE, IS_SIGNED>::ValueType;
358 static_assert(BIT_SIZE != 0,
"Variable dynamic bit fields not allowed here!");
359 if constexpr (
sizeof(ValueType) <= 4)
361 if constexpr (std::is_signed_v<ValueType>)
363 writer.writeSignedBits32(value, BIT_SIZE);
367 writer.writeUnsignedBits32(value, BIT_SIZE);
372 if constexpr (std::is_signed_v<ValueType>)
374 writer.writeSignedBits64(value, BIT_SIZE);
378 writer.writeUnsignedBits64(value, BIT_SIZE);
383 template <
typename T>
384 void write(BitStreamWriter& writer, DynIntWrapper<T> value, uint8_t
numBits)
386 if constexpr (
sizeof(T) <= 4)
388 if constexpr (std::is_signed_v<T>)
390 writer.writeSignedBits32(value,
numBits);
394 writer.writeUnsignedBits32(value,
numBits);
399 if constexpr (std::is_signed_v<T>)
401 writer.writeSignedBits64(value,
numBits);
405 writer.writeUnsignedBits64(value,
numBits);
410 inline void write(BitStreamWriter& writer,
VarInt16 value)
412 writer.writeVarInt16(value);
415 inline void write(BitStreamWriter& writer,
VarInt32 value)
417 writer.writeVarInt32(value);
420 inline void write(BitStreamWriter& writer,
VarInt64 value)
422 writer.writeVarInt64(value);
425 inline void write(BitStreamWriter& writer,
VarInt value)
427 writer.writeVarInt(value);
430 inline void write(BitStreamWriter& writer,
VarUInt16 value)
432 writer.writeVarUInt16(value);
435 inline void write(BitStreamWriter& writer,
VarUInt32 value)
437 writer.writeVarUInt32(value);
440 inline void write(BitStreamWriter& writer,
VarUInt64 value)
442 writer.writeVarUInt64(value);
445 inline void write(BitStreamWriter& writer,
VarUInt value)
447 writer.writeVarUInt(value);
450 inline void write(BitStreamWriter& writer,
VarSize value)
452 writer.writeVarSize(value);
455 inline void write(BitStreamWriter& writer,
Float16 value)
457 writer.writeFloat16(value);
460 inline void write(BitStreamWriter& writer,
Float32 value)
462 writer.writeFloat32(value);
465 inline void write(BitStreamWriter& writer,
Float64 value)
467 writer.writeFloat64(value);
470 inline void write(BitStreamWriter& writer,
BytesView value)
472 writer.writeBytes(value);
475 inline void write(BitStreamWriter& writer, std::string_view value)
477 writer.writeString(value);
480 template <
typename ALLOC>
481 inline void write(BitStreamWriter& writer,
const BasicBitBufferView<ALLOC>& value)
483 writer.writeBitBuffer(value.get());
Span< const uint8_t > getData() const
size_t getBitSize() const
void writeBool(Bool data)
void writeVarUInt16(VarUInt16 data)
Span< const uint8_t > getBuffer() const
void writeVarInt32(VarInt32 data)
void writeFloat64(Float64 data)
void writeVarUInt64(VarUInt64 data)
~BitStreamWriter()=default
size_t getBufferBitSize() const
BitPosType getBitPosition() const
void writeVarInt16(VarInt16 data)
const uint8_t * getWriteBuffer() const
void writeSignedBits64(int64_t data, uint8_t numBits=64)
BitStreamWriter(const BitStreamWriter &&)=delete
void writeVarInt64(VarInt64 data)
BitStreamWriter(const BitStreamWriter &)=delete
BitStreamWriter & operator=(const BitStreamWriter &)=delete
void writeUnsignedBits32(uint32_t data, uint8_t numBits=32)
void writeFloat32(Float32 data)
void writeSignedBits32(int32_t data, uint8_t numBits=32)
void writeVarUInt(VarUInt data)
void writeBytes(BytesView data)
void writeUnsignedBits64(uint64_t data, uint8_t numBits=64)
void writeBitBuffer(const BasicBitBuffer< ALLOC > &bitBuffer)
BitStreamWriter(BasicBitBuffer< ALLOC > &bitBuffer)
BitStreamWriter(uint8_t *buffer, size_t bufferBitSize, BitsTag)
BitStreamWriter & operator=(BitStreamWriter &&)=delete
void writeString(std::string_view data)
void writeFloat16(Float16 data)
void writeVarUInt32(VarUInt32 data)
void writeVarSize(VarSize data)
bool hasWriteBuffer() const
void writeVarInt(VarInt data)
void setBitPosition(BitPosType position)
void alignTo(size_t alignment)
CppRuntimeException(const char *message="")
constexpr pointer data() const noexcept
constexpr iterator begin() const noexcept
uint8_t numBits(uint64_t numValues)
Span< const uint8_t > BytesView
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
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
uint32_t convertSizeToUInt32(size_t value)
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