I'm creating a class which uses a custom buffer. I want to offer the possibility to pass an external memory address (for higher interoperability between languages) or (for convenience) to specify a custom allocator
type. The following code outlines what I mean:
template<typename int_type, class alloc = void>
class uses_custom_buffers
: uses_custom_buffers<int_type, void>
{
public:
uses_custom_buffers<int_type, alloc>* set_buffer(std::size_t count, std::size_t size)
{
typename alloc::rebind<int_type*>::other pointer_alloc;
int_type** buffer = pointer_alloc.allocate(count);
for (std::size_t i = 0; i < count; ++i)
buffer[i] = this->m_alloc.allocate(size);
this->uses_custom_buffers<int_type, void>::set_buffer(count, buffer, size);
return this;
}
private:
using uses_custom_buffers<int_type, void>::set_buffer;
alloc m_alloc;
};
template<typename int_type>
class uses_custom_buffers<int_type, void>
{
public:
uses_custom_buffers<int_type, void>* set_buffer(std::size_t count, int_type** buffers, std::size_t size)
{
this->m_buf = buffers;
this->m_count = count;
this->m_size = size;
return this;
}
private:
int_type** m_buf;
std::size_t m_count,
m_size;
};
Please note: This example doesn't care about deallocating any resource or exception safeness (to simplify matters). Do you see any kind of problems with that design?
uses_custom_buffers
? And is it an option to follow the STL allocator design? – Kerrek SB Jun 25 '11 at 10:49typename alloc::template rebind<int_type*>::other pointer_alloc;
– Kerrek SB Jun 25 '11 at 12:10