In short, my question is: If you have class, MyClass<T>, how can you change the class definition to support cases where you have MyClass<T, Alloc>, similar to how, say, STL vector provides.
I need this functionality to support an allocator for shared memory. Specifically, I am trying to implement a ring buffer in shared memory. Currently it has the following ctor:
template<typename ItemType> SharedMemoryBuffer<ItemType>::SharedMemoryBuffer( unsigned long capacity, std::string name ) where ItemType is the type of the data to be placed in each slot of the buffer.
Now, this works splendid when I create the buffer from the main program thus
SharedMemoryBuffer<int>* sb; sb = new SharedMemoryBuffer<int>(BUFFER_CAPACITY + 1, sharedMemoryName); However, in this case the buffer itself is not created in shared memory and so is not accessible to other processes. What I want to do is to be able to do something like
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; typedef SharedMemoryBuffer<int, ShmemAllocator> MyBuffer; managed_shared_memory segment(create_only, "MySharedMemory", 65536); const ShmemAllocator alloc_inst (segment.get_segment_manager()); MyBuffer *mybuf = segment.construct<MyBuffer>("MyBuffer")(alloc_inst); However, I don't know how to go about adding an explicit allocator to the class template.
shm_addris avoid*pointer to shared memory you can doMyBuffer *pBuf = new (shm_Addr) MyBuffer;and the newMyBufferwill be constructed at the given location.