fast integer types
This commit is contained in:
parent
b64b185d73
commit
e638b09be6
|
@ -44,7 +44,7 @@ public:
|
|||
BigEndian, //!< Big Endian, Network Order
|
||||
};
|
||||
|
||||
bufferstream(uint8_t * p, std::streamsize l, byteorder o = BigEndian);
|
||||
bufferstream(uint8_t *p, std::streamsize l, byteorder o = BigEndian);
|
||||
|
||||
// input sequence
|
||||
uint32_t available();
|
||||
|
@ -98,7 +98,7 @@ public:
|
|||
T readType()
|
||||
{
|
||||
T ret = 0;
|
||||
int width = sizeof(T);
|
||||
uint_fast8_t width = sizeof(T);
|
||||
if (in_avail() < width)
|
||||
setstate(std::ios_base::failbit);
|
||||
if (!good())
|
||||
|
@ -112,11 +112,11 @@ public:
|
|||
auto bytes = reinterpret_cast<uint8_t*>(&ret);
|
||||
switch (order_) {
|
||||
case LittleEndian:
|
||||
for (int i = 0; i < width; i++)
|
||||
for (int_fast8_t i = 0; i < width; i++)
|
||||
bytes[i] = get();
|
||||
break;
|
||||
case BigEndian:
|
||||
for (int i = width; --i >= 0;)
|
||||
for (int_fast8_t i = width; --i >= 0;)
|
||||
bytes[i] = get();
|
||||
break;
|
||||
}
|
||||
|
@ -132,18 +132,19 @@ public:
|
|||
template<typename T>
|
||||
void writeType (const T& val)
|
||||
{
|
||||
if (sizeof(T) == 1) {
|
||||
uint_fast8_t width = sizeof(T);
|
||||
if (width == 1) {
|
||||
put(static_cast<uint8_t>(val));
|
||||
return;
|
||||
}
|
||||
auto bytes = reinterpret_cast<const uint8_t*>(&val);
|
||||
switch (order_) {
|
||||
case LittleEndian:
|
||||
for (int i = 0; i < sizeof(T); i++)
|
||||
for (int_fast8_t i = 0; i < width; i++)
|
||||
put(bytes[i]);
|
||||
break;
|
||||
case BigEndian:
|
||||
for (int i = sizeof(T); --i >= 0;)
|
||||
for (int_fast8_t i = width; --i >= 0;)
|
||||
put(bytes[i]);
|
||||
break;
|
||||
}
|
||||
|
@ -153,10 +154,15 @@ private:
|
|||
byteorder order_;
|
||||
};
|
||||
|
||||
using Buffer = std::shared_ptr<bufferstream>; //!< memory managed unsigned 8-bit buffer
|
||||
|
||||
/**
|
||||
* @brief The inheritable base of data objects that can be read from and written to a bufferstream.
|
||||
* Memory-managed unsigned 8-bit buffer
|
||||
*/
|
||||
using Buffer = std::shared_ptr<bufferstream>;
|
||||
|
||||
|
||||
/**
|
||||
* @brief The base for data objects that can be read from and written to a bufferstream.
|
||||
*/
|
||||
struct streamable
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue