Data Fields | |
| MX | mutex_ |
| int | counter_ |
You generally use it by declaring a PMyClass pointer :
typedef SmartPtr<MyClass> PMyClass;
Then you use PMyClass just as if it was MyClass*, except that you never have to use 'delete' on it.
The == and != operators are inspired from the tips and tricks from http://www.awprofessional.com/isapi/product_id~%7B30931484-F804-4CDD-AD31-E168340DBA60%7D/catalog/article.asp
This Smart Pointer support the following operations:
SmartPtr<X> sp1, sp3;
SmartPtr<Y> sp2;
if (sp1) // test for non-null
if (!sp1) // test for null
if (sp1 == 0) // test for null
if (sp1 != 0) // test for non-null
if (sp1 == sp2) // test for equality (even if types X and Y are unrelated)
if (sp1 != sp2) // test for difference (even if types X and Y are unrelated)
sp1 < sp3 // order relation between smart ptrs on the same type
sp1 = sp3 // assignment between smart ptrs on the same type
sp1 = sp2 // assignment between unrelated types. this implies a
// dynamic_cast<>, and thus 'sp1' may be null after the '='
// if the types did not match
*sp1 // access the object
sp1-> // access the object to call a member
sp1.getPtr() // retrieve the underlying pointer (USE WITH GREAT CARE!!)
By default, the smart pointer is thread-safe, but you can also create faster, non thread-safe, smart pointer by doing:
SmartPtr<X, SmartPtrAllocator<X, ACE_Thread_Mutex>, ACE_Thread_Mutex> sp;
It also possible to customize how the 'SmartPtrData' housekeeping block is allocated by providing a custom "SmartPtrDataAllocator" class to the template. Two such classes exist:
SmartPtrData<MX>& X::smartPtrData() throw();
1.3-rc2