#ifndef ARCHIVES_HPP_TLVURDQK #define ARCHIVES_HPP_TLVURDQK #include #include #include #include #include namespace vanetza { /** * This is a drop-in replacement for boost::archive::binary_iarchive */ class InputArchive { public: using InputStream = std::basic_istream; using StreamBuffer = std::basic_streambuf; class Exception : public std::runtime_error { using std::runtime_error::runtime_error; }; enum class ErrorCode { Ok, IncompleteData, ExcessiveLength, ConstraintViolation, }; InputArchive(InputStream& is); InputArchive(StreamBuffer& buf); template InputArchive& operator>>(T& t) { static_assert(std::is_integral::value == true, "only integral types are supported"); char* ptr = reinterpret_cast(&t); load_binary(ptr, sizeof(T)); return *this; } void load_binary(unsigned char* data, std::size_t len); void load_binary(char* data, std::size_t len); char peek_byte(); std::size_t remaining_bytes(); bool is_good() const; ErrorCode error_code() const; void fail(ErrorCode error_code); private: StreamBuffer* m_stream_buffer; ErrorCode m_error_code = ErrorCode::Ok; }; /** * This is a drop-in replacement for boost::archive::binary_oarchive */ class OutputArchive { public: using OutputStream = std::basic_ostream; using StreamBuffer = std::basic_streambuf; class Exception : public std::runtime_error { using std::runtime_error::runtime_error; }; OutputArchive(OutputStream& os); OutputArchive(StreamBuffer& buf); template OutputArchive& operator<<(const T& t) { static_assert(std::is_integral::value == true, "only integral types are supported"); const char* ptr = reinterpret_cast(&t); save_binary(ptr, sizeof(T)); return *this; } void save_binary(const unsigned char* data, std::size_t len); void save_binary(const char* data, std::size_t len); private: StreamBuffer* m_stream_buffer; }; } // namespace vanetza #endif /* ARCHIVES_HPP_TLVURDQK */