| 1 | #include <boost/interprocess/managed_shared_memory.hpp>
|
|---|
| 2 | #include <boost/interprocess/allocators/allocator.hpp>
|
|---|
| 3 | #include <boost/interprocess/containers/map.hpp>
|
|---|
| 4 | #include <boost/interprocess/containers/vector.hpp>
|
|---|
| 5 | #include <boost/interprocess/containers/string.hpp>
|
|---|
| 6 |
|
|---|
| 7 | using namespace boost::interprocess;
|
|---|
| 8 |
|
|---|
| 9 | //Typedefs of allocators and containers
|
|---|
| 10 | typedef managed_shared_memory::segment_manager segment_manager_t;
|
|---|
| 11 | typedef allocator<void, segment_manager_t> void_allocator;
|
|---|
| 12 | typedef allocator<int, segment_manager_t> int_allocator;
|
|---|
| 13 | typedef vector<int, int_allocator> int_vector;
|
|---|
| 14 | typedef allocator<int_vector, segment_manager_t> int_vector_allocator;
|
|---|
| 15 | typedef vector<int_vector, int_vector_allocator> int_vector_vector;
|
|---|
| 16 | typedef allocator<char, segment_manager_t> char_allocator;
|
|---|
| 17 | typedef basic_string<char, std::char_traits<char>, char_allocator> char_string;
|
|---|
| 18 |
|
|---|
| 19 | class complex_data
|
|---|
| 20 | {
|
|---|
| 21 | int id_;
|
|---|
| 22 | char_string char_string_;
|
|---|
| 23 | int_vector_vector int_vector_vector_;
|
|---|
| 24 |
|
|---|
| 25 | public:
|
|---|
| 26 | //Since void_allocator is convertible to any other allocator<T>, we can simplify
|
|---|
| 27 | //the initialization taking just one allocator for all inner containers.
|
|---|
| 28 | complex_data(int id, const char *name, const void_allocator &void_alloc)
|
|---|
| 29 | : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
|
|---|
| 30 | {}
|
|---|
| 31 | //Other members...
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | //Definition of the map holding a string as key and complex_data as mapped type
|
|---|
| 35 | typedef std::pair<const char_string, complex_data> map_value_type;
|
|---|
| 36 | typedef std::pair<char_string, complex_data> movable_to_map_value_type;
|
|---|
| 37 | typedef allocator<map_value_type, segment_manager_t> map_value_type_allocator;
|
|---|
| 38 | typedef map< char_string, complex_data
|
|---|
| 39 | , std::less<char_string>, map_value_type_allocator> complex_map_type;
|
|---|
| 40 |
|
|---|
| 41 | int main ()
|
|---|
| 42 | {
|
|---|
| 43 | shared_memory_object::remove("MySharedMemory");
|
|---|
| 44 | remove_shared_memory_on_destroy remove_on_destroy("MySharedMemory");
|
|---|
| 45 | {
|
|---|
| 46 | //Create shared memory
|
|---|
| 47 | managed_shared_memory segment(create_only,"MySharedMemory", 65536);
|
|---|
| 48 |
|
|---|
| 49 | //An allocator convertible to any allocator<T, segment_manager_t> type
|
|---|
| 50 | void_allocator alloc_inst (segment.get_segment_manager());
|
|---|
| 51 |
|
|---|
| 52 | //Construct the shared memory map and fill it
|
|---|
| 53 | complex_map_type *mymap = segment.construct<complex_map_type>
|
|---|
| 54 | //(object name), (first ctor parameter, second ctor parameter)
|
|---|
| 55 | ("MyMap")(std::less<char_string>(), alloc_inst);
|
|---|
| 56 |
|
|---|
| 57 | for(int i = 0; i < 100; ++i){
|
|---|
| 58 | //Both key(string) and value(complex_data) need an allocator in their constructors
|
|---|
| 59 | char_string key_object(alloc_inst);
|
|---|
| 60 | complex_data mapped_object(i, "default_name", alloc_inst);
|
|---|
| 61 | map_value_type value(key_object, mapped_object);
|
|---|
| 62 | //Modify values and insert them in the map
|
|---|
| 63 | mymap->insert(value);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return 0;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|