• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 #include <boost/config.hpp>
19 
20 // std
21 #include <set>
22 #include <map>
23 #include <cstddef>
24 #include <cassert>
25 #include <algorithm>
26 #include <sstream>
27 #include <algorithm>
28 
29 #include <boost/core/lightweight_test.hpp>
30 
31 // Boost
32 #include <boost/archive/text_oarchive.hpp>
33 #include <boost/archive/text_iarchive.hpp>
34 
35 // Boost.Bimap
36 #include <boost/bimap/bimap.hpp>
37 
38 
39 template< class Bimap, class Archive >
save_bimap(const Bimap & b,Archive & ar)40 void save_bimap(const Bimap & b, Archive & ar)
41 {
42     using namespace boost::bimaps;
43 
44     ar << b;
45 
46     const typename Bimap::left_const_iterator left_iter = b.left.begin();
47     ar << left_iter;
48 
49     const typename Bimap::const_iterator iter = ++b.begin();
50     ar << iter;
51 }
52 
53 
54 
55 
test_bimap_serialization()56 void test_bimap_serialization()
57 {
58     using namespace boost::bimaps;
59 
60     typedef bimap<int,double> bm;
61 
62     std::set< bm::value_type > data;
63     data.insert( bm::value_type(1,0.1) );
64     data.insert( bm::value_type(2,0.2) );
65     data.insert( bm::value_type(3,0.3) );
66     data.insert( bm::value_type(4,0.4) );
67 
68     std::ostringstream oss;
69 
70     // Save it
71     {
72         bm b;
73 
74         b.insert(data.begin(),data.end());
75 
76         boost::archive::text_oarchive oa(oss);
77 
78         save_bimap(b,oa);
79     }
80 
81     // Reload it
82     {
83         bm b;
84 
85         std::istringstream iss(oss.str());
86         boost::archive::text_iarchive ia(iss);
87 
88         ia >> b;
89 
90         BOOST_TEST( std::equal( b.begin(), b.end(), data.begin() ) );
91 
92         bm::left_const_iterator left_iter;
93 
94         ia >> left_iter;
95 
96         BOOST_TEST( left_iter == b.left.begin() );
97 
98         bm::const_iterator iter;
99 
100         ia >> iter;
101 
102         BOOST_TEST( iter == ++b.begin() );
103     }
104 
105 }
106 
107 
main()108 int main()
109 {
110     test_bimap_serialization();
111     return boost::report_errors();
112 }
113 
114