|
Boost Users : |
Subject: Re: [Boost-users] [serialization] Deserialize by pointer withoutcalling new/constructor
From: averigin (averigin_at_[hidden])
Date: 2010-10-13 21:36:47
> While I don't quite see it as a bomb, I know that probably just indicates
> short-sightedness on my part. Regardless, I am in the "just make it work
> right now" boat, so any help you can offer would be much appreciated.
>
> P.S.: I did manage a temporary hack of archive/detail/iserializer.hpp
> pointer_iserializer::load_object_ptr() that (badly) assumed that if the
> passed pointer is not NULL, then it is a pointer to an allocated object so
> there's no need to create a new object. Coupled with a do-nothing
> load_construct_data() method this worked great for
> partial-deserialization, but causes memory corruption for normal
> deserialization since the assumption is not always true (nor is it a good
> assumption to try an force on other users).
>
I finally found a half-decent work-around:
I added a template specialization of the
boost::archive::detail::pointer_iserializer::load_object_ptr() method for
each of the classes which I will be creating manually and then
deserializing.
On top of this, I disabled tracking for them, enabled exporting and added
a call to void_cast_register() to enable (de)serialization by a base-class
pointer.
Here's some code snippets to elaborate the above (hopefully they display
fine):
// In the header file
class MyClass: public cSimpleModule {
...
};
BOOST_CLASS_TRACKING(MyClass, boost::serialization::track_never)
namespace boost {
namespace archive {
namespace detail {
template<>
void pointer_iserializer<boost::archive::text_iarchive,
MyClass>::load_object_ptr(
basic_iarchive &ar,
void * & x,
const unsigned int file_version) const {
boost::archive::text_iarchive & ar_impl =
boost::serialization::smart_cast_reference<boost::archive::text_iarchive
&>(ar);
ar_impl >> boost::serialization::make_nvp(NULL,
*reinterpret_cast<MyClass*&>(x));
}
}
}
}
// In the cc file
BOOST_CLASS_EXPORT(MyClass)
MyClass::MyClass(void) {
boost::serialization::void_cast_register<MyClass, cModule>(
static_cast<MyClass*>(NULL),
static_cast<cModule*>(NULL));
...
}
...
-- Adam
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net