The com_ptr encapsulates the COM interface pointer as a "smart pointer". It is conceptually similar to boost::counted_ptr, but based on COM reference counting. While it looks like Microsoft ATL's _com_ptr_t, it is far simpler with fewer loopholes and gotchas.
AddRef is always only done at construction and Release at destruction, thus typical C++ usage takes care of reference-counting issues. Swaps, resets and assignment operators all create internal temporaries to preserve these invariants, ensuring the object does not leak references.
Comparisons use COM equality rules: the interface pointers are converted to IUnknown pointers for comparison.
On Mac OS X, you can use this to access a CFPlugIn.
| T | The COM interface type. |
Public Types | |
| typedef T | element_type |
| The COM interface type. | |
Public Member Functions | |
| template<typename T2> | |
| com_ptr (const com_ptr< T2 > &other) | |
| Constructs from a different interface pointer other. | |
| com_ptr (com_ptr &other) | |
| Constructs a copy of other. | |
| com_ptr (element_type *ptr=NULL) | |
| Constructs from a raw COM interface ptr. | |
| element_type * | get () const |
| Returns the raw interface pointer. | |
| element_type & | operator * () const |
| Returns a reference to the COM type. | |
| element_type * | operator-> () const |
| Returns a pointer to the COM type. | |
| template<typename T2> | |
| com_ptr & | operator= (const com_ptr< T2 > &other) |
| Assigns from a different interface pointer other. | |
| com_ptr & | operator= (const com_ptr &other) |
| Assigns from other. | |
| void | reset (element_type *ptr=NULL) const |
| Resets the pointer to point to ptr. | |
| void | swap (com_ptr &other) |
| Swaps interface pointer with other. | |
| ~com_ptr () | |
| Destructs the pointer. | |
Related Functions | |
| (Note that these are not member functions.) | |
| bool | operator!= (const com_ptr< T1 > &left, const com_ptr< T2 > &right) |
| Checks whether the IUnknown pointer of left is not equal to the IUnknown pointer of right. | |
| bool | operator< (const com_ptr< T1 > &left, const com_ptr< T2 > &right) |
| Checks whether the IUnknown pointer of left is less than the IUnknown pointer of right. | |
| bool | operator== (const com_ptr< T1 > &left, const com_ptr< T2 > &right) |
| Checks whether the IUnknown pointer of left is equal to the IUnknown pointer of right. | |
| void | swap (com_ptr< T > &left, com_ptr< T > &right) |
| Swaps the interface pointers of left and right. | |