BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_nullableallocatedvalue

Detailed Description

Outline

Purpose

Provide a template for nullable allocated (out-of-place) objects.

Classes

See also
bdlb_nullablevalue

Description

This component provides a template class, bdlb::NullableAllocatedValue<TYPE>, that has nearly the same interface as bdlb::NullableValue (see bdlb_nullablevalue ), but, in contrast with that template class, the implementation of bdlb::NullableAllocatedValue does not require that the TYPE parameter be a complete type when the class is instantiated. However, the template parameter TYPE must be complete when methods of the class (and free operators) are instantiated.

For small types (no larger than a pointer) with simple alignment needs, the object is embedded into the NullableAllocatedValue object. For types that do not fit these requirements, the object of template parameter TYPE that is managed by a bdlb::NullableAllocatedValue<TYPE> object is allocated out-of-place.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

Suppose we want to create a linked list of nodes that contain integers:

struct LinkedListNode {
int d_value;
};
Definition bdlb_nullableallocatedvalue.h:174

Note that bdlb::NullableValue<LinkedListNode> cannot be used for d_next because bdlb::NullableValue requires that the template parameter TYPE be a complete type when the class is instantiated.

We can now traverse a linked list and add a new value at the end using the following code:

/// Add the specified `value` to the end of the list that contains the
/// specified `node`.
void addValueAtEnd(LinkedListNode *node, int value)
{
while (!node->d_next.isNull()) {
node = &node->d_next.value();
}
node->d_next.makeValue();
node = &node->d_next.value();
node->d_value = value;
}