and then it compiles. Which of course I know I should not do, but only did so to see if I could get it to call my template specialization for my serialize code of my strut with out having to perform in intrusive serialization and agian was just a test to see if boost would call my serialize function. I have revered this so fear not... this is not the problem.
namespace boost { namespace serialization {
template<class Archive>
void serialize( Archive& ar, non_intrusive& ni ){
ar & ni.a;
}
which is of course wrapped in the boost::serialization namespace... full example below
I am guessing I have not done something correctly and I have been successful in the past as I said serializing structures, vectors, and maps. This is the first time I tried to serialize a map with a struct. I also converted my code (not the example) to use a vector of std::pair<int, non_intrusive> and got the same error.
//-------------------------------------------------------------------------------------------
#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>
struct non_intrusive{
int id;
std::string name;
};
typedef std::map<int, non_intrusive> a_map_t;
namespace boost { namespace serialization {
// Not sure if I need to do this bit, but I have seen references to this
// syntax on the web.
template<class Archive>
void serialize( Archive& ar, std::pair<int, non_intrusive> & ni_pair ){
ar & std::pair<int, non_intrusive>( ni_pair );
}
template<class Archive>
void serialize( Archive& ar, non_intrusive& ni ){
ar & ni.a;
}
}}
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" );
}