ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::serialization::Serializer< S > Class Template Referenceabstract

Serializer class. More...

#include <Serializer.hpp>

Inheritance diagram for ecstasy::serialization::Serializer< S >:
Collaboration diagram for ecstasy::serialization::Serializer< S >:

Public Member Functions

 Serializer ()=default
 Construct a new Serializer.
 
 ~Serializer () override=default
 Destroy the Serializer.
 
constexpr S & inner ()
 Get a reference to the inner serializer.
 
void importFile (const std::filesystem::path &filename) override
 Import data from a file into the serializer.
 
void importBytes (const std::string &content) override
 Import data from a string into the serializer.
 
void exportFile (const std::filesystem::path &filename) const override
 Export the serializer content to a file.
 
std::string exportBytes () const override
 Export the serializer content to a string.
 
template<typename U >
requires concepts::has_extraction_operator<S, U> || traits::has_save_impl_for_type_v<S, U>
S & save (const U &object)
 Save an object to the serializer.
 
template<typename C >
S & saveEntityComponent (const C &component)
 Save an entity component to the serializer.
 
template<typename... Cs>
S & saveEntity (const RegistryEntity &entity)
 Save an entity to the serializer with explicit components.
 
S & saveEntity (RegistryEntity &entity)
 Save all registered components of an entity to the serializer.
 
template<typename U >
requires is_constructible<U> || (std::is_default_constructible_v<U> && traits::can_update_type_v<S, U>) || traits::has_load_impl_for_type_v<S, U>
load ()
 Load an object from the serializer.
 
RegistryEntity loadEntity (Registry &registry)
 Load an entity component from the serializer.
 
template<typename U >
requires traits::has_update_impl_for_type_v<S, U> || std::is_fundamental_v<U> || concepts::has_insertion_operator<S, U>
S & update (U &object)
 Update an existing object from the serializer.
 
S & updateEntity (RegistryEntity &entity)
 Update all registered components of an entity from the serializer.
 
template<typename U >
S & operator<< (const U &object)
 Operator overload to simplify the save method.
 
template<typename U >
S & operator>> (U &object)
 Operator overload to simplify the update method.
 
- Public Member Functions inherited from ecstasy::serialization::ISerializer
virtual ~ISerializer ()=default
 Destroy the ISerializer.
 
virtual void clear ()=0
 Clear the serializer content.
 
virtual size_t size () const =0
 Get the size of the serializer content (in bytes).
 
virtual void importFile (const std::filesystem::path &filename)=0
 Import data from a file into the serializer.
 
virtual void importBytes (const std::string &content)=0
 Import data from a string into the serializer.
 
virtual void importStream (std::istream &stream)=0
 Import data from a stream into the serializer.
 
virtual void exportFile (const std::filesystem::path &filename) const =0
 Export the serializer content to a file.
 
virtual std::string exportBytes () const =0
 Export the serializer content to a string.
 
virtual void exportStream (std::ostream &stream) const =0
 Export the serializer content to a stream.
 

Static Public Member Functions

template<typename C >
static IEntityComponentSerializerregisterComponent (std::string_view name)
 Register a component to this serializer type.
 
template<typename T >
static bool hasEntityComponentSerializer ()
 Get the registered components of the serializer.
 

Static Public Attributes

template<typename U >
static constexpr bool is_constructible = std::is_constructible_v<U, std::add_lvalue_reference_t<S>>
 Check if a type is constructible from the serializer.
 

Protected Types

using Parent = Serializer< S >
 Parent serializer type, used for inheriting classes.
 
using OptionalEntityComponentSerializer = std::optional< std::reference_wrapper< IEntityComponentSerializer > >
 

Protected Member Functions

virtual OptionalEntityComponentSerializer loadComponentSerializer ()=0
 Load the next component serializer from the stream.
 
virtual void beforeSaveEntity (RegistryEntity &entity)
 Optional method triggered at the start of saveEntity.
 
virtual void afterSaveEntity (RegistryEntity &entity)
 Optional method triggered at the end of saveEntity.
 
virtual void beforeUpdateEntity (RegistryEntity &entity)
 Optional method triggered at the start of updateEntity.
 
virtual void afterUpdateEntity (RegistryEntity &entity)
 Optional method triggered at the end of updateEntity.
 

Detailed Description

template<typename S>
class ecstasy::serialization::Serializer< S >

Serializer class.

Template Parameters
SChild serializer type. This is required to call the sub serializer class methods because template and virtual methods cannot be mixed.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 69 of file Serializer.hpp.

Member Typedef Documentation

◆ OptionalEntityComponentSerializer

template<typename S >
using ecstasy::serialization::Serializer< S >::OptionalEntityComponentSerializer = std::optional<std::reference_wrapper<IEntityComponentSerializer> >
protected

Definition at line 431 of file Serializer.hpp.

◆ Parent

template<typename S >
using ecstasy::serialization::Serializer< S >::Parent = Serializer<S>
protected

Parent serializer type, used for inheriting classes.

Definition at line 72 of file Serializer.hpp.

Constructor & Destructor Documentation

◆ Serializer()

template<typename S >
ecstasy::serialization::Serializer< S >::Serializer ( )
default

Construct a new Serializer.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

◆ ~Serializer()

template<typename S >
ecstasy::serialization::Serializer< S >::~Serializer ( )
overridedefault

Destroy the Serializer.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-06-11)

Member Function Documentation

◆ afterSaveEntity()

template<typename S >
virtual void ecstasy::serialization::Serializer< S >::afterSaveEntity ( RegistryEntity entity)
inlineprotectedvirtual

Optional method triggered at the end of saveEntity.

Parameters
[in]entityEntity being saved.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-11)

Reimplemented in ecstasy::serialization::JsonSerializer, and ecstasy::serialization::RawSerializer.

Definition at line 465 of file Serializer.hpp.

466 {
467 }

◆ afterUpdateEntity()

template<typename S >
virtual void ecstasy::serialization::Serializer< S >::afterUpdateEntity ( RegistryEntity entity)
inlineprotectedvirtual

Optional method triggered at the end of updateEntity.

Parameters
[in]entityEntity being updated.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-11)

Reimplemented in ecstasy::serialization::JsonSerializer.

Definition at line 489 of file Serializer.hpp.

490 {
491 }

◆ beforeSaveEntity()

template<typename S >
virtual void ecstasy::serialization::Serializer< S >::beforeSaveEntity ( RegistryEntity entity)
inlineprotectedvirtual

Optional method triggered at the start of saveEntity.

Parameters
[in]entityEntity being saved.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-11)

Reimplemented in ecstasy::serialization::JsonSerializer.

Definition at line 453 of file Serializer.hpp.

454 {
455 }

◆ beforeUpdateEntity()

template<typename S >
virtual void ecstasy::serialization::Serializer< S >::beforeUpdateEntity ( RegistryEntity entity)
inlineprotectedvirtual

Optional method triggered at the start of updateEntity.

Parameters
[in]entityEntity being updated.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-11)

Reimplemented in ecstasy::serialization::JsonSerializer.

Definition at line 477 of file Serializer.hpp.

478 {
479 }

◆ exportBytes()

template<typename S >
std::string ecstasy::serialization::Serializer< S >::exportBytes ( ) const
inlineoverridevirtual

Export the serializer content to a string.

Note
Calls the exportStream method with a stringstream.
Returns
std::string String representation of the serializer content.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-09-12)

Implements ecstasy::serialization::ISerializer.

Definition at line 142 of file Serializer.hpp.

143 {
144 std::stringstream stream;
145
146 exportStream(stream);
147 return stream.str();
148 }
virtual void exportStream(std::ostream &stream) const =0
Export the serializer content to a stream.
T str(T... args)

◆ exportFile()

template<typename S >
void ecstasy::serialization::Serializer< S >::exportFile ( const std::filesystem::path filename) const
inlineoverridevirtual

Export the serializer content to a file.

Note
Calls the exportStream method with a file stream.
Parameters
[in]filenamePath to the file to export the serializer content to.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-09-12)

Implements ecstasy::serialization::ISerializer.

Definition at line 134 of file Serializer.hpp.

135 {
136 std::ofstream fstream(filename);
137
138 exportStream(fstream);
139 }

◆ hasEntityComponentSerializer()

template<typename S >
template<typename T >
static bool ecstasy::serialization::Serializer< S >::hasEntityComponentSerializer ( )
inlinestatic

Get the registered components of the serializer.

Template Parameters
TType of the component to get.
Returns
bool True if the component is registered, false otherwise.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-25)

Definition at line 421 of file Serializer.hpp.

422 {
423 auto atype = rtti::TypeRegistry::getInstance().find<T>();
424
425 if (atype.has_value())
426 return atype->get().template hasSerializer<S>();
427 return false;
428 }
static TypeRegistry & getInstance() noexcept
Get the Instance object.
OptionalATypeReference find(const T &target) const noexcept
Search for a registered type.

◆ importBytes()

template<typename S >
void ecstasy::serialization::Serializer< S >::importBytes ( const std::string content)
inlineoverridevirtual

Import data from a string into the serializer.

Note
Calls the importStream method with a stringstream.
Parameters
[in]contentString to import.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-09-12)

Implements ecstasy::serialization::ISerializer.

Definition at line 126 of file Serializer.hpp.

127 {
128 std::stringstream stream(content);
129
130 importStream(stream);
131 }
virtual void importStream(std::istream &stream)=0
Import data from a stream into the serializer.

◆ importFile()

template<typename S >
void ecstasy::serialization::Serializer< S >::importFile ( const std::filesystem::path filename)
inlineoverridevirtual

Import data from a file into the serializer.

Note
Calls the importStream method with a file stream.
Parameters
[in]filenamePath to the file to import.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-09-12)

Implements ecstasy::serialization::ISerializer.

Definition at line 118 of file Serializer.hpp.

119 {
120 std::ifstream fstream(filename);
121
122 importStream(fstream);
123 }

◆ inner()

template<typename S >
constexpr S & ecstasy::serialization::Serializer< S >::inner ( )
inlineconstexpr

Get a reference to the inner serializer.

Returns
constexpr S& Reference to the inner serializer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 112 of file Serializer.hpp.

113 {
114 return reinterpret_cast<S &>(*this);
115 }

◆ load()

template<typename S >
U ecstasy::serialization::Serializer< S >::load ( )
inline

Load an object from the serializer.

Note
This construct a new object and consume the associated data from the serializer.
Template Parameters
UType of the object to load.
Returns
U Loaded object.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 259 of file Serializer.hpp.

260 {
261 if constexpr (traits::has_load_impl_for_type_v<S, U>)
262 return inner().template loadImpl<U>();
263 else if constexpr (is_constructible<U>) {
264 return U(inner());
265 } else {
266 U object;
267 inner().update(object);
268 return object;
269 }
270 }
constexpr S & inner()
Get a reference to the inner serializer.

◆ loadComponentSerializer()

template<typename S >
virtual OptionalEntityComponentSerializer ecstasy::serialization::Serializer< S >::loadComponentSerializer ( )
protectedpure virtual

Load the next component serializer from the stream.

Note
Return std::nullopt_t if no more components are available.
Returns
OptionalEntityComponentSerializer Optional reference to the next component serializer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-04)

Implemented in ecstasy::serialization::JsonSerializer, and ecstasy::serialization::RawSerializer.

◆ loadEntity()

template<typename S >
RegistryEntity ecstasy::serialization::Serializer< S >::loadEntity ( Registry registry)
inline

Load an entity component from the serializer.

Parameters
[in]registryRegistry to load the component to.
Returns
RegistryEntity Loaded entity.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-04)

Definition at line 282 of file Serializer.hpp.

283 {
284 RegistryEntity entity(registry.entityBuilder().build(), registry);
285
286 updateEntity(entity);
287 return entity;
288 }
S & updateEntity(RegistryEntity &entity)
Update all registered components of an entity from the serializer.

◆ operator<<()

template<typename S >
template<typename U >
S & ecstasy::serialization::Serializer< S >::operator<< ( const U &  object)
inline

Operator overload to simplify the save method.

Template Parameters
UType of the object to save.
Parameters
[in]objectObject to save.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 365 of file Serializer.hpp.

366 {
367 return inner().save(object);
368 }

◆ operator>>()

template<typename S >
template<typename U >
S & ecstasy::serialization::Serializer< S >::operator>> ( U &  object)
inline

Operator overload to simplify the update method.

Template Parameters
UType of the object to update.
Parameters
[in]objectObject to update.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 383 of file Serializer.hpp.

384 {
385 return inner().update(object);
386 }

◆ registerComponent()

template<typename S >
template<typename C >
static IEntityComponentSerializer & ecstasy::serialization::Serializer< S >::registerComponent ( std::string_view  name)
inlinestatic

Register a component to this serializer type.

This is required for calls to saveEntity calls without explicit component types.

Template Parameters
CComponent type to register.
Parameters
[in]nameName of the component type.
Returns
IEntityComponentSerializer& Reference to the registered serializer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-04)

Definition at line 403 of file Serializer.hpp.

404 {
405 rtti::AType &type = rtti::TypeRegistry::getInstance().registerType<C>(name);
406
407 return dynamic_cast<rtti::Type<C> &>(type).template registerSerializer<S>();
408 }
AType & registerType(std::string_view name)
Register a type in the registry.

◆ save()

template<typename S >
template<typename U >
requires concepts::has_extraction_operator<S, U> || traits::has_save_impl_for_type_v<S, U>
S & ecstasy::serialization::Serializer< S >::save ( const U &  object)
inline

Save an object to the serializer.

Template Parameters
UType of the object to save.
Parameters
[in]objectObject to save.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 164 of file Serializer.hpp.

165 {
166 if constexpr (traits::has_save_impl_for_type_v<S, U>)
167 return inner().saveImpl(object);
168 else
169 return object >> inner();
170 }

◆ saveEntity() [1/2]

template<typename S >
template<typename... Cs>
S & ecstasy::serialization::Serializer< S >::saveEntity ( const RegistryEntity entity)
inline

Save an entity to the serializer with explicit components.

Template Parameters
CsComponents to save.
Parameters
[in]entityEntity to save.
Returns
S& Reference to this for chain calls.
Exceptions
std::out_of_rangeIf the entity does not have one of the components.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-06-11)

Definition at line 206 of file Serializer.hpp.

207 {
208 S &s = inner();
209 (saveEntityComponent(entity.get<Cs>()), ...);
210 return s;
211 }
S & saveEntityComponent(const C &component)
Save an entity component to the serializer.

◆ saveEntity() [2/2]

template<typename S >
S & ecstasy::serialization::Serializer< S >::saveEntity ( RegistryEntity entity)
inline

Save all registered components of an entity to the serializer.

Note
See registerComponent for registering components.
Parameters
[in]entityEntity to save.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-04)

Definition at line 225 of file Serializer.hpp.

226 {
227 beforeSaveEntity(entity);
228 auto storages = entity.getRegistry().getEntityStorages(entity);
229
230 for (IStorage &storage : storages) {
231 auto optional_type = ecstasy::rtti::TypeRegistry::getInstance().find(storage.getComponentTypeInfos());
232
233 if (optional_type.has_value()) {
234 auto serializer = optional_type->get().tryGetSerializer<S>();
235
236 if (serializer.has_value())
237 serializer->get().save(*this, storage, entity);
238 }
239 }
240 afterSaveEntity(entity);
241 return inner();
242 }
virtual void beforeSaveEntity(RegistryEntity &entity)
Optional method triggered at the start of saveEntity.
virtual void afterSaveEntity(RegistryEntity &entity)
Optional method triggered at the end of saveEntity.

◆ saveEntityComponent()

template<typename S >
template<typename C >
S & ecstasy::serialization::Serializer< S >::saveEntityComponent ( const C &  component)
inline

Save an entity component to the serializer.

This includes the component type before the component data.

Template Parameters
CComponent type.
Parameters
[in]componentComponent to save.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-06-11)

Definition at line 186 of file Serializer.hpp.

187 {
188 return inner() << typeid(C) << component;
189 }

◆ update()

template<typename S >
template<typename U >
requires traits::has_update_impl_for_type_v<S, U> || std::is_fundamental_v<U> || concepts::has_insertion_operator<S, U>
S & ecstasy::serialization::Serializer< S >::update ( U &  object)
inline

Update an existing object from the serializer.

Note
If the object is fundamental, it will use the assignment operator, otherwise the << operator is expected to be implemented in U.
Template Parameters
UType of the object to update.
Parameters
[in]objectExisting object to update.
Returns
S& Reference to this for chain calls.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 308 of file Serializer.hpp.

309 {
310 if constexpr (traits::has_update_impl_for_type_v<S, U>)
311 return inner().updateImpl(object);
312 else {
313 if constexpr (std::is_fundamental_v<U>)
314 object = inner().template load<U>();
315 else
316 object << inner();
317 return inner();
318 }
319 }

◆ updateEntity()

template<typename S >
S & ecstasy::serialization::Serializer< S >::updateEntity ( RegistryEntity entity)
inline

Update all registered components of an entity from the serializer.

Note
Missing components are loaded, existing components are updated.
Parameters
[in]entityEntity to update.
Returns
S& Reference to this for chain calls.
Exceptions
std::out_of_rangeIf an entity component is not registered in the serializer.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-10-04)

Definition at line 335 of file Serializer.hpp.

336 {
337 beforeUpdateEntity(entity);
339
340 while (component.has_value()) {
341 rtti::AType &type = rtti::TypeRegistry::getInstance().get(component->get().getType());
342 IStorage &storage = entity.getRegistry().getStorages().get(std::type_index(type.getStorageTypeInfo()));
343
344 component->get().load(*this, storage, entity);
345
346 component = loadComponentSerializer();
347 }
348 afterUpdateEntity(entity);
349 return inner();
350 }
AType & get(const T &target) const
Get a reference to the AType instance matching the target.
virtual void beforeUpdateEntity(RegistryEntity &entity)
Optional method triggered at the start of updateEntity.
virtual OptionalEntityComponentSerializer loadComponentSerializer()=0
Load the next component serializer from the stream.
virtual void afterUpdateEntity(RegistryEntity &entity)
Optional method triggered at the end of updateEntity.
std::optional< std::reference_wrapper< IEntityComponentSerializer > > OptionalEntityComponentSerializer

Member Data Documentation

◆ is_constructible

template<typename S >
template<typename U >
constexpr bool ecstasy::serialization::Serializer< S >::is_constructible = std::is_constructible_v<U, std::add_lvalue_reference_t<S>>
staticconstexpr

Check if a type is constructible from the serializer.

Note
The type must be constructible from a non const reference to the inner serializer.
Template Parameters
UType to check.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2024-04-30)

Definition at line 86 of file Serializer.hpp.


The documentation for this class was generated from the following file: