c++ - Cereal - multiple de-serialization -


i new cereal, , have (possible simple) question:

is there way deserialize multiple objects when don't know number of objects inside (xml) archive?

i tried like:

std::ifstream is("c:\\data.xml"); cereal::xmlinputarchive archive(is);  while (is.good() && !is.eof()) {                    try{         objectin oin;         archive(oin);         objectlist.push_back(oin);     }     catch (exception e){     } } 

let's have 3 objects in xml file , xml receive hasn't containing object number. so, in code, first 3 iteration ok, 4th generates "unhandled exception @ 0x0035395e in cerealtest.exe: 0xc0000005: access violation reading location 0x00000018."

do have suggestion?

let me ask question before trying answer question: if serializing unknown number of items, why not place items in container designed hold variable number of items? use std::vector store objectin , handle number of them. code like:

std::vector<myobjects> vec; {   cereal::xmlinputarchive ar("filename");   ar( vec ); } // in habit of using cereal archives in raii fashion 

the above works number of objects serialized, assuming cereal generated xml begin with. can add or remove elements vector in xml code , work properly.


if insistent on reading unknown number of objects , not placing them in container designed hold variable number of elements, can (but warned not idea - should try change serialization strategy , not this):

{   cereal::xmlinputarchive ar("filename");   try   {     while( true )     {       objectin ob;       ar( ob );       objectlist.push_back(oin);     }   catch( ... )   { } } 

again let me stress fundamentally problem serialization strategy , should serializing container instead of items a-la-carte if don't know how many there be. above code can't handle reading in else, tries blindly read things in until encounters exception. if objects followed naming pattern, use name-value-pairs (cereal::make_nvp) retrieve them name.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -