feat(Binary): add Writer, ByteBuffer, DecodeBudget, and explicit endian codecs (M-BINARY-02, M-BINARY-03)
This commit is contained in:
1 parent
9ac76bbcf9
commit
eacb8a4602
10 files changed
+1130
-130
No files matched your search
@@ -1,4 +1,7 @@
|
||||
#include <Void/Binary/ByteBuffer.hpp>
|
||||
#include <Void/Binary/Reader.hpp>
|
||||
#include <Void/Binary/Writer.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
@@ -7,8 +10,31 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
const std::array bytes{std::byte{0x01}, std::byte{0x02}};
|
||||
Void::Binary::Reader reader{bytes};
|
||||
const auto value = reader.read<std::uint16_t>(std::endian::big);
|
||||
return value && *value == 0x0102 ? 0 : 1;
|
||||
}
|
||||
std::array<std::byte, 16> scratch{};
|
||||
Void::Binary::Writer writer{scratch};
|
||||
if (!writer.writeBig<std::uint16_t>(0x0102) ||
|
||||
!writer.writeBool(true) ||
|
||||
!writer.writeF64(3.5, std::endian::little)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Void::Binary::Reader reader{writer.written()};
|
||||
const auto u16 = reader.readBig<std::uint16_t>();
|
||||
const auto b = reader.readBool();
|
||||
const auto d = reader.readF64(std::endian::little);
|
||||
if (!u16 || *u16 != 0x0102 || !b || !*b || !d || *d != 3.5) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Void::Memory::BudgetResource budget{1024};
|
||||
Void::Binary::ByteBuffer buf{&budget, 64};
|
||||
if (!buf.writeLittle<std::uint32_t>(0x11223344u) ||
|
||||
!buf.writeString("ok")) {
|
||||
return 1;
|
||||
}
|
||||
const auto frozen = buf.freeze();
|
||||
auto fr = frozen.reader();
|
||||
const auto v = fr.readLittle<std::uint32_t>();
|
||||
const auto s = fr.readString(2);
|
||||
return v && *v == 0x11223344u && s && *s == "ok" ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user