1//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
4// Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// See http://www.boost.org/libs/interprocess for documentation.
8//
9//////////////////////////////////////////////////////////////////////////////
10
11#include <boost/interprocess/detail/workaround.hpp>
12//[doc_map
13#include <boost/interprocess/managed_shared_memory.hpp>
14#include <boost/interprocess/containers/map.hpp>
15#include <boost/interprocess/allocators/allocator.hpp>
16#include <functional>
17#include <utility>
18//<-
19#include "../test/get_process_id_name.hpp"
20//->
21
22int main ()
23{
24 using namespace boost::interprocess;
25
26 //Remove shared memory on construction and destruction
27 struct shm_remove
28 {
29 //<-
30 #if 1
31 shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); }
32 ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); }
33 #else
34 //->
35 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
36 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
37 //<-
38 #endif
39 //->
40 } remover;
41 //<-
42 (void)remover;
43 //->
44
45 //Shared memory front-end that is able to construct objects
46 //associated with a c-string. Erase previous shared memory with the name
47 //to be used and create the memory segment at the specified address and initialize resources
48 //<-
49 #if 1
50 managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
51 #else
52 //->
53 managed_shared_memory segment
54 (create_only
55 ,"MySharedMemory" //segment name
56 ,65536); //segment size in bytes
57 //<-
58 #endif
59 //->
60
61 //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
62 //so the allocator must allocate that pair.
63 typedef int KeyType;
64 typedef float MappedType;
65 typedef std::pair<const int, float> ValueType;
66
67 //Alias an STL compatible allocator of for the map.
68 //This allocator will allow to place containers
69 //in managed shared memory segments
70 typedef allocator<ValueType, managed_shared_memory::segment_manager>
71 ShmemAllocator;
72
73 //Alias a map of ints that uses the previous STL-like allocator.
74 //Note that the third parameter argument is the ordering function
75 //of the map, just like with std::map, used to compare the keys.
76 typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;
77
78 //Initialize the shared memory STL-compatible allocator
79 ShmemAllocator alloc_inst (segment.get_segment_manager());
80
81 //Construct a shared memory map.
82 //Note that the first parameter is the comparison function,
83 //and the second one the allocator.
84 //This the same signature as std::map's constructor taking an allocator
85 MyMap *mymap =
86 segment.construct<MyMap>(name: "MyMap") //object name
87 (std::less<int>() //first ctor parameter
88 ,alloc_inst); //second ctor parameter
89
90 //Insert data in the map
91 for(int i = 0; i < 100; ++i){
92 mymap->insert(x: std::pair<const int, float>(i, (float)i));
93 }
94 return 0;
95}
96//]
97
98

source code of boost/libs/interprocess/example/doc_map.cpp