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>
18namespace sourcemeta::core {
45 auto put_byte(
const std::uint8_t value) -> void;
47 auto put_word(
const std::uint16_t value) -> void;
59 auto put_bytes(
const std::byte *data,
const std::size_t size) -> void;
64 [[nodiscard]]
auto position() const noexcept -> std::
size_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));
73 std::ostream *stream_;
74 std::size_t offset_{0};
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;
123 [[nodiscard]]
auto position() const noexcept -> std::
size_t;
137 template <typename T>
138 requires std::is_trivially_copyable_v<T>
139 [[nodiscard]] auto get() -> T {
141 this->
get_bytes(
reinterpret_cast<std::byte *
>(&value),
sizeof(T));
147 std::istream *stream_{
nullptr};
148 std::size_t offset_{0};
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.