Bernhard Maeder | 22 May 15:37
Picon
Favicon

[serialization] Handling of references into a std::map

Hello all

I'm trying to serialize a std::map along with some pointers that are referencing
onto some of the Value elements of that map. What I see now, is that those
references are not loaded correctly. Here's what I do:

#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>

struct dummy 
{
    template<typename Archive> 
    void serialize(Archive & ar, const unsigned int version)
    {        
    }  
};

struct map_ref_test
{
    std::map<std::size_t, dummy> m;    
    dummy * ref;

    template<typename Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & m;
        ar & ref;
    }      
(Continue reading)

Johan Råde | 22 May 17:18
Picon
Picon
Favicon

Re: [serialization] Handling of references into a std::map

Bernhard Maeder wrote:

> I'm trying to serialize a std::map along with some pointers that are referencing
> onto some of the Value elements of that map. What I see now, is that those
> references are not loaded correctly. 

You could save a reference to the map and the value of the key for the element.
When you load the archive, you can use that information to reconstruct the reference to the element.

--Johan Råde
Bernhard Maeder | 22 May 19:27
Picon
Favicon

Re: [serialization] Handling of references into a std::map

Johan Råde <rade <at> maths.lth.se> writes:

> 
> Bernhard Maeder wrote:
> 
> You could save a reference to the map and the value of the key for the element.
> When you load the archive, you can use that information to reconstruct the
reference to the element.
> 
> --Johan Råde

Yes, I could do that, but it would be more complicated. In my real application,
I don't keep the references to that map's values in the same place with the map;
the reconstruction process would thus influence many other components as well,
which I wouldn't want to do. If I understand Boost.Serialization correctly (and
that is of course in doubt here! :-), it is able to accomplish these things for
you and should thus make the serialization code much less verbose.

Bernhard

_______________________________________________
Boost-users mailing list
Boost-users <at> lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Robert Ramey | 22 May 18:42

Re: [serialization] Handling of references into a std::map

This can't work.

load a pointer creates a new pointer with a copy of the original object.

adding the following to your test should make this clear

...
std::cout << "ref: " << *m.ref << std::endl;
...
 std::cout << "ref: " << *m2.ref << std::endl;

Robert Ramey

Bernhard Maeder wrote:
> Hello all
>
> I'm trying to serialize a std::map along with some pointers that are
> referencing onto some of the Value elements of that map. What I see
> now, is that those references are not loaded correctly. Here's what I
> do:
>
> #include <sstream>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/serialization/map.hpp>
>
> struct dummy
> {
>    template<typename Archive>
>    void serialize(Archive & ar, const unsigned int version)
(Continue reading)

Sohail Somani | 22 May 19:28

Re: [serialization] Handling of references into a std::map

Robert Ramey wrote:
> This can't work.
> 
> load a pointer creates a new pointer with a copy of the original object.
> 
> adding the following to your test should make this clear
> 
> ...
> std::cout << "ref: " << *m.ref << std::endl;
> ...
>  std::cout << "ref: " << *m2.ref << std::endl;
> 

Ok, the author will know more than I! For the OP, you can have a map of 
pointers which will work.

--

-- 
Sohail Somani
http://uint32t.blogspot.com
Bernhard Maeder | 22 May 19:22
Picon
Favicon

Re: [serialization] Handling of references into a std::map

Robert Ramey <ramey <at> rrsd.com> writes:

> 
> This can't work.
> 
> load a pointer creates a new pointer with a copy of the original object.
> 
> adding the following to your test should make this clear
> 
> ...
> std::cout << "ref: " << *m.ref << std::endl;
> ...
>  std::cout << "ref: " << *m2.ref << std::endl;
> 

Hmm, if I understand you correctly, then m2.ref should point to a copy of m.m[1]
then, but not the same instance? The thing is, m2.ref isn't even that.

Here's an enhanced example: I add a value variable to my dummy struct:

struct dummy 
{
    int j;
    dummy() : j(-1) {}
    dummy(int i) : j(i) {}    
    template<typename Archive> void serialize(Archive & ar, const unsigned int
version)
    {
        ar & j;
    }  
(Continue reading)

Sohail Somani | 22 May 19:15

Re: [serialization] Handling of references into a std::map

Bernhard Maeder wrote:
> Hello all
> 
> I'm trying to serialize a std::map along with some pointers that are referencing
> onto some of the Value elements of that map. What I see now, is that those
> references are not loaded correctly. Here's what I do:

This seems like a bug. If you change the map to an array or a std 
vector, it works as expected.

--

-- 
Sohail Somani
http://uint32t.blogspot.com
Bernhard Maeder | 22 May 19:29
Picon
Favicon

Re: [serialization] Handling of references into a std::map

Sohail Somani <sohail <at> taggedtype.net> writes:

> 
> Bernhard Maeder wrote:
> > Hello all
> > 
> > I'm trying to serialize a std::map along with some pointers that are
> > referencing
> > onto some of the Value elements of that map. What I see now, is that those
> > references are not loaded correctly. Here's what I do:
> 
> This seems like a bug. If you change the map to an array or a std 
> vector, it works as expected.
> 

Yes, I noticed that, too. Do you know if it's fixed in 1.34 or 1.35?

Bernhard
Sohail Somani | 22 May 23:25

Re: [serialization] Handling of references into a std::map

Bernhard Maeder wrote:
> Sohail Somani <sohail <at> taggedtype.net> writes:
> 
>> Bernhard Maeder wrote:
>>> Hello all
>>>
>>> I'm trying to serialize a std::map along with some pointers that are
>>> referencing
>>> onto some of the Value elements of that map. What I see now, is that those
>>> references are not loaded correctly. Here's what I do:

>> This seems like a bug. If you change the map to an array or a std 
>> vector, it works as expected.
>>
> 
> Yes, I noticed that, too. Do you know if it's fixed in 1.34 or 1.35?

I was testing against 1.35 so I assume not. I took a look at the code as 
well but I do not know why it didn't work as the code for the vector 
looks almost exactly the same as the code for a map.

At the very least, take your example and file a ticket at 
http://svn.boost.org

--

-- 
Sohail Somani
http://uint32t.blogspot.com

Gmane