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 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
12 //[doc_unordered_map
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15
16 //<-
17 //Shield against external warnings
18 #include <boost/interprocess/detail/config_external_begin.hpp>
19 //->
20
21 #include <boost/unordered_map.hpp> //boost::unordered_map
22
23 //<-
24 #include <boost/interprocess/detail/config_external_end.hpp>
25 #include "../test/get_process_id_name.hpp"
26 //->
27
28 #include <functional> //std::equal_to
29 #include <boost/functional/hash.hpp> //boost::hash
30
31 //<-
32 #include "../test/get_process_id_name.hpp"
33 //->
34
main()35 int main ()
36 {
37 using namespace boost::interprocess;
38 //Remove shared memory on construction and destruction
39 struct shm_remove
40 {
41 //<-
42 #if 1
43 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
44 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
45 #else
46 //->
47 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
48 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
49 //<-
50 #endif
51 //->
52 } remover;
53 //<-
54 (void)remover;
55 //->
56
57 //Create shared memory
58 //<-
59 #if 1
60 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
61 #else
62 //->
63 managed_shared_memory segment(create_only, "MySharedMemory", 65536);
64 //<-
65 #endif
66 //->
67
68 //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
69 //so the allocator must allocate that pair.
70 typedef int KeyType;
71 typedef float MappedType;
72 typedef std::pair<const int, float> ValueType;
73
74 //Typedef the allocator
75 typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
76
77 //Alias an unordered_map of ints that uses the previous STL-like allocator.
78 typedef boost::unordered_map
79 < KeyType , MappedType
80 , boost::hash<KeyType> ,std::equal_to<KeyType>
81 , ShmemAllocator>
82 MyHashMap;
83
84 //Construct a shared memory hash map.
85 //Note that the first parameter is the initial bucket count and
86 //after that, the hash function, the equality function and the allocator
87 MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap") //object name
88 ( 3, boost::hash<int>(), std::equal_to<int>() //
89 , segment.get_allocator<ValueType>()); //allocator instance
90
91 //Insert data in the hash map
92 for(int i = 0; i < 100; ++i){
93 myhashmap->insert(ValueType(i, (float)i));
94 }
95 return 0;
96 }
97 //]
98 #include <boost/interprocess/detail/config_end.hpp>
99