Implementing a generic serialization system using typeid()
can be a powerful way to handle complex object hierarchies. Here's an approach to create such a system:
First, define a base class for all serializable objects:
#include <string>
class Serializable {
public:
virtual ~Serializable() = default;
virtual std::string Serialize() const = 0;
virtual void Deserialize(const std::string& data) = 0;
};
Next, create a serialization manager that uses typeid()
to handle different types:
#include <string>
#include <typeinfo>
#include <functional>
#include <memory>
#include <unordered_map>
#include <stdexcept>
class Serializable {/*...*/}
class SerializationManager {
public:
template <typename T>
void RegisterType() {
const std::type_info& type = typeid(T);
serializers_[type.hash_code()]
= [](const Serializable* obj) {
return static_cast<const T*>(obj)
->Serialize();
};
deserializers_[type.hash_code()]
= [](const std::string& data) {
auto obj = std::make_unique<T>();
obj->Deserialize(data);
return obj;
};
}
std::string Serialize(const Serializable* obj) {
const std::type_info& type = typeid(*obj);
auto it = serializers_.find(type.hash_code());
if (it != serializers_.end()) {
return it->second(obj);
}
throw std::runtime_error(
"Type not registered for serialization"
);
}
std::unique_ptr<Serializable> Deserialize(
const std::string& data,
const std::type_info& type
) {
auto it = deserializers_.find(type.hash_code());
if (it != deserializers_.end()) {
return it->second(data);
}
throw std::runtime_error(
"Type not registered for deserialization"
);
}
private:
std::unordered_map<size_t, std::function<
std::string(const Serializable*)>>
serializers_;
std::unordered_map<size_t, std::function<
std::unique_ptr<Serializable>(const std::string&)>>
deserializers_;
};
Let's implement some serializable classes and use our system:
#include <functional>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <unordered_map>
class Serializable {/*...*/}
class SerializationManager {/*...*/}
class Player : public Serializable {
public:
Player(const std::string& name = "", int level = 0)
: name_(name), level_(level) {}
std::string Serialize() const override {
return name_ + "," + std::to_string(level_);
}
void Deserialize(const std::string& data) override {
size_t comma = data.find(',');
name_ = data.substr(0, comma);
level_ = std::stoi(data.substr(comma + 1));
}
void Print() const {
std::cout << "Player: " << name_
<< ", Level: " << level_ << "\n";
}
private:
std::string name_;
int level_;
};
class Monster : public Serializable {
public:
Monster(const std::string& type = "", int hp = 0)
: type_(type), hp_(hp) {}
std::string Serialize() const override {
return type_ + "," + std::to_string(hp_);
}
void Deserialize(const std::string& data) override {
size_t comma = data.find(',');
type_ = data.substr(0, comma);
hp_ = std::stoi(data.substr(comma + 1));
}
void Print() const {
std::cout << "Monster: " << type_
<< ", HP: " << hp_ << "\n";
}
private:
std::string type_;
int hp_;
};
int main() {
SerializationManager manager;
manager.RegisterType<Player>();
manager.RegisterType<Monster>();
Player player("Alice", 10);
Monster monster("Dragon", 100);
std::string playerData = manager.Serialize(&player);
std::string monsterData = manager.Serialize(&monster);
std::cout << "Serialized player: "
<< playerData << "\n";
std::cout << "Serialized monster: "
<< monsterData << "\n";
auto deserializedPlayerPtr = manager.Deserialize(
playerData, typeid(Player));
auto deserializedMonsterPtr = manager.Deserialize(
monsterData, typeid(Monster));
auto deserializedPlayer = dynamic_cast<Player*>(
deserializedPlayerPtr.get());
auto deserializedMonster = dynamic_cast<Monster*>(
deserializedMonsterPtr.get());
if (deserializedPlayer) deserializedPlayer->Print();
if (deserializedMonster) deserializedMonster->Print();
}
Serialized player: Alice,10
Serialized monster: Dragon,100
Player: Alice, Level: 10
Monster: Dragon, HP: 100
This system uses typeid()
to identify object types and select the appropriate serialization and deserialization functions. It's flexible and can handle complex hierarchies, but keep in mind that it relies on RTTI and may have performance implications in large-scale applications.
Answers to questions are automatically generated and may not have been reviewed.
typeid()
Learn to identify and react to object types at runtime using RTTI, dynamic casting and the typeid()
operator