Ticket #7927: bimap.c++

File bimap.c++, 2.1 KB (added by jdy@…, 10 years ago)

sample code showing bimap w/ interprocess failure

Line 
1
2#include<iostream>
3#include<utility>
4#include<functional>
5
6#include <boost/interprocess/allocators/allocator.hpp>
7#include <boost/interprocess/managed_mapped_file.hpp>
8#include <boost/bimap.hpp>
9
10int main(int argc, char** argv) {
11
12 typedef int key_t;
13 typedef int pay_t;
14
15 typedef typename std::pair<key_t, pay_t> val_t;
16
17 typedef boost::interprocess::managed_mapped_file segment_t;
18 typedef segment_t::segment_manager segment_manager_t;
19
20 typedef boost::interprocess::allocator< val_t, segment_manager_t> allocator_t;
21
22 typedef boost::bimaps::bimap< key_t, pay_t, allocator_t> map_t;
23
24 {
25 std::cout << "Writing" << std::endl;
26 segment_t segment(boost::interprocess::open_or_create,"scratch.bimap",10000);
27 allocator_t allocator(segment.get_segment_manager());
28
29 map_t* map = segment.find_or_construct<map_t>("bimap")(allocator);
30
31 std::cout << "There were " << map->size() << " relations" << std::endl;
32 for( map_t::const_iterator iter = map->begin(); iter != map->end(); ++iter ) {
33 std::cout << iter->left << " <--> " << iter->right << std::endl;
34 }
35
36 map->insert( map_t::value_type(1, 'z' ) );
37 map->insert( map_t::value_type(2, 'Z' ) );
38
39 std::cout << "There are " << map->size() << " relations" << std::endl;
40 for( map_t::const_iterator iter = map->begin(), iend = map->end();
41 iter != iend; ++iter ) {
42 std::cout << iter->left << " <--> " << iter->right << std::endl;
43 }
44 }
45
46 {
47 std::cout << "Reading" << std::endl;
48 segment_t* segment = new segment_t(boost::interprocess::open_read_only,"scratch.bimap");
49 allocator_t* allocator = new allocator_t(segment->get_segment_manager());
50
51 map_t* map = segment->find<map_t>("bimap").first;
52
53 std::cout << "There were " << map->size() << " relations" << std::endl;
54 for( map_t::const_iterator iter = map->begin(); iter != map->end(); ++iter ) {
55 std::cout << iter->left << " <--> " << iter->right << std::endl;
56 }
57
58 delete allocator;
59 delete segment;
60 }
61
62 // segment_t::shrink_to_fit("scratch.bimap");
63
64 return 0;
65}