Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
io_binary.h
1#ifndef SOURCEMETA_CORE_IO_BINARY_H_
2#define SOURCEMETA_CORE_IO_BINARY_H_
3
4#ifndef SOURCEMETA_CORE_IO_EXPORT
5#include <sourcemeta/core/io_export.h>
6#endif
7
8#include <sourcemeta/core/io_fileview.h>
9
10#include <cstddef> // std::byte, std::size_t
11#include <cstdint> // std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t
12#include <istream> // std::istream
13#include <ostream> // std::ostream
14#include <type_traits> // std::is_trivially_copyable_v
15
16namespace sourcemeta::core {
17
30class SOURCEMETA_CORE_IO_EXPORT BinaryWriter {
31public:
33 BinaryWriter(std::ostream &stream) noexcept;
34
35 // Prevent copying, as this class is tied to a stream resource
36 BinaryWriter(const BinaryWriter &) = delete;
37 BinaryWriter(BinaryWriter &&) = delete;
38 auto operator=(const BinaryWriter &) -> BinaryWriter & = delete;
39 auto operator=(BinaryWriter &&) -> BinaryWriter & = delete;
40
42 auto put_byte(const std::uint8_t value) -> void;
44 auto put_word(const std::uint16_t value) -> void;
46 auto put_dword(const std::uint32_t value) -> void;
48 auto put_qword(const std::uint64_t value) -> void;
49
51 auto put_bytes(const std::byte *data, const std::size_t size) -> void;
52
54 [[nodiscard]] auto position() const -> std::size_t;
55
56private:
57 template <typename T>
58 requires std::is_trivially_copyable_v<T>
59 auto put(const T &value) -> void {
60 this->put_bytes(reinterpret_cast<const std::byte *>(&value), sizeof(T));
61 }
62
63 std::ostream *stream_;
64};
65
77class SOURCEMETA_CORE_IO_EXPORT BinaryReader {
78public:
80 BinaryReader(const FileView &view) noexcept;
82 BinaryReader(std::istream &stream) noexcept;
83
84 // Prevent copying, as this class is tied to an input resource
85 BinaryReader(const BinaryReader &) = delete;
86 BinaryReader(BinaryReader &&) = delete;
87 auto operator=(const BinaryReader &) -> BinaryReader & = delete;
88 auto operator=(BinaryReader &&) -> BinaryReader & = delete;
89
91 [[nodiscard]] auto get_byte() -> std::uint8_t;
93 [[nodiscard]] auto get_word() -> std::uint16_t;
95 [[nodiscard]] auto get_dword() -> std::uint32_t;
97 [[nodiscard]] auto get_qword() -> std::uint64_t;
98
100 auto get_bytes(std::byte *destination, const std::size_t size) -> void;
101
103 [[nodiscard]] auto position() const -> std::size_t;
104
106 auto seek(const std::size_t position) -> void;
107
109 [[nodiscard]] auto has_more_data() const -> bool;
110
111private:
112 template <typename T>
113 requires std::is_trivially_copyable_v<T>
114 [[nodiscard]] auto get() -> T {
115 T value;
116 this->get_bytes(reinterpret_cast<std::byte *>(&value), sizeof(T));
117 return value;
118 }
119
120 // Exactly one is non-null
121 const FileView *view_{nullptr};
122 std::istream *stream_{nullptr};
123 std::size_t offset_{0};
124};
125
126} // namespace sourcemeta::core
127
128#endif
auto position() const -> std::size_t
The current cursor position in bytes.
auto put_dword(const std::uint32_t value) -> void
Write a 32-bit unsigned integer.
auto get_word() -> std::uint16_t
Read a 16-bit unsigned integer.
auto get_bytes(std::byte *destination, const std::size_t size) -> void
Read a raw sequence of bytes.
auto has_more_data() const -> bool
Whether the source has unconsumed bytes at the current cursor.
auto put_word(const std::uint16_t value) -> void
Write a 16-bit unsigned integer.
auto seek(const std::size_t position) -> void
Move the cursor to position.
BinaryReader(const FileView &view) noexcept
Construct a reader over the given file view.
auto put_bytes(const std::byte *data, const std::size_t size) -> void
Write a raw sequence of bytes.
auto get_qword() -> std::uint64_t
Read a 64-bit unsigned integer.
auto put_qword(const std::uint64_t value) -> void
Write a 64-bit unsigned integer.
auto get_dword() -> std::uint32_t
Read a 32-bit unsigned integer.
BinaryWriter(std::ostream &stream) noexcept
Construct a writer over the given output stream.
BinaryReader(std::istream &stream) noexcept
Construct a reader over the given input stream.
auto put_byte(const std::uint8_t value) -> void
Write a single byte.
auto position() const -> std::size_t
The number of bytes written so far.
auto get_byte() -> std::uint8_t
Read a single byte.
Definition io_fileview.h:31