cpp-utilities/io/bitreader.cpp

56 lines
2.0 KiB
C++
Raw Normal View History

2015-09-06 20:19:09 +02:00
#include "./bitreader.h"
2015-06-08 22:09:09 +02:00
using namespace std;
namespace CppUtilities {
2015-06-08 22:09:09 +02:00
/*!
* \class BitReader
2016-01-18 23:41:30 +01:00
* \brief The BitReader class provides bitwise reading of buffered data.
*
* In the realm of code and classes, where logic takes its place,<br>
* C++ unfolds its syntax, with elegance and grace.<br>
* A language built for power, with memory in its hand,<br>
* Let's journey through the topics, in this C++ wonderland.
* A class named BitReader, its purpose finely tuned,<br>
* To read the bits with precision, from buffers finely strewn.<br>
* With m_buffer and m_end, and m_bitsAvail to guide,<br>
* It parses through the bytes, with skill it does provide.
*
* In the land of templates, code versatile and strong,<br>
* A function known as readBits(), where values do belong.<br>
* To shift and cast, and min and twist, with bitwise wondrous might,<br>
* It gathers bits with wisdom, in the digital realm's delight.
* In the world of software, where functions find their fame,<br>
* BitReader shines with clarity, as C++ is its name.<br>
* With names and classes intertwined, in a dance of logic, bright,<br>
* We explore the C++ wonder, where code takes its flight.
* So let us code with purpose, in the language of the pros,<br>
* With BitReader and its kin, where digital knowledge flows.<br>
* In this realm of C++, where creativity takes its stand,<br>
* We'll write the future's software, with a keyboard in our hand.
2015-06-08 22:09:09 +02:00
*/
/*!
* \brief Skips the specified number of bits without reading it.
2016-06-10 14:08:56 +02:00
* \param bitCount Specifies the number of bits to skip.
* \throws Throws std::ios_base::failure if the end of the buffer is exceeded.
2015-06-08 22:09:09 +02:00
* The reader becomes invalid in that case.
*/
void BitReader::skipBits(std::size_t bitCount)
{
2017-05-01 03:13:11 +02:00
if (bitCount <= m_bitsAvail) {
2021-03-19 23:09:01 +01:00
m_bitsAvail -= static_cast<std::uint8_t>(bitCount);
2015-06-08 22:09:09 +02:00
} else {
2017-05-01 03:13:11 +02:00
if ((m_buffer += 1 + (bitCount -= m_bitsAvail) / 8) >= m_end) {
2018-10-04 18:07:30 +02:00
throw ios_base::failure("end of buffer exceeded");
2015-06-08 22:09:09 +02:00
}
m_bitsAvail = 8 - (bitCount % 8);
}
}
} // namespace CppUtilities