Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
p6comptr.h
Go to the documentation of this file.
1 
10 #ifndef P6COMPTR_H__
11 #define P6COMPTR_H__ 1
12 
13 #include "p6runtimeapi.h"
14 
15 #ifdef __cplusplus
16 namespace P6R {
17 
26  template<typename I>
27  class p6ComPtrNoAddrefRelease : public I
28  {
29  P6NOEXPORT P6R::P6INT32 P6STDCALL addref() = 0;
30  P6NOEXPORT P6R::P6INT32 P6STDCALL release() = 0;
31  };
32 
42  template<typename I> class p6ComPtrBase
43  {
44  protected:
45 
51  p6ComPtrBase(I *pRawPtr = NULL)
52  : m_pRaw(pRawPtr)
53  {
54  if(m_pRaw) { m_pRaw->addref(); }
55  }
56 
58  {
59  //P6R::p6ICom* pTmp = m_pRaw;
60  I* pTmp = m_pRaw;
61 
62  m_pRaw = NULL;
63  if(pTmp) {
64  pTmp->release();
65  }
66  }
67 
68  //private:
69  // This causes compilation errors with stlpd_std::list<> on windows VC++ 2005
70  // We'd like to make this private to prevent people from doing the wrong
71  // thing with address of. Using this operator can cause leaks when p6ComPtr
72  // has already been assigned an interface pointer. The pointer will
73  // not be released before it is potentially overwritten. addressofWithRelease()
74  // should be used instead.
75  //const p6ComPtrBase<I>* operator&() const { return this; }
76 
77  public:
78  typedef I element_type;
79 
81 
82  operator I*() const { return static_cast<I*>(m_pRaw); }
83  I& operator*() const { P6ASSERT(NULL != m_pRaw); return *static_cast<I*>(m_pRaw); }
85  P6BOOL operator==(I* pI) const { return (pI == m_pRaw) ? P6TRUE:P6FALSE; }
86  P6BOOL operator!=(I* pI) const { return (pI != m_pRaw) ? P6TRUE:P6FALSE; }
87  P6BOOL operator<(I* pI) const { return (m_pRaw < pI) ? P6TRUE:P6FALSE; }
88  P6BOOL operator!() const { return (NULL == m_pRaw) ? P6TRUE:P6FALSE; }
89 
90  inline I** addressof()
91  {
92  return reinterpret_cast<I**>(&m_pRaw);
93  }
94 
95  inline I** addressofWithRelease()
96  {
97  release();
98  return reinterpret_cast<I**>(&m_pRaw);
99  }
100 
109  inline P6VOID swap(I** ppIface)
110  {
111  I *tmp = *ppIface;
112  *ppIface = reinterpret_cast<I*>(m_pRaw);
113  m_pRaw = tmp;
114  }
115 
120  {
121  I *tmp = rhs.m_pRaw;
122  rhs.m_pRaw = m_pRaw;
123  m_pRaw = tmp;
124  }
125 
126  inline I* get() const
127  {
128  return reinterpret_cast<I*>(m_pRaw);
129  }
130 
136  void attach(I* pIface)
137  {
138  release();
139  m_pRaw = pIface;
140  }
141 
148  inline I* detach()
149  {
150  I* p = m_pRaw;
151  m_pRaw = NULL;
152  return p;
153  }
154 
163  inline P6VOID detach(I** ppIface)
164  {
165  *ppIface = NULL;
166  this->swap(ppIface);
167  }
168 
182  inline P6R::P6ERR createInstance(P6R::p6ICom *pOuter,const P6REFCID cid,const P6REFIID riid)
183  {
184  release();
185  return p6CreateInstance(pOuter,cid,riid,reinterpret_cast<P6VOID**>(&m_pRaw));
186  }
187 
198  {
199  release();
200  return p6GetRuntimeIface(riid,reinterpret_cast<P6VOID**>(&m_pRaw));
201  }
202 
216  template<typename Q> P6R::P6ERR queryInterface(const P6REFIID riid,Q** ppIface)
217  {
218  P6ASSERT(NULL != m_pRaw);
219  return m_pRaw->queryInterface(riid,reinterpret_cast<P6VOID**>(ppIface));
220  }
221 
222  protected:
223  I *m_pRaw;
224  };
225 
232  template<typename I> class p6ComPtr : public p6ComPtrBase<I>
233  {
234  public:
235  typedef I element_type;
240  explicit p6ComPtr(I* ip = NULL) : p6ComPtrBase<I>(ip)
241  { }
242 
249  p6ComPtr(const P6REFCID rclsid,const P6REFIID riid,P6R::P6ERR *pErr = NULL) : p6ComPtrBase<I>(NULL)
250  {
251  P6ERR err = p6ComPtrBase<I>::createInstance(NULL,rclsid,riid);
252  P6ASSERT(eOk == err);
253  if(pErr) (*pErr) = err;
254  }
255 
256  p6ComPtr(const P6REFIID riid,P6R::P6ERR *pErr = NULL)
257  {
259  P6ASSERT(eOk == err);
260  if(pErr) (*pErr) = err;
261  }
262 
272  p6ComPtr(p6ICom *pCom,const P6REFIID riid,P6R::P6ERR *pErr = NULL) : p6ComPtrBase<I>(0)
273  {
274  p6ComPtr<I> tmp;
275  P6R::P6ERR err = pCom->queryInterface(riid,(P6VOID**)tmp.addressof());
276  P6ASSERT(eOk == err);
277  if(pErr) (*pErr) = err;
278  if(P6SUCCEEDED(err)) p6ComPtrBase<I>::swap(tmp);
279  }
280 
284  p6ComPtr(const p6ComPtr<I>& rSI) : p6ComPtrBase<I>(rSI.m_pRaw)
285  { }
286 
291  {
292  if(*this != ip) {
293  p6ComPtr<I> tmp(ip);
294  this->swap(tmp);
295  }
296  return *this;
297  }
298 
303  {
304  if(*this!=cp) {
305  p6ComPtr<I> tmp(cp);
306  this->swap(tmp);
307  }
308  return *this;
309  }
310  };
311 
317  template<typename Interface>
319  { left.swap(right); }
320 
327  template<> class p6ComPtr<p6ICom> : public p6ComPtrBase<p6ICom>
328  {
329  public:
331 
336  : p6ComPtrBase<p6ICom>(NULL)
337  {
338  }
339 
343  explicit p6ComPtr(p6ICom *pI)
344  : p6ComPtrBase<p6ICom>(pI)
345  {
346  }
347 
351  p6ComPtr(const p6ComPtr<p6ICom> &smartPtr)
352  : p6ComPtrBase<p6ICom>(smartPtr.m_pRaw)
353  {
354  }
355 
356  operator p6ICom*() const
357  {
358  return get();
359  }
360 
365  {
366  if(*this != ip) {
367  p6ComPtr<p6ICom> tmp(ip);
368  this->swap(tmp);
369  }
370  return *this;
371  }
372 
377  {
378  if(*this!=cp) {
379  p6ComPtr<p6ICom> tmp(cp);
380  this->swap(tmp);
381  }
382  return *this;
383  }
384  };
385 
386 
430 #define VALIDATECOMPTR(type,smartComPtr) IID_##type, reinterpret_cast<P6R::P6VOID**>(static_cast<type**>((smartComPtr).addressofWithRelease()))
431 
432 
433 } // namespace
434 #endif // __cplusplus
435 
436 #endif
437 
#define P6STDCALL
Definition: p6defs.h:123
#define P6TRUE
Definition: p6types.h:134
A smart pointer implementation to help manage and use [p6]COM based interfaces.
Definition: p6comptr.h:232
P6API P6ERR P6CCALL p6GetRuntimeIface(const P6R::P6IID &iid, P6VOID **ppIface)
This method is used to retreive any of the loader runtime interfaces.
P6VOID swap(p6ComPtr< Interface > &left, p6ComPtr< Interface > &right)
In order for generic algorithms to find p6ComPtr&#39;s swap() implementation, we must also provide a non-...
Definition: p6comptr.h:318
#define P6REFIID
Definition: p6defs.h:197
I ** addressof()
Definition: p6comptr.h:90
p6ComPtrBase(I *pRawPtr=NULL)
Default constructor.
Definition: p6comptr.h:51
I * detach()
Detach the owned inteface from the smart pointer and return a dumb pointer without release() directly...
Definition: p6comptr.h:148
p6ComPtr< I > & operator=(I *ip)
Assign from a dumb pointer of the correct type.
Definition: p6comptr.h:290
unsigned char P6BOOL
Boolean type.
Definition: p6types.h:133
P6R::P6ERR createInstance(P6R::p6ICom *pOuter, const P6R::P6CID &cid, const P6R::P6IID &riid)
Create a new component instance and assign the result to this smart pointer.
Definition: p6comptr.h:182
P6BOOL operator!=(I *pI) const
Definition: p6comptr.h:86
p6ComPtr(const P6R::P6IID &riid, P6R::P6ERR *pErr=NULL)
Definition: p6comptr.h:256
P6VOID swap(p6ComPtrBase< I > &rhs)
Exchange ownership.
Definition: p6comptr.h:119
p6ComPtr(p6ICom *pCom, const P6R::P6IID &riid, P6R::P6ERR *pErr=NULL)
Construct from the specified interface via queryInterface() This will query the interface passed in p...
Definition: p6comptr.h:272
P6VOID detach(I **ppIface)
Detach the owned interface from the smart pointer without release().
Definition: p6comptr.h:163
#define P6NOEXPORT
Definition: p6defs.h:120
I & operator*() const
Definition: p6comptr.h:83
p6ComPtr(p6ICom *pI)
Contruct from a dumb pointer of the correct type.
Definition: p6comptr.h:343
p6ComPtr()
Default constructor.
Definition: p6comptr.h:335
P6ERR(P6CCALL * P6ASSERT)(const P6WCHAR *pszExpr, const P6CHAR *pszFile, P6UINT32 nLine)
Definition: p6dllapi.h:58
I ** addressofWithRelease()
Definition: p6comptr.h:95
void attach(I *pIface)
Take ownership of a new interface without addref().
Definition: p6comptr.h:136
P6COM runtime interfaces definitions.
p6ComPtr(const p6ComPtr< I > &rSI)
Copy.
Definition: p6comptr.h:284
P6R::P6ERR getRuntimeIface(const P6R::P6IID &riid)
Get an instance of a runtime interface and adding the result to this smart pointer.
Definition: p6comptr.h:197
virtual P6R::P6ERR queryInterface(const P6R::P6IID &iid, P6VOID **ppIface)=0
This method queries the component for a specific interface.
The base interface all [p6]COM components must derive from and implement.
Definition: p6comdef.h:97
P6API P6ERR P6CCALL p6CreateInstance(p6ICom *pOuter, const P6R::P6CID &cid, const P6R::P6IID &iid, P6VOID **ppIface)
Creates a single uninitialized instance of the class/interface associated with the specified componen...
p6ComPtr< I > & operator=(const p6ComPtr< I > &cp)
Assign from a smart pointer of the correct type.
Definition: p6comptr.h:302
P6BOOL operator==(I *pI) const
Definition: p6comptr.h:85
A template specialization to support the p6ICom interface.
Definition: p6comptr.h:327
p6ComPtr(I *ip=NULL)
Construct from a raw pointer of the correct type Must be derrived from p6ICom.
Definition: p6comptr.h:240
P6VOID release()
Definition: p6comptr.h:57
p6ComPtrNoAddrefRelease< I > * operator->() const
Definition: p6comptr.h:84
p6ComPtr< p6ICom > & operator=(const p6ComPtr< p6ICom > &cp)
Assign from a smart pointer of the correct type.
Definition: p6comptr.h:376
p6ComPtr< p6ICom > & operator=(p6ICom *ip)
Assign from a dumb pointer of the correct type.
Definition: p6comptr.h:364
P6BOOL operator<(I *pI) const
Definition: p6comptr.h:87
P6UINT32 P6ERR
COM err return type see P6ERR.h.
Definition: p6types.h:141
Base class which implements common functionality for p6ComPtr and it&#39;s p6ICom specialization.
Definition: p6comptr.h:42
P6R::P6ERR queryInterface(const P6R::P6IID &riid, Q **ppIface)
A type safe version of queryInterface().
Definition: p6comptr.h:216
#define P6SUCCEEDED(err)
Definition: p6err.h:49
P6BOOL operator!() const
Definition: p6comptr.h:88
Template used to make the addref() and release() methods inaccessable when dereferncing a p6ComPtr&lt;&gt; ...
Definition: p6comptr.h:27
p6ComPtr(const P6R::P6CID &rclsid, const P6R::P6IID &riid, P6R::P6ERR *pErr=NULL)
Construct a new instance given the component ID and interface ID.
Definition: p6comptr.h:249
#define P6REFCID
Definition: p6defs.h:196
void P6VOID
Definition: p6types.h:109
int32_t P6INT32
Definition: p6types.h:76
p6ComPtr(const p6ComPtr< p6ICom > &smartPtr)
Copy constructor.
Definition: p6comptr.h:351
P6VOID swap(I **ppIface)
Exchange ownership.
Definition: p6comptr.h:109
#define P6FALSE
Definition: p6types.h:135