Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-users] [serialization] working example of (intrusive) serialization of class with no default constructor? and handling of nested classes with no default constructor
From: Hazrat Pradipta Ranjali (mrdipta_at_[hidden])
Date: 2011-10-06 23:17:51


Hi Alfredo,

Boost::Serialization have a working a sample already.
You can either get it from :
http://www.boost.org/doc/libs/1_47_0/libs/serialization/test/test_non_default_ctor.cpp

Or,

You can check the corresponding file on your boost package:
\boost_1_47_0\libs\serialization\test\
It contains some other sample as well

On Sat, Oct 1, 2011 at 03:27, alfC <alfredo.correa_at_[hidden]> wrote:

> Hi,
>
> this question has been asked thousand of times and I still can not see a
> detailed answer. How can I deserialize a class with no default constructor.
> The usual response is to point to this documentation
> http://www.boost.org/doc/libs/1_35_0/libs/serialization/doc/serialization.html#constructors. I read the documentation many time and I can not figure out what does it
> mean and how it can be used.
>
> Does any body has a working small example where this is effectively used?
>
> Ideally, I am looking for something like the following, I am willing to
> accept other options, like making a2 a pointer, etc.
>
> int main(){
> {
> A a( ... ); // proper construction syntax
> std::ofstream ofs("A.xml");
> boost::archive::xml_oarchive oa(ofs);
> a & BOOST_SERIALIZATION_NVP(a);
> }
> {
> std::ifstream ifs("A.xml");
> boost::archive::xml_iarchive ia(ifs);
> A a2(ia); // construct from iarchive or some other intermediate class
> {
> a2 & BOOST_SERIALIZATION_NVP(a2); //data can still be loaded in
> existing object (as usual)
> }
> }
> return 0;
> }
>
> Even if I want to implement my own reading of the xml_iarchive inside the
> constructor of A, it looks like I need to access the nested elements of the
> class before accessing to the level of the class (I am thinking in xml terms
> here).
>
> This is where I am stuck, the syntax above forces me to define
>
> A::A(boost::archive::xml_iarchive& ia){ ... }
>
> If all members are default constructible I can do:
>
> A::A(boost::archive::xml_iarchive& ia){
> ia & BOOST_SERIALIZATION_NVP(*this);
> }
> (yes, the solution in intrusive but the at least I avoid default
> constructor at all cost)
>
> but if one of the member is not default constructible (and I can modify
> that class) I should be able to do something like,
>
> A::A(boost::archive::xml_iarchive& ia) : member1( ...ia?... ){
> // ia & BOOST_SERIALIZATION_NVP(member2); // not possible because the
> archive is still waiting for class of type A to be read
> }
>
> which has two problems, 1) I need to access the elements of the archive
> (e.g. member2 before A is really handled by boost.serialization) 2.a) for
> member1 it regresses the problem to the member1 type, which is fine because
> all this objects will at some point be composed only from build-in "default
> constructible" types 2.b) if I have no way to modify member1 and it is not
> default constructible then I can't continue but that is also fine because
> that means that member1 was not serializable anyway.
>
> The real problem is 1). because if I have access to all the classes I will
> reach a point in which I have to read a built-in class from the iarchive,
> but I have have access to those nested elements directly, at least not with
> the documented interface.
>
>
> Thanks,
> Alfredo
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



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