////////////////////////////////////////////////////////////////////////////// // // (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 #if BOOST_CXX_VERSION >= 201103L //[doc_multi_index #include #include #include //<- //Shield against external warnings #include //-> #include #include #include //<- #include #include "../test/get_process_id_name.hpp" //-> using namespace boost::interprocess; namespace bmi = boost::multi_index; typedef managed_shared_memory::allocator::type char_allocator; typedef boost::container::basic_string, char_allocator>shm_string; //Data to insert in shared memory struct employee { typedef char_allocator allocator_type; //enables uses-allocator protocol int id; int age; shm_string name; employee( int id_ , int age_ , const char *name_ , const char_allocator &a) : id(id_), age(age_), name(name_, a) {} }; //Tags struct id{}; struct age{}; struct name{}; // Define a multi_index_container of employees with following indices: // - a unique index sorted by employee::int, // - a non-unique index sorted by employee::name, // - a non-unique index sorted by employee::age. typedef bmi::multi_index_container< employee, bmi::indexed_by< bmi::ordered_unique , bmi::member >, bmi::ordered_non_unique< bmi::tag, bmi::member >, bmi::ordered_non_unique , bmi::member > >, managed_shared_memory::allocator::type > employee_set; 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; //-> //Create shared memory managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); //Construct the multi_index in shared memory (classic construction) employee_set *es = segment.construct ("My MultiIndex Container") //Container's name in shared memory ( employee_set::ctor_args_list() , segment.get_allocator()); //Ctor parameters //Now insert elements char_allocator ca(segment.get_allocator()); es->insert(employee(0,31, "Joe", ca)); es->insert(employee(1,27, "Robert", ca)); es->insert(employee(2,40, "John", ca)); segment.destroy_ptr(es); //Now re-construct it using the uses-allocator protocol es = segment.construct ("My MultiIndex Container") //Container's name in shared memory ( employee_set::ctor_args_list() ); //Allocator parameters is implicit //Now emplace elements (more natural, the allocator is implicitly propagated) es->emplace(0,31, "Joe"); es->emplace(1,27, "Robert"); es->emplace(2,40, "John"); segment.destroy_ptr(es); return 0; } //] #else ////#if BOOST_CXX_VERSION >= 201103L int main() { return 0; } #endif //#if BOOST_CXX_VERSION >= 201103L