134 lines
3.9 KiB
C
134 lines
3.9 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
|
||
|
*/
|
||
|
class bufferstream
|
||
|
: public std::enable_shared_from_this<bufferstream>
|
||
|
, private std::basic_streambuf<uint8_t>
|
||
|
, public std::basic_iostream<uint8_t>
|
||
|
{
|
||
|
public:
|
||
|
bufferstream(uint8_t * p, std::streamsize l);
|
||
|
|
||
|
// 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);
|
||
|
|
||
|
// stream objects
|
||
|
bufferstream& operator>> (streamable& obj);
|
||
|
bufferstream& operator<< (const streamable& obj);
|
||
|
|
||
|
// strings
|
||
|
void readString(std::string& str, const int fixed_length = 0);
|
||
|
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()
|
||
|
{
|
||
|
if (in_avail() < sizeof(T))
|
||
|
setstate(std::ios_base::failbit);
|
||
|
if (!good())
|
||
|
return 0;
|
||
|
T ret = 0;
|
||
|
auto data = reinterpret_cast<uint8_t*>(&ret);
|
||
|
for (int i = sizeof(T); --i >= 0; )
|
||
|
data[i] = get();
|
||
|
if (!in_avail())
|
||
|
setstate(std::ios_base::eofbit);
|
||
|
return ret;
|
||
|
}
|
||
|
/**
|
||
|
* @brief writeType
|
||
|
* @param val
|
||
|
*/
|
||
|
template<typename T>
|
||
|
void writeType (const T& val)
|
||
|
{
|
||
|
auto data = reinterpret_cast<const uint8_t*>(&val);
|
||
|
for (int i = sizeof(T); --i >= 0; )
|
||
|
put(data[i]);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief The inheritable base of 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;
|
||
|
};
|