////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion GaztaƱaga 2004-2006. 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 #include #include #include #include #include #include #include "printcontainer.hpp" /*****************************************************************/ /* */ /* This example repeats the same operations with std::list and */ /* interprocess::list using the node allocator */ /* and compares the values of both containers */ /* */ /*****************************************************************/ using namespace boost::interprocess; //We will work with wide characters for shared memory objects //Alias node allocator type typedef node_allocator shmem_node_allocator_t; //Alias list types typedef list MyShmList; typedef std::list MyStdList; //Function to check if both lists are equal bool CheckEqual(MyShmList *shmlist, MyStdList *stdlist) { return std::equal(shmlist->begin(), shmlist->end(), stdlist->begin()); } int main () { const int memsize = 65536; const char *const shMemName = "MySharedMemory"; const int max = 100; //Named new capable shared mem allocator //Create shared memory shared_memory_object::remove(shMemName); wmanaged_shared_memory segment(create_only, shMemName, memsize); segment.reserve_named_objects(100); //Shared memory allocator must be always be initialized //since it has no default constructor MyShmList *shmlist = segment.construct(L"MyList") (segment.get_segment_manager()); MyStdList *stdlist = new MyStdList; int i; for(i = 0; i < max; ++i){ shmlist->push_back(i); stdlist->push_back(i); } if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->erase(shmlist->begin()++); stdlist->erase(stdlist->begin()++); if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->pop_back(); stdlist->pop_back(); if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->pop_front(); stdlist->pop_front(); if(!CheckEqual(shmlist, stdlist)) return 1; std::vector aux_vect; #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) aux_vect.assign(50, -1); shmlist->assign(aux_vect.begin(), aux_vect.end()); stdlist->assign(aux_vect.begin(), aux_vect.end()); if(!CheckEqual(shmlist, stdlist)) return 1; #endif shmlist->sort(); stdlist->sort(); if(!CheckEqual(shmlist, stdlist)) return 1; #if !BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) aux_vect.assign(50, 0); #endif shmlist->insert(shmlist->begin(), aux_vect.begin(), aux_vect.end()); stdlist->insert(stdlist->begin(), aux_vect.begin(), aux_vect.end()); shmlist->unique(); stdlist->unique(); shmlist->size(); if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->sort(std::greater()); stdlist->sort(std::greater()); if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->resize(shmlist->size()/2); stdlist->resize(stdlist->size()/2); if(!CheckEqual(shmlist, stdlist)) return 1; shmlist->remove(*shmlist->begin()); stdlist->remove(*stdlist->begin()); if(!CheckEqual(shmlist, stdlist)) return 1; for(i = 0; i < max; ++i){ shmlist->push_back(i); stdlist->push_back(i); } MyShmList othershmlist(*shmlist); MyStdList otherstdlist(*stdlist); shmlist->splice(shmlist->begin(), othershmlist); stdlist->splice(stdlist->begin(), otherstdlist); if(!CheckEqual(shmlist, stdlist)) return 1; othershmlist = *shmlist; otherstdlist = *stdlist; shmlist->merge(othershmlist, std::greater()); stdlist->merge(otherstdlist, std::greater()); if(!CheckEqual(shmlist, stdlist)) return 1; segment.destroy(L"MyList"); delete stdlist; return 0; } #include