Re: How to serialize reference members using boost::serialization
* Abhishek Padmanabh:
Thank you for your replies.
Here is an example that I had prepared for working with references
(and you can see how const is handled without const_cast<>) -
http://www.codeguru.com/forum/showthread.php?t=415510
Your code (it would be better to just include it, because it's short):
<CODE>
#include <fstream>
#include <string>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
//for name value pairs when doing XML archiving
#include <boost/serialization/nvp.hpp>
class MyClassWithNoDefaultConstructor;
std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object);
//global integer variable... whose reference will be a member of
MyClassWithNoDefaultConstructor
int global_int = 10;
class MyClassWithNoDefaultConstructor
{
public:
MyClassWithNoDefaultConstructor(int i, int& ref_)
: intMember(i), ref(ref_)
{}
private:
int intMember;
int& ref;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(intMember);
ar & BOOST_SERIALIZATION_NVP(ref);
}
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object);
};
std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object)
{
os << "\nMyClassWithNoDefaultConstructor contents:\n";
os << "intMember - " << object.intMember << "\n";
os << "ref - " << object.ref << "\n";
return os;
}
namespace boost
{
namespace serialization
{
template<class Archive>
inline void save_construct_data(Archive & ar, const
MyClassWithNoDefaultConstructor* t, const unsigned int file_version)
{
// save data required to construct instance
ar << t->intMember;
ar << &(t->ref);
}
template<class Archive>
inline void load_construct_data(Archive & ar,
MyClassWithNoDefaultConstructor* t, const unsigned int file_version)
{
// retrieve data from archive required to construct new
instance
int m;
//int * ptr;
int * ptr = new int();
ar >> m;
ar >> ptr;
::new(t)MyClassWithNoDefaultConstructor(m, *ptr);
}
}
}
void SerializeMyClassWithNoDefaultConstructor(const std::string&
filename)
{
MyClassWithNoDefaultConstructor object(111, global_int);
std::ofstream ofs(filename.c_str());
assert(ofs.good());
boost::archive::xml_oarchive xml_oa(ofs);
xml_oa << BOOST_SERIALIZATION_NVP(object);
}
void DeserializeMyClassWithNoDefaultConstructor(const std::string&
filename)
{
char * buffer = new char[sizeof(MyClassWithNoDefaultConstructor)];
MyClassWithNoDefaultConstructor* ptr =
reinterpret_cast<MyClassWithNoDefaultConstructor*>(buffer);
std::ifstream ifs(filename.c_str());
assert(ifs.good());
std::cout << "inside deserialize()" << std::endl;
boost::archive::xml_iarchive xml_ia(ifs);
std::cout << "xml_iarchive constructed" << std::endl;
xml_ia >> BOOST_SERIALIZATION_NVP(*ptr);
std::cout << "deserialized" << std::endl;
std::cout << *ptr;
ptr->~MyClassWithNoDefaultConstructor();
delete[] buffer;
buffer=NULL; ptr=NULL;
}
int main()
{
const std::string filenameMyClassWithNoDefaultConstructor="/tmp/
testfileMyClassWithNoDefaultConstructor.xml";
try
{
SerializeMyClassWithNoDefaultConstructor
(filenameMyClassWithNoDefaultConstructor);
DeserializeMyClassWithNoDefaultConstructor
(filenameMyClassWithNoDefaultConstructor);
}
catch(const boost::archive::archive_exception& ex)
{
std::cout << ex.what() << "\n";
}
catch(const std::exception& ex)
{
std::cout << ex.what() << "\n";
}
return 0;
}
</CODE>
It works
Sort of. reinterpret_cast introduces undefined behavior. For
example, it may be that the Boost serialization framework is calling
the 'serialize' member on that uninitialized object. Also, when
using '::new' you really should include the <new> header. Presumably
it's included by chance by one of the other headers.
but I must say that I don't feel I am handling the reference
member correctly.
The relevant code:
// save data required to construct instance
ar << t->intMember;
ar << &(t->ref);
// retrieve data from archive required to construct new instance
int m;
//int * ptr;
int * ptr = new int();
ar >> m;
ar >> ptr;
::new(t)MyClassWithNoDefaultConstructor(m, *ptr);
First, the 'new int()' is unnecessary and it's a memory leak, because
the first thing you do afterwards is to overwrite the pointer.
Second, the in-place construction won't work well in a hierarchy of
classes, so it's not a general technique.
You're not handling the reference member correctly because in the
original object it's bound to a global. The serialization framework
doesn't know anything about your globals. The only default a
serialization framework can apply is to assume that a pointer points
to a dynamically allocated object (I don't know if Boost does that),
and otherwise you'll have to handle it yourself.
The code above might seem to work when serialization and
deserialization is done within the same process (instance of your
program).
When deserialization is done in some other process, most likely
you'll end up with a garbage pointer. I don't know what magic Boost
serialization applies: if magic is applied you may instead end up
with a pointer to a dynamically allocated int. What you won't end up
with is a pointer to the global, unless purely by chance.
Because the boost documentation says that references
should be serialized as pointers but I don't think I am doing that.
That's what you're doing. It's evidently not correct in the sense of
"reproducing" the original object with a reference to a global. But
whether the code is correct with respect to Boost serialization
requirements, I can't say, because I don't know those requirements.
From my code it simply looks like any other member. When I try to
make_nvp for the member by pointer, I start getting long compiler
errors. Moreover, if we do that via pointer, I am not clear on few
things:
1. Who allocates memory that the deserialized pointer will point
to?
2. Will it be my responsibility to clean it?
3. What if that object is shared across multiple other serialized
objects which have references to it? How does the user code change?
Hopefully someone familiar with Boost serialization can answer that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]