2023-04-02 13:13:26 -04:00
|
|
|
/*
|
|
|
|
bufferstream.h
|
|
|
|
|
|
|
|
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
struct streamable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Input/Output stream for unsigned 8-bit buffers
|
|
|
|
*/
|
|
|
|
class bufferstream
|
|
|
|
: public std::enable_shared_from_this<bufferstream>
|
|
|
|
, private std::basic_streambuf<uint8_t>
|
|
|
|
, public std::basic_iostream<uint8_t>
|
|
|
|
{
|
|
|
|
public:
|
2023-04-03 15:39:42 -04:00
|
|
|
/// @brief The byteorder enum
|
|
|
|
enum byteorder {
|
|
|
|
LittleEndian, //!< Little Endian
|
|
|
|
BigEndian, //!< Big Endian, Network Order
|
|
|
|
};
|
|
|
|
|
2023-05-15 10:48:59 -04:00
|
|
|
bufferstream(uint8_t *p, std::streamsize l, byteorder o = BigEndian);
|
2023-04-02 13:13:26 -04:00
|
|
|
|
|
|
|
// input sequence
|
|
|
|
uint32_t available();
|
|
|
|
uint8_t * data();
|
|
|
|
|
|
|
|
// output sequence
|
|
|
|
uint32_t size();
|
|
|
|
uint8_t * base();
|
|
|
|
|
|
|
|
// unsigned integer
|
|
|
|
bufferstream& operator>> (uint8_t& val);
|
|
|
|
bufferstream& operator>> (uint16_t& val);
|
|
|
|
bufferstream& operator>> (uint32_t& val);
|
|
|
|
bufferstream& operator>> (uint64_t& val);
|
|
|
|
bufferstream& operator<< (const uint8_t& val);
|
|
|
|
bufferstream& operator<< (const uint16_t& val);
|
|
|
|
bufferstream& operator<< (const uint32_t& val);
|
|
|
|
bufferstream& operator<< (const uint64_t& val);
|
|
|
|
|
|
|
|
// signed integer
|
|
|
|
bufferstream& operator>> (int8_t& val);
|
|
|
|
bufferstream& operator>> (int16_t& val);
|
|
|
|
bufferstream& operator>> (int32_t& val);
|
|
|
|
bufferstream& operator>> (int64_t& val);
|
|
|
|
bufferstream& operator<< (const int8_t& val);
|
|
|
|
bufferstream& operator<< (const int16_t& val);
|
|
|
|
bufferstream& operator<< (const int32_t& val);
|
|
|
|
bufferstream& operator<< (const int64_t& val);
|
|
|
|
|
2023-04-23 11:36:17 -04:00
|
|
|
// floating point
|
|
|
|
bufferstream& operator>> (float& val);
|
|
|
|
bufferstream& operator>> (double& val);
|
|
|
|
bufferstream& operator<< (const float& val);
|
|
|
|
bufferstream& operator<< (const double& val);
|
|
|
|
|
2023-04-02 13:13:26 -04:00
|
|
|
// stream objects
|
|
|
|
bufferstream& operator>> (streamable& obj);
|
|
|
|
bufferstream& operator<< (const streamable& obj);
|
|
|
|
|
|
|
|
// strings
|
2023-05-13 11:23:27 -04:00
|
|
|
void readString(std::string& str, const int fixed_length = 0, const bool terminated = true);
|
2023-04-02 13:13:26 -04:00
|
|
|
void writeString(const std::string& str, const size_t fixed_length = 0,
|
|
|
|
const bool terminated = true);
|
|
|
|
|
|
|
|
// reinterpreted i/o
|
|
|
|
/**
|
|
|
|
* @brief readType
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
T readType()
|
|
|
|
{
|
2023-04-03 15:39:42 -04:00
|
|
|
T ret = 0;
|
2023-05-15 10:48:59 -04:00
|
|
|
uint_fast8_t width = sizeof(T);
|
2023-04-03 15:39:42 -04:00
|
|
|
if (in_avail() < width)
|
|
|
|
setstate(std::ios_base::failbit);
|
|
|
|
if (!good())
|
2023-04-02 13:13:26 -04:00
|
|
|
return ret;
|
2023-04-03 15:39:42 -04:00
|
|
|
if (width == 1)
|
|
|
|
{
|
|
|
|
ret = static_cast<T>(get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto bytes = reinterpret_cast<uint8_t*>(&ret);
|
|
|
|
switch (order_) {
|
|
|
|
case LittleEndian:
|
2023-05-15 10:48:59 -04:00
|
|
|
for (int_fast8_t i = 0; i < width; i++)
|
2023-04-03 15:39:42 -04:00
|
|
|
bytes[i] = get();
|
|
|
|
break;
|
|
|
|
case BigEndian:
|
2023-05-15 10:48:59 -04:00
|
|
|
for (int_fast8_t i = width; --i >= 0;)
|
2023-04-03 15:39:42 -04:00
|
|
|
bytes[i] = get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!in_avail())
|
|
|
|
setstate(std::ios_base::eofbit);
|
|
|
|
return ret;
|
2023-04-02 13:13:26 -04:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief writeType
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void writeType (const T& val)
|
|
|
|
{
|
2023-05-15 10:48:59 -04:00
|
|
|
uint_fast8_t width = sizeof(T);
|
|
|
|
if (width == 1) {
|
2023-04-03 15:39:42 -04:00
|
|
|
put(static_cast<uint8_t>(val));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto bytes = reinterpret_cast<const uint8_t*>(&val);
|
|
|
|
switch (order_) {
|
|
|
|
case LittleEndian:
|
2023-05-15 10:48:59 -04:00
|
|
|
for (int_fast8_t i = 0; i < width; i++)
|
2023-04-03 15:39:42 -04:00
|
|
|
put(bytes[i]);
|
|
|
|
break;
|
|
|
|
case BigEndian:
|
2023-05-15 10:48:59 -04:00
|
|
|
for (int_fast8_t i = width; --i >= 0;)
|
2023-04-03 15:39:42 -04:00
|
|
|
put(bytes[i]);
|
|
|
|
break;
|
|
|
|
}
|
2023-04-02 13:13:26 -04:00
|
|
|
}
|
2023-04-03 15:39:42 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
byteorder order_;
|
2023-04-02 13:13:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-05-15 10:48:59 -04:00
|
|
|
* 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.
|
2023-04-02 13:13:26 -04:00
|
|
|
*/
|
|
|
|
struct streamable
|
|
|
|
{
|
|
|
|
virtual ~streamable() {};
|
|
|
|
/**
|
|
|
|
* @brief streamSize
|
|
|
|
* @return length (count of octets) on the wire
|
|
|
|
*/
|
|
|
|
virtual size_t streamSize() const = 0;
|
|
|
|
/**
|
|
|
|
* @brief fill structure data from input stream
|
|
|
|
*/
|
|
|
|
virtual void iStream(std::shared_ptr<bufferstream>) = 0;
|
|
|
|
/**
|
|
|
|
* @brief write structure data to output stream
|
|
|
|
*/
|
|
|
|
virtual void oStream(std::shared_ptr<bufferstream>) const = 0;
|
|
|
|
};
|