Boost logo

Boost Users :

From: michael mathews (muckjail_at_[hidden])
Date: 2006-11-16 13:00:11


Robert,
Would this be the correct template to modify? I cannot find the
function you mention anywhere. Of course I could be looking in the
wrong places.

//////////////////////////////////////////////////////////////////////
// implementation of serialization for STL containers
//

template<class Archive, class Container>
inline void save_collection(Archive & ar, const Container &s)
{
    // record number of elements
    unsigned int count = s.size();
    ar << make_nvp("count", const_cast<const unsigned int &>(count));
    BOOST_DEDUCED_TYPENAME Container::const_iterator it = s.begin();
    while(count-- > 0){
        //if(0 == (ar.get_flags() &
boost::archive::no_object_creation))
                // note borland emits a no-op without the explicit
namespace
                boost::serialization::save_construct_data_adl(ar,
&(*it), 0U);
        ar << boost::serialization::make_nvp("item", *it++);
    }
}

One more question. The examples say that you should be able to
serialize a list like this:

#include <boost/serialization/list.hpp>

class bus_route
{
    friend class boost::serialization::access;
    std::list<bus_stop *> stops;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & stops;
    }
public:
    bus_route(){}
};

Does this just work because it is a list of pointers?

Thanks for your help

michael
--- Robert Ramey <ramey_at_[hidden]> wrote:

> and xml_archive requires that ALL elements have a name. So
>
> > list< boost::serialization::nvp<V> > sl;
> > ...
> > ar & sl;
> > ...
>
> would have to be changed to:
>
> > list< boost::serialization::nvp<V> > sl;
> > ...
> > ar & BOOST_SERIALIZATION_NVP(sl);
> > ...
>
> which would be OK... But there is one thing you might not like.
>
> You'll see an extra level tags added by the standard serialization
> of std::list. These tags have the name "item" - because at
> that level, there is not way to know what name they should have.
>
> I'm guessing this would bother you. In that case you should
> use your own enhanced version of serialization of list items.
>
> The first thing I would try would be to make
>
> template<class std::list<boost::nvp<T> > >
> void serialize(boost::archive::xml_archive &ar,
> std::list<boost::nvp<T>
> const unsigned int version){
> copy the standard implementation eliminating the NVP("item"...)
> }
>
> This would be more specialized than the one based on std::list<T> and
> hence
> would take precedence.
>
> Assuming this gives you what you want, you could then make versions
> for
> all the different collections. And a separate set for xml_warchive.
> Slightly
> tedious but a simple process which would solve the problem for
> eternity.
>
> Just included all the above specializations in my_xml_archive.hpp
> along
> with inclusion of the standard xml_archive.hpp
>
> Robert Ramey
>
> michael mathews wrote:
> > Correct I have a list of NVP's. But that is what I need. The idea
> is
> > to have a list of name value pairs where the name is used to
> generate
> > the tag and the value is the value to save. Isn't that how a NVP
> gets
> > serialized to an xml archive?
> >
> > I have also tried
> > list< boost::serialization::nvp<V> > sl;
> > ...
> > ar & sl;
> > ...
> > which I thought would work since the elements of the list are NVP's
> > It also gets caught in the trap.
> >
> > michael
> >
> > --- Robert Ramey <ramey_at_[hidden]> wrote:
> >
> >> This isn't an NVP - its a list of NVPs
> >>
> >> Try
> >> list<V> sl;
> >> ...
> >> ar & BOOST_SERIALIZATION_NVP(sl);
> >>
> >> Robert Ramey
> >>
> >> michael mathews wrote:
> >>> Well I thought it should and I suspect that I am doing something
> >>> silly but this is basically the class I have:
> >>>
> >>>
> >>> template <class N, class V> class test
> >>> {
> >>> friend class boost::serialization::access;
> >>> list< boost::serialization::nvp<V> > sl;
> >>> template<class Archive>
> >>> void serialize(Archive & ar, const unsigned int version)
> >>> {
> >>> ar & BOOST_SERIALIZATION_NVP(sl);
> >>>
> >>> }
> >>>
> >>> public:
> >>> {
> >>> ...
> >>> }
> >>> and it gets caught in the compile trap
> >>>
> >>> template<class T>
> >>> void save_override(T & t, BOOST_PFTO int)
> >>> {
> >>> // If your program fails to compile here, its most likely
> due
> >>> to
> >>> // not specifying an nvp wrapper around the variable to
> >>> // be serialized.
> >>> BOOST_STATIC_ASSERT(0 == sizeof(T));
> >>> }
> >>> I thought that a nvp should be serializable automatically.
> >>> Ideas?
> >>> Thanks in advance
> >>> michael
> >>>
> >>> --- Robert Ramey <ramey_at_[hidden]> wrote:
> >>>
> >>>> Doesn't it already do this?
> >>>>
> >>>> Robert Ramey
> >>>>
> >>>> michael mathews wrote:
> >>>>> Hi All,
> >>>>>
> >>>>> I have a class that contains a list of name value pairs . I
> would
> >>>>> like the serialization to use the names for the tag names in
> the
> >>>>> xml archive. Is there a way to do this or do I need to write my
> >>>>> own custom archive?
> >>>>>
> >>>>> Thanks in advance
> >>>>>
> >>>>> Michael
> >>>>>
> >>>>>
> >>>>> __________________________________________________
> >>>>> Do You Yahoo!?
> >>>>> Tired of spam? Yahoo! Mail has the best spam protection around
> >>>>> http://mail.yahoo.com
> >>>>
> >>>>
> >>>>
> >>>> _______________________________________________
> >>>> Boost-users mailing list
> >>>> Boost-users_at_[hidden]
> >>>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >>>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>
> >
>
____________________________________________________________________________________
> >>> Sponsored Link
> >>>
> >>> Online degrees - find the right program to advance your career.
> >>> Www.nextag.com
> >>
> >>
> >>
> >> _______________________________________________
> >> Boost-users mailing list
> >> Boost-users_at_[hidden]
> >> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> >>
> >
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Sponsored Link
> >
> > Don't quit your job - take classes online
> > www.Classesusa.com
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

 
____________________________________________________________________________________
The all-new Yahoo! Mail beta
Fire up a more powerful email and get things done faster.
http://new.mail.yahoo.com


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