|
BDE 4.14.0 Production release
|
Provide an utility that adapts callable objects to functors.
This component provides a single utility template, FunctorAdapter, that adapts a parameterized adaptee type, which can be any callable object type, to a target functor type. This adaptation enables a client to inherit from the target functor type even if the adaptee callable object type is a function pointer type. This is particularly useful if the client of the callable object type wants to take advantage of the empty-base optimization to avoid paying storage cost when the callable object type is a functor type with no data members.
FunctorAdapter defines an alias to the target functor type. If the adaptee type is a functor type, the target type is an alias to the adaptee type. If the adaptee type is a function pointer type, the target type is a functor type that delegates to a function referred to by a function pointer of the adaptee type.
This section illustrates the intended use of this component.
Suppose that we wanted to define a binder that binds a binary predicate of a parameterized type to a value passed on construction. Also suppose that we wanted to use the empty-base optimization to avoid paying storage cost when the predicate type is a functor type with no data members. Unfortunately, the binary predicate type may be a function pointer type, which cannot serve as a base class. The solution is to have the binder inherit from FunctorAdapter::Type, which adapts a function pointer type to a functor type that is a suitable base class.
First, we define the class Bind2ndInteger, which inherits from FunctorAdapter::Type to take advantage of the empty-base optimization:
Then, we implement the methods of the Bind2ndInteger class:
Here, we implement the operator() member function that simply delegates to BINARY_PREDICATE
Next, we define a function, intCompareFunction, that compares two integers:
Now, we define a Bind2ndInteger object functorLessThan10 using the std::less<int> functor as the parameterized BINARY_PREDICATE and invoke the function call operator:
Finally, we define a Bind2ndInteger object functionLessThan10 passing the address of intCompareFunction on construction and invoke the function call operator: