Salut,
Je suis occupé à tester la sérialization avec boost sur des classes templates...
Je pars donc d'une classe du genre de
avec un code de sérialization non intrusif proche de
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 template <typename Value, int> struct FARFELUE_API TPoint; template <typename Value> struct FARFELUE_API TPoint<Value, 2> { typedef Value value_type; typedef TPoint<value_type, 2> point_type; TPoint() = default; TPoint(value_type x, value_type y):x(x),y(y){} value_type x; value_type y; }; template <typename Value> struct FARFELUE_API TPoint<Value, 3> { typedef Value value_type; typedef TPoint<value_type, 3> point_type; TPoint() = default; TPoint(value_type x, value_type y, value_type z):x(x),y(y),z(z){} value_type x; value_type y; value_type z; };
(1) Tant que je travaile avec des couples boost::archive::text_oarchive / boost::archive::text_iarchive ou boost::archive::binary_oarchive / boost::archive::binary_iarchive, sous réserve d'adapter les inclusion d'en-tete de boost::archive, un code proche de
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 namespace boost { namespace serialization { template<typename Archive, typename T> void serialize( Archive & ar, TPoint<T,2> & pt, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(pt.x) & BOOST_SERIALIZATION_NVP(pt.y); } template<typename Archive, typename T> void serialize( Archive & ar, TPoint<T,3> & pt, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(pt.x) & BOOST_SERIALIZATION_NVP(pt.y) & BOOST_SERIALIZATION_NVP(pt.z); } } } typedef boost::archive::text_oarchive output_file; /* (1) */ typedef boost::archive::text_iarchive output_file; /* (1) */
compile et fonctionne parfaitement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 void save(TPoint<int, 2> const & point) { std::cout << "before save " <<std::endl <<point.x << " " << point.y<<std::endl; std::ofstream ofs("point.xml"); assert(ofs.good()); output_file of(ofs); of << point; } void load(TPoint<int, 2> & point) { std::ifstream ifs("point.xml"); assert(ifs.good()); input_file ia(ifs); ia >> point; std::cout << "after reading " <<std::endl <<point.x << " " << point.y<<std::endl; } int main() { TPoint<int , 2> point(0,4); save(point); TPoint<int , 2> loaded; load(loaded); return 0; }
Par contre, dés que j'essaye de travailler avec des archive xml, j'obtiens l'erreur
et ce, uniquement si j'essaye de décommenter la fonction "load"...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 ..\..\boostMinGW\include\boost-1_47\boost\archive\basic_xml_iarchive.hpp||In member function 'void boost::archive::basic_xml_iarchive<Archive>::load_override(T&, int) [with T = TPoint<int, 2>, Archive = boost::archive::xml_iarchive]':| ..\..\boostMinGW\include\boost-1_47\boost\archive\xml_iarchive.hpp:93|9|instantiated from 'void boost::archive::xml_iarchive_impl<Archive>::load_override(T&, int) [with T = TPoint<int, 2>, Archive = boost::archive::xml_iarchive]'| ..\..\boostMinGW\include\boost-1_47\boost\archive\detail\interface_iarchive.hpp:60|9|instantiated from 'Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = TPoint<int, 2>, Archive = boost::archive::xml_iarchive]'| C:\projects\farfelue\test\main.cpp:39|11|instantiated from here| ..\..\boostMinGW\include\boost-1_47\boost\archive\basic_xml_iarchive.hpp|70|error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<TPoint<int, 2> >::************)'| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|
Le plus bizard de l'histoire, c'est que, pour autant que le contenu de la fonction load soit commentée, la compilation s'effectue correctement (ca, c'est normal) mais que le fichier généré par save semble être tout à fait correct...
- Aurais-je raté quelque chose
- Devrais-je veiller à créer une spécialisation de is_wrapper
- Est-ce, simplement, un bug de boost
Partager