Boost logo

Boost Users :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2006-07-20 12:03:35


Hi Berenguer,

flat_map has the same interface as map so if you have more problems, try
to make an small example using map. Regarding flat_xxx family:

The comparison function must compare keys, not mapped types:

std::less<size_t>

The allocator must allocate pairs:

allocator <std::pair<size_t, myShmNode*>, segment_manager>

if you were using boost::shmem::map the stored objects are const keys so
you would need

allocator <std::pair<const size_t, myShmNode*>, segment_manager>

This is your modified example. Compiles fine in Visual 7.1. I left
commented the code I've changed:

#include <vector>
#include <boost/shmem/named_shared_object.hpp>
#include <boost/shmem/containers/string.hpp>
#include <boost/shmem/containers/flat_set.hpp>
#include <boost/shmem/containers/flat_map.hpp>
#include <boost/shmem/allocators/allocator.hpp>
#include <boost/shmem/offset_ptr.hpp>
#include <boost/functional/hash.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>

class myShmNode
{
         //whatever
};

typedef boost::shmem::allocator
    <std::pair<std::size_t, myShmNode *>/*myShmNode **/
    ,boost::shmem::named_shared_object::segment_manager>
       myShmNodePtrAllocator;

typedef boost::shmem::flat_multimap
    <size_t
    ,myShmNode*
    ,std::less<std::size_t/*myShmNode**/>
    ,myShmNodePtrAllocator> MyMultiMap;

int main(int argc, char *argv[])
{
    boost::shmem::named_shared_object segment;
    segment.create( "/MySharedMemory", 1048576 );

    //Initialize shared memory STL-compatible allocators
    myShmNodePtrAllocator nodesPtrsAllocator(segment.get_segment_manager());

    std::less<std::size_t/*myShmNode* */ > comparator2;
    MyMultiMap* ptrsMultiMap
       = segment.construct<MyMultiMap>
          (boost::shmem::anonymous_instance)
             (comparator2,nodesPtrsAllocator);

    typedef std::pair<size_t,myShmNode*> multiMapNodeType;
    multiMapNodeType multiMapNode;

    myShmNode* myNode;
    multiMapNode.first = 1;
    multiMapNode.second = myNode;

    ptrsMultiMap->insert(multiMapNode);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net