TComPtr.h
Go to the documentation of this file.
1 #ifndef iwin_TComPtr_included
2 #define iwin_TComPtr_included
3 
4 
5 // ATL includes
6 #include <objbase.h>
7 
8 // ACF includes
9 #include "istd/TPointerBase.h"
10 
11 
12 namespace iwin
13 {
14 
15 
19 template <class Type>
20 class TComPtr: public istd::TPointerBase<Type>
21 {
22 public:
24 
28  TComPtr(Type* ptr = NULL, bool addRef = true);
29 
34  TComPtr(const TComPtr& ptr);
35 
40  ~TComPtr();
41 
45  bool CoCreateInstance(REFCLSID rclsid, IUnknown* outerPtr = NULL, quint32 clsContext = CLSCTX_ALL);
46 
50  void Reset();
51 
55  void SetPtr(Type* ptr, bool addRef = true);
56 
60  Type* PopPtr();
65  void TakeOver(TComPtr& sourcePtr);
66 
70  void IncRef();
74  void DecRef();
75 
79  TComPtr& operator=(const TComPtr& ptr);
83  Type** operator&();
84 };
85 
86 
87 // inline methods
88 
89 template <class Type>
90 inline TComPtr<Type>::TComPtr(Type* ptr, bool addRef)
91 : BaseClass(ptr)
92 {
93  if (addRef){
94  IncRef();
95  }
96 }
97 
98 
99 template <class Type>
101 : BaseClass(ptr)
102 {
103  IncRef();
104 }
105 
106 
107 template <class Type>
109 {
110  DecRef();
111 }
112 
113 
114 template <class Type>
115 inline void TComPtr<Type>::Reset()
116 {
117  SetPtr(NULL);
118 }
119 
120 
121 template <class Type>
122 inline void TComPtr<Type>::SetPtr(Type* ptr, bool addRef)
123 {
124  DecRef();
125 
126  BaseClass::SetPtr(ptr);
127 
128  if (addRef){
129  IncRef();
130  }
131 }
132 
133 
134 template <class Type>
135 inline Type* TComPtr<Type>::PopPtr()
136 {
137  Type* slavePtr = BaseClass::GetPtr();
138  BaseClass::SetPtr(NULL);
139 
140  return slavePtr;
141 }
142 
143 
144 template <class Type>
146 {
147  SetPtr(sourcePtr.PopPtr(), false);
148 }
149 
150 
151 template <class Type>
153 {
154  return &BaseClass::GetPtrRef();
155 }
156 
157 
158 // public methods
159 
160 template <class Type>
161 bool TComPtr<Type>::CoCreateInstance(REFCLSID rclsid, IUnknown* outerPtr, quint32 clsContext)
162 {
163  Reset();
164 
165  return !FAILED(::CoCreateInstance(rclsid, outerPtr, clsContext, __uuidof(Type), (void**)operator&()));
166 }
167 
168 
169 template <class Type>
171 {
172  Type* ptr = BaseClass::GetPtr();
173  if (ptr == NULL){
174  return;
175  }
176 
177  ptr->AddRef();
178 }
179 
180 
181 template <class Type>
183 {
184  Type* ptr = BaseClass::GetPtr();
185  if (ptr == NULL){
186  return;
187  }
188 
189  ptr->Release();
190 }
191 
192 
193 template <class Type>
195 {
196  SetPtr(ptr.GetPtr(), true);
197 
198  return *this;
199 }
200 
201 
202 } // namespace iwin
203 
204 
205 #endif // !iwin_TComPtr_included
206 
207 
Type ** operator&()
Address operator.
Definition: TComPtr.h:152
void SetPtr(Type *ptr, bool addRef=true)
Set new value of internal pointer.
Definition: TComPtr.h:122
void TakeOver(TComPtr &sourcePtr)
Take internal pointer over.
Definition: TComPtr.h:145
void DecRef()
Decrease reference counter.
Definition: TComPtr.h:182
Pointer wrapper providing access to Windows COM objects.
Definition: TComPtr.h:20
TComPtr(Type *ptr=NULL, bool addRef=true)
Set new value of internal pointer.
Definition: TComPtr.h:90
void Reset()
It decreses COM reference pointer and set this pointer to NULL.
Definition: TComPtr.h:115
void IncRef()
Increase reference counter.
Definition: TComPtr.h:170
#define NULL
~TComPtr()
Destructor.
Definition: TComPtr.h:108
TComPtr & operator=(const TComPtr &ptr)
Assign operator.
Definition: TComPtr.h:194
bool CoCreateInstance(REFCLSID rclsid, IUnknown *outerPtr=NULL, quint32 clsContext=CLSCTX_ALL)
Create instance of COM object using its CLSID.
Definition: TComPtr.h:161
Type * PopPtr()
Reset internal pointer value without decreasing of reference counter.
Definition: TComPtr.h:135
istd::TPointerBase< Type > BaseClass
Definition: TComPtr.h:23

© 2007-2011 Witold Gantzke and Kirill Lepskiy