| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Encoding | ||
| 6 | * \brief Encoder and stream for raw binary output | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_ENCODING_RAW_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_ENCODING_RAW_HPP_ | ||
| 10 | |||
| 11 | #include <span> | ||
| 12 | #include <istream> | ||
| 13 | |||
| 14 | #include <gridformat/common/serialization.hpp> | ||
| 15 | #include <gridformat/common/output_stream.hpp> | ||
| 16 | |||
| 17 | namespace GridFormat { | ||
| 18 | |||
| 19 | //! \addtogroup Encoding | ||
| 20 | //! \{ | ||
| 21 | |||
| 22 | //! For compatibility with Base64 | ||
| 23 | struct RawDecoder { | ||
| 24 | 2171 | Serialization decode_from(std::istream& stream, std::size_t num_decoded_bytes) const { | |
| 25 |
1/2✓ Branch 1 taken 2171 times.
✗ Branch 2 not taken.
|
2171 | Serialization result{num_decoded_bytes}; |
| 26 |
1/2✓ Branch 1 taken 2171 times.
✗ Branch 2 not taken.
|
2171 | auto chars = result.template as_span_of<char>(); |
| 27 |
1/2✓ Branch 3 taken 2171 times.
✗ Branch 4 not taken.
|
2171 | stream.read(chars.data(), chars.size()); |
| 28 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 2171 times.
|
2171 | if (stream.gcount() != static_cast<std::istream::pos_type>(chars.size())) |
| 29 | ✗ | throw IOError("Could not read the requested number of bytes from input stream"); | |
| 30 | 2171 | return result; | |
| 31 | ✗ | } | |
| 32 | |||
| 33 | template<std::size_t s> | ||
| 34 | std::size_t decode(std::span<char, s> chars) const { | ||
| 35 | return chars.size(); | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | //! Wrapper around a given stream to write raw binary data | ||
| 40 | template<typename OStream> | ||
| 41 | class RawBinaryStream : public OutputStreamWrapperBase<OStream> { | ||
| 42 | using Byte = std::byte; | ||
| 43 | public: | ||
| 44 | 36572 | explicit constexpr RawBinaryStream(OStream& s) | |
| 45 | 36572 | : OutputStreamWrapperBase<OStream>(s) | |
| 46 | 36572 | {} | |
| 47 | |||
| 48 | template<typename T, std::size_t size> | ||
| 49 | 117039 | void write(std::span<T, size> data) { | |
| 50 | 117039 | this->_write_raw(data); | |
| 51 | 117039 | } | |
| 52 | }; | ||
| 53 | |||
| 54 | //! \} group Encoding | ||
| 55 | |||
| 56 | } // namespace GridFormat | ||
| 57 | |||
| 58 | namespace GridFormat::Encoding { | ||
| 59 | |||
| 60 | //! \addtogroup Encoding | ||
| 61 | //! \{ | ||
| 62 | |||
| 63 | //! Raw binary encoder | ||
| 64 | struct RawBinary { | ||
| 65 | template<typename S> | ||
| 66 | 36572 | constexpr auto operator()(S& s) const noexcept { | |
| 67 | 36572 | return RawBinaryStream{s}; | |
| 68 | } | ||
| 69 | }; | ||
| 70 | |||
| 71 | //! Instance of the raw binary encoder | ||
| 72 | inline constexpr RawBinary raw; | ||
| 73 | |||
| 74 | //! \} group Encoding | ||
| 75 | |||
| 76 | } // namespace GridFormat::Encoding | ||
| 77 | |||
| 78 | #endif // GRIDFORMAT_COMMON_ENCODING_RAW_HPP_ | ||
| 79 |