-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Serializer updateEntity and loadEntity
Linked: #150
- Loading branch information
1 parent
913eba1
commit 3ce576f
Showing
16 changed files
with
521 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
#include "ecstasy/config.hpp" | ||
#include "ecstasy/serialization/RawSerializer.hpp" | ||
#include "util/meta/Traits.hpp" | ||
#include "ecstasy/serialization/traits/can_load_type.hpp" | ||
#include "ecstasy/serialization/traits/can_save_type.hpp" | ||
#include "ecstasy/serialization/traits/can_update_type.hpp" | ||
|
||
#ifndef ECSTASY_SERIALIZERS | ||
#define ECSTASY_SERIALIZERS util::meta::Traits<ecstasy::serialization::RawSerializer> | ||
|
@@ -48,9 +50,9 @@ namespace ecstasy::serialization | |
template <typename S1, typename... Ss> | ||
struct ComponentSerializer<util::meta::Traits<S1, Ss...>> { | ||
/// | ||
/// @brief Serialize a component with the given serializer. | ||
/// @brief Save a component with the given serializer. | ||
/// | ||
/// @note This function will try to serialize the component with each serializer in the list, stopping when the | ||
/// @note This function will try to save the component with each serializer in the list, stopping when the | ||
/// targeted serializer is found (using @p stype). | ||
/// | ||
/// @tparam Comp Component type. | ||
|
@@ -65,14 +67,14 @@ namespace ecstasy::serialization | |
/// @since 1.0.0 (2024-06-11) | ||
/// | ||
template <typename Comp> | ||
static ISerializer &serialize(ISerializer &serializer, const std::type_info &stype, const Comp &component) | ||
static ISerializer &save(ISerializer &serializer, const std::type_info &stype, const Comp &component) | ||
{ | ||
(trySerialize<S1>(serializer, stype, component) || ... || trySerialize<Ss>(serializer, stype, component)); | ||
(trySave<S1>(serializer, stype, component) || ... || trySave<Ss>(serializer, stype, component)); | ||
return serializer; | ||
} | ||
|
||
/// | ||
/// @brief Try to serialize a component with the given serializer. The serialization is done if S is the same as | ||
/// @brief Try to save a component with the given serializer. The serialization is done if S is the same as | ||
/// @p stype. | ||
/// | ||
/// @tparam S Serializer type. | ||
|
@@ -88,7 +90,7 @@ namespace ecstasy::serialization | |
/// @since 1.0.0 (2024-06-11) | ||
/// | ||
template <typename S, typename Comp> | ||
static bool trySerialize(ISerializer &serializer, const std::type_info &stype, const Comp &component) | ||
static bool trySave(ISerializer &serializer, const std::type_info &stype, const Comp &component) | ||
{ | ||
if constexpr (traits::can_save_type_v<S, Comp>) { | ||
if (stype == typeid(S)) { | ||
|
@@ -98,10 +100,112 @@ namespace ecstasy::serialization | |
} | ||
return false; | ||
} | ||
|
||
/// | ||
/// @brief Update a component with the given serializer. | ||
/// | ||
/// @tparam Comp Component type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] component Component to update. | ||
/// | ||
/// @return ISerializer& The serializer. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename Comp> | ||
static ISerializer &update(ISerializer &serializer, const std::type_info &stype, Comp &component) | ||
{ | ||
(tryUpdate<S1>(serializer, stype, component) || ... || tryUpdate<Ss>(serializer, stype, component)); | ||
return serializer; | ||
} | ||
|
||
/// | ||
/// @brief Try to update a component with the given serializer. | ||
/// | ||
/// @tparam S Serializer type. | ||
/// @tparam Comp Component type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] component Component to update. | ||
/// | ||
/// @return bool True if the component was updated, false otherwise. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename S, typename Comp> | ||
static bool tryUpdate(ISerializer &serializer, const std::type_info &stype, Comp &component) | ||
{ | ||
if constexpr (traits::can_update_type_v<S, Comp>) { | ||
if (stype == typeid(S)) { | ||
dynamic_cast<S &>(serializer).update(component); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// | ||
/// @brief Load a component with the given serializer for a specifc entity. | ||
/// | ||
/// @tparam Storage Storage type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] storage Storage to use. | ||
/// @param[in] entityId Entity id to which the storage must be loaded. | ||
/// | ||
/// @return ISerializer& The serializer. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename Storage> | ||
static ISerializer &load( | ||
ISerializer &serializer, const std::type_info &stype, Storage &storage, size_t entityId) | ||
{ | ||
(tryLoad<S1>(serializer, stype, storage, entityId) || ... | ||
|| tryLoad<Ss>(serializer, stype, storage, entityId)); | ||
return serializer; | ||
} | ||
|
||
/// | ||
/// @brief Try to load a component with the given serializer for a specific entity. | ||
/// | ||
/// @tparam S Serializer type. | ||
/// @tparam Storage Storage type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] storage Storage to use. | ||
/// @param[in] entityId Entity id to which the storage must be loaded. | ||
/// | ||
/// @return bool True if the component was loaded, false otherwise. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename S, typename Storage> | ||
static bool tryLoad(ISerializer &serializer, const std::type_info &stype, Storage &storage, size_t entityId) | ||
{ | ||
if constexpr (traits::can_load_type_v<S, typename Storage::Component> | ||
&& std::movable<typename Storage::Component>) { | ||
if (stype == typeid(S)) { | ||
storage.insert( | ||
entityId, dynamic_cast<S &>(serializer).template load<typename Storage::Component>()); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
/// | ||
/// @brief Serialize a component with the given serializer if possible. | ||
/// @brief Save a component with the given serializer if possible. | ||
/// | ||
/// @tparam C Component type. | ||
/// | ||
|
@@ -115,9 +219,50 @@ namespace ecstasy::serialization | |
/// @since 1.0.0 (2024-06-11) | ||
/// | ||
template <typename C> | ||
ISerializer &serialize(ISerializer &serializer, const std::type_info &stype, const C &component) | ||
ISerializer &save(ISerializer &serializer, const std::type_info &stype, const C &component) | ||
{ | ||
return ComponentSerializer<Serializers>::save(serializer, stype, component); | ||
} | ||
|
||
/// | ||
/// @brief Update a component with the given serializer if possible. | ||
/// | ||
/// @tparam C Component type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] component Component to update. | ||
/// | ||
/// @return ISerializer& The serializer. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename C> | ||
ISerializer &update(ISerializer &serializer, const std::type_info &stype, C &component) | ||
{ | ||
return ComponentSerializer<Serializers>::update(serializer, stype, component); | ||
} | ||
|
||
/// | ||
/// @brief Load a component with the given serializer if possible. | ||
/// | ||
/// @tparam Storage Storage type. | ||
/// | ||
/// @param[in] serializer Serializer to use. | ||
/// @param[in] stype Type of the serializer. | ||
/// @param[in] storage Storage to use. | ||
/// @param[in] entityId Entity id to which the storage must be loaded. | ||
/// | ||
/// @return ISerializer& The serializer. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-06-25) | ||
/// | ||
template <typename Storage> | ||
ISerializer &load(ISerializer &serializer, const std::type_info &stype, Storage &storage, size_t entityId) | ||
{ | ||
return ComponentSerializer<Serializers>::serialize(serializer, stype, component); | ||
return ComponentSerializer<Serializers>::load(serializer, stype, storage, entityId); | ||
} | ||
|
||
} // namespace ecstasy::serialization | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.