Changeset 27607 in vbox for trunk/include
- Timestamp:
- Mar 22, 2010 6:13:07 PM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 59148
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/VBox/com/ptr.h
r23223 r27607 73 73 void LogRef(const char *pcszFormat, ...); 74 74 } 75 76 /**77 * Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.78 */79 template <class C>80 class ComStrongRef81 {82 protected:83 84 static void addref(C *p)85 {86 p->AddRef();87 }88 static void release(C *p)89 {90 p->Release();91 }92 };93 94 /**95 * Weak referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.96 */97 template <class C>98 class ComWeakRef99 {100 protected:101 102 static void addref(C * /* p */) {}103 static void release(C * /* p */) {}104 };105 75 106 76 /** … … 169 139 * Base template for smart COM pointers. Not intended to be used directly. 170 140 */ 171 template <class C , template <class> class RefOps = ComStrongRef>172 class ComPtrBase : protected RefOps <C>141 template <class C> 142 class ComPtrBase 173 143 { 174 144 public: … … 190 160 protected: 191 161 192 ComPtrBase () : p (NULL) {} 193 ComPtrBase (const ComPtrBase &that) : p (that.p) { addref(); } 194 ComPtrBase (C *that_p) : p (that_p) { addref(); } 195 196 ~ComPtrBase() { release(); } 197 198 ComPtrBase &operator= (const ComPtrBase &that) 199 { 200 safe_assign (that.p); 201 return *this; 202 } 203 204 ComPtrBase &operator= (C *that_p) 205 { 206 safe_assign (that_p); 162 ComPtrBase() 163 : p(NULL) 164 {} 165 166 ComPtrBase(const ComPtrBase &that) 167 : p(that.p) 168 { 169 addref(); 170 } 171 172 ComPtrBase(C *that_p) 173 : p(that_p) 174 { 175 addref(); 176 } 177 178 ~ComPtrBase() 179 { 180 release(); 181 } 182 183 ComPtrBase &operator=(const ComPtrBase &that) 184 { 185 safe_assign(that.p); 186 return *this; 187 } 188 189 ComPtrBase &operator=(C *that_p) 190 { 191 safe_assign(that_p); 207 192 return *this; 208 193 } … … 221 206 } 222 207 223 bool operator! 224 225 bool operator< 226 bool operator== 208 bool operator!() const { return isNull(); } 209 210 bool operator<(C* that_p) const { return p < that_p; } 211 bool operator==(C* that_p) const { return p == that_p; } 227 212 228 213 template <class I> 229 bool equalsTo 230 { 231 return ComPtrEquals 214 bool equalsTo(I *aThat) const 215 { 216 return ComPtrEquals(p, aThat); 232 217 } 233 218 234 219 template <class OC> 235 bool equalsTo 236 { 237 return equalsTo ((OC *)oc);220 bool equalsTo(const ComPtrBase <OC> &oc) const 221 { 222 return equalsTo((OC*)oc); 238 223 } 239 224 240 225 /** Intended to pass instances as in parameters to interface methods */ 241 operator C* 226 operator C*() const { return p; } 242 227 243 228 /** … … 245 230 * pointer). 246 231 */ 247 NoAddRefRelease <C> *operator->() const248 { 249 AssertMsg 250 return (NoAddRefRelease <C> *)p;232 NoAddRefRelease<C>* operator->() const 233 { 234 AssertMsg(p, ("Managed pointer must not be null\n")); 235 return (NoAddRefRelease<C>*)p; 251 236 } 252 237 253 238 template <class I> 254 HRESULT queryInterfaceTo 239 HRESULT queryInterfaceTo(I **pp) const 255 240 { 256 241 if (pp) … … 258 243 if (p) 259 244 { 260 return p->QueryInterface (COM_IIDOF (I), (void **)pp);245 return p->QueryInterface(COM_IIDOF(I), (void**)pp); 261 246 } 262 247 else … … 282 267 { 283 268 if (p) 284 RefOps <C>::addref (p);269 p->AddRef(); 285 270 } 286 271 … … 288 273 { 289 274 if (p) 290 RefOps <C>::release (p);275 p->Release(); 291 276 } 292 277 … … 295 280 /* be aware of self-assignment */ 296 281 if (that_p) 297 RefOps <C>::addref (that_p);282 that_p->AddRef(); 298 283 release(); 299 284 p = that_p; … … 309 294 * @param I COM interface class 310 295 */ 311 template <class I , template <class> class RefOps = ComStrongRef>312 class ComPtr : public ComPtrBase <I, RefOps>313 { 314 typedef ComPtrBase <I, RefOps> Base;296 template <class I> 297 class ComPtr : public ComPtrBase<I> 298 { 299 typedef ComPtrBase<I> Base; 315 300 316 301 public: 317 302 318 ComPtr 319 ComPtr (const ComPtr &that) : Base(that) {}320 ComPtr &operator=(const ComPtr &that)303 ComPtr() : Base() {} 304 ComPtr(const ComPtr &that) : Base(that) {} 305 ComPtr& operator=(const ComPtr &that) 321 306 { 322 307 Base::operator= (that); … … 325 310 326 311 template <class OI> 327 ComPtr (OI *that_p) : Base () { operator=(that_p); }312 ComPtr(OI *that_p) : Base() { operator=(that_p); } 328 313 329 314 /* specialization for I */ 330 ComPtr (I *that_p) : Base(that_p) {}315 ComPtr(I *that_p) : Base(that_p) {} 331 316 332 317 template <class OC> 333 ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *)oc); }318 ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); } 334 319 335 320 template <class OI> 336 ComPtr &operator= 321 ComPtr &operator=(OI *that_p) 337 322 { 338 323 if (that_p) 339 that_p->QueryInterface (COM_IIDOF (I), (void **)Base::asOutParam());324 that_p->QueryInterface(COM_IIDOF(I), (void**)Base::asOutParam()); 340 325 else 341 326 Base::setNull(); … … 346 331 ComPtr &operator=(I *that_p) 347 332 { 348 Base::operator= 333 Base::operator=(that_p); 349 334 return *this; 350 335 } 351 336 352 337 template <class OC> 353 ComPtr &operator= (const ComPtr <OC, RefOps> &oc)354 { 355 return operator= ((OC *)oc);338 ComPtr &operator=(const ComPtr<OC> &oc) 339 { 340 return operator=((OC*)oc); 356 341 } 357 342 … … 365 350 I *obj = NULL; 366 351 #if !defined (VBOX_WITH_XPCOM) 367 rc = CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF(I),368 (void **)&obj);352 rc = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF(I), 353 (void**)&obj); 369 354 #else /* !defined (VBOX_WITH_XPCOM) */ 370 nsCOMPtr 371 rc = NS_GetComponentManager (getter_AddRefs(manager));372 if (SUCCEEDED 373 rc = manager->CreateInstance (clsid, nsnull, NS_GET_IID(I),374 355 nsCOMPtr<nsIComponentManager> manager; 356 rc = NS_GetComponentManager(getter_AddRefs(manager)); 357 if (SUCCEEDED(rc)) 358 rc = manager->CreateInstance(clsid, nsnull, NS_GET_IID(I), 359 (void **) &obj); 375 360 #endif /* !defined (VBOX_WITH_XPCOM) */ 376 361 *this = obj; 377 if (SUCCEEDED 362 if (SUCCEEDED(rc)) 378 363 obj->Release(); 379 364 return rc; … … 389 374 * method is fully equivalent to #createInprocObject() for now. 390 375 */ 391 HRESULT createLocalObject 376 HRESULT createLocalObject(const CLSID &clsid) 392 377 { 393 378 #if !defined (VBOX_WITH_XPCOM) 394 379 HRESULT rc; 395 380 I *obj = NULL; 396 rc = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF(I),397 (void **)&obj);381 rc = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF(I), 382 (void**)&obj); 398 383 *this = obj; 399 if (SUCCEEDED 384 if (SUCCEEDED(rc)) 400 385 obj->Release(); 401 386 return rc; 402 387 #else /* !defined (VBOX_WITH_XPCOM) */ 403 return createInprocObject 388 return createInprocObject(clsid); 404 389 #endif /* !defined (VBOX_WITH_XPCOM) */ 405 390 } … … 412 397 * @param serverName Name of the server to create an object within. 413 398 */ 414 HRESULT createObjectOnServer 399 HRESULT createObjectOnServer(const CLSID &clsid, const char *serverName) 415 400 { 416 401 HRESULT rc; 417 402 I *obj = NULL; 418 nsCOMPtr <ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);419 if (SUCCEEDED 403 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc); 404 if (SUCCEEDED(rc)) 420 405 { 421 406 PRUint32 serverID = 0; 422 rc = ipcServ->ResolveClientName 407 rc = ipcServ->ResolveClientName(serverName, &serverID); 423 408 if (SUCCEEDED (rc)) 424 409 { 425 nsCOMPtr <ipcIDConnectService> dconServ = 426 do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc); 427 if (SUCCEEDED (rc)) 428 rc = dconServ->CreateInstance (serverID, clsid, NS_GET_IID (I), 429 (void **) &obj); 410 nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc); 411 if (SUCCEEDED(rc)) 412 rc = dconServ->CreateInstance(serverID, clsid, NS_GET_IID(I), 413 (void**)&obj); 430 414 } 431 415 } 432 416 *this = obj; 433 if (SUCCEEDED 417 if (SUCCEEDED(rc)) 434 418 obj->Release(); 435 419 return rc; … … 443 427 * another interface pointer disregarding its type. 444 428 */ 445 template <template <class> class RefOps>446 class ComPtr <IUnknown, RefOps> : public ComPtrBase <IUnknown, RefOps>447 { 448 typedef ComPtrBase <IUnknown, RefOps> Base;429 template<> 430 class ComPtr<IUnknown> : public ComPtrBase<IUnknown> 431 { 432 typedef ComPtrBase<IUnknown> Base; 449 433 450 434 public: 451 435 452 ComPtr 453 ComPtr 454 ComPtr &operator=(const ComPtr &that)455 { 456 Base::operator= 436 ComPtr() : Base() {} 437 ComPtr(const ComPtr &that) : Base (that) {} 438 ComPtr& operator=(const ComPtr &that) 439 { 440 Base::operator=(that); 457 441 return *this; 458 442 } 459 443 460 444 template <class OI> 461 ComPtr (OI *that_p) : Base () { operator=(that_p); }445 ComPtr(OI *that_p) : Base() { operator=(that_p); } 462 446 463 447 template <class OC> 464 ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *)oc); }448 ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); } 465 449 466 450 template <class OI> 467 ComPtr &operator= 451 ComPtr &operator=(OI *that_p) 468 452 { 469 453 if (that_p) 470 that_p->QueryInterface (COM_IIDOF (IUnknown), (void **)Base::asOutParam());454 that_p->QueryInterface(COM_IIDOF(IUnknown), (void**)Base::asOutParam()); 471 455 else 472 456 Base::setNull(); … … 475 459 476 460 template <class OC> 477 ComPtr &operator= (const ComPtr <OC, RefOps> &oc)478 { 479 return operator= ((OC *)oc);461 ComPtr &operator=(const ComPtr<OC> &oc) 462 { 463 return operator=((OC*)oc); 480 464 } 481 465 }; … … 489 473 * @param C class that implements some COM interface 490 474 */ 491 template <class C , template <class> class RefOps = ComStrongRef>492 class ComObjPtr : public ComPtrBase <C, RefOps>493 { 494 typedef ComPtrBase <C, RefOps> Base;475 template <class C> 476 class ComObjPtr : public ComPtrBase<C> 477 { 478 typedef ComPtrBase<C> Base; 495 479 496 480 public: 497 481 498 ComObjPtr 499 ComObjPtr (const ComObjPtr &that) : Base(that) {}500 ComObjPtr (C *that_p) : Base(that_p) {}501 502 ComObjPtr &operator=(const ComObjPtr &that)503 { 504 Base::operator= 505 return *this; 506 } 507 508 ComObjPtr &operator=(C *that_p)509 { 510 Base::operator= 482 ComObjPtr() : Base() {} 483 ComObjPtr(const ComObjPtr &that) : Base(that) {} 484 ComObjPtr(C *that_p) : Base(that_p) {} 485 486 ComObjPtr& operator=(const ComObjPtr &that) 487 { 488 Base::operator=(that); 489 return *this; 490 } 491 492 ComObjPtr& operator=(C *that_p) 493 { 494 Base::operator=(that_p); 511 495 return *this; 512 496 } … … 530 514 #if !defined (VBOX_WITH_XPCOM) 531 515 # ifdef VBOX_COM_OUTOFPROC_MODULE 532 CComObjectNoLock <C> *obj = new CComObjectNoLock<C>();516 CComObjectNoLock<C> *obj = new CComObjectNoLock<C>(); 533 517 if (obj) 534 518 { … … 540 524 rc = E_OUTOFMEMORY; 541 525 # else 542 CComObject 543 rc = CComObject <C>::CreateInstance(&obj);526 CComObject<C> *obj = NULL; 527 rc = CComObject<C>::CreateInstance(&obj); 544 528 # endif 545 529 #else /* !defined (VBOX_WITH_XPCOM) */ 546 CComObject <C> *obj = new CComObject<C>();530 CComObject<C> *obj = new CComObject<C>(); 547 531 if (obj) 548 532 rc = obj->FinalConstruct();
Note:
See TracChangeset
for help on using the changeset viewer.