Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Serialization: Restoringobjectstate(preventcreation of a new object)?
From: Robert Ramey (ramey_at_[hidden])
Date: 2010-08-31 12:34:49


Robert Ramey wrote:
> ray_at_[hidden] wrote:
>>> That's likely something else entirely. We'd have to see more code.
>> Forget my previous post, the exception was due to mixed up ofstream
>> and ifstream...
>>
>> Just to resume - if I have a list of pointers to the objects (all
>> derived from the same base class) and I'd like to serialize them
>> through a reference, a code like this would be needed:
>>
>> // List of pointers
>> A a;
>> B b; // Derived from A
>> C c; // Derived from C
>> std::list<A*> list;
>> list.push_back(&a); // Base
>> list.push_back(&b); // Derived
>> list.push_back(&c); // Derived
>> ...
>> // Save
>> std::ofstream ofs("c:/test.xml");
>> boost::archive::xml_oarchive oa(ofs);
>> BOOST_FOREACH(A* pA, list)
>> {
>> if (dynamic_cast<B*>(pA))
>> {
>> B& b = *static_cast<B*>(pA);
>> oa & BOOST_SERIALIZATION_NVP(b);
>> }
>> else if (dynamic_cast<C*>(pA))
>> {
>> C& c = *pA;
>> BOOST_SERIALIZATION_NVP(c);
>> }
>> else
>> {
>> A& a = *pA;
>> BOOST_SERIALIZATION_NVP(a);
>> }
>> }
>>
>>
>> Am I right?
>
> It's not absolutely clear what your object here is.
> But the following would be equivalent to the above as far as I can
> tell - and a lot simpler
>
> // Save
> std::ofstream ofs("c:/test.xml");
> boost::archive::xml_oarchive oa(ofs);
> oa << list;

whoops, I missed the part about skipping the pointer part.

So I guess your method above would work - though I'd have to think about
it.

of course if you had

std::list<A &> list;

there would be nothing to do.

Robert Ramey


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