[Serialization] Serializing boost::tuples

Hi there, I know that serializing of std::pair is part of the current serialization lib. I'm just wondering if there is something for the boost's tuples? Is it maybe in a different header file? I think that would be a cool feature and not so difficult to implement. But maybe I'm naive? Thanks, Christian

I don't know of anyone having done it. I would expect it to be similar to pair. If you do this, feel free to upload it to the vault. Robert Ramey Christian Henning wrote:
Hi there, I know that serializing of std::pair is part of the current serialization lib. I'm just wondering if there is something for the boost's tuples? Is it maybe in a different header file?
I think that would be a cool feature and not so difficult to implement. But maybe I'm naive?
Thanks, Christian

Hi Robert, I would be interested in doing that. Here is what I would like to achieve: typedef tuple< string, string, string > Key; typedef map< Key, int > MyMap; int _tmain(int argc, _TCHAR* argv[]) { MyMap aMap; { const MyMap& caMap = aMap; ofstream ofs( "Settings.txt" ); boost::archive::text_oarchive oa( ofs ); oa & caMap; } return 0; } This code fails because no serialize function is defined for the Key type. Can you point me to the code section that does the serialization of std::pair. Thanks, Christian

boost/serialization/utility.hpp since pair is in <utility> Christian Henning wrote:
Hi Robert, I would be interested in doing that. Here is what I would like to achieve:
typedef tuple< string, string, string > Key; typedef map< Key, int > MyMap;
int _tmain(int argc, _TCHAR* argv[]) { MyMap aMap;
{ const MyMap& caMap = aMap; ofstream ofs( "Settings.txt" ); boost::archive::text_oarchive oa( ofs );
oa & caMap; }
return 0; }
This code fails because no serialize function is defined for the Key type.
Can you point me to the code section that does the serialization of std::pair.
Thanks, Christian

Thanks Robert, here is my proof of concept for a triple ( tuple with 3 entries ). Any comments? I'm on MSVC 7.1. namespace boost { namespace serialization { // triple template<class Archive, class F, class S, class T> inline void serialize( Archive & ar, boost::tuple<F, S, T>& t, const unsigned int /* file_version */ ) { ar & boost::serialization::make_nvp("first" , t.get<0>() ); ar & boost::serialization::make_nvp("second", t.get<1>() ); ar & boost::serialization::make_nvp("third" , t.get<2>() ); } } // serialization } // namespace boost using namespace std; using namespace boost; typedef tuple< string, string, string > Key; typedef map< Key, int > MyMap; void print( pair<Key, int> oEntry ) { std::cout << oEntry.first; std::cout << " - " << oEntry.second << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { MyMap aMap; aMap[ make_tuple( "aa", "bb", "cc" ) ] = 90; { const MyMap& caMap = aMap; ofstream ofs( "Settings.txt" ); boost::archive::text_oarchive oa( ofs ); oa & caMap; } MyMap aMap_2; { ifstream ifs( "Settings.txt" ); boost::archive::text_iarchive ia( ifs ); ia & aMap_2; } for_each( aMap_2.begin(), aMap_2.end(), print ); return 0; } Regards, Christian

Hi Robert, I uploaded the code to the vault/serialization. The code allows to serialize tuples up to 10 values. I also included test cases. Regards, Christian
participants (2)
-
Christian Henning
-
Robert Ramey