Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-12-03 17:12:59


OK

Heres the short explanation using float as an example:

Its not a great idea to set the tracking trait for float to tracking because
you'll end up tracking ALL the floats int the archive.

So you make a very small class that just contains a float. That as wrapper
class. This is to function just like a float except that it has a different
class. So it will have the default serialization trace assigned since its
not a primitive. This means it will be tracked by default.

struct tracked_float {
    float m_float; // the real data
    template<class Archive>
    void serialize(Archive &ar, unsigned int version){
        ar & m_float;
    }
    // casting operators - not compiled
    float operator float () const {
        return m_float;
    }
    float & operator float() {
        return m_float;
    }
};

Now just use tracked_float for those floats you want to serialize through a
pointer and just float for other float variables.

This is more or less equivalent to using

BOOST_STRONG_TYPEDEF(tracked_float, float)

template<class Archive>
void serialize(Archive &ar, tracked_float & tf){
    ar & tf.m_float;
}

I don't know if that helps - but there it is.

Robert Ramey

"Jeffrey Holle" <jeff.holle_at_[hidden]> wrote in message
news:coqlem$mvg$1_at_sea.gmane.org...
> Just how am I supposed to use a BOOST_STRONG_TYPEDEF produced type?
> If I make it the type of the class attribute, I get an error everywhere
> that I use it. Usage examples would be helpful. For instance, what does:
> m_pByPointerPrefix = new char[strlen(src.m_pByPointerPrefix)+1];
> need to be turned into when m_pByPointerPrefix goes from a "char" to
> tracked_char"?
>
> If I only need to introduce tracked_char in the serialize methods, I
> need examples of doing this too...
>
>
> Robert Ramey wrote:
> > pointers to primitive objects are not serializable by default. This is
> > explained in the document which describes serialization traits. The
reason
> > for this is
> >
> > a) suppose one place in the program I serialization a pointer to a
float.
> > Then addresses of ALLL the floats in the program would end up being
> > seriailzed which is probably not what one wants.
> >
> > The solution is to make a wrapper so the the type has its own class
> >
> > BOOST_STRONG_TYPEDEF(float, tracked_float)
> >
> > Then use a tracked_float instead of a normal float where you want to
> > serialize it through a pointer.
> >
> > Note that strings and characters are both considered primitive types in
this
> > context - I just used float as an example.
> >
> > This is the second time this question has come up so I guess we need to
> > augment the manual and/or include another static assertion in the code.
> >
> > Good Luck - hope this his helpful
> >
> > Robert Ramey
> >
> >
> >
> > "Jeffrey Holle" <jeff.holle_at_[hidden]> wrote in message
> > news:coqcn7$uhv$1_at_sea.gmane.org...
> >
> >>At this point, the last compilation problem in applying the
> >>boost.serialization library to my application has to do with pointers.
> >>
> >>Specifically, its pointers to std::string and char member attributes.
> >>
> >>I have other pointer member attributes that compile successfully, but
> >>this point to objects that have serialize methods.
> >>
> >>I have attempted use of register_type and am including
> >>"serialization/string.hpp" in the modules that have pointer to string
> >>attributes. These actions were not affective.
> >>
> >>As an example of the type of compilation error, when I uncomment the
> >>following attribute's archive attempt:
> >> char *m_pByPointerPrefix;
> >>I get:
> >>
> >>/home/jholle/boost_1_32_0/boost/serialization/access.hpp: In static
> >>member function `static void
> >>boost::serialization::access::serialize(Archive&, T&, unsigned int)
> >>[with Archive = boost::archive::binary_iarchive, T = char]':
> >>/home/jholle/boost_1_32_0/boost/serialization/serialization.hpp:78:
> >>instantiated from `void boost::serialization::serialize(Archive&, T&,
> >>unsigned int) [with Archive = boost::archive::binary_iarchive, T =
char]'
> >>/home/jholle/boost_1_32_0/boost/serialization/serialization.hpp:121:
> >>instantiated from `void boost::serialization::serialize_adl(Archive&,
> >>T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T =
> >>char]'
> >>/home/jholle/boost_1_32_0/boost/archive/detail/iserializer.hpp:155:
> >>instantiated from `void boost::archive::detail::iserializer<Archive,
> >>T>::load_object_data(boost::archive::detail::basic_iarchive&, void*,
> >>unsigned int) const [with Archive = boost::archive::binary_iarchive, T =
> >>char]'
> >>VariableName.cpp:102: instantiated from here
> >>/home/jholle/boost_1_32_0/boost/serialization/access.hpp:106: error:
> >>request for member `serialize' in `t', which is of non-class type `char'
> >>
> >>
> >>What is the solution to this?


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