Skip to content
Snippets Groups Projects
Commit 51c39cbe authored by Vojtěch Moravec's avatar Vojtěch Moravec
Browse files

Memory Bit Stream.

parent 1fb8bc46
No related branches found
No related tags found
No related merge requests found
#include "memory_bit_stream.h"
/******************************************************************************************************************************
* OutMemoryBitStream implementation
****************************************************************************************************************************/
OutMemoryBitStream::OutMemoryBitStream()
{
bitBuffer = 0;
bitBufferSize = 0;
}
OutMemoryBitStream::~OutMemoryBitStream()
{
flush_bit_buffer();
bitBuffer = 0;
bitBufferSize = 0;
}
void OutMemoryBitStream::operator<<(const bool &bit)
{
write_bit(bit);
}
void OutMemoryBitStream::write_bit(const bool &bit)
{
++bitBufferSize;
if (bit)
{
bitBuffer |= (1 << (8 - bitBufferSize));
}
if (bitBufferSize == 8)
{
flush_bit_buffer();
}
}
void OutMemoryBitStream::flush_bit_buffer()
{
if (bitBufferSize > 0)
{
buffer.push_back(bitBuffer);
bitBuffer = 0;
bitBufferSize = 0;
}
}
ByteArray OutMemoryBitStream::get_buffer() const
{
return buffer;
}
ByteArray OutMemoryBitStream::get_flushed_buffer()
{
flush_bit_buffer();
return buffer;
}
/******************************************************************************************************************************
* InMemoryBitStream implementation
****************************************************************************************************************************/
InMemoryBitStream::InMemoryBitStream(const ByteArray *buffer, size_t bufferPosition)
{
memoryBuffer = buffer;
memoryBufferPosition = bufferPosition;
bitBufferSize = 0;
bitBuffer = 0;
}
InMemoryBitStream::~InMemoryBitStream()
{
memoryBuffer = nullptr;
memoryBufferPosition = 0;
bitBufferSize = 0;
bitBuffer = 0;
}
void InMemoryBitStream::read_byte_to_bit_buffer()
{
if (memoryBufferPosition < memoryBuffer->size())
{
bitBuffer = (*memoryBuffer)[memoryBufferPosition++];
bitBufferSize = 8;
}
else
{
assert(false && "Out ouf memory buffer");
}
}
bool InMemoryBitStream::read_bit()
{
if (bitBufferSize == 0)
{
read_byte_to_bit_buffer();
}
--bitBufferSize;
bool result = bitBuffer & (1 << bitBufferSize);
return result;
}
void InMemoryBitStream::operator>>(bool &bit)
{
bit = read_bit();
}
bool InMemoryBitStream::can_read() const
{
return ((bitBufferSize > 0) || (memoryBufferPosition < memoryBuffer->size()));
}
\ No newline at end of file
#pragma once
#include "../custom_types.h"
// Class allowing to write invidual bits to memory buffer.
class OutMemoryBitStream
{
private:
// Memory buffer.
ByteArray buffer;
// Bit buffer.
byte bitBuffer;
// Actual size of bit buffer.
byte bitBufferSize = 0;
public:
OutMemoryBitStream();
~OutMemoryBitStream();
// Write bit to memory stream.
void write_bit(const bool &bit);
// Write bit to memory stream.
void operator<<(const bool &bit);
// If flush buffer is not flushed, flush it to main buffer and clear it.
void flush_bit_buffer();
// Get buffer of written bits, which may not be flushed.
ByteArray get_buffer() const;
// Get buffer of written bits, which is flushed.
ByteArray get_flushed_buffer();
};
// Class alowing to read invidual bits from memory buffer.
class InMemoryBitStream
{
private:
// Memory buffer.
const ByteArray *memoryBuffer = nullptr;
// Position in memory buffer.
size_t memoryBufferPosition = 0;
// Bit buffer.
byte bitBuffer = 0;
// Current bit buffer size.
byte bitBufferSize = 0;
// Read byte into bit buffer.
void read_byte_to_bit_buffer();
public:
InMemoryBitStream(const ByteArray *buffer, size_t bufferPosition = 0);
~InMemoryBitStream();
// Read one bit from memory buffer.
bool read_bit();
// Read one bit from memory buffer.
void operator>>(bool &bit);
// Check wheter atleast one bit can be read.
bool can_read() const;
};
#include "memory_bit_stream.cpp"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment