Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] serializing a pointer withoutcausingobject tracking
From: Guy Premont (guy.premont_at_[hidden])
Date: 2013-04-02 10:35:17


I had the same problem, where I did not want a particular serialized
object to be tracked. It happened when I serialize an object that is
created on the stack and the class is usually tracked but I don't want the
instance on the stack to be tracked.

I've done a little template class that directly calls the serialize method
of the target class, and it works well...

        template<typename T>
        struct NoTracking : public T, public wrapper_traits<
NoTracking<T>,

object_class_info,
                                                             track_never,

version<T>::value >
        {
            NoTracking() : T() { }

            template<typename T0>
            NoTracking(const T0& t0) : T(t0) { }

            template<typename T0, typename T1>
            NoTracking(const T0& t0, const T1& t1) : T(t0, t1) { }

            template<typename T0, typename T1, typename T2>
            NoTracking(const T0& t0, const T1& t1, const T2& t2) : T(t0,
t1, t2) { }

            template<class Archive>
            void serialize(Archive & ar, const unsigned int version)
            {
                boostVx::serialization::serialize(ar,
static_cast<T&>(*this), version);
            }
        };

The serialize function looks like:
    void serialize(Archive & ar, unsigned int file_version)
    {
        NoTracking<Vx::VxTransform> transform = getTransform();

        ar & make_nvp("Transform", transform);

        if ( Archive::is_loading::value )
        {
           setTransform(transform);
        }
    }

--
Guy Prémont, D.Sc.
Architecte logiciel senior / Senior software architect
CM Labs Simulations Inc. http://www.cm-labs.com/
Tel. 514-287-1166 ext. 237
-----Original Message-----
From: Boost-users [mailto:boost-users-bounces_at_[hidden]] On Behalf
Of Robert Ramey
Sent: April-01-13 7:51 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [serialization] serializing a pointer
withoutcausingobject tracking
Robert Ramey wrote:
> Stefan Strasser wrote:
>> Am 30.03.2013 19:03, schrieb Robert Ramey:
>>>> is there a solution to this? is there a serialization wrapper that
>>>> says "don't track this type just because of this pointer"?
>>>
>>> so you want "don't track this particular save - but leave others as
>>> normal" Try something like
>>>
>>> class A ....
>>>
>>> class UntrackedA : public A {
>>> ...
>>> };
>>>
>>> BOOST_SERIALIZATION_TRACK(UntrackedA, track_never);
>>
>> interesting idea. does this eventually track the object if the base
>> class of UntrackedA is track_selectively and saved through a pointer
>> anywhere else?
>>
>> for example, an object graph that stores a pointer to its own root
>> (through intermediaries in practice):
>>
>> class A{ //tracking level: selectively
>>   void serialize(...){ ar & a; }
>>   A *a; //==this
>> };
>>
>> class UntrackedA : A{ //tracking level: never
>>   void serialize(...){ ar & base_object<A>(*this); } }
>>
>> void save(UntrackedA *a){ ar << a; }
>>
>> is "a" serialized twice, or tracked because of track_selectively and
>> the pointer serialization in A::serialize?
>
> lol - I didn't think about this when I responded though I'm sure I did
> it when I made the code.  I'm going to speculate since it's easier
> than doing any work.
>
> class UntrackedA : public A {
>    // instead of this:
>    void serialize(Archive & ar, unsigned int file_version){
>        boost::serialization:base_object<A>(*this);
>    }
>    // consider this
>    void serialize(Archive & ar, unsigned int file_version){
>        ar & m_a; // where m_a is a member of a
>        ar & m_b; // other member of a etc ...
>    }
>   ...
>
better yet try this:
class UntrackedA : public A {
};
just use the base class implementation of serialize function via
inheritance
Robert Ramey
>
> Robert Ramey
_______________________________________________
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