BDE 4.14.0 Production release
|
Functions | |
template<class TO_TYPE , class FROM_TYPE > | |
static TO_TYPE | bsls::PointerCastUtil::cast (FROM_TYPE from) |
Provide function to cast between function and data pointers.
This component, bsls::PointerCastUtil
, provides a utility function to allow casting between function and data pointers without triggering compiler warnings. Such casts are legal in the latest C++ standard, but were not always so.
This section illustrates intended use of this component.
Suppose there is an event-handling service that requires registration of a combination of object and closure value, and that invokes a method on the object, passing back the closure.
First we define the service and its handler:
Then, we want to define a handler that will receive a function pointer as the closure object and invoke it. In order to do that, we must cast it to a function pointer, but some compilers may not allow it. We can use bsls::PointerCastUtil::cast
to accomplish this:
Next, we will set up a sample service and our handler function:
Finally, we will register our handler and then trigger events to verify that our handler is recording them correctly. To register the function pointer as a closure object, we must cast it to a data pointer. Again, we can use bsls::PointerCastUtil::cast
to accomplish this:
|
inlinestatic |
Return the specified from
value cast to TO_TYPE
, casting it in two steps, first to an integer type the size of a pointer and then to the target type. This function is intended to be used to cast between function and data pointers, as doing such a cast directly was once illegal. The behavior is undefined unless both FROM_TYPE
and TO_TYPE
are not larger than the intermediate integer type.
Static asserts ensuring that neither FROM_TYPE
nor TO_TYPE
is larger than the intermediate integer type. Note that bslmf_assert cannot be used here because of package dependency rules.