Description
Hello,
I wanted to ask you if you thought it was feasible to report errors in basic_snapshot_loader and snapshot classes, or if I am missing something obvious. I use zppbits (https://github.com/eyalz800/zpp_bits) to serialize my actual entities and components, and the function for serialization/deserialization returns a std::errc indicating success/failure. To use the snapshot and snapshot loader classes, I implemented the operator() overload, and return the std::errc that zppbits gives back to me, but it seems in my calling code (load_archive) doesn't have a chance to inspect the errors.
ie:
entt/src/entt/entity/snapshot.hpp
Line 122 in f2c4174
entt/src/entt/entity/snapshot.hpp
Line 87 in f2c4174
entt/src/entt/entity/snapshot.hpp
Line 193 in f2c4174
Am I missing something obvious, is there a way to get a return value from my operator() overload?
If it helps here's my code:
using U8Buffer = std::vector<std::uint8_t>;
using U8Writer = zpp::bits::out<U8Buffer>;
using U8Reader = zpp::bits::in<std::span<std::uint8_t const> const>;
void
load_archive(auto& dest, auto& archive) noexcept
{
// TODO: ask entt if it's possible to return serde failures
dest
.template get<entt::entity>(archive)
.template get<Coord2D>(archive)
.template get<TileType>(archive)
.template get<IsHiddenTile>(archive)
.template get<PassageNumber>(archive)
.template get<RoomNumber>(archive)
.template get<TrapType>(archive)
.template get<TileSeenByPlayer>(archive)
.template get<TileShroud>(archive)
.template get<Item>(archive)
.template get<TileItem>(archive)
.template get<Inscription>(archive)
.template get<AmuletData>(archive)
.template get<ArmorData>(archive)
.template get<FoodData>(archive)
.template get<PotionData>(archive)
.template get<RingData>(archive)
.template get<ScrollData>(archive)
.template get<StickData>(archive)
.template get<WeaponData>(archive)
.template get<DamageInfo>(archive)
.template get<Monster>(archive)
.template get<TileMonster>(archive)
.template get<SlowState>(archive)
.template get<MonsterDisguise>(archive)
.template get<SeenByFarsight>(archive)
.template get<HallucinationData>(archive)
;
}
class RegistryDeserializer final
{
std::span<uint8_t const> const buffer_;
U8Reader reader_;
public:
explicit RegistryDeserializer(std::span<uint8_t const> const buffer) noexcept
: buffer_{buffer}
, reader_{{buffer_}}
{}
public:
template <typename T>
[[nodiscard]] std::errc
operator()(T& data) noexcept
{
return reader_(data);
}
public:
static auto
deserialize(std::span<uint8_t const> const bytes) noexcept
{
Registry registry;
{
entt::basic_snapshot_loader loader{registry};
RegistryDeserializer deserializer{bytes};
load_archive(loader, deserializer);
loader.orphans();
}
return registry;
}
};
class RegistrySerializer final
{
U8Buffer& buffer_;
U8Writer writer_;
public:
explicit RegistrySerializer(U8Buffer& buffer) noexcept
: buffer_{buffer}
, writer_{{buffer_}}
{}
template <typename T>
[[nodiscard]] std::errc
operator()(T const& component) noexcept
{
return writer_(component);
}
public:
static auto
serialize(Registry const& registry) noexcept
{
U8Buffer buffer;
{
entt::snapshot snapshot{registry};
RegistrySerializer serializer{buffer};
load_archive(snapshot, serializer);
}
return buffer;
}
};