Game Development Reference
In-Depth Information
The SerializationManager class stores pointers to Serializable objects in an unordered_map . Each
of the Serializable objects will be given a unique ID that is used as the key in this collection. The
file name we would like to use for the save file is stored in the m_filename variable.
There are three methods used to manage the objects that are handled by the SerializationManager
class. The RegisterSerializable , RemoveSerializable , and GetSerializable methods are shown
in Listing 24-2.
Listing 24-2. The RegisterSerializable , RemoveSerializable , and GetSerializable Methods
void SerializationManager::RegisterSerializable(Serializable* pSerializable)
{
assert(m_serializables.find(pSerializable->GetId()) == m_serializables.end());
m_serializables.emplace{ pSerializable->GetId(), pSerializable };
}
void SerializationManager::RemoveSerializable(Serializable* pSerializable)
{
auto iter = m_serializables.find(pSerializable->GetId());
if (iter != m_serializables.end())
{
m_serializables.erase(iter);
}
}
Serializable* SerializationManager::GetSerializable(unsigned int serializableId) const
{
Serializable* pSerializable{ nullptr };
auto iter = m_serializables.find(serializableId);
if (iter != m_serializables.end())
{
pSerializable = iter->second;
}
return pSerializable;
}
These methods are all fairly straightforward and manage adding, removing, and retrieving
Serializable addresses from the m_serializables unordered_map .
The Save method is responsible for looping over all of the Serializable objects and asking them to
write their data to an ofstream object. Listing 24-3 shows the Save method and how the ofstream
object is initialized and moved.
Listing 24-3. The SerializableManager::Save
void SerializationManager::Save()
{
std::ofstream file{ m_filename };
file << true;
file << std::endl;
 
Search WWH ::




Custom Search