Changeset 6440 in vbox for trunk/include/VBox/com
- Timestamp:
- Jan 22, 2008 1:35:10 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 27415
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/VBox/com/ptr.h
r5999 r6440 62 62 { 63 63 protected: 64 64 65 static void addref (C *p) { p->AddRef(); } 65 66 static void release (C *p) { p->Release(); } … … 73 74 { 74 75 protected: 76 75 77 static void addref (C *p) {} 76 78 static void release (C *p) {} … … 78 80 79 81 /** 82 * Equality operations for the ComPtrBase template. 83 */ 84 template <class C> 85 class ComPtrEqOps 86 { 87 protected: 88 89 template <class I> 90 static bool equals (C *aThis, I *aThat) 91 { 92 IUnknown *thatUnk = NULL, *thisUnk = NULL; 93 if (aThat) 94 aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk); 95 if (aThis) 96 aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk); 97 bool equal = thisUnk == thatUnk; 98 if (thisUnk) 99 thisUnk->Release(); 100 if (thatUnk) 101 thatUnk->Release(); 102 return equal; 103 } 104 105 /* specialization for IUnknown */ 106 template<> 107 static bool equals <IUnknown> (C *aThis, IUnknown *aThat) 108 { 109 IUnknown *thisUnk = NULL; 110 if (aThis) 111 aThis->QueryInterface (COM_IIDOF (IUnknown), (void **) &thisUnk); 112 bool equal = thisUnk == aThat; 113 if (thisUnk) 114 thisUnk->Release(); 115 return equal; 116 } 117 }; 118 119 /** Specialization for IUnknown */ 120 template<> 121 class ComPtrEqOps <IUnknown> 122 { 123 protected: 124 125 template <class I> 126 static bool equals (IUnknown *aThis, I *aThat) 127 { 128 IUnknown *thatUnk = NULL; 129 if (aThat) 130 aThat->QueryInterface (COM_IIDOF (IUnknown), (void **) &thatUnk); 131 bool equal = aThis == thatUnk; 132 if (thatUnk) 133 thatUnk->Release(); 134 return equal; 135 } 136 137 /* specialization for IUnknown */ 138 template<> 139 static bool equals <IUnknown> (IUnknown *aThis, IUnknown *aThat) 140 { 141 return aThis == aThat; 142 } 143 }; 144 145 /** 80 146 * Base template for smart COM pointers. Not intended to be used directly. 81 147 */ 82 148 template <class C, template <class> class RefOps = ComStrongRef> 83 class ComPtrBase : protected RefOps <C> 149 class ComPtrBase : protected RefOps <C>, protected ComPtrEqOps <C> 84 150 { 85 151 public: 86 152 87 / / a special template to disable AddRef()/Release()153 /* special template to disable AddRef()/Release() */ 88 154 template <class I> 89 class NoAddRefRelease : public I { 155 class NoAddRefRelease : public I 156 { 90 157 private: 91 158 #if !defined (VBOX_WITH_XPCOM) … … 106 173 ~ComPtrBase() { release(); } 107 174 108 ComPtrBase &operator= (const ComPtrBase &that) { 175 ComPtrBase &operator= (const ComPtrBase &that) 176 { 109 177 safe_assign (that.p); 110 178 return *this; 111 179 } 112 ComPtrBase &operator= (C *that_p) { 180 181 ComPtrBase &operator= (C *that_p) 182 { 113 183 safe_assign (that_p); 114 184 return *this; … … 117 187 public: 118 188 119 void setNull() { 189 void setNull() 190 { 120 191 release(); 121 192 p = NULL; 122 193 } 123 194 124 bool isNull() const { 195 bool isNull() const 196 { 125 197 return (p == NULL); 126 198 } 199 127 200 bool operator! () const { return isNull(); } 128 201 … … 131 204 132 205 template <class I> 133 bool equalsTo (I *i) const { 134 IUnknown *this_unk = NULL, *that_unk = NULL; 135 if (i) 136 i->QueryInterface (COM_IIDOF (IUnknown), (void**) &that_unk); 137 if (p) 138 p->QueryInterface (COM_IIDOF (IUnknown), (void**) &this_unk); 139 bool equal = this_unk == that_unk; 140 if (that_unk) 141 that_unk->Release(); 142 if (this_unk) 143 this_unk->Release(); 144 return equal; 206 bool equalsTo (I *aThat) const 207 { 208 return equals (p, aThat); 145 209 } 146 210 147 211 template <class OC> 148 bool equalsTo (const ComPtrBase <OC> &oc) const { 212 bool equalsTo (const ComPtrBase <OC> &oc) const 213 { 149 214 return equalsTo ((OC *) oc); 150 215 } … … 157 222 * pointer). 158 223 */ 159 NoAddRefRelease <C> *operator-> () const { 224 NoAddRefRelease <C> *operator-> () const 225 { 160 226 AssertMsg (p, ("Managed pointer must not be null\n")); 161 227 return (NoAddRefRelease <C> *) p; … … 163 229 164 230 template <class I> 165 HRESULT queryInterfaceTo (I **pp) const { 166 if (pp) { 167 if (p) { 168 return p->QueryInterface (COM_IIDOF (I), (void**) pp); 169 } else { 231 HRESULT queryInterfaceTo (I **pp) const 232 { 233 if (pp) 234 { 235 if (p) 236 { 237 return p->QueryInterface (COM_IIDOF (I), (void **) pp); 238 } 239 else 240 { 170 241 *pp = NULL; 171 242 return S_OK; 172 243 } 173 } else {174 return E_INVALIDARG;175 244 } 245 246 return E_INVALIDARG; 176 247 } 177 248 178 249 /** Intended to pass instances as out parameters to interface methods */ 179 C **asOutParam() { 250 C **asOutParam() 251 { 180 252 setNull(); 181 253 return &p; … … 184 256 private: 185 257 186 void addref() { 258 void addref() 259 { 187 260 if (p) 188 261 RefOps <C>::addref (p); 189 262 } 190 void release() { 263 264 void release() 265 { 191 266 if (p) 192 267 RefOps <C>::release (p); 193 268 } 194 269 195 void safe_assign (C *that_p) { 196 // be aware of self-assignment 270 void safe_assign (C *that_p) 271 { 272 /* be aware of self-assignment */ 197 273 if (that_p) 198 274 RefOps <C>::addref (that_p); … … 219 295 ComPtr () : Base() {} 220 296 ComPtr (const ComPtr &that) : Base (that) {} 221 ComPtr &operator= (const ComPtr &that) { 297 ComPtr &operator= (const ComPtr &that) 298 { 222 299 Base::operator= (that); 223 300 return *this; … … 226 303 template <class OI> 227 304 ComPtr (OI *that_p) : Base () { operator= (that_p); } 228 // specialization for I 305 306 /* specialization for I */ 229 307 ComPtr (I *that_p) : Base (that_p) {} 230 308 … … 233 311 234 312 template <class OI> 235 ComPtr &operator= (OI *that_p) { 313 ComPtr &operator= (OI *that_p) 314 { 236 315 if (that_p) 237 316 that_p->QueryInterface (COM_IIDOF (I), (void **) Base::asOutParam()); … … 240 319 return *this; 241 320 } 242 // specialization for I 243 ComPtr &operator= (I *that_p) { 321 322 /* specialization for I */ 323 ComPtr &operator=(I *that_p) 324 { 244 325 Base::operator= (that_p); 245 326 return *this; … … 247 328 248 329 template <class OC> 249 ComPtr &operator= (const ComPtr <OC, RefOps> &oc) { 330 ComPtr &operator= (const ComPtr <OC, RefOps> &oc) 331 { 250 332 return operator= ((OC *) oc); 251 333 } … … 316 398 PRUint32 serverID = 0; 317 399 rc = ipcServ->ResolveClientName (serverName, &serverID); 318 if (SUCCEEDED (rc)) { 400 if (SUCCEEDED (rc)) 401 { 319 402 nsCOMPtr <ipcIDConnectService> dconServ = 320 403 do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc); … … 346 429 ComPtr () : Base() {} 347 430 ComPtr (const ComPtr &that) : Base (that) {} 348 ComPtr &operator= (const ComPtr &that) { 431 ComPtr &operator= (const ComPtr &that) 432 { 349 433 Base::operator= (that); 350 434 return *this; … … 358 442 359 443 template <class OI> 360 ComPtr &operator= (OI *that_p) { 444 ComPtr &operator= (OI *that_p) 445 { 361 446 if (that_p) 362 447 that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam()); … … 367 452 368 453 template <class OC> 369 ComPtr &operator= (const ComPtr <OC, RefOps> &oc) { 454 ComPtr &operator= (const ComPtr <OC, RefOps> &oc) 455 { 370 456 return operator= ((OC *) oc); 371 457 } … … 390 476 ComObjPtr (const ComObjPtr &that) : Base (that) {} 391 477 ComObjPtr (C *that_p) : Base (that_p) {} 392 ComObjPtr &operator= (const ComObjPtr &that) { 478 479 ComObjPtr &operator= (const ComObjPtr &that) 480 { 393 481 Base::operator= (that); 394 482 return *this; 395 483 } 396 ComObjPtr &operator= (C *that_p) { 484 485 ComObjPtr &operator= (C *that_p) 486 { 397 487 Base::operator= (that_p); 398 488 return *this; … … 412 502 * does otherwise. 413 503 */ 414 HRESULT createObject() { 504 HRESULT createObject() 505 { 415 506 HRESULT rc; 416 507 #if !defined (VBOX_WITH_XPCOM) 417 508 # ifdef VBOX_COM_OUTOFPROC_MODULE 418 509 CComObjectNoLock <C> *obj = new CComObjectNoLock <C>(); 419 if (obj) { 510 if (obj) 511 { 420 512 obj->InternalFinalConstructAddRef(); 421 513 rc = obj->FinalConstruct(); 422 514 obj->InternalFinalConstructRelease(); 423 } else { 515 } 516 else 424 517 rc = E_OUTOFMEMORY; 425 }426 518 # else 427 519 CComObject <C> *obj = NULL; … … 430 522 #else /* !defined (VBOX_WITH_XPCOM) */ 431 523 CComObject <C> *obj = new CComObject <C>(); 432 if (obj) {524 if (obj) 433 525 rc = obj->FinalConstruct(); 434 } else {526 else 435 527 rc = E_OUTOFMEMORY; 436 }437 528 #endif /* !defined (VBOX_WITH_XPCOM) */ 438 529 *this = obj;
Note:
See TracChangeset
for help on using the changeset viewer.