Path: /sdk/add_on/serializer/
The CSerializer
implements support for serializing the values of global variables in a module, for example in order to reload a slightly modified version of the script without reinitializing everything. It will resolve primitives and script classes automatically, including references and handles. For application registered types, the application needs to implement callback objects to show how these should be serialized.
The implementation currently has some limitations:
- It can only serialize to memory, i.e. it is not possible to save the values to a file.
- If the variables changed type when restoring, the serializer cannot restore the value.
- The serializer will attempt to backup all objects, but in some cases an application may not want to backup the actual object, but only a reference to it, e.g. an internal application object referenced by the script. Currently there is no way of telling the serializer to do differently in this case.
- If the module holds references to objects from another module it will probably fail in restoring the values.
- Todo:
- Show how to serialize extra objects too. And explain about memory management for restored objects
- Todo:
- Explain that handles to registered types without factories will be kept as-is
- Todo:
- Registered pod-types do not need a special user type as the serializer will simply keep a bitwise copy
Public C++ interface
class CSerializer
{
public:
CSerializer();
~CSerializer();
void AddUserType(CUserType *ref, const std::string &name);
void *GetPointerToRestoredObject(void *originalObject);
};
Example usage
struct CStringType;
struct CArrayType;
{
CSerializer backup;
backup.AddUserType(new CStringType(), "string");
backup.AddUserType(new CArrayType(), "array");
backup.Store(mod);
CompileModule(modName);
backup.Restore(mod);
}
struct CStringType : public CUserType
{
void Store(CSerializedValue *val, void *ptr)
{
val->SetUserData(new std::string(*(std::string*)ptr));
}
void Restore(CSerializedValue *val, void *ptr)
{
std::string *buffer = (std::string*)val->GetUserData();
*(std::string*)ptr = *buffer;
}
void CleanupUserData(CSerializedValue *val)
{
std::string *buffer = (std::string*)val->GetUserData();
delete buffer;
}
};
struct CArrayType : public CUserType
{
void Store(CSerializedValue *val, void *ptr)
{
CScriptArray *arr = (CScriptArray*)ptr;
for( unsigned int i = 0; i < arr->GetSize(); i++ )
val->m_children.push_back(new CSerializedValue(val ,"", "", arr->At(i), arr->GetElementTypeId()));
}
void Restore(CSerializedValue *val, void *ptr)
{
CScriptArray *arr = (CScriptArray*)ptr;
arr->Resize(
asUINT(val->m_children.size()));
for( size_t i = 0; i < val->m_children.size(); ++i )
val->m_children[i]->Restore(arr->At(
asUINT(i)), arr->GetElementTypeId());
}
};