* Default constructor. Creates an OwnerPointer that does not own any object.
*/
OwnerPointer()
+ : mObject(NULL)
{
- mObject = NULL;
}
/**
* @param[in] object A pointer to a heap allocated object.
*/
OwnerPointer( T* object )
+ : mObject(object)
{
- mObject = object;
}
/**
* @param[in] other The pointer that gives away the ownership.
*/
OwnerPointer( OwnerPointer& other )
+ : mObject(NULL)
{
- Init( other );
+ Swap( other );
}
/**
{
if( this != &other ) // no self-assignment
{
- Reset();
- Init( other );
+ // Creation of temportaty object to prevent premature deletion of object
+ OwnerPointer(other).Swap(*this);
}
// return self
return mObject;
}
+ /**
+ * Swap owned objects
+ */
+ void Swap( OwnerPointer& other )
+ {
+ T* tmp = mObject;
+ mObject = other.mObject;
+ other.mObject = tmp;
+ }
+
// Handle comparisons - This is a variation of the safe bool idiom
/**
*/
void ThisIsSaferThanReturningVoidStar() const {}
-private:
-
- /**
- * Initialise this pointer from another one.
- * ownerPointer parameter looses ownership.
- * @param ownerPointer owner pointer
- */
- void Init( OwnerPointer& ownerPointer )
- {
- mObject = ownerPointer.mObject;
- ownerPointer.mObject = NULL;
- }
-
// data
T* mObject; ///< Raw pointer to the object
};