Creating and Using Deallocators
Passing ownership of a buffer to an
RWBasicUString involves supplying the
RWBasicUString with an
RWBasicUString::Deallocator object.
RWBasicUString::Deallocator is an abstract base class that cannot be instantiated directly. Instead, a deallocator can be created in one of two ways:
An
RWBasicUString::StaticDeallocator object wraps a pointer to a class static method or a global function. As a convenience,
RWBasicUString supplies three such functions:
USE_DELETE(),
USE_FREE(), and
USE_NONE(). For example, the following code creates an
RWBasicUString::StaticDeallocator that invokes
delete[] to deallocate string buffers. These buffers are returned from a third-party library that allocates buffers via
new:
// Create a deallocator. It will be re-used by multiple
// RWBasicUString instances.
RWBasicUString::StaticDeallocator
deallocator(RWBasicUString::USE_DELETE);
// Return RWBasicUStrings that reference externally-supplied
// buffers.
RWBasicUString
getStringFromOutsideSource()
{
RWUChar16 *array = callToOutsideSource();
return RWBasicUString(array, &deallocator);
}
The subclass can deallocate string buffers in the manner of its choice, to match the manner in which the buffers are allocated.
The use of
RWBasicUString::StaticDeallocator allows the client to choose
delete[],
free(), or custom memory-management mechanisms. The use of an externally supplied deallocation method can also be used to satisfy the heap management requirements of MS-Windows dynamic linked libraries, which in some situations may create their own heap in addition to that of the calling process.