Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bslstl_function_rep
[Package bslstl]

Provide a non-template, common implementation for bsl::function. More...

Namespaces

namespace  bslstl

Detailed Description

Outline
Purpose:
Provide a non-template, common implementation for bsl::function.
Classes:
bslstl::Function_Rep Representation of a bsl::function object
See also:
Component bslstl_function
Description:
This private, subordinate component to bslstl_function provides a non-template class, Function_Rep, that is the data representation of bsl::function (see bslstl_function). The bsl::function class template uses bslstl::Function_Rep to store the callable held by the function (its target), the allocator, and a pointer to the function it uses to indirectly invoke the target (the invoker function).
The client of this component, bsl::function, is a complex class that is templated in two ways:
  1. The class itself has a template parameter representing the prototype (argument and return types) of its call operator. E.g., type bsl::function<int(char*)> has member int operator()(char*);.
  2. Several of its constructors are templated on a callable type and wrap an object of that type. By using type erasure, the type of the wrapped target is not part of the type of the bsl::function.
The Function_Rep class takes care of the runtime polymorphism required by (2), above. It stores the target object (which can be of any size), copy- or move-constructs it, destroys it, and returns its runtime type, size, and address. Nothing in Function_Rep is concerned with the call prototype.
Function_Rep is a quasi-value-semantic type: It doesn't provide copy and move constructors or assignment operators, but it does have the abstract notion of an in-memory value (the target object, if not empty) and it provides methods for copying, moving, swapping, and destroying that value. There is no ability to provide equality comparison because the wrapped object is not required to provide equality comparison operations.
Function_Rep has only one constructor, which creates an empty object (one with no target) using a specified allocator. The methods of Function_Rep are a collection of primitive operations for setting, getting, copy constructing, move constructing, or swapping the target object, as well as accessing the allocator and invoker function pointer. The methods to set and get the invoker pointer represent it as a generic function pointer (the closest we could get to void * for function pointers), so it is up to the caller to cast the pointer back to a specific function pointer type before invoking it.