-
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 new entity serialization methods
Linked: #150
- Loading branch information
1 parent
7cd665e
commit 90fe105
Showing
8 changed files
with
432 additions
and
6 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
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 |
---|---|---|
|
@@ -1037,7 +1037,7 @@ namespace ecstasy | |
template <std::derived_from<ISystem> S> | ||
S &getSystem() | ||
{ | ||
return _storages.get<S>(); | ||
return _systems.get<S>(); | ||
} | ||
|
||
/// | ||
|
@@ -1179,7 +1179,7 @@ namespace ecstasy | |
void runSystems(size_t group, size_t mask); | ||
|
||
/// | ||
/// @brief Get a reference to the storages instances. | ||
/// @brief Get a const reference to the storages instances. | ||
/// | ||
/// @return constexpr const Instances<IStorage>& Const reference to the storages instance. | ||
/// | ||
|
@@ -1191,6 +1191,19 @@ namespace ecstasy | |
return _storages; | ||
} | ||
|
||
/// | ||
/// @brief Get a reference to the storages instances. | ||
/// | ||
/// @return constexpr Instances<IStorage>& Reference to the storages instance. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
constexpr Instances<IStorage> &getStorages() | ||
{ | ||
return _storages; | ||
} | ||
|
||
private: | ||
Instances<ResourceBase> _resources; | ||
Instances<IStorage> _storages; | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// | ||
/// @file EntityComponentSerializer.hpp | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @brief | ||
/// @version 1.0.0 | ||
/// @date 2024-10-03 | ||
/// | ||
/// @copyright Copyright (c) ECSTASY 2024 | ||
/// | ||
/// | ||
|
||
#ifndef ECSTASY_COMPONENT_COMPONENT_SERIALIZER_HPP_ | ||
#define ECSTASY_COMPONENT_COMPONENT_SERIALIZER_HPP_ | ||
|
||
#include "ecstasy/serialization/IEntityComponentSerializer.hpp" | ||
|
||
namespace ecstasy::serialization | ||
{ | ||
/// | ||
/// @brief Entity component serializer class bound to a specific component and a serializer type. | ||
/// | ||
/// The class instance doesn't contains any data except its vtable. | ||
/// | ||
/// @tparam Component Type of the component to serialize. | ||
/// @tparam Serializer Type of the serializer to use to serialize the component. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
template <typename Component, std::derived_from<ISerializer> Serializer> | ||
class EntityComponentSerializer : public IEntityComponentSerializer { | ||
public: | ||
/// Type of the storage used to store the component. | ||
using StorageType = getStorageType<Component>; | ||
|
||
/// | ||
/// @brief Construct a new Component Rtti | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
EntityComponentSerializer() : IEntityComponentSerializer() | ||
{ | ||
} | ||
|
||
/// | ||
/// @brief Destroy the Component Rtti | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
~EntityComponentSerializer() override = default; | ||
|
||
/// @copydoc IEntityComponentSerializer::save | ||
ISerializer &save( | ||
ISerializer &serializer, const IStorage &storage, const RegistryEntity &entity) const override final | ||
{ | ||
return dynamic_cast<Serializer &>(serializer) | ||
.template saveEntityComponent<Component>( | ||
dynamic_cast<const StorageType &>(storage).at(entity.getIndex())); | ||
} | ||
|
||
/// @copydoc IEntityComponentSerializer::load | ||
ISerializer &load(ISerializer &serializer, IStorage &storage, RegistryEntity &entity) const override final | ||
{ | ||
if (!storage.contains(entity.getIndex())) { | ||
dynamic_cast<StorageType &>(storage).insert( | ||
entity.getIndex(), dynamic_cast<Serializer &>(serializer).template load<Component>()); | ||
return serializer; | ||
} else | ||
return dynamic_cast<Serializer &>(serializer) | ||
.template update<Component>(dynamic_cast<StorageType &>(storage).at(entity.getIndex())); | ||
} | ||
|
||
/// @copydoc IEntityComponentSerializer::getStorageTypeIndex | ||
std::type_index getStorageTypeIndex() const override final | ||
{ | ||
return std::type_index(typeid(StorageType)); | ||
} | ||
}; | ||
|
||
} // namespace ecstasy::serialization | ||
|
||
#endif /* !ECSTASY_COMPONENT_COMPONENT_SERIALIZER_HPP_ */ |
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/// | ||
/// @file IEntityComponentSerializer.hpp | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @brief File containing the IEntityComponentSerializer class, used to serialize entity components from an IStorage | ||
/// instance. | ||
/// @version 1.0.0 | ||
/// @date 2024-10-03 | ||
/// | ||
/// @copyright Copyright (c) ECSTASY 2024 | ||
/// | ||
/// | ||
|
||
#ifndef ECSTASY_COMPONENT_ICOMPONESERIALIZERTTI_HPP_ | ||
#define ECSTASY_COMPONENT_ICOMPONESERIALIZERTTI_HPP_ | ||
|
||
#include <functional> | ||
#include <typeindex> | ||
|
||
namespace ecstasy | ||
{ | ||
class IStorage; | ||
class RegistryEntity; | ||
|
||
namespace serialization | ||
{ | ||
class ISerializer; | ||
|
||
/// | ||
/// @brief Type erased interface for serializing entity components. | ||
/// | ||
/// This interface is used to serialize entity components from IStorage/ISerializer instances when we don't know | ||
/// the type of the component/serializer. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
class IEntityComponentSerializer { | ||
public: | ||
/// | ||
/// @brief Destroy the IEntityComponentSerializer | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
virtual ~IEntityComponentSerializer() = default; | ||
|
||
/// | ||
/// @brief Save the component to the serializer. | ||
/// | ||
/// @param[in] serializer Reference to the serializer to save the component to. | ||
/// @param[in] storage Const reference to the storage containing the component. | ||
/// @param[in] entity Const reference to the entity associated with the component. | ||
/// | ||
/// @return ISerializer& Reference to the serializer for chaining. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
virtual ISerializer &save( | ||
ISerializer &serializer, const IStorage &storage, const RegistryEntity &entity) const = 0; | ||
|
||
/// | ||
/// @brief Load an entity component from the serializer. | ||
/// | ||
/// @param[in] serializer Reference to the serializer to load the component from. | ||
/// @param[in] storage Reference to the storage to load the component to. | ||
/// @param[in] entity Reference to the entity to load/update the component to. | ||
/// | ||
/// @return ISerializer& Reference to the serializer for chaining. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
virtual ISerializer &load(ISerializer &serializer, IStorage &storage, RegistryEntity &entity) const = 0; | ||
|
||
/// | ||
/// @brief Get the Storage Type Index of the component. | ||
/// | ||
/// @return std::type_index Type index of the storage. | ||
/// | ||
/// @author Andréas Leroux ([email protected]) | ||
/// @since 1.0.0 (2024-10-04) | ||
/// | ||
virtual std::type_index getStorageTypeIndex() const = 0; | ||
}; | ||
|
||
} // namespace serialization | ||
} // namespace ecstasy | ||
|
||
#endif /* !ECSTASY_COMPONENT_ICOMPONENT_SERIALIZER_HPP_ */ |
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.