Outline
Purpose
Provide a non-template, common implementation for bsl::function
.
Classes
- See also
- 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:
- 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*);
.
- 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.
◆ Function_Rep()
bslstl::Function_Rep::Function_Rep |
( |
const allocator_type & |
allocator | ) |
|
|
inlineexplicit |
Create an empty object using the specified allocator
to supply memory.
◆ functionManager()
template<class FUNC , bool INPLACE>
bslstl::Function_Rep::ManagerRet bslstl::Function_Rep::functionManager |
( |
ManagerOpCode |
opCode, |
|
|
Function_Rep * |
rep, |
|
|
void * |
srcVoidPtr |
|
) |
| |
◆ get_allocator()
◆ installFunc()
Do nothing if the specified func
is a null pointer, otherwise allocate storage (either in-place within this object's small-object buffer or out-of-place from the allocator) to hold a target of (template parameter) type FUNC
, forward the func
to the constructor of the new target, set d_funcManager_p
to manage the new target, and set d_invoker_p
to the specified invoker
address. The behavior is undefined unless this object is empty on entry. Note that FUNC
will not qualify for the small-object optimization unless bsl::is_nothrow_move_constructible<FUNC>::value
is true
.
◆ invoker()
Return a pointer the invoker function set using installFunc
or a null pointer if this object is empty.
◆ isEmpty()
bool bslstl::Function_Rep::isEmpty |
( |
| ) |
const |
|
inline |
Return true
if invoker()
is a null pointer, indicating that this object has no target object.
◆ ManagerRet() [1/2]
bslstl::Function_Rep::ManagerRet::ManagerRet |
( |
std::size_t |
s | ) |
|
|
inline |
◆ ManagerRet() [2/2]
bslstl::Function_Rep::ManagerRet::ManagerRet |
( |
void * |
p | ) |
|
|
inline |
◆ operator std::size_t()
bslstl::Function_Rep::ManagerRet::operator std::size_t |
( |
| ) |
const |
|
inline |
Return the size_t
stored in this union. The behavior is undefined unless this object was constructed with a size_t
.
◆ operator TP *()
template<class TP >
bslstl::Function_Rep::ManagerRet::operator TP * |
( |
| ) |
const |
|
inline |
Return the pointer stored in this union. The behavior is undefined unless this object was constructed with a TP *
.
◆ target()
template<class TP >
TP * bslstl::Function_Rep::target |
( |
| ) |
const |
|
inline |
If typeid(TP) == this->target_type()
, return a pointer offering modifiable access to this object's target; otherwise return a null pointer. Note that this function is const
but returns a non-const
pointer because, according to the C++ Standard, function
(and therefore this representation) does not adhere to logical constness conventions.
◆ targetRaw()
template<class TP , bool INPLACE>
TP * bslstl::Function_Rep::targetRaw |
( |
| ) |
const |
|
inline |
Return a pointer offering modifiable access to this object's target. If INPLACE
is true, then the object is assumed to be allocated inplace in the small object buffer. Note that this function is const
but returns a non-const
pointer because this type does not adhere to logical constness conventions. The behavior is undefined unless typeid(TP) == this->target_type()
and INPLACE
correctly identifies whether the target is inplace.