Replaced noexcept with MIJIN_NOEXCEPT macro and added PODWrapper for simplifying serializing POD types.

This commit is contained in:
Patrick 2024-12-13 22:20:22 +01:00
parent 781bad3c8e
commit aa804a2a99

View File

@ -49,9 +49,20 @@ using Archive = ArchiveBase<Stream*>;
using OwningArchive = ArchiveBase<std::unique_ptr<Stream>>; using OwningArchive = ArchiveBase<std::unique_ptr<Stream>>;
template<typename TContent>
struct PODWrapper
{
TContent* content;
};
template<typename TContent>
PODWrapper<TContent> pod(TContent& content) MIJIN_NOEXCEPT
{
return PODWrapper<TContent>(&content);
}
template<typename TPointer, typename TValue> requires (std::is_const_v<TValue>) template<typename TPointer, typename TValue> requires (std::is_const_v<TValue>)
Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) noexcept Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) MIJIN_NOEXCEPT
{ {
if (archive.getMode() != ArchiveMode::WRITE) if (archive.getMode() != ArchiveMode::WRITE)
{ {
@ -63,7 +74,7 @@ Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) noexcept
} }
template<typename TPointer> template<typename TPointer>
Task<> c_serialize(ArchiveBase<TPointer>& archive, std::string& value) noexcept Task<> c_serialize(ArchiveBase<TPointer>& archive, std::string& value) MIJIN_NOEXCEPT
{ {
if (archive.getError() != StreamError::SUCCESS) if (archive.getError() != StreamError::SUCCESS)
{ {
@ -81,7 +92,7 @@ Task<> c_serialize(ArchiveBase<TPointer>& archive, std::string& value) noexcept
} }
template<typename TPointer, typename TValue> requires (!std::is_const_v<TValue> && (std::is_arithmetic_v<TValue> || std::is_enum_v<TValue>)) template<typename TPointer, typename TValue> requires (!std::is_const_v<TValue> && (std::is_arithmetic_v<TValue> || std::is_enum_v<TValue>))
Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) noexcept Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) MIJIN_NOEXCEPT
{ {
if (archive.getError() != StreamError::SUCCESS) if (archive.getError() != StreamError::SUCCESS)
{ {
@ -99,7 +110,7 @@ Task<> c_serialize(ArchiveBase<TPointer>& archive, TValue& value) noexcept
} }
template<typename TPointer, typename TElement, typename TAllocator> template<typename TPointer, typename TElement, typename TAllocator>
Task<> c_serialize(ArchiveBase<TPointer>& archive, std::vector<TElement, TAllocator>& value) noexcept Task<> c_serialize(ArchiveBase<TPointer>& archive, std::vector<TElement, TAllocator>& value) MIJIN_NOEXCEPT
{ {
if (archive.getError() != StreamError::SUCCESS) if (archive.getError() != StreamError::SUCCESS)
{ {
@ -129,7 +140,7 @@ Task<> c_serialize(ArchiveBase<TPointer>& archive, std::vector<TElement, TAlloca
} }
template<typename TPointer, typename TContent> template<typename TPointer, typename TContent>
Task<> c_serialize(ArchiveBase<TPointer> archive, Optional<TContent>& value) noexcept Task<> c_serialize(ArchiveBase<TPointer> archive, Optional<TContent>& value) MIJIN_NOEXCEPT
{ {
if (archive.getError() != StreamError::SUCCESS) if (archive.getError() != StreamError::SUCCESS)
{ {
@ -155,6 +166,32 @@ Task<> c_serialize(ArchiveBase<TPointer> archive, Optional<TContent>& value) noe
} }
} }
} }
template<typename TPointer, typename TContent>
Task<> c_serialize(ArchiveBase<TPointer>& archive, PODWrapper<TContent> value) MIJIN_NOEXCEPT
{
if (archive.getError() != StreamError::SUCCESS)
{
co_return;
}
if (archive.getMode() == ArchiveMode::READ)
{
if constexpr (std::is_const_v<TContent>)
{
MIJIN_ERROR("Attempt to write to a const object.");
archive.setError(StreamError::IO_ERROR);
}
else
{
archive.setError(co_await archive.getStream().c_read(*value.content));
}
}
else
{
archive.setError(co_await archive.getStream().c_write(*value.content));
}
co_return;
}
} // namespace mijin } // namespace mijin
#endif // !defined(MIJIN_IO_ARCHIVE_HPP_INCLUDED) #endif // !defined(MIJIN_IO_ARCHIVE_HPP_INCLUDED)