1#ifndef SOURCEMETA_CORE_IO_BINARY_H_
2#define SOURCEMETA_CORE_IO_BINARY_H_
4#ifndef SOURCEMETA_CORE_IO_EXPORT
5#include <sourcemeta/core/io_export.h>
8#include <sourcemeta/core/io_fileview.h>
16namespace sourcemeta::core {
42 auto put_byte(
const std::uint8_t value) -> void;
44 auto put_word(
const std::uint16_t value) -> void;
51 auto put_bytes(
const std::byte *data,
const std::size_t size) -> void;
54 [[nodiscard]]
auto position() const -> std::
size_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));
63 std::ostream *stream_;
93 [[nodiscard]]
auto get_word() -> std::uint16_t;
100 auto get_bytes(std::byte *destination,
const std::size_t size) -> void;
103 [[nodiscard]]
auto position() const -> std::
size_t;
112 template <typename T>
113 requires std::is_trivially_copyable_v<T>
114 [[nodiscard]] auto get() -> T {
116 this->
get_bytes(
reinterpret_cast<std::byte *
>(&value),
sizeof(T));
122 std::istream *stream_{
nullptr};
123 std::size_t offset_{0};
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.