|
BDE 4.14.0 Production release
|
Provide efficient creation of nodes used in a node-based container.
This component implements a mechanism, BidirectionalNodePool, that creates and destroys bslalg::BidirectionalListNode objects holding objects of a (template parameter) type VALUE for use in hash-table-based containers.
A BidirectionalNodePool uses a memory pool provided by the bslstl_simplepool component in its implementation to provide memory for the nodes (see bslstl_simplepool ).
BidirectionalNodePool uses an allocator of the (template parameter) type ALLOCATOR specified at construction to allocate memory. BidirectionalNodePool supports allocators meeting the requirements of the C++ standard allocator requirements ([allocator.requirements], C++11 17.6.3.5).
If ALLOCATOR is bsl::allocator and the (template parameter) type VALUE defines the bslma::UsesBslmaAllocator trait, then the bslma::Allocator object specified at construction will be supplied to constructors of the (template parameter) type VALUE in the cloneNode method and emplaceIntoNewNode method overloads.
This section illustrates intended use of this component.
Suppose that we want to define a bidirectional linked list that can hold elements of a template parameter type. bslstl::BidirectionalNodePool can be used to create and destroy nodes that make up a linked list.
First, we create an elided definition of the class template MyList:
Now, we define the methods of MyMatrix:
Here, we call the memory pool's deleteNode method to destroy the value attribute of the node and return its memory footprint back to the pool:
Here, we call the memory pool's emplaceIntoNewNode method to allocate a node and copy-construct the specified value at the value attribute of the node:
Note that the memory pool will allocate the footprint of the node using the allocator specified at construction. If the (template parameter) type ALLOCATOR is an instance of bsl::allocator and the (template parameter) type VALUE has the bslma::UsesBslmaAllocator trait, then the allocator specified at construction will also be supplied to the copy-constructor of VALUE.
Here, just like how we implemented the pushFront method, we call the pool's emplaceIntoNewNode method to allocate a node and copy-construct the specified value at the value attribute of the node: