////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006-2009. 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 #include #include #include //[doc_complex_map #include #include // bma #include #include #include #include //<- // #include "../test/get_process_id_name.hpp" //-> using namespace boost::interprocess; //Typedefs of allocators and containers //typedef managed_shared_memory::segment_manager segment_manager_t; typedef managed_mapped_file::segment_manager segment_manager_t; typedef allocator void_allocator; typedef allocator int_allocator; typedef vector int_vector; typedef allocator int_vector_allocator; typedef vector int_vector_vector; typedef allocator char_allocator; typedef basic_string, char_allocator> char_string; typedef std::less char_comparator; class complex_data { public: int id_; char_string char_string_; // vector of vectors of int: [ [1,2,3], [4,5], ... ] int_vector_vector int_vector_vector_; int_vector int_vector_; //Since void_allocator is convertible to any other allocator, we can simplify //the initialization taking just one allocator for all inner containers. complex_data(int id, const char *name, const void_allocator &void_alloc) : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc), int_vector_( 10, -1, void_alloc ), f_map_( char_comparator(), void_alloc ) {} //Other members... typedef std::pair map_value_type; typedef std::pair movable_to_map_value_type; typedef allocator field_map_value_allocator; typedef map< char_string, int, char_comparator, field_map_value_allocator > field_map; field_map f_map_; }; //Definition of the map holding a string as key and complex_data as mapped type typedef std::pair map_value_type; typedef std::pair movable_to_map_value_type; typedef allocator map_value_type_allocator; typedef map< char_string, complex_data , char_comparator, map_value_type_allocator> complex_map_type; template void print( const T& container ) { std::cout << "[ "; BOOST_FOREACH( const typename T::value_type& x, container ) { std::cout << x << " "; } std::cout << "]"; } int main () { //Remove shared memory on construction and destruction /*struct shm_remove { //<- #if 1 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } #else //-> shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } //<- #endif //-> } remover;*/ struct test_struct { void operator()( complex_data& x ) { /*x.f_map_.clear(); std::cout << "x f_map size: " << x.f_map_.size() << std::endl;*/ // BUGGY std::cout << "id: " << x.id_ << " v of v of ints size: " << x.int_vector_.size(); x.int_vector_.push_back( (int)getpid() ); std::cout << " pid pushed back: " << x.int_vector_.size() << std::endl; print( x.int_vector_ ); std::cout << std::endl; } } test_struct; try { //Create shared memory //<- #if 1 //managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); managed_mapped_file segment( open_or_create, "doc_complex_map.MEM", 65536 ); #else //-> managed_shared_memory segment(create_only,"MySharedMemory", 65536); //<- #endif //-> //An allocator convertible to any allocator type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.find_or_construct //(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less(), alloc_inst); for(int i = 0; i < 100; ++i){ char v_name[32]; snprintf( v_name, sizeof( v_name ), "v_%d", i ); //Both key(string) and value(complex_data) need an allocator in their constructors char_string key_object( v_name, alloc_inst); complex_data mapped_object(i, "default_name", alloc_inst); map_value_type value(key_object, mapped_object); //Modify values and insert them in the map std::pair ret = mymap->insert(value); test_struct( ret.first->second ); } std::cout << "my map size: " << mymap->size() << std::endl; return 0; } catch ( const interprocess_exception& e ) { std::cout << e.what() << std::endl; } } //] #include