/* 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 #include #include struct streamable; /** * @brief Input/Output stream for unsigned 8-bit buffers */ class bufferstream : public std::enable_shared_from_this , private std::basic_streambuf , public std::basic_iostream { public: /// @brief The byteorder enum enum byteorder { LittleEndian, //!< Little Endian BigEndian, //!< Big Endian, Network Order }; bufferstream(uint8_t * p, std::streamsize l, byteorder o = BigEndian); // 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 T readType() { T ret = 0; int width = sizeof(T); if (in_avail() < width) setstate(std::ios_base::failbit); if (!good()) return ret; if (width == 1) { ret = static_cast(get()); } else { auto bytes = reinterpret_cast(&ret); switch (order_) { case LittleEndian: for (int i = 0; i < width; i++) bytes[i] = get(); break; case BigEndian: for (int i = width; --i >= 0;) bytes[i] = get(); break; } } if (!in_avail()) setstate(std::ios_base::eofbit); return ret; } /** * @brief writeType * @param val */ template void writeType (const T& val) { if (sizeof(T) == 1) { put(static_cast(val)); return; } auto bytes = reinterpret_cast(&val); switch (order_) { case LittleEndian: for (int i = 0; i < sizeof(T); i++) put(bytes[i]); break; case BigEndian: for (int i = sizeof(T); --i >= 0;) put(bytes[i]); break; } } private: byteorder order_; }; /** * @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) = 0; /** * @brief write structure data to output stream */ virtual void oStream(std::shared_ptr) const = 0; };