1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
15 #include <boost/interprocess/containers/map.hpp>
16 #include <boost/interprocess/containers/vector.hpp>
17 #include <boost/interprocess/containers/string.hpp>
19 #include "../test/get_process_id_name.hpp"
22 using namespace boost::interprocess
;
24 //Typedefs of allocators and containers
25 typedef managed_shared_memory::segment_manager segment_manager_t
;
26 typedef allocator
<void, segment_manager_t
> void_allocator
;
27 typedef allocator
<int, segment_manager_t
> int_allocator
;
28 typedef vector
<int, int_allocator
> int_vector
;
29 typedef allocator
<int_vector
, segment_manager_t
> int_vector_allocator
;
30 typedef vector
<int_vector
, int_vector_allocator
> int_vector_vector
;
31 typedef allocator
<char, segment_manager_t
> char_allocator
;
32 typedef basic_string
<char, std::char_traits
<char>, char_allocator
> char_string
;
37 char_string char_string_
;
38 int_vector_vector int_vector_vector_
;
41 //Since void_allocator is convertible to any other allocator<T>, we can simplify
42 //the initialization taking just one allocator for all inner containers.
43 complex_data(int id
, const char *name
, const void_allocator
&void_alloc
)
44 : id_(id
), char_string_(name
, void_alloc
), int_vector_vector_(void_alloc
)
49 //Definition of the map holding a string as key and complex_data as mapped type
50 typedef std::pair
<const char_string
, complex_data
> map_value_type
;
51 typedef std::pair
<char_string
, complex_data
> movable_to_map_value_type
;
52 typedef allocator
<map_value_type
, segment_manager_t
> map_value_type_allocator
;
53 typedef map
< char_string
, complex_data
54 , std::less
<char_string
>, map_value_type_allocator
> complex_map_type
;
58 //Remove shared memory on construction and destruction
63 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
64 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
67 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
68 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
77 //Create shared memory
80 managed_shared_memory
segment(create_only
,test::get_process_id_name(), 65536);
83 managed_shared_memory
segment(create_only
,"MySharedMemory", 65536);
88 //An allocator convertible to any allocator<T, segment_manager_t> type
89 void_allocator
alloc_inst (segment
.get_segment_manager());
91 //Construct the shared memory map and fill it
92 complex_map_type
*mymap
= segment
.construct
<complex_map_type
>
93 //(object name), (first ctor parameter, second ctor parameter)
94 ("MyMap")(std::less
<char_string
>(), alloc_inst
);
96 for(int i
= 0; i
< 100; ++i
){
97 //Both key(string) and value(complex_data) need an allocator in their constructors
98 char_string
key_object(alloc_inst
);
99 complex_data
mapped_object(i
, "default_name", alloc_inst
);
100 map_value_type
value(key_object
, mapped_object
);
101 //Modify values and insert them in the map
102 mymap
->insert(value
);
107 #include <boost/interprocess/detail/config_end.hpp>