GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
raw.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
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
17namespace GridFormat {
18
21
23struct RawDecoder {
24 Serialization decode_from(std::istream& stream, std::size_t num_decoded_bytes) const {
25 Serialization result{num_decoded_bytes};
26 auto chars = result.template as_span_of<char>();
27 stream.read(chars.data(), chars.size());
28 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 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
40template<typename OStream>
41class RawBinaryStream : public OutputStreamWrapperBase<OStream> {
42 using Byte = std::byte;
43 public:
44 explicit constexpr RawBinaryStream(OStream& s)
45 : OutputStreamWrapperBase<OStream>(s)
46 {}
47
48 template<typename T, std::size_t size>
49 void write(std::span<T, size> data) {
50 this->_write_raw(data);
51 }
52};
53
55
56} // namespace GridFormat
57
58namespace GridFormat::Encoding {
59
62
64struct RawBinary {
65 template<typename S>
66 constexpr auto operator()(S& s) const noexcept {
67 return RawBinaryStream{s};
68 }
69};
70
72inline constexpr RawBinary raw;
73
75
76} // namespace GridFormat::Encoding
77
78#endif // GRIDFORMAT_COMMON_ENCODING_RAW_HPP_
Wrapper around a given stream to write raw binary data.
Definition: raw.hpp:41
constexpr RawBinary raw
Instance of the raw binary encoder.
Definition: raw.hpp:72
Raw binary encoder.
Definition: raw.hpp:64
For compatibility with Base64.
Definition: raw.hpp:23