Defining a Template for Managing Ownership of Interfaces

Note

Indexing Service is no longer supported as of Windows XP and is unavailable for use as of Windows 8. Instead, use Windows Search for client side search and Microsoft Search Server Express for server side search.

 

The following code segment defines a template to aid in the management of interface ownership. The sample uses this template with the ICommand, ICommandTree, IRowset, IAccessor, and other interfaces.

template<class T> class XInterface
{
public:
    XInterface( T * p = 0 ) : _p( p ) {}
    ~XInterface() { if ( 0 != _p ) _p->Release(); }
    T * operator->() { return _p; }
    T * GetPointer() const { return _p; }
    IUnknown ** GetIUPointer() { return (IUnknown **) &amp;_p; }
    T ** GetPPointer() { return &amp;_p; }
    void ** GetQIPointer() { return (void **) &amp;_p; }
    T * Acquire() { T * p = _p; _p = 0; return p; }
    BOOL IsNull() { return ( 0 == _p ); }

private:
    T * _p;
};