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 l
* @param o * @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_streambuf<uint8_t>()
, std::basic_iostream<uint8_t>(this) , std::basic_iostream<uint8_t>(this)
, order_(o) , order_(o)

View File

@ -31,6 +31,8 @@ struct streamable;
/** /**
* @brief Input/Output stream for unsigned 8-bit buffers * @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 class bufferstream
: public std::enable_shared_from_this<bufferstream> : public std::enable_shared_from_this<bufferstream>
@ -38,13 +40,22 @@ class bufferstream
, public std::basic_iostream<uint8_t> , public std::basic_iostream<uint8_t>
{ {
public: public:
/// @brief The byteorder enum /**
enum byteorder { * @brief The endian enum is not available until C++20.
LittleEndian, //!< Little Endian *
BigEndian, //!< Big Endian, Network Order * \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 // input sequence
size_t available(); size_t available();
@ -155,7 +166,7 @@ public:
} }
private: private:
byteorder order_; endian order_;
}; };

View File

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