|
Boost : |
Subject: [boost] [Serialization] deserialize stl container,complier error
From: Simon Cheung (zhiliangz_at_[hidden])
Date: 2009-11-26 20:43:30
Hi all,
boost:1.4.0
compiler: vs 2005
code:
#define Data_Io_Load_Save(class_name, members) \
friend class boost::serialization::access; \
template<class Archive> \
void serialize(Archive & ar, const unsigned int version) \
{ ar members; }
#define Data_Io_Load_Save_Base(base_class)
BOOST_SERIALIZATION_BASE_OBJECT_NVP(base_class)
#define Auto_Value_Decl(type) BOOST_SERIALIZATION_NVP(type)
struct TestStruct
{
int type;
int ina[10];
std::map<std::string, std::string> mapstr;
Data_Io_Load_Save(TestStruct, &Auto_Value_Decl(type)
&Auto_Value_Decl(ina)
&Auto_Value_Decl(mapstr)
);
};
void TestSerial()
{
TestStruct ts, its;
ts.type = 2222;
ts. mapstr ["str1"] = "str1";
ts. mapstr ["str2"] = "str2";
ts. mapstr ["str3"] = "str3";
ts. mapstr ["str4"] = "str4";
// save
std::ofstream ofs("proto.txt");
boost::archive::xml_oarchive oa(ofs);
oa << Auto_Value_Decl(ts. mapstr); //
oa << Auto_Value_Decl(ts); //
ofs.close();
//load
std::ifstream ifs("proto.txt");
boost::archive::xml_iarchive ia(ifs);
ts.mapint.clear();
ia & Auto_Value_Decl(ts. mapstr); // error
ia & Auto_Value_Decl(its); // error
ifs.close();
}
because loading " std::map<std::string, std::string> mapstr;", the compiler
stop at stack_constructor.hpp:64, the line is " this->address()->~T(); "
so, I changed "stack_constructor.hpp"
template<typename T >
struct stack_allocate
{
T * address() {
return static_cast<T*>(&storage_/*.address()*/);
}
T & reference() {
return * address();
}
private:
//typedef BOOST_DEDUCED_TYPENAME boost::aligned_storage<
// sizeof(T),
// #if BOOST_WORKAROUND(__BORLANDC__,BOOST_TESTED_AT(0x560))
// 8
// #else
// boost::alignment_of<T>::value
// #endif
//> type;
T storage_;
};
// construct element on the stack
template<class Archive, class T>
struct stack_construct : public stack_allocate<T>
{
stack_construct(Archive & ar, const unsigned int version){
//// note borland emits a no-op without the explicit namespace
//boost::serialization::load_construct_data_adl(
// ar,
// this->address(),
// version
//);
}
~stack_construct(){
//this->address()->~T(); // undo load_construct_data above
}
};
then, all be ok.
Regards.
Simon Cheung.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk