• 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 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
20 
21 #include <boost/config.hpp>
22 
23 #include <fstream>
24 #include <string>
25 #include <cassert>
26 
27 #include <boost/bimap/bimap.hpp>
28 
29 #include <boost/archive/text_oarchive.hpp>
30 #include <boost/archive/text_iarchive.hpp>
31 
32 using namespace boost::bimaps;
33 
main()34 int main()
35 {
36     //[ code_bimap_and_boost_serialization
37 
38     typedef bimap< std::string, int > bm_type;
39 
40     // Create a bimap and serialize it to a file
41     {
42         bm_type bm;
43         bm.insert( bm_type::value_type("one",1) );
44         bm.insert( bm_type::value_type("two",2) );
45 
46         std::ofstream ofs("data");
47         boost::archive::text_oarchive oa(ofs);
48 
49         oa << const_cast<const bm_type&>(bm); /*<
50             We must do a const cast because Boost.Serialization archives
51             only save const objects. Read Boost.Serializartion docs for the
52             rationale behind this decision >*/
53 
54         /*<< We can only serialize iterators if the bimap was serialized first.
55              Note that the const cast is not required here because we create
56              our iterators as const. >>*/
57         const bm_type::left_iterator left_iter = bm.left.find("two");
58         oa << left_iter;
59 
60         const bm_type::right_iterator right_iter = bm.right.find(1);
61         oa << right_iter;
62     }
63 
64     // Load the bimap back
65     {
66         bm_type bm;
67 
68         std::ifstream ifs("data", std::ios::binary);
69         boost::archive::text_iarchive ia(ifs);
70 
71         ia >> bm;
72 
73         assert( bm.size() == 2 );
74 
75         bm_type::left_iterator left_iter;
76         ia >> left_iter;
77 
78         assert( left_iter->first == "two" );
79 
80         bm_type::right_iterator right_iter;
81         ia >> right_iter;
82 
83         assert( right_iter->first == 1 );
84     }
85     //]
86 
87     return 0;
88 }
89 
90