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 namespace P6R {
14 
24  template<typename I> class p6ComPtrBase
25  {
26  protected:
27 
33  p6ComPtrBase(I *pRawPtr = NULL)
34  : m_pRaw(pRawPtr)
35  {
36  if(m_pRaw) { m_pRaw->addref(); }
37  }
38 
40  {
41  //P6R::p6ICom* pTmp = m_pRaw;
42  I* pTmp = m_pRaw;
43 
44  m_pRaw = NULL;
45  if(pTmp) {
46  pTmp->release();
47  }
48  }
49 
50  //private:
51  // This causes compilation errors with stlpd_std::list<> on windows VC++ 2005
52  // We'd like to make this private to prevent people from doing the wrong
53  // thing with address of. Using this operator can cause leaks when p6ComPtr
54  // has already been assigned an interface pointer. The pointer will
55  // not be released before it is potentially overwritten. addressofWithRelease()
56  // should be used instead.
57  //const p6ComPtrBase<I>* operator&() const { return this; }
58 
59  public:
60  typedef I element_type;
61 
63 
64  operator I*() const { return static_cast<I*>(m_pRaw); }
65  I& operator*() const { P6ASSERT(NULL != m_pRaw); return *static_cast<I*>(m_pRaw); }
66  I* operator->() const { P6ASSERT(NULL != m_pRaw); return static_cast<I*>(m_pRaw); }
67  P6BOOL operator==(I* pI) const { return (pI == m_pRaw) ? P6TRUE:P6FALSE; }
68  P6BOOL operator!=(I* pI) const { return (pI != m_pRaw) ? P6TRUE:P6FALSE; }
69  P6BOOL operator<(I* pI) const { return (m_pRaw < pI) ? P6TRUE:P6FALSE; }
70  P6BOOL operator!() const { return (NULL == m_pRaw) ? P6TRUE:P6FALSE; }
71 
72  inline I** addressof()
73  {
74  return reinterpret_cast<I**>(&m_pRaw);
75  }
76 
77  inline I** addressofWithRelease()
78  {
79  release();
80  return reinterpret_cast<I**>(&m_pRaw);
81  }
82 
91  inline P6VOID swap(I** ppIface)
92  {
93  I *tmp = *ppIface;
94  *ppIface = reinterpret_cast<I*>(m_pRaw);
95  m_pRaw = tmp;
96  }
97 
102  {
103  I *tmp = rhs.m_pRaw;
104  rhs.m_pRaw = m_pRaw;
105  m_pRaw = tmp;
106  }
107 
108  inline I* get() const
109  {
110  return reinterpret_cast<I*>(m_pRaw);
111  }
112 
118  void attach(I* pIface)
119  {
120  release();
121  m_pRaw = pIface;
122  }
123 
130  inline I* detach()
131  {
132  I* p = m_pRaw;
133  m_pRaw = NULL;
134  return p;
135  }
136 
145  inline P6VOID detach(I** ppIface)
146  {
147  *ppIface = NULL;
148  swap(ppIface);
149  }
150 
164  inline P6R::P6ERR createInstance(P6R::p6ICom *pOuter,const P6R::CID &cid,const P6R::IID &riid)
165  {
166  release();
167  return p6CreateInstance(pOuter,cid,riid,reinterpret_cast<P6VOID**>(&m_pRaw));
168  }
169 
179  inline P6R::P6ERR getRuntimeIface(const P6R::IID &riid)
180  {
181  release();
182  return p6GetRuntimeIface(riid,reinterpret_cast<P6VOID**>(&m_pRaw));
183  }
184 
198  template<typename Q> P6R::P6ERR queryInterface(const P6R::IID &riid,Q** ppIface)
199  {
200  P6ASSERT(NULL != m_pRaw);
201  return m_pRaw->queryInterface(riid,reinterpret_cast<P6VOID**>(ppIface));
202  }
203 
204  protected:
205  I *m_pRaw;
206  };
207 
214  template<typename I> class p6ComPtr : public p6ComPtrBase<I>
215  {
216  public:
217  typedef I element_type;
222  explicit p6ComPtr(I* ip = NULL) : p6ComPtrBase<I>(ip)
223  { }
224 
231  p6ComPtr(const P6R::CID &rclsid,const P6R::IID &riid,P6R::P6ERR *pErr = NULL) : p6ComPtrBase<I>(NULL)
232  {
233  P6ERR err = p6ComPtrBase<I>::createInstance(NULL,rclsid,riid);
234  P6ASSERT(eOk == err);
235  if(pErr) (*pErr) = err;
236  }
237 
238  p6ComPtr(const P6R::IID &riid,P6R::P6ERR *pErr = NULL)
239  {
241  P6ASSERT(eOk == err);
242  if(pErr) (*pErr) = err;
243  }
244 
254  p6ComPtr(p6ICom *pCom,const P6R::IID &riid,P6R::P6ERR *pErr = NULL) : p6ComPtrBase<I>(0)
255  {
256  p6ComPtr<I> tmp;
257  P6R::P6ERR err = pCom->queryInterface(riid,(P6VOID**)tmp.addressof());
258  P6ASSERT(eOk == err);
259  if(pErr) (*pErr) = err;
260  if(P6SUCCEEDED(err)) p6ComPtrBase<I>::swap(tmp);
261  }
262 
266  p6ComPtr(const p6ComPtr<I>& rSI) : p6ComPtrBase<I>(rSI.m_pRaw)
267  { }
268 
273  {
274  if(*this != ip) {
275  p6ComPtr<I> tmp(ip);
276  swap(tmp);
277  }
278  return *this;
279  }
280 
285  {
286  if(*this!=cp) {
287  p6ComPtr<I> tmp(cp);
288  swap(tmp);
289  }
290  return *this;
291  }
292  };
293 
300  template<> class p6ComPtr<p6ICom> : public p6ComPtrBase<p6ICom>
301  {
302  public:
304 
309  : p6ComPtrBase<p6ICom>(NULL)
310  {
311  }
312 
316  explicit p6ComPtr(p6ICom *pI)
317  : p6ComPtrBase<p6ICom>(pI)
318  {
319  }
320 
324  p6ComPtr(const p6ComPtr<p6ICom> &smartPtr)
325  : p6ComPtrBase<p6ICom>(smartPtr.m_pRaw)
326  {
327  }
328 
329  operator p6ICom*() const
330  {
331  return get();
332  }
333 
338  {
339  if(*this != ip) {
340  p6ComPtr<p6ICom> tmp(ip);
341  swap(tmp);
342  }
343  return *this;
344  }
345 
350  {
351  if(*this!=cp) {
352  p6ComPtr<p6ICom> tmp(cp);
353  swap(tmp);
354  }
355  return *this;
356  }
357  };
358 
359 
403 #define VALIDATECOMPTR(type,smartComPtr) IID_##type, reinterpret_cast<P6R::P6VOID**>(static_cast<type**>((smartComPtr).addressofWithRelease()))
404 
405 
406 
407 } // namespace
408 
409 #endif
410 
#define P6TRUE
Definition: p6types.h:102
P6R::P6ERR createInstance(P6R::p6ICom *pOuter, const P6R::CID &cid, const P6R::IID &riid)
Create a new component instance and assign the result to this smart pointer.
Definition: p6comptr.h:164
A smart pointer implementation to help manage and use [p6]COM based interfaces.
Definition: p6comptr.h:214
P6R::P6ERR(* P6ASSERT)(const P6R::P6WCHAR *pszExpr, const P6R::P6CHAR *pszFile, P6R::P6UINT32 nLine)
Definition: p6dllapi.h:57
I ** addressof()
Definition: p6comptr.h:72
P6API P6R::P6ERR p6CreateInstance(P6R::p6ICom *pOuter, const P6R::CID &cid, const P6R::IID &iid, P6R::P6VOID **ppIface)
Creates a single uninitialized instance of the class/interface associated with the specified componen...
p6ComPtrBase(I *pRawPtr=NULL)
Default constructor.
Definition: p6comptr.h:33
I * detach()
Detach the owned inteface from the smart pointer and return a dumb pointer without release() directly...
Definition: p6comptr.h:130
p6ComPtr< I > & operator=(I *ip)
Assign from a dumb pointer of the correct type.
Definition: p6comptr.h:272
bool P6BOOL
Boolean type.
Definition: p6types.h:101
P6BOOL operator!=(I *pI) const
Definition: p6comptr.h:68
P6VOID swap(p6ComPtrBase< I > &rhs)
Exchange ownership.
Definition: p6comptr.h:101
P6VOID detach(I **ppIface)
Detach the owned interface from the smart pointer without release().
Definition: p6comptr.h:145
I & operator*() const
Definition: p6comptr.h:65
P6R::P6ERR getRuntimeIface(const P6R::IID &riid)
Get an instance of a runtime interface and adding the result to this smart pointer.
Definition: p6comptr.h:179
p6ComPtr(p6ICom *pI)
Contruct from a dumb pointer of the correct type.
Definition: p6comptr.h:316
p6ComPtr()
Default constructor.
Definition: p6comptr.h:308
I ** addressofWithRelease()
Definition: p6comptr.h:77
virtual P6R::P6ERR queryInterface(const P6R::IID &iid, P6R::P6VOID **ppIface)=0
This method queries the component for a specific interface.
void attach(I *pIface)
Take ownership of a new interface without addref().
Definition: p6comptr.h:118
p6ComPtr(const p6ComPtr< I > &rSI)
Copy.
Definition: p6comptr.h:266
The base interface all [p6]COM components must derive from and implement.
Definition: p6comdef.h:96
p6ComPtr< I > & operator=(const p6ComPtr< I > &cp)
Assign from a smart pointer of the correct type.
Definition: p6comptr.h:284
P6BOOL operator==(I *pI) const
Definition: p6comptr.h:67
A template specialization to support the p6ICom interface.
Definition: p6comptr.h:300
p6ComPtr(I *ip=NULL)
Construct from a raw pointer of the correct type Must be derrived from p6ICom.
Definition: p6comptr.h:222
P6VOID release()
Definition: p6comptr.h:39
p6ComPtr< p6ICom > & operator=(const p6ComPtr< p6ICom > &cp)
Assign from a smart pointer of the correct type.
Definition: p6comptr.h:349
A universally unique indentifier (UUID).
Definition: p6types.h:131
P6R::P6ERR queryInterface(const P6R::IID &riid, Q **ppIface)
A type safe version of queryInterface().
Definition: p6comptr.h:198
p6ComPtr< p6ICom > & operator=(p6ICom *ip)
Assign from a dumb pointer of the correct type.
Definition: p6comptr.h:337
p6ComPtr(const P6R::CID &rclsid, const P6R::IID &riid, P6R::P6ERR *pErr=NULL)
Construct a new instance given the component ID and interface ID.
Definition: p6comptr.h:231
P6BOOL operator<(I *pI) const
Definition: p6comptr.h:69
P6UINT32 P6ERR
COM err return type see P6ERR.h.
Definition: p6types.h:109
Base class which implements common functionality for p6ComPtr and it&#39;s p6ICom specialization.
Definition: p6comptr.h:24
p6ComPtr(const P6R::IID &riid, P6R::P6ERR *pErr=NULL)
Definition: p6comptr.h:238
I * operator->() const
Definition: p6comptr.h:66
#define P6SUCCEEDED(err)
Definition: p6err.h:50
P6API P6R::P6ERR p6GetRuntimeIface(const P6R::IID &iid, P6R::P6VOID **ppIface)
This method is used to retreive any of the loader runtime interfaces.
P6BOOL operator!() const
Definition: p6comptr.h:70
void P6VOID
Definition: p6types.h:75
p6ComPtr(p6ICom *pCom, const P6R::IID &riid, P6R::P6ERR *pErr=NULL)
Construct from the specified interface via queryInterface() This will query the interface passed in p...
Definition: p6comptr.h:254
p6ComPtr(const p6ComPtr< p6ICom > &smartPtr)
Copy constructor.
Definition: p6comptr.h:324
P6VOID swap(I **ppIface)
Exchange ownership.
Definition: p6comptr.h:91
#define P6FALSE
Definition: p6types.h:103