#pragma once #include "binary-writer.hpp" #include "binary-serializer.hpp" #include "compat/assert.hpp" #include #include namespace floormat::Serialize { template It> constexpr binary_writer::binary_writer(It it) noexcept : it{it}, _bytes_written{0} {} template It> template constexpr void binary_writer::write(T x) noexcept { union { T datum; char bytes[sizeof(T)]; } buf; if (std::is_constant_evaluated()) for (std::size_t i = 0; i < std::size(buf.bytes); i++) buf.bytes[i] = 0; _bytes_written += sizeof(T); if constexpr(sizeof(T) == 1) buf.bytes[0] = (char)x; else if (!std::is_constant_evaluated()) buf.datum = maybe_byteswap(x); else for (std::size_t i = 0; i < sizeof(T); x >>= 8, i++) buf.bytes[i] = (char)(unsigned char)x; for (std::size_t i = 0; i < sizeof(T); i++) *it++ = buf.bytes[i]; } template It> template void binary_writer::write(T x) noexcept { union { T datum; char bytes[sizeof(T)]; } buf; _bytes_written += sizeof(T); buf.datum = maybe_byteswap(x); for (std::size_t i = 0; i < sizeof(T); i++) *it++ = buf.bytes[i]; } template It, serializable T> constexpr binary_writer& operator<<(binary_writer& writer, T x) noexcept { writer.template write(x); return writer; } template It> constexpr void binary_writer::write_asciiz_string(StringView str) noexcept { fm_debug_assert(str.flags() & StringViewFlag::NullTerminated); const auto sz = str.size(); _bytes_written += sz + 1; for (std::size_t i = 0; i < sz; i++) *it++ = str[i]; *it++ = '\0'; } } // namespace floormat::Serialize