#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_SERIALIZATION_BINARYWRITER_H #define OPENMW_COMPONENTS_DETOURNAVIGATOR_SERIALIZATION_BINARYWRITER_H #include #include #include #include #include namespace DetourNavigator::Serialization { struct BinaryWriter { public: explicit BinaryWriter(std::byte* dest, const std::byte* end) : mDest(dest), mEnd(end) { assert(mDest <= mEnd); } BinaryWriter(const BinaryWriter&) = delete; template void operator()(Format&& format, const T& value) { if constexpr (std::is_arithmetic_v) { if (mEnd - mDest < static_cast(sizeof(value))) throw std::runtime_error("Not enough space"); std::memcpy(mDest, &value, sizeof(value)); mDest += sizeof(value); } else { format(*this, value); } } template auto operator()(Format&& format, const T* data, std::size_t count) { if constexpr (std::is_arithmetic_v) { const std::size_t size = sizeof(T) * count; if (mEnd - mDest < static_cast(size)) throw std::runtime_error("Not enough space"); std::memcpy(mDest, data, size); mDest += size; } else { format(*this, data, count); } } private: std::byte* mDest; const std::byte* const mEnd; }; } #endif