SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Serializable.hpp
Go to the documentation of this file.
1#ifndef _SERIALIZABLE_H_
2#define _SERIALIZABLE_H_
3
4#include <iostream>
5#include <string>
6#include <memory>
7#include <unordered_map>
8#include <any>
9#include <stdexcept>
10#include <nlohmann/json.hpp>
11#include <yaml-cpp/yaml.h>
12
13namespace Sleak {
14
15 // Forward declarations
17
18 /// File format a Serializable can be saved to or loaded from.
19 /// @ingroup filesystem
23 YAML,
26 };
27
28 /**
29 * @interface ISerializationContext
30 * @brief Interface for serialization contexts.
31 * @ingroup filesystem
32 */
34 public:
35 virtual ~ISerializationContext() = default;
36
37 // Serialization methods
38 virtual void Write(const std::string& key, const std::any& value) = 0;
39 virtual std::any Read(const std::string& key) const = 0;
40
41 // Stream-based methods
42 virtual void WriteToStream(std::ostream& stream) const = 0;
43 virtual void ReadFromStream(std::istream& stream) = 0;
44 };
45
46 /**
47 * @class Serializable
48 * @brief Base class for objects that can be serialized and deserialized.
49 * @ingroup filesystem
50 */
52 public:
53 virtual ~Serializable() = default;
54
55 /**
56 * @brief Serializes the object using the specified context.
57 * @param context The serialization context.
58 */
59 virtual void Serialize(std::unique_ptr<ISerializationContext> context) const = 0;
60
61 /**
62 * @brief Deserializes the object using the specified context.
63 * @param context The deserialization context.
64 */
65 virtual void Deserialize(std::unique_ptr<ISerializationContext> context) = 0;
66
67 /**
68 * @brief Serializes the object to a file.
69 * @param filePath The path to the file where the object will be saved.
70 */
71 void SerializeToFile(const std::string& filePath) const;
72
73 /**
74 * @brief Deserializes the object from a file.
75 * @param filePath The path to the file from which the object will be loaded.
76 */
77 void DeserializeFromFile(const std::string& filePath);
78 };
79
80 /**
81 * @class SerializationFactory
82 * @brief Detects file format and creates the appropriate serialization context.
83 * @ingroup filesystem
84 */
86 public:
87 /// Picks a format from filePath's extension (.bin/.json/.yaml/.yml).
88 static SerializationFormat DetectFormat(const std::string& filePath);
89 /// Builds the context implementation matching format.
90 static std::unique_ptr<ISerializationContext> CreateContext(SerializationFormat format);
91 };
92
93} // namespace Sleak
94
95#endif // _SERIALIZABLE_H_
Interface for serialization contexts.
virtual void WriteToStream(std::ostream &stream) const =0
virtual ~ISerializationContext()=default
virtual void Write(const std::string &key, const std::any &value)=0
virtual std::any Read(const std::string &key) const =0
virtual void ReadFromStream(std::istream &stream)=0
Base class for objects that can be serialized and deserialized.
void DeserializeFromFile(const std::string &filePath)
Deserializes the object from a file.
virtual ~Serializable()=default
void SerializeToFile(const std::string &filePath) const
Serializes the object to a file.
virtual void Serialize(std::unique_ptr< ISerializationContext > context) const =0
Serializes the object using the specified context.
virtual void Deserialize(std::unique_ptr< ISerializationContext > context)=0
Deserializes the object using the specified context.
Detects file format and creates the appropriate serialization context.
static std::unique_ptr< ISerializationContext > CreateContext(SerializationFormat format)
Builds the context implementation matching format.
static SerializationFormat DetectFormat(const std::string &filePath)
Picks a format from filePath's extension (.bin/.json/.yaml/.yml).
SerializationFormat
Root namespace for everything the engine exposes.
Definition Camera.hpp:10