| 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_complex_map |
| 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> |
| 18 | //<- |
| 19 | #include "../test/get_process_id_name.hpp" |
| 20 | //-> |
| 21 | |
| 22 | using namespace boost::interprocess; |
| 23 | |
| 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; |
| 33 | |
| 34 | class complex_data |
| 35 | { |
| 36 | int id_; |
| 37 | char_string char_string_; |
| 38 | int_vector_vector int_vector_vector_; |
| 39 | |
| 40 | public: |
| 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) |
| 45 | {} |
| 46 | //Other members... |
| 47 | //<- |
| 48 | int get_id() { return id_; }; |
| 49 | char_string get_char_string() { return char_string_; }; |
| 50 | int_vector_vector get_int_vector_vector() { return int_vector_vector_; }; |
| 51 | //-> |
| 52 | }; |
| 53 | |
| 54 | //Definition of the map holding a string as key and complex_data as mapped type |
| 55 | typedef std::pair<const char_string, complex_data> map_value_type; |
| 56 | typedef std::pair<char_string, complex_data> movable_to_map_value_type; |
| 57 | typedef allocator<map_value_type, segment_manager_t> map_value_type_allocator; |
| 58 | typedef map< char_string, complex_data |
| 59 | , std::less<char_string>, map_value_type_allocator> complex_map_type; |
| 60 | |
| 61 | int main () |
| 62 | { |
| 63 | //Remove shared memory on construction and destruction |
| 64 | struct shm_remove |
| 65 | { |
| 66 | //<- |
| 67 | #if 1 |
| 68 | shm_remove() { shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 69 | ~shm_remove(){ shared_memory_object::remove(filename: test::get_process_id_name()); } |
| 70 | #else |
| 71 | //-> |
| 72 | shm_remove() { shared_memory_object::remove("MySharedMemory" ); } |
| 73 | ~shm_remove(){ shared_memory_object::remove("MySharedMemory" ); } |
| 74 | //<- |
| 75 | #endif |
| 76 | //-> |
| 77 | } remover; |
| 78 | //<- |
| 79 | (void)remover; |
| 80 | //-> |
| 81 | |
| 82 | //Create shared memory |
| 83 | //<- |
| 84 | #if 1 |
| 85 | managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); |
| 86 | #else |
| 87 | //-> |
| 88 | managed_shared_memory segment(create_only,"MySharedMemory" , 65536); |
| 89 | //<- |
| 90 | #endif |
| 91 | //-> |
| 92 | |
| 93 | //An allocator convertible to any allocator<T, segment_manager_t> type |
| 94 | void_allocator alloc_inst (segment.get_segment_manager()); |
| 95 | |
| 96 | //Construct the shared memory map and fill it |
| 97 | complex_map_type *mymap = segment.construct<complex_map_type> |
| 98 | //(object name), (first ctor parameter, second ctor parameter) |
| 99 | (name: "MyMap" )(std::less<char_string>(), alloc_inst); |
| 100 | |
| 101 | for(int i = 0; i < 100; ++i){ |
| 102 | //Both key(string) and value(complex_data) need an allocator in their constructors |
| 103 | char_string key_object(alloc_inst); |
| 104 | complex_data mapped_object(i, "default_name" , alloc_inst); |
| 105 | map_value_type value(key_object, mapped_object); |
| 106 | //Modify values and insert them in the map |
| 107 | mymap->insert(x&: value); |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | //] |
| 112 | |
| 113 | |