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
#include
#include
#include
#include
#include
#include
#include
struct non_intrusive{
int id;
std::string name;
};
typedef std::map 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>
<count>1</count>
0
<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>