Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::serialization - Serialization (non intrusive) of a map containing in and structure.
From: Brian Davis (bitminer_at_[hidden])
Date: 2014-03-11 13:00:28


Thanks Steven for the reply. While this was not the final answer to the
problem (as it was one problem) it did help lead to the solution which was
as Steven stated ..... and a missing nvp around the ni.a variable.

Note the std::pair<> code was not necessary and was removed.

below is working version and xml file for those who may experience this in
the future.

------------------------------------------------------------------------

#include <string>
#include <fstream>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/version.hpp>

struct non_intrusive{
  int id;
  std::string name;
};

typedef std::map<int, non_intrusive> a_map_t;

namespace boost { namespace serialization {

template<class Archive>
void serialize( Archive& ar, non_intrusive& ni, const unsigned int version
){
    ar & boost::serialization::make_nvp( "id", ni.id );
    ar & boost::serialization::make_nvp( "name", ni.name );
}

}}
// +----------------------------------------------------------------------

void save
(
 const a_map_t& map_var, const std::string& file_name
)
{
  boost::filesystem::path file_path = file_name;
  boost::filesystem::path pathstr = file_path.remove_leaf();
  if( pathstr != "" )
    boost::filesystem::create_directory( pathstr );

  std::ofstream ofs( file_name.c_str(), std::ios_base::out |
std::ios_base::trunc);

  boost::archive::xml_oarchive xml(ofs);
      xml << BOOST_SERIALIZATION_NVP( map_var );
}

// +----------------------------------------------------------------------

a_map_t load(const std::string& file_name)
{
  a_map_t map_var;
  // std::ifstream ifs(file_name.c_str(), std::fstream::binary |
std::fstream::in);
  std::ifstream ifs(file_name.c_str() );
  if( !ifs )
    {
      non_intrusive od;
      od.id = -1;
      od.name = "unknown device";

      map_var.insert(
            a_map_t::value_type( 0, od ) );

      save( map_var, file_name );

    }else
    {
      boost::archive::xml_iarchive xml(ifs);
      xml >> BOOST_SERIALIZATION_NVP( map_var );
    }
  return map_var;
}

// +----------------------------------------------------------------------
int main( void ){

    a_map_t map;
    map = load("map.xml");
    save(map, "map.xm" );

}

------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="6">
<map_var class_id="0" tracking_level="0" version="0">
    <count>1</count>
    <item_version>0</item_version>
    <item class_id="1" tracking_level="0" version="0">
        <first>0</first>
        <second class_id="2" tracking_level="0" version="0">
            <id>-1</id>
            <name>unknown device</name>
        </second>
    </item>
</map_var>
</boost_serialization>



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