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 <string> // std::string
15#include <string_view> // std::string_view
16#include <type_traits> // std::is_trivially_copyable_v
17
18namespace sourcemeta::core {
19
33class SOURCEMETA_CORE_IO_EXPORT BinaryWriter {
34public:
36 BinaryWriter(std::ostream &stream);
37
38 // Prevent copying, as this class is tied to a stream resource
39 BinaryWriter(const BinaryWriter &) = delete;
40 BinaryWriter(BinaryWriter &&) = delete;
41 auto operator=(const BinaryWriter &) -> BinaryWriter & = delete;
42 auto operator=(BinaryWriter &&) -> BinaryWriter & = delete;
43
45 auto put_byte(const std::uint8_t value) -> void;
47 auto put_word(const std::uint16_t value) -> void;
49 auto put_dword(const std::uint32_t value) -> void;
51 auto put_qword(const std::uint64_t value) -> void;
52
54 auto put_varint(const std::uint64_t value) -> void;
56 auto put_varint_zigzag(const std::int64_t value) -> void;
57
59 auto put_bytes(const std::byte *data, const std::size_t size) -> void;
61 auto put_string(const std::string_view value) -> void;
62
64 [[nodiscard]] auto position() const noexcept -> std::size_t;
65
66private:
67 template <typename T>
68 requires std::is_trivially_copyable_v<T>
69 auto put(const T &value) -> void {
70 this->put_bytes(reinterpret_cast<const std::byte *>(&value), sizeof(T));
71 }
72
73 std::ostream *stream_;
74 std::size_t offset_{0};
75};
76
90class SOURCEMETA_CORE_IO_EXPORT BinaryReader {
91public:
93 BinaryReader(const FileView &view) noexcept;
95 BinaryReader(std::istream &stream);
96
97 // Prevent copying, as this class is tied to an input resource
98 BinaryReader(const BinaryReader &) = delete;
99 BinaryReader(BinaryReader &&) = delete;
100 auto operator=(const BinaryReader &) -> BinaryReader & = delete;
101 auto operator=(BinaryReader &&) -> BinaryReader & = delete;
102
104 [[nodiscard]] auto get_byte() -> std::uint8_t;
106 [[nodiscard]] auto get_word() -> std::uint16_t;
108 [[nodiscard]] auto get_dword() -> std::uint32_t;
110 [[nodiscard]] auto get_qword() -> std::uint64_t;
111
113 [[nodiscard]] auto get_varint() -> std::uint64_t;
115 [[nodiscard]] auto get_varint_zigzag() -> std::int64_t;
116
118 auto get_bytes(std::byte *destination, const std::size_t size) -> void;
120 [[nodiscard]] auto get_string(const std::size_t size) -> std::string;
121
123 [[nodiscard]] auto position() const noexcept -> std::size_t;
124
126 auto seek(const std::size_t position) -> void;
127
130 auto rewind(const std::size_t relative_offset, const std::size_t position)
131 -> std::size_t;
132
134 [[nodiscard]] auto has_more_data() const -> bool;
135
136private:
137 template <typename T>
138 requires std::is_trivially_copyable_v<T>
139 [[nodiscard]] auto get() -> T {
140 T value;
141 this->get_bytes(reinterpret_cast<std::byte *>(&value), sizeof(T));
142 return value;
143 }
144
145 // Exactly one is non-null
146 const FileView *view_{nullptr};
147 std::istream *stream_{nullptr};
148 std::size_t offset_{0};
149};
150
151} // namespace sourcemeta::core
152
153#endif
auto get_string(const std::size_t size) -> std::string
Read the given number of bytes as a string.
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 position() const noexcept -> std::size_t
The current position in bytes.
auto has_more_data() const -> bool
Whether the source has unconsumed bytes at the current cursor.
auto position() const noexcept -> std::size_t
The current cursor position in bytes.
BinaryWriter(std::ostream &stream)
Construct a writer over the given output stream.
auto rewind(const std::size_t relative_offset, const std::size_t position) -> std::size_t
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.
auto put_string(const std::string_view value) -> void
Write the characters of a string.
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.
auto put_varint(const std::uint64_t value) -> void
Write an unsigned integer as a variable-length integer.
auto put_varint_zigzag(const std::int64_t value) -> void
Write a signed integer as a zigzag variable-length integer.
auto get_varint_zigzag() -> std::int64_t
Read a zigzag variable-length integer as a signed integer.
auto put_byte(const std::uint8_t value) -> void
Write a single byte.
BinaryReader(std::istream &stream)
Construct a reader over the given input stream.
auto get_varint() -> std::uint64_t
Read a variable-length integer as an unsigned integer.
auto get_byte() -> std::uint8_t
Read a single byte.
Definition io_fileview.h:32