Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlb_nullableallocatedvalue
[Package bdlb]

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

Namespaces

namespace  bdlb

Detailed Description

Outline
Purpose:
Provide a template for nullable allocated (out-of-place) objects.
Classes:
bdlb::NullableAllocatedValue template for nullable allocated objects
See also:
Component 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.
Note that, as a consequence, the object of template parameter TYPE that is managed by a bdlb::NullableAllocatedValue<TYPE> object is necessarily allocated out-of-place. This implies that an allocator is in effect for any bdlb::NullableAllocatedValue<TYPE> object, regardless of the TYPE. In particular, a bdlb::NullableAllocatedValue<int> object, for example, incurs use of the default allocator (in effect at the time of creation of the object) unless an alternative allocator is supplied at construction.
Usage:
The following snippets of code illustrate use of this component.
Suppose we want to create a linked list of nodes that contain integers:
  struct LinkedListNode {
      int                                          d_value;
      bdlb::NullableAllocatedValue<LinkedListNode> d_next;
  };
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:
  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;
  }