CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::SerializationContext Class Reference

A context that is shared between multiple serializer and deserializer instances. More...

#include <CeresEngine/Serialization/Serialization.hpp>

Classes

struct  CustomSerialization
 Holds data for custom serialization for a custom serializer/deserializer. More...
 

Public Member Functions

 SerializationContext () noexcept=default
 Creates a new root serialization context.
 
 SerializationContext (const SerializationContext &parent)
 Creates a new serialization sub-context with the given parent.
 
template<typename T >
void addConstructor (Constructor &&constructor)
 Registers a new constructor for type T.
 
Box construct (SerializationState &state, const Type &type) const
 Constructs a new instance of the given type.
 
template<typename T >
Box construct (SerializationState &state) const
 Constructs a new instance of the given type T.
 
template<typename T >
void addPropertyMigration (const StringView propertyName, PropertyMigration &&migration)
 Adds a new property migration for the given propertyName for type T.
 
template<typename T , typename CustomSerializer , typename CustomDeserializer >
void addCustomSerialization (CustomSerializer &&customSerializer, CustomDeserializer &&customDeserializer)
 Registers a new custom serializer for type T.
 
template<typename T , typename CustomSerializer , typename CustomDeserializer >
void addCustomSerialization (const CustomSerializationOptions &options, CustomSerializer &&customSerializer, CustomDeserializer &&customDeserializer)
 Registers a new custom serializer for type T.
 
void addCustomSerialization (const Type &type, CustomSerializer &&customSerializer, CustomDeserializer &&customDeserializer)
 Registers a new custom serializer for type.
 
void addCustomSerialization (const Type &type, const CustomSerializationOptions &options, CustomSerializer &&customSerializer, CustomDeserializer &&customDeserializer)
 Registers a new custom serializer for type.
 
const CustomSerializationOptionsgetCustomSerializationOptions (const Type &type) const
 Gets a custom serializer/deserializer for the given type, if any exists.
 
const CustomSerializergetCustomSerializer (const Type &type) const
 Gets a custom serializer for the given type, if any exists.
 
const CustomDeserializergetCustomDeserializer (const Type &type) const
 Gets a custom deserializer for the given type, if any exists.
 
void willSerialize (const Box &object) const
 Fires the willSerialize callback for the given object.
 
void didSerialize (const Box &object) const
 Fires the didSerialize callback for the given object.
 
void willSerializeProperty (const Box &object, const ClassProperty &property) const
 Fires the willSerializeProperty callback for the given property of the object.
 
void didSerializeProperty (const Box &object, const ClassProperty &property) const
 Fires the didSerializeProperty callback for the given property of the object.
 
void willDeserialize (const Box &object) const
 Fires the willDeserialize callback for the given object.
 
void didDeserialize (const Box &object) const
 Fires the didDeserialize callback for the given object.
 
void willDeserializeProperty (const Box &object, const ClassProperty &property) const
 Fires the willDeserializeProperty callback for the given property of the object.
 
void didDeserializeProperty (const Box &object, const ClassProperty &property) const
 Fires the didDeserializeProperty callback for the given property of the object.
 

Static Public Member Functions

static const SerializationContextgetDefault ()
 Gets the default serialization context to be used in case the user doesn't provide one himself.
 

Private Types

using Constructor = DeserializerConstructor
 A type that represents the constructor function.
 
using PropertyMigration = DeserializerPropertyMigrator
 Represents the signature of the function that performs migration of an abandoned serialized property.
 
template<typename Func >
using ForEachContextReturnType = std::invoke_result_t< Func, const SerializationContext & >
 Determines the return type for the forEachContext call.
 

Private Member Functions

template<typename Func >
ForEachContextReturnType< FuncforEachContext (Func &&func, const ForEachContextReturnType< Func > &defaultValue={}) const
 Executes the given function on each context until there are no more parent contexts or func returns a truthy value.
 
template<typename K , typename V , typename Hash , typename KeyEqual , typename RawAllocator >
const VrecursiveLookup (HashMap< K, V, Hash, KeyEqual, RawAllocator > SerializationContext::*ptr, const K &key) const
 Recursively lookups the given hash map in this context and all parents until the root.
 

Private Attributes

const SerializationContextmParent = nullptr
 The parent context.
 
HashMap< TypeID, ConstructormConstructors
 A hash map that indexes constructors by type.
 
HashMap< TypeID, HashMap< String, PropertyMigration > > mPropertyMigrations
 
HashMap< TypeID, CustomSerializationmCustomSerialization
 A map of registered custom serialization functions.
 

Detailed Description

A context that is shared between multiple serializer and deserializer instances.

Member Typedef Documentation

◆ Constructor

A type that represents the constructor function.

◆ ForEachContextReturnType

Determines the return type for the forEachContext call.

◆ PropertyMigration

Represents the signature of the function that performs migration of an abandoned serialized property.

Constructor & Destructor Documentation

◆ SerializationContext() [1/2]

CeresEngine::SerializationContext::SerializationContext ( )
explicitdefaultnoexcept

Creates a new root serialization context.

◆ SerializationContext() [2/2]

CeresEngine::SerializationContext::SerializationContext ( const SerializationContext parent)
inline

Creates a new serialization sub-context with the given parent.

Member Function Documentation

◆ addConstructor()

template<typename T >
void CeresEngine::SerializationContext::addConstructor ( Constructor &&  constructor)
inline

Registers a new constructor for type T.

Template Parameters
TThe type to register a constructor for.
Parameters
constructorA function to be called whenever a new instance of type T is required by the serializer.

◆ addCustomSerialization() [1/4]

void CeresEngine::SerializationContext::addCustomSerialization ( const CustomSerializationOptions options,
CustomSerializer &&  customSerializer,
CustomDeserializer &&  customDeserializer 
)
inline

Registers a new custom serializer for type T.

Template Parameters
TThe type to register a new custom serializer/deserializer for.
Parameters
optionsA structure that contains options for the custom serializer.
customSerializerThe custom serializer function.
customDeserializerThe custom deserializer function.

◆ addCustomSerialization() [2/4]

void CeresEngine::SerializationContext::addCustomSerialization ( const Type type,
const CustomSerializationOptions options,
CustomSerializer &&  customSerializer,
CustomDeserializer &&  customDeserializer 
)

Registers a new custom serializer for type.

Parameters
typeThe type to register a new custom serializer/deserializer for.
optionsA structure that contains options for the custom serializer.
customSerializerThe custom serializer function.
customDeserializerThe custom deserializer function.

◆ addCustomSerialization() [3/4]

void CeresEngine::SerializationContext::addCustomSerialization ( const Type type,
CustomSerializer &&  customSerializer,
CustomDeserializer &&  customDeserializer 
)
inline

Registers a new custom serializer for type.

Parameters
typeThe type to register a new custom serializer/deserializer for.
customSerializerThe custom serializer function.
customDeserializerThe custom deserializer function.

◆ addCustomSerialization() [4/4]

void CeresEngine::SerializationContext::addCustomSerialization ( CustomSerializer &&  customSerializer,
CustomDeserializer &&  customDeserializer 
)
inline

Registers a new custom serializer for type T.

Template Parameters
TThe type to register a new custom serializer/deserializer for.
Parameters
customSerializerThe custom serializer function.
customDeserializerThe custom deserializer function.

◆ addPropertyMigration()

template<typename T >
void CeresEngine::SerializationContext::addPropertyMigration ( const StringView  propertyName,
PropertyMigration &&  migration 
)
inline

Adds a new property migration for the given propertyName for type T.

Template Parameters
TThe type of the object to add a new migration for.
Parameters
propertyNameThe name of the property to execute the migration with.
migrationA function-like object that executes the migration for the property.

◆ construct() [1/2]

template<typename T >
Box CeresEngine::SerializationContext::construct ( SerializationState state) const
inline

Constructs a new instance of the given type T.

◆ construct() [2/2]

Box CeresEngine::SerializationContext::construct ( SerializationState state,
const Type type 
) const

Constructs a new instance of the given type.

◆ didDeserialize()

void CeresEngine::SerializationContext::didDeserialize ( const Box object) const

Fires the didDeserialize callback for the given object.

◆ didDeserializeProperty()

void CeresEngine::SerializationContext::didDeserializeProperty ( const Box object,
const ClassProperty property 
) const

Fires the didDeserializeProperty callback for the given property of the object.

◆ didSerialize()

void CeresEngine::SerializationContext::didSerialize ( const Box object) const

Fires the didSerialize callback for the given object.

◆ didSerializeProperty()

void CeresEngine::SerializationContext::didSerializeProperty ( const Box object,
const ClassProperty property 
) const

Fires the didSerializeProperty callback for the given property of the object.

◆ forEachContext()

template<typename Func >
ForEachContextReturnType< Func > CeresEngine::SerializationContext::forEachContext ( Func &&  func,
const ForEachContextReturnType< Func > &  defaultValue = {} 
) const
private

Executes the given function on each context until there are no more parent contexts or func returns a truthy value.

For most cases, func should return an Optional, but pointers or pointer-like objects are valid return types.

Parameters
funcThe function to be called.
defaultValueThe default value to be returned if all calls to func() returned falsy values.
Returns
The return value of the last call that returned a truthy value or the given defaultValue.

◆ getCustomDeserializer()

const CustomDeserializer & CeresEngine::SerializationContext::getCustomDeserializer ( const Type type) const

Gets a custom deserializer for the given type, if any exists.

If no custom deserializer exists, returns a nullptr callable.

Parameters
typeThe type to get a custom deserializer for.
Returns
The custom deserializer function.

◆ getCustomSerializationOptions()

const CustomSerializationOptions & CeresEngine::SerializationContext::getCustomSerializationOptions ( const Type type) const

Gets a custom serializer/deserializer for the given type, if any exists.

If no custom serializer or deserializer exists, returns a default one.

Parameters
typeThe type to get a custom serialization options for.
Returns
The custom serialization options.

◆ getCustomSerializer()

const CustomSerializer & CeresEngine::SerializationContext::getCustomSerializer ( const Type type) const

Gets a custom serializer for the given type, if any exists.

If no custom serializer exists, returns a nullptr callable.

Parameters
typeThe type to get a custom serializer for.
Returns
The custom serializer function.

◆ getDefault()

static const SerializationContext & CeresEngine::SerializationContext::getDefault ( )
static

Gets the default serialization context to be used in case the user doesn't provide one himself.

◆ recursiveLookup()

const V * CeresEngine::SerializationContext::recursiveLookup ( HashMap< K, V, Hash, KeyEqual, RawAllocator > SerializationContext::*  ptr,
const K key 
) const
private

Recursively lookups the given hash map in this context and all parents until the root.

If a match is found, returns a reference to it's value.

Parameters
ptrA member pointer to the hash map to perform the lookup on.
keyThe key value to be looked up.
Returns
The matching value or an empty optional if no such key exists.

◆ willDeserialize()

void CeresEngine::SerializationContext::willDeserialize ( const Box object) const

Fires the willDeserialize callback for the given object.

◆ willDeserializeProperty()

void CeresEngine::SerializationContext::willDeserializeProperty ( const Box object,
const ClassProperty property 
) const

Fires the willDeserializeProperty callback for the given property of the object.

◆ willSerialize()

void CeresEngine::SerializationContext::willSerialize ( const Box object) const

Fires the willSerialize callback for the given object.

◆ willSerializeProperty()

void CeresEngine::SerializationContext::willSerializeProperty ( const Box object,
const ClassProperty property 
) const

Fires the willSerializeProperty callback for the given property of the object.

Member Data Documentation

◆ mConstructors

HashMap<TypeID, Constructor> CeresEngine::SerializationContext::mConstructors
private

A hash map that indexes constructors by type.

◆ mCustomSerialization

HashMap<TypeID, CustomSerialization> CeresEngine::SerializationContext::mCustomSerialization
private

A map of registered custom serialization functions.

◆ mParent

const SerializationContext* CeresEngine::SerializationContext::mParent = nullptr
private

The parent context.

Allows creating nested contexts that specialize from a common parent.

◆ mPropertyMigrations

HashMap<TypeID, HashMap<String, PropertyMigration> > CeresEngine::SerializationContext::mPropertyMigrations
private

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