Quick Links: |
Provide function to cast between function and data pointers. More...
Namespaces | |
namespace | bsls |
Functions | |
template<class TO_TYPE , class FROM_TYPE > | |
static TO_TYPE | bsls::PointerCastUtil::cast (FROM_TYPE from) |
bsls::PointerCastUtil | namespace for pointer-casting function |
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. struct Handler { virtual void handle(void *closure) = 0; }; class Service { Handler *d_handler_p; void *d_closure_p; public: void registerHandler(Handler *handler, void *closure = 0) { d_handler_p = handler; d_closure_p = closure; } void eventOccurred() { d_handler_p->handle(d_closure_p); } };
bsls::PointerCastUtil::cast
to accomplish this: struct MyHandler : Handler { void handle(void *closure) { bsls::PointerCastUtil::cast<void(*)()>(closure)(); } };
Service aService; static int counter = 0; void event() { ++counter; }
bsls::PointerCastUtil::cast
to accomplish this: MyHandler ah; aService.registerHandler(&ah, bsls::PointerCastUtil::cast<void *>(event)); aService.eventOccurred(); aService.eventOccurred(); assert(counter == 2);
TO_TYPE bsls::PointerCastUtil::cast | ( | FROM_TYPE | from | ) | [inline, static, inherited] |
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.
References BSLS_ANNOTATION_UNUSED.