beautypg.com

HP Integrity NonStop H-Series User Manual

Page 202

background image

// Alternative allocator uses an interface class
// (allocator_interface)
// to get type safety.
//
class my_allocator
{
public:
typedef implementation_defined size_type;
typedef implementation_defined difference_type;
my_allocator();
~my_allocator();
void * allocate (size_type n, void * = 0);
void deallocate (void* p);
size_type max_size (size_type size) const
};

We've also included a listing of the full implementation of the allocator_interface class, to show
how a standard container will use your class. The section entitled "Building Containers &
Generic Algorithms" provides a full description of how the containers use the alternative
interface.

template
class allocator_interface
{
public:
typedef Allocator allocator_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
protected:
allocator_type* alloc_;
public:
allocator_interface() : alloc_(0) { ; }
allocator_interface(Allocator* a) : alloc_(a) { ; }
void alloc(Allocator* a)
{
alloc_ = a;
}
pointer address (T& x)
{
return static_cast(&x);
}

This manual is related to the following products: