ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::serialization::RawSerializer Class Reference

RawSerializer class. More...

#include <RawSerializer.hpp>

Inheritance diagram for ecstasy::serialization::RawSerializer:
Collaboration diagram for ecstasy::serialization::RawSerializer:

Public Member Functions

 RawSerializer ()=default
 Construct a new Raw Serializer instance.
 
 RawSerializer (const std::string &content)
 Construct a new Raw Serializer and import the content from a string.
 
 RawSerializer (const std::filesystem::path &filename)
 Construct a new Raw Serializer and import the content from a file.
 
 RawSerializer (std::istream &stream)
 Construct a new Raw Serializer and import the content from a stream.
 
 ~RawSerializer () override=default
 Destroy the RawSerializer.
 
void clear () override final
 Clear the serializer content.
 
void resetCursors (bool read=true, bool write=true)
 Reset the read and write cursor to the begining of the stream.
 
void resetReadCursor ()
 Reset the read cursor to the begining of the stream.
 
void resetWriteCursor ()
 Reset the write cursor to the begining of the stream.
 
size_t size () const override final
 Get the size of the serializer content (in bytes).
 
void importStream (std::istream &stream) override final
 Import data from a stream into the serializer.
 
void exportStream (std::ostream &stream) const override final
 Export the serializer content to a stream.
 
template<typename T , typename = typename std::enable_if< (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>) || std::is_bounded_array_v<T> || util::meta::is_std_vector<T>::value || std::is_same_v<T, std::type_info> , int>::type>
requires (!std::is_fundamental_v<T>)
RawSerializersaveImpl (const T &object)
 Save an object to the serializer.
 
template<typename U >
requires std::is_fundamental_v<U>
RawSerializersaveImpl (U object)
 Save an object to the serializer.
 
template<typename U , typename = typename std::enable_if<( std::is_same_v<U, std::string> || std::is_bounded_array_v<U> || util::meta::is_std_vector<U>::value) , int>::type>
RawSerializerupdateImpl (U &object)
 Update an existing object from the serializer.
 
template<typename U >
requires std::is_fundamental_v<U> || std::is_same_v<U, std::string>
loadImpl ()
 Load an object from the serializer.
 
template<typename U >
RawSerializerappendRaw (const U &object)
 Append a type to the serializer as it is in the memory.
 
void write (const char *bytes, size_t size)
 Append a string to the serializer.
 
template<typename U >
loadRaw ()
 Load a raw type from the serializer.
 
std::stringstreamgetStream () noexcept
 Get the string stream of the serializer.
 
- Public Member Functions inherited from ecstasy::serialization::Serializer< RawSerializer >
 Serializer ()=default
 Construct a new Serializer.
 
 ~Serializer () override=default
 Destroy the Serializer.
 
constexpr RawSerializerinner ()
 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.
 
RawSerializersave (const U &object)
 Save an object to the serializer.
 
RawSerializersaveEntityComponent (const C &component)
 Save an entity component to the serializer.
 
RawSerializersaveEntity (const RegistryEntity &entity)
 Save an entity to the serializer with explicit components.
 
RawSerializersaveEntity (RegistryEntity &entity)
 Save all registered components of an entity to the serializer.
 
load ()
 Load an object from the serializer.
 
RegistryEntity loadEntity (Registry &registry)
 Load an entity component from the serializer.
 
RawSerializerupdate (U &object)
 Update an existing object from the serializer.
 
RawSerializerupdateEntity (RegistryEntity &entity)
 Update all registered components of an entity from the serializer.
 
RawSerializeroperator<< (const U &object)
 Operator overload to simplify the save method.
 
RawSerializeroperator>> (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.
 

Private Member Functions

void afterSaveEntity (RegistryEntity &entity) override final
 Optional method triggered at the end of saveEntity.
 
OptionalEntityComponentSerializer loadComponentSerializer () override final
 Load the next component serializer from the stream.
 

Private Attributes

std::stringstream _stream
 Internal string stream used as a buffer.
 

Additional Inherited Members

- Static Public Member Functions inherited from ecstasy::serialization::Serializer< RawSerializer >
static IEntityComponentSerializerregisterComponent (std::string_view name)
 Register a component to this serializer type.
 
static bool hasEntityComponentSerializer ()
 Get the registered components of the serializer.
 
- Static Public Attributes inherited from ecstasy::serialization::Serializer< RawSerializer >
static constexpr bool is_constructible
 Check if a type is constructible from the serializer.
 
- Protected Types inherited from ecstasy::serialization::Serializer< RawSerializer >
using Parent = Serializer< RawSerializer >
 Parent serializer type, used for inheriting classes.
 
using OptionalEntityComponentSerializer = std::optional< std::reference_wrapper< IEntityComponentSerializer > >
 
- Protected Member Functions inherited from ecstasy::serialization::Serializer< RawSerializer >
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

RawSerializer class.

This class is used to serialize and deserialize raw data in a string stream used as a bytes buffer. Fundamental types are serialized as is, and other types are serialized by calling the custom save/update methods.

Note
Some fundamental types are handled by the serializer:
  • fundamental types are saved as is.
  • std::string and std::string_view are saved as a uint32_t size followed by the string bytes.
  • Bounded arrays are saved by iterating over each element and saving them.
  • std::vector are saved as a uint32_t size followed by each element.
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 39 of file RawSerializer.hpp.

Constructor & Destructor Documentation

◆ RawSerializer() [1/4]

ecstasy::serialization::RawSerializer::RawSerializer ( )
default

Construct a new Raw Serializer instance.

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)

◆ RawSerializer() [2/4]

ecstasy::serialization::RawSerializer::RawSerializer ( const std::string content)
inline

Construct a new Raw Serializer and import the content from a string.

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)

Definition at line 57 of file RawSerializer.hpp.

58 {
59 importBytes(content);
60 }
void importBytes(const std::string &content) override
Import data from a string into the serializer.

◆ RawSerializer() [3/4]

ecstasy::serialization::RawSerializer::RawSerializer ( const std::filesystem::path filename)
inlineexplicit

Construct a new Raw Serializer and import the content from a file.

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)

Definition at line 70 of file RawSerializer.hpp.

71 {
72 importFile(filename);
73 }
void importFile(const std::filesystem::path &filename) override
Import data from a file into the serializer.

◆ RawSerializer() [4/4]

ecstasy::serialization::RawSerializer::RawSerializer ( std::istream stream)
inline

Construct a new Raw Serializer and import the content from a stream.

Parameters
[in]streamInput stream.
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)

Definition at line 82 of file RawSerializer.hpp.

83 {
84 importStream(stream);
85 }
void importStream(std::istream &stream) override final
Import data from a stream into the serializer.

◆ ~RawSerializer()

ecstasy::serialization::RawSerializer::~RawSerializer ( )
overridedefault

Destroy the RawSerializer.

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()

void ecstasy::serialization::RawSerializer::afterSaveEntity ( RegistryEntity entity)
inlinefinaloverrideprivatevirtual

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 from ecstasy::serialization::Serializer< RawSerializer >.

Definition at line 329 of file RawSerializer.hpp.

330 {
331 // Notify the end of the entity
332 save<std::size_t>(0);
333 }

◆ appendRaw()

template<typename U >
RawSerializer & ecstasy::serialization::RawSerializer::appendRaw ( const U &  object)
inline

Append a type to the serializer as it is in the memory.

Warning
You must not use this for pointers or references.
Template Parameters
UType of the object to append.
Parameters
[in]objectObject to append.
Returns
RawSerializer& 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 270 of file RawSerializer.hpp.

271 {
272 write(reinterpret_cast<const char *>(&object), sizeof(object));
273 return *this;
274 }
void write(const char *bytes, size_t size)
Append a string to the serializer.

◆ clear()

void ecstasy::serialization::RawSerializer::clear ( )
inlinefinaloverridevirtual

Clear 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 96 of file RawSerializer.hpp.

97 {
98 _stream.str("");
99 }
std::stringstream _stream
Internal string stream used as a buffer.
T str(T... args)

◆ exportStream()

void ecstasy::serialization::RawSerializer::exportStream ( std::ostream stream) const
inlinefinaloverridevirtual

Export the serializer content to a stream.

Parameters
[in]streamStream 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 155 of file RawSerializer.hpp.

156 {
157 std::stringstream &s = const_cast<std::stringstream &>(_stream);
158 std::streampos pos = s.tellg();
159
160 // Read from the begining of the stream
161 s.seekg(0);
162 stream << s.rdbuf();
163 // Replace the read cursor at the original position
164 s.seekg(pos);
165 }
T rdbuf(T... args)
T seekg(T... args)
T tellg(T... args)

◆ getStream()

std::stringstream & ecstasy::serialization::RawSerializer::getStream ( )
inlinenoexcept

Get the string stream of the serializer.

Returns
std::stringstream& Reference to the string stream of 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)

Definition at line 319 of file RawSerializer.hpp.

320 {
321 return _stream;
322 }

◆ importStream()

void ecstasy::serialization::RawSerializer::importStream ( std::istream stream)
inlinefinaloverridevirtual

Import data from a stream into the serializer.

Parameters
[in]streamStream 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 147 of file RawSerializer.hpp.

148 {
149 clear();
150 _stream << stream.rdbuf();
151 _stream.seekp(0);
152 }
void clear() override final
Clear the serializer content.
T seekp(T... args)

◆ loadComponentSerializer()

OptionalEntityComponentSerializer ecstasy::serialization::RawSerializer::loadComponentSerializer ( )
inlinefinaloverrideprivatevirtual

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)

Implements ecstasy::serialization::Serializer< RawSerializer >.

Definition at line 336 of file RawSerializer.hpp.

337 {
338 size_t hash = loadRaw<std::size_t>();
339
340 if (hash == 0)
341 return std::nullopt;
342
343 // Will raise an exception if the serializer is not registered
345 }
ecstasy::serialization::IEntityComponentSerializer & getSerializer() const noexcept
Get a reference to the entity component serializer for the given serializer type.
Definition AType.hpp:175
static TypeRegistry & getInstance() noexcept
Get the Instance object.
AType & get(const T &target) const
Get a reference to the AType instance matching the target.
RawSerializer()=default
Construct a new Raw Serializer instance.

◆ loadImpl()

template<typename U >
requires std::is_fundamental_v<U> || std::is_same_v<U, std::string>
U ecstasy::serialization::RawSerializer::loadImpl ( )
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 242 of file RawSerializer.hpp.

243 {
244 if constexpr (std::is_fundamental_v<U>)
245 return loadRaw<U>();
246 else if constexpr (std::is_same<U, std::string>::value) {
247 uint32_t size = loadRaw<uint32_t>();
250
251 return std::string(_stream.view().data() + pos, size);
252 }
253 }
size_t size() const override final
Get the size of the serializer content (in bytes).

◆ loadRaw()

template<typename U >
U ecstasy::serialization::RawSerializer::loadRaw ( )
inline

Load a raw type from the serializer.

Note
This is the opposite of appendRaw.
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 303 of file RawSerializer.hpp.

304 {
306 const U *result = reinterpret_cast<const U *>(_stream.view().data() + pos);
307 _stream.seekg(pos + std::streamoff(sizeof(U)));
308 return *result;
309 }

◆ resetCursors()

void ecstasy::serialization::RawSerializer::resetCursors ( bool  read = true,
bool  write = true 
)
inline

Reset the read and write cursor to the begining of the stream.

Parameters
[in]readWhether to reset the read cursor.
[in]writeWhether to reset the write cursor.
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)

Definition at line 110 of file RawSerializer.hpp.

111 {
112 if (read)
114 if (write)
116 }
void resetWriteCursor()
Reset the write cursor to the begining of the stream.
void resetReadCursor()
Reset the read cursor to the begining of the stream.

◆ resetReadCursor()

void ecstasy::serialization::RawSerializer::resetReadCursor ( )
inline

Reset the read cursor to the begining of the stream.

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)

Definition at line 124 of file RawSerializer.hpp.

125 {
126 _stream.seekg(0);
127 }

◆ resetWriteCursor()

void ecstasy::serialization::RawSerializer::resetWriteCursor ( )
inline

Reset the write cursor to the begining of the stream.

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)

Definition at line 135 of file RawSerializer.hpp.

136 {
137 _stream.seekp(0);
138 }

◆ saveImpl() [1/2]

template<typename T , typename = typename std::enable_if< (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>) || std::is_bounded_array_v<T> || util::meta::is_std_vector<T>::value || std::is_same_v<T, std::type_info> , int>::type>
requires (!std::is_fundamental_v<T>)
RawSerializer & ecstasy::serialization::RawSerializer::saveImpl ( const T &  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 178 of file RawSerializer.hpp.

179 {
180 if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>) {
181 save(static_cast<uint32_t>(object.size()));
182 _stream << object;
183 } else if constexpr (std::is_bounded_array_v<T>) {
184 for (size_t i = 0; i < std::extent_v<T>; i++)
185 save(object[i]);
186 } else if constexpr (util::meta::is_std_vector<T>::value) {
187 *this << object.size();
188 for (const auto &elem : object)
189 *this << elem;
190 } else if constexpr (std::is_same_v<T, std::type_info>) {
191 save(rtti::TypeRegistry::getInstance().get(object).getHash());
192 } else {
193 return Parent::save(object);
194 }
195 return *this;
196 }
RawSerializer & save(const U &object)
Save an object to the serializer.
Check if a type is a std::vector.

◆ saveImpl() [2/2]

template<typename U >
requires std::is_fundamental_v<U>
RawSerializer & ecstasy::serialization::RawSerializer::saveImpl ( 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 201 of file RawSerializer.hpp.

202 {
203 write(reinterpret_cast<const char *>(&object), sizeof(object));
204 return *this;
205 }

◆ size()

size_t ecstasy::serialization::RawSerializer::size ( ) const
inlinefinaloverridevirtual

Get the size of the serializer content (in bytes).

Returns
size_t Size of the serializer content in bytes.
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 141 of file RawSerializer.hpp.

142 {
143 return _stream.str().size();
144 }

◆ updateImpl()

template<typename U , typename = typename std::enable_if<( std::is_same_v<U, std::string> || std::is_bounded_array_v<U> || util::meta::is_std_vector<U>::value) , int>::type>
RawSerializer & ecstasy::serialization::RawSerializer::updateImpl ( 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 217 of file RawSerializer.hpp.

218 {
219 if constexpr (std::is_same_v<U, std::string>)
220 object = load<U>();
221 else if constexpr (std::is_bounded_array_v<U>)
222 for (size_t i = 0; i < std::extent_v<U>; i++)
223 update(object[i]);
224 else if constexpr (util::meta::is_std_vector<U>::value) {
225 auto size = load<decltype(object.size())>();
226 object.clear();
227 object.reserve(size);
228 for (size_t i = 0; i < size; i++) {
229 if constexpr (is_constructible<typename U::value_type>)
230 object.emplace_back(*this);
231 else
232 object.push_back(std::move(load<typename U::value_type>()));
233 }
234 } else
235 return Parent::update(object);
236 return *this;
237 }
RawSerializer & update(U &object)
Update an existing object from the serializer.
U load()
Load an object from the serializer.

◆ write()

void ecstasy::serialization::RawSerializer::write ( const char *  bytes,
size_t  size 
)
inline

Append a string to the serializer.

Parameters
[in]bytesBytes to append.
[in]sizeSize of the bytes buffer to append.
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 285 of file RawSerializer.hpp.

286 {
287 _stream.write(bytes, static_cast<std::streamsize>(size));
288 }
T write(T... args)

Member Data Documentation

◆ _stream

std::stringstream ecstasy::serialization::RawSerializer::_stream
private

Internal string stream used as a buffer.

Definition at line 326 of file RawSerializer.hpp.


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