1
0
Fork 0

immitate the C++20 byteorder enum

This commit is contained in:
Kevin Matz 2023-05-18 14:37:50 -04:00
parent 17f9ee21d6
commit 14e4bdc5b5
3 changed files with 20 additions and 9 deletions

View File

@ -30,7 +30,7 @@
* @param l
* @param o
*/
bufferstream::bufferstream(uint8_t * p, std::streamsize l, byteorder o)
bufferstream::bufferstream(uint8_t * p, std::streamsize l, endian o)
: std::basic_streambuf<uint8_t>()
, std::basic_iostream<uint8_t>(this)
, order_(o)

View File

@ -31,6 +31,8 @@ struct streamable;
/**
* @brief Input/Output stream for unsigned 8-bit buffers
*
* By default, wide types will be handled as big-endian, the network byte order.
*/
class bufferstream
: public std::enable_shared_from_this<bufferstream>
@ -38,13 +40,22 @@ class bufferstream
, public std::basic_iostream<uint8_t>
{
public:
/// @brief The byteorder enum
enum byteorder {
LittleEndian, //!< Little Endian
BigEndian, //!< Big Endian, Network Order
/**
* @brief The endian enum is not available until C++20.
*
* \note This implimentation may be compiler dependant.
*
* \note After upgrading the project to C++20 or better, drop this if favor of
* the standard implimentation in <bit.h>
*/
enum class endian
{
little = __ORDER_LITTLE_ENDIAN__,
big = __ORDER_BIG_ENDIAN__,
native = __BYTE_ORDER__,
};
bufferstream(uint8_t *p, std::streamsize l, byteorder o = BigEndian);
bufferstream(uint8_t *p, std::streamsize l, endian o = endian::big); // default network byte-order
// input sequence
size_t available();
@ -155,7 +166,7 @@ public:
}
private:
byteorder order_;
endian order_;
};

View File

@ -126,7 +126,7 @@ void DmxWidget::parseMessageBuffer()
// fill it with data
std::shared_ptr<bufferstream> stream(new bufferstream(
reinterpret_cast<uint8_t*>(message_rx_buffer_.data()+fixed_length-1),
length, bufferstream::LittleEndian));
length, bufferstream::endian::little));
msg->iStream(stream);
// ship it
routeRxMessage(msg);
@ -144,7 +144,7 @@ void DmxWidget::sendMessage(std::shared_ptr<ENTTEC::Pro::MessageData> msg) const
char buffer[length + fixed_length];
std::shared_ptr<bufferstream> stream(new bufferstream(reinterpret_cast<uint8_t*>(buffer),
sizeof(buffer),
bufferstream::LittleEndian));
bufferstream::endian::little));
*stream << ENTTEC::Pro::START_DELIMITER;
*stream << msg->label;
*stream << length;