I\'m creating a map (from string to string in this example) in shared memory using Boost.Interprocess. The compiler seems to want to force me, during retrieval from the map,
The following is adapted from the C++03-compatible code linked from @sehe's comment on vis own answer. Although I don't really know what I'm doing, I've generalized it in the way that seemed fitting for maps whose keys are not of the same type as their mapped values, by adding a second allocator type to the mutable_pair and map_gen definitions. I've made it clear which namespace everything comes from and removed the multimap_gen definition for brevity. It compiles to allow map< string vector-type objects in shared memory to be queried with const char *.
The one thing I wasn't sure of was whether ValueType needs to make an appearance in the rebind line, but it seems to compile and work as is so far...
#include
#include
#include
#include
#include
#include
#include
#include
namespace sehe
{
template < typename T1, typename T2, typename Alloc1, typename Alloc2 >
struct mutable_pair
{
typedef T1 first_type;
typedef T2 second_type;
mutable_pair( Alloc1 alloc1, Alloc2 alloc2 ) : first( T1( alloc1 ) ), second( T2( alloc2 ) ) {}
mutable_pair( const T1 & f, const T2 & s ) : first( f ), second( s ) {}
mutable_pair( const std::pair< T1, T2 > & p ) : first( p.first ), second( p.second ) {}
T1 first;
mutable T2 second;
};
template >
struct map_gen
{
typedef boost::multi_index::multi_index_container
<
Element,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_unique< boost::multi_index::member< Element, KeyType, &Element::first >, Compare >
>,
typename KeyAllocator::template rebind::other
> type;
};
typedef struct {
template bool operator()(const T &t, const U &u) const { return t < u; }
} Comparator;
}
// Typedefs of allocators and containers
namespace Shared
{
typedef boost::interprocess::managed_shared_memory Segment;
typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager;
typedef boost::interprocess::allocator Allocator;
typedef boost::interprocess::allocator CharAlloc;
typedef boost::interprocess::basic_string, CharAlloc> String;
typedef boost::interprocess::allocator< String, SegmentManager > StringAlloc;
typedef boost::interprocess::allocator< int, SegmentManager > IntAlloc;
typedef boost::interprocess::vector< int, IntAlloc > IntVector;
typedef boost::interprocess::allocator< IntVector, SegmentManager > IntVectorAlloc;
typedef sehe::map_gen< String, String, sehe::Comparator, StringAlloc, StringAlloc >::type StringStringMap;
typedef sehe::map_gen< String, IntVector, sehe::Comparator, StringAlloc, IntVectorAlloc >::type StringIntVectorMap;
}