Boost logo

Boost Users :

Subject: Re: [Boost-users] Specifying a NVP wrapper
From: François Mauger (mauger_at_[hidden])
Date: 2011-03-18 13:06:58


simone pilozzi a écrit :
> Hi All,
>
> I am working with the serialization library and I made an example
> class like this.
>
>
> class
>
> Contact{
>
> public:
>
> Contact(){}
>
> Contact(string& name, int phonenumber,long contactdate):name_(name),
>
> pnumber_(phonenumber),contactdate_(contactdate){}
>
> const string getName() const{return name_;}
>
> const long getPhone() const{return pnumber_;}
>
> const long getDate() const{return contactdate_;}
>
> private
>
> :
>
> string name_;
>
> long pnumber_,contactdate_; friend class boost::serialization::access;
> template<typename Archive> void serialize(Archive& ar, const unsigned
> int version){
>
> ar & boost::serialization::make_nvp(
>
> "name", name_);
>
> ar & boost::serialization::make_nvp(
>
> "phone", pnumber_);
>
> ar & boost::serialization::make_nvp(
>
> "contactdate", contactdate_);
>
> }
>
> };
>
>
>
> Now I would like to allocate the xml file created with the use of this
> class in a vector, and this is the function:
>
> void
>
> getFromFile(std::vector<Contact>& contacts,const char* file){
>
> std::ifstream ifile(file);
>
> boost::archive::xml_iarchive ia(ifile);
>
> ia >> contacts;
>
> ifile.close();
>
> }
>
> The compiler complains because it misses a NVP wrapper for the class.
> I could not find an example for that in boost.
>
> Could you help me ?
>
> Thanks in advance
>
> Simone
>

Hi simone

I guess you should use :

ia >> boost::serialization::make_nvp("contacts", contacts);

in place of what you propose (which is correct for text_archive by the way).
AFAIU, the NVP wrapper is needed at all levels of your (de)serialization
code,
not only within the 'serialize' methods for class attributes.

have a look on the XML archives you can generate with such method (using
some basic text editor), you will understand
how Boost/serialization XML archives system generates XML tags and why NVP
wrappers are mandatory here.

regards

frc

-- 
François Mauger 
  Groupe "Interactions Fondamentales et Nature du Neutrino"
  NEMO-3/SuperNEMO Collaboration
LPC Caen-CNRS/IN2P3-UCBN-ENSICAEN
Département de Physique -- Université de Caen Basse-Normandie
Adresse/address:
  Laboratoire de Physique Corpusculaire de Caen (UMR 6534)
  ENSICAEN 
  6, Boulevard du Marechal Juin
  14050 CAEN Cedex
  FRANCE
Courriel/e-mail: mauger_at_[hidden] 
Tél./phone:      02 31 45 25 12 / (+33) 2 31 45 25 12
Fax:             02 31 45 25 49 / (+33) 2 31 45 25 49

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