1
0
Fork 0
OpenLCP/lib/bufferstream/bufferstream.h

185 lines
5.2 KiB
C++

/*
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
*
* By default, wide types will be handled as big-endian, the network byte order.
*/
class bufferstream
: public std::enable_shared_from_this<bufferstream>
, private std::basic_streambuf<uint8_t>
, public std::basic_iostream<uint8_t>
{
public:
/**
* @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, endian o = endian::big); // default network byte-order
// input sequence
size_t available();
uint8_t *data();
// output sequence
size_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);
// floating point
bufferstream &operator>> (float &val);
bufferstream &operator>> (double &val);
bufferstream &operator<< (const float &val);
bufferstream &operator<< (const double &val);
// stream objects
bufferstream &operator>> (streamable &obj);
bufferstream &operator<< (const streamable &obj);
// null-terminated strings
bufferstream &operator>> (std::string &str);
bufferstream &operator<< (const std::string &str);
// strings
void readString(std::string& str, const int fixed_length = 0, const bool terminated = true);
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()
{
T ret = 0;
uint_fast8_t width = sizeof(T);
if (in_avail() < width)
setstate(std::ios_base::failbit);
if (!good())
return ret;
if (width == 1)
{
ret = static_cast<T>(get());
}
else
{
auto bytes = reinterpret_cast<uint8_t*>(&ret);
if (order_ == endian::native)
for (int_fast8_t i = 0; i < width; i++)
bytes[i] = get();
else
for (int_fast8_t i = width; --i >= 0;)
bytes[i] = get();
}
if (!in_avail())
setstate(std::ios_base::eofbit);
return ret;
}
/**
* @brief writeType
* @param val
*/
template<typename T>
void writeType (const T& val)
{
uint_fast8_t width = sizeof(T);
if (width == 1) {
put(static_cast<uint8_t>(val));
return;
}
auto bytes = reinterpret_cast<const uint8_t*>(&val);
if (order_ == endian::native)
for (int_fast8_t i = 0; i < width; i++)
put(bytes[i]);
else
for (int_fast8_t i = width; --i >= 0;)
put(bytes[i]);
}
private:
endian order_;
};
/**
* @brief The base for data objects that can be read from and written to a bufferstream.
*/
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;
};