////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #include //[doc_complex_map #include #include #include #include #include //<- #include "../test/get_process_id_name.hpp" //-> using namespace boost::interprocess; namespace bc = boost::container; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager seg_mngr_t; typedef allocator void_alloc_t; typedef bc::vector > int_vec_t; typedef bc::vector > int_vec_vec_t; typedef bc::basic_string , allocator > string_t; class complex_data { string_t string_; int_vec_vec_t int_vec_vec_; public: complex_data(const char *name, const void_alloc_t &valloc) //void_alloc_t is convertible to allocator : string_(name, valloc), int_vec_vec_(valloc) {} //<- string_t get_char_string() { return string_; }; int_vec_vec_t get_int_vector_vector() { return int_vec_vec_; }; //-> }; //Definition of a shared memory map typedef std::pair map_value_type; typedef allocator map_value_type_allocator; typedef bc::map< string_t, complex_data, std::less , map_value_type_allocator> complex_map_type; int main () { //<- //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } } remover; (void)remover; //-> managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); //Create shared memory //An allocator convertible to any allocator type void_alloc_t alloc_inst (segment.get_segment_manager()); //Construct the map calling map(key_compare, allocator_type), the allocator argument is explicit complex_map_type *mymap = segment.construct ("MyMap")(std::less(), alloc_inst); //Both key(string) and value(complex_data) need an allocator in their constructors string_t key_object(alloc_inst); complex_data mapped_object("default_name", alloc_inst); map_value_type value(key_object, mapped_object); //Modify values and insert them in the map mymap->insert(value); return 0; } //]