[serialization] suggestion: specialize make_nvp to create nvp<T> only if using basic_xml_i/oarchive

I wanted the ability to serialize to binary archives (over a socket) and xml archives (file) without writing code for the two cases (nvp w/xml and no-nvp with non-xml). I whipped up a quick make_nvp wrapper that simply forwards on the value if not serializing to a xml archive. Others might find this useful, not sure if its good behavior to have right in the library. It does require adding the Archive type as a tempalte param to the make_nvp call. template <typename Archive, typename T> struct make_nvp_impl { typedef T& result_type; static T& do_make_nvp(const char *tag, T & value) { return value; } }; template <typename Archive, typename T> struct make_nvp_impl<boost::archive::basic_xml_iarchive<Archive>, T> { typedef boost::serialization::nvp<T> result_type; static boost::serialization::nvp<T> do_make_nvp(const char *tag, T & value) { return boost::serialization::make_nvp(tag,value); } }; template <typename Archive, typename T> struct make_nvp_impl<boost::archive::basic_xml_oarchive<Archive>, T> { typedef boost::serialization::nvp<T> result_type; static boost::serialization::nvp<T> do_make_nvp(const char *tag, T & value) { return boost::serialization::make_nvp(tag,value); } }; template <typename Archive, typename T> typename make_nvp_impl<Archive, T>::result_type make_nvp(const char* tag, T& value) { return make_nvp_impl<Archive,T>::do_make_nvp(tag,value); } So now: struct point { int x, y; template<class Archive> void serialize(Archive &ar, const unsigned int /*version*/) { ar & make_nvp<Archive>("x",x); ar & make_nvp<Archive>("y",y); } }; will serialize properly for xml archives and non-xml archives. Chris

Chris Fairles wrote:
I wanted the ability to serialize to binary archives (over a socket) and xml archives (file) without writing code for the two cases (nvp w/xml and no-nvp with non-xml).
The library as it stands now permits this. I don't see any functional difference between the code you post and that in the library in the beginning. The implemenation in the library is different as I didn't wanted to permit its usage with compilers which have broken implemenations of partial function template ordering - but as far as I can see it does the same thing. Robert Ramey

On 9/18/07, Robert Ramey <ramey@rrsd.com> wrote:
Chris Fairles wrote:
I wanted the ability to serialize to binary archives (over a socket) and xml archives (file) without writing code for the two cases (nvp w/xml and no-nvp with non-xml).
The library as it stands now permits this. I don't see any functional difference between the code you post and that in the library in the beginning. The implemenation in the library is different as I didn't wanted to permit its usage with compilers which have broken implemenations of partial function template ordering - but as far as I can see it does the same thing.
Robert Ramey
Hm. I'll have to look more closely at the libraries impl, but I was getting junk reading from a binary archive written through make_nvp call's in the class' serialize function. using the posted impl, things seemed to work. The code you refer to exists in 1.34.1 I assume? Chris
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Chris Fairles wrote:
Hm. I'll have to look more closely at the libraries impl, but I was getting junk reading from a binary archive written through make_nvp call's in the class' serialize function. using the posted impl, things seemed to work. The code you refer to exists in 1.34.1 I assume?
Look at the way all the tests are written. The whole serialization library is designed so that the selection of archive and data type don't interact unless explicitly specified. Robert Ramey
participants (2)
-
Chris Fairles
-
Robert Ramey