I would like to serialize a class with an attribute as a list of pointers to a common class
This is the parent class from which the generic class is generated:
class Base{
public :
friend class boost::serialization::access;
virtual ~Base(){}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
}
virtual string Getid() = 0 ;
};
General class:
template<typename T>
class GenericBase : public Base
{
public:
friend class boost::serialization::access;
GenericBase<T>(string id){}
~GenericBase(){}
string id;
vector<T> data
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Base>(*this);
ar & BOOST_SERIALIZATION_NVP( id);
ar & BOOST_SERIALIZATION_NVP( data);
}
string Getid() { return id; }
};
The class I want to serialize
class Use
{
public:
friend class boost::serialization::access;
int Id;
map<string, Base*> BaseDatas;
Use();
~Use();
};
So, after reading the serialization extension document ( http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#derivedpointers ), I tried this in the serialization code:
main(){
Use u = Use();
std::ofstream ofs(filename, ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa.template register_type<GenericBase<Type1> >();
oa.template register_type<GenericBase<Type2> >();
oa.template register_type<GenericBase<Type3> >();
oa<<u;
}
I got a message
error: 'template' (as disambiguator) is allowed only inside templates
so i replaced
oa.template register_type> ();
oa.register_type ();
it worked and I was able to save the text and binary (I checked the data)
for download now I just used these lines:
main(){
Use u;
std::ifstream ifs(filename, ios::binary);
ia.register_type<GenericBase<Type1> >();
boost::archive::binary_iarchive ia(ifs);
ia>>u;
}
this made me wrong:
error: GenericBase:: GenericBase() '
- , 2 , http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#constructors
namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(
Archive & ar, const my_class * t, const unsigned int file_version)
{
ar << t->m_attribute;
}
template<class Archive>
inline void load_construct_data(
Archive & ar, my_class * t, const unsigned int file_version)
{
int attribute;
ar >> attribute;
::new(t)my_class(attribute);
}
}}
? Use?
map<string, Base*> BaseDatas;
?
;)