I am reading other's code and there is a serialization version of this class:
struct ObjectInfo
{
int32_t m_typeId;
string m_objectName;
vector<int32_t> m_haveKeysId;
map<int32_t,double> m_objectFeatures;
ObjectInfo():m_typeId(-1),m_objectName("")
{
m_objectFeatures.clear();
m_haveKeysId.clear();
}
}
The binary version of it is the following:
struct ObjectInfo_B
{
int32_t m_typeId;
int32_t m_objectNamePos;
int32_t m_startIndex;
int32_t m_endIndex;
int32_t m_haveKeysIdStartIndex;
int32_t m_haveKeysIdEndIndex;
ObjectInfo_B()
{
m_typeId = -1;
m_objectNamePos = 0;
m_startIndex = -1;
m_endIndex = -1;
m_haveKeysIdStartIndex = -1;
m_haveKeysIdEndIndex = -1;
}
Then there is a vector of ObjectInfo:
vector<ObjectInfo> *objectsVec;
ObjectInfo_B *bObjects;
...
Now the code to convert is like below:
startIndex = 0;
int32_t curBufferSize = 0;
for(size_t i = 0;i<objectsVec->size();i++)
{
bObjects[i].m_typeId = (*objectsVec)[i].m_typeId;
bObjects[i].m_objectNamePos = curBufferSize;
strcpy(m_objectNameBuffer+curBufferSize,(*objectsVec)[i].m_objectName.c_str());
curBufferSize += (*objectsVec)[i].m_objectName.size() + 1;
bObjects[i].m_startIndex = startIndex;
bObjects[i].m_endIndex = startIndex + (*objectsVec)[i].m_objectFeatures.size();
startIndex = bObjects[i].m_endIndex;
bObjects[i].m_haveKeysIdStartIndex = haveKeyStartIndex;
bObjects[i].m_haveKeysIdEndIndex = haveKeyStartIndex +(*objectsVec)[i].m_haveKeysId.size();
...
fwrite((char*)bObjects,sizeof(ObjectInfo_B),wcount,output);
This seems to be very complicated, and I am not farmilaria with serialization. Is there an easier way to do it in C++? A quick search indicates that this below can do similar things, but can it do the conversion for the above code in a much simpler way?
https://www.boost.org/doc/libs/1_37_0/libs/serialization/doc/index.html