beautypg.com

HP Integrity NonStop H-Series User Manual

Page 204

background image

Standard C++ Library allocators, otherwise you must use the alternative.

The first place that you use RWSTD_ALLOCATOR is when determining which typenames the
container must use to reflect the interface. To do this, place the equivalent of the following code
in your container class definition:

#ifdef RWSTD_ALLOCATOR
typedef typename Allocator::types::reference
reference;
typedef typename Allocator::types::const_reference
const_reference;
typedef typename Allocator::types::pointer
link_type;
Allocator the_allocator;
#else
typedef typename
allocator_interface::reference reference;
typedef typename
allocator_interface::const_reference
const_reference;
typedef typename
allocator_interface::pointer link_type;
Allocator alloc;
allocator_interface value_allocator;
allocator_interface node_allocator;
#endif

Notice that the alternative allocator (allocator_interface) has two parts: value_allocator and
node_allocator. You will need to assemble these inside the constructor for your container, if you
use the alternative allocator. In our example, the mechanism for initializing allocator_interface
objects looks like this:

#ifndef RWSTD_ALLOCATOR
node_allocator.alloc(alloc);
value_allocator.alloc(alloc);
#endif

Let's look at some examples of how we support both interfaces in calls to functions.

In this first example, the max_size member function will use the appropriate allocator object.

size_type max_size () const
#ifdef RWSTD_ALLOCATOR
{ return the_allocator.max_size(); }
#else
{ return node_allocator.max_size(); }
#endif

A second example shows the use of the construct and address allocator functions to construct a

This manual is related to the following products: