Friday, October 23, 2009

Smart Pointer Advantages


3 4



Smart Pointer Advantages



A smart pointer is a class that encapsulates a raw interface pointer. It stands between the interface pointer and all code that wants to invoke methods on this interface. Such a class ought to provide its benefits in as unobtrusive a manner as possible—that is, an interface pointer encapsulated by a smart pointer should still feel like an interface pointer to the C++ programmer. To this end, smart pointers implement operators such as & and ->, allowing for COM+ API interaction and method access in precisely the same manner that a raw interface pointer would. Given all that, such an encapsulation class can be expected to provide the following benefits more or less invisibly, behind the scenes:




  • Automatic reference counting. A smart pointer should call AddRef when it acquires its raw interface pointer and Release when it releases it. This practice guarantees that all method invocations occur via a valid reference at whatever scope the smart pointer might have been instantiated.


  • Implicit type conversions. Environments such as Visual Basic and Java hide from developers the mechanics of obtaining one type of interface pointer from another. As long as an object supports both interfaces, the details of COM+'s QueryInterface-driven navigation from one interface to another via IUnknown are not interesting to the programmer—not even the C++ programmer. A smart pointer class can provide conversion operators for other smart pointer or raw interface pointer types.


  • Usage verifications. The smart pointer can detect a variety of incorrect or dangerous practices and either generate an assert failure or throw an exception. Such practices include using operators -> or * when the encapsulated interface pointer is NULL, using operator & when it is not, and so on. Instead of raising an error, the smart pointer could attempt to deal with the situation. In the latter case, for example, it might release the encapsulated raw interface pointer before returning its address.


  • Programming model simplifications. Instead of encapsulating the raw interface type, a smart pointer can encapsulate an extended version of the interface, which contains method wrappers and other adornments. These enhancements can include the following:


    • Exception generation on method invocation failure.


    • C++ data member access syntax for setting or retrieving interface properties.


    • Binding of interface ID to interface type.


    • Prevention of interface name collisions through namespaces.





While the use of such an enhanced interface is, strictly speaking, a topic separate from that of smart pointer classes, we deal with them together here because the compiler facility that generates smart pointer types is the same as the one that generates the enhanced interfaces. I will describe the compiler facility in more detail later in this chapter.



Another programming model simplification integrates the smart pointer interface with a portion of the COM+ API: COM+ object instancing can then be performed through methods on the smart pointer class (or through its constructor) instead of by taking the address of the encapsulated raw pointer and passing it to API functions.



No comments: