CopperSpice API  1.9.1
QScopedPointer< T, Deleter > Class Template Reference

Contains a pointer to an object and takes exclusive ownership. More...

Public Typedefs

using deleter_type = typename std::unique_ptr< T, Deleter >::deleter_type
 
using DeleterType = deleter_type
 
using element_type = typename std::unique_ptr< T, Deleter >::element_type
 
using ElementType = element_type
 
using pointer = typename std::unique_ptr< T, Deleter >::pointer
 
using Pointer = pointer
 

Public Methods

 QScopedPointer (Pointer p, Deleter d) noexcept
 
 QScopedPointer (Pointer p=nullptr) noexcept
 
 QScopedPointer (QScopedPointer< T, Deleter > &&other)
 
 QScopedPointer (std::unique_ptr< T, Deleter > &&p) noexcept
 
 ~QScopedPointer ()
 
Pointer data () const noexcept
 
Pointer get () const noexcept
 
const Deleter & get_deleter () const noexcept
 
Deleter & get_deleter () noexcept
 
bool isNull () const noexcept
 
 operator bool () const noexcept
 
bool operator! () const noexcept
 
ElementTypeoperator* () const noexcept
 
Pointer operator-> () const noexcept
 
QScopedPointer & operator= (QScopedPointer< T, Deleter > &&other)
 
Pointer release () noexcept
 
void reset (Pointer other=nullptr)
 
void swap (QScopedPointer< T, Deleter > &other) noexcept
 
Pointer take () noexcept
 

Related Functions

These are not member functions

bool operator!= (const QScopedPointer< T, Deleter > &ptr1, std::nullptr_t)
 
bool operator!= (const QScopedPointer< T1, Deleter1 > &ptr1, const QScopedPointer< T2, Deleter2 > &ptr2)
 
bool operator!= (const QScopedPointer< T1, Deleter1 > &ptr1, const T2 *ptr2)
 
bool operator!= (const T1 *ptr1, const QScopedPointer< T2, Deleter2 > &ptr2)
 
bool operator!= (std::nullptr_t, const QScopedPointer< T, Deleter > &ptr2)
 
bool operator< (const QScopedPointer< T1 > &ptr1, const QScopedPointer< T2 > ptr2)
 
bool operator<= (const QScopedPointer< T1 > &ptr1, const QScopedPointer< T2 > ptr2)
 
bool operator== (const QScopedPointer< T, Deleter > &ptr1, std::nullptr_t)
 
bool operator== (const QScopedPointer< T1, Deleter1 > &ptr1, const QScopedPointer< T2, Deleter2 > &ptr2)
 
bool operator== (const QScopedPointer< T1, Deleter1 > &ptr1, const T2 *ptr2)
 
bool operator== (const T1 *ptr1, const QScopedPointer< T2, Deleter2 > &ptr2)
 
bool operator== (std::nullptr_t, const QScopedPointer< T, Deleter > &ptr2)
 
bool operator> (const QScopedPointer< T1 > &ptr1, const QScopedPointer< T2 > ptr2)
 
bool operator>= (const QScopedPointer< T1 > &ptr1, const QScopedPointer< T2 > ptr2)
 
QScopedPointer< T > QMakeScoped (Args &&...args)
 
QScopedPointer< T > QMakeUnique (Args &&...args)
 

Detailed Description

template<typename T, typename Deleter = std::default_delete<T>>
class QScopedPointer< T, Deleter >

The QScopedPointer class contains a pointer to an object and takes exclusive ownership of the lifetime of the object.

There is no reference count for a QScopedPointer since no other smart pointers can point to the given object. Any pointer class which takes responsibility for the lifetime of the object it points to is considered a smart pointer.

When this smart pointer class is destroyed the object it points to will be deleted. This ensures the object is always destroyed when the pointer goes out of scope.

Example Using a Raw Pointer

The following is an example which shows why using a raw pointer can be complicated. Notice the code to delete both raw pointers must be duplicated in three different places.

void myFunction(bool useSubClass)
{
MyClass *somePtr = useSubClass ? new MyClass() : new MySubClass;
QIODevice *device = handsOverOwnership();
if (m_value > 3) {
delete somePtr;
delete device;
return;
}
try {
process(device);
} catch (...) {
delete somePtr;
delete device;
throw;
}
delete somePtr;
delete device;
}

Example Using QScopedPointer

When a smart pointer is used instead of a raw pointer the source code can be simplified.

void myFunction(bool useSubClass)
{
QScopedPointer<MyClass> somePtr(useSubClass ? new MyClass() : new MySubClass);
QScopedPointer<QIODevice> device(handsOverOwnership());
if (m_value > 3) }
return;
}
process(device);
}

Destroying Objects

The purpose of a destructor is to clean up the resources owned by the object. Destroying a smart pointer object needs to do a bit more work. It is also responsible for releaseing the dynamic memory allocated to the object the smart pointer was pointing to. Most dynamically allocated objects are destroyed by simply calling the delete operator as shown below.

Widget *ptr = new Widget();
// do something
delete ptr;

When using a smart pointer which owns the object it is pointing to, users do not call delete directly. The destructor method is responsible for calling a method in a Deleter class which will release the memory allocated to the object. The exact call in the Deleter class must correspond to the way the object was created.

Allocation Deleter Class Destruction
new T() std::default_delete<T> Default Deleter class, calls operator delete()
new T[ ] std::default_delete<T[ ]> Default Deleter class, calls operator delete[ ]
malloc() QMallocDeleter Custom Deleter, calls free()

Custom Deleter Classes

QScopedPointer has a default Deleter class which is sufficient for most objects. If your application requires a custom Deleter class the name of this struct or class must be passed as the second template parameter to QScopedPointer.

// uses default object deleter
// uses default array deleter
QScopedPointer<int[]> ptr_B(new int[42]);
// uses a CopperSpice custom Deleter class
QScopedPointer<int, QMallocDeleter> ptr1(reinterpret_cast<int *>(malloc(42)));
// uses a custom deleter class supplied in your application
struct MyCustomDeleter
{
void operator()(MyClass *ptr) const {
myDeleteFunction(ptr);
}
};

Custom Deleters

The syntax for using a custom deleter with QScoperedPointer has been modified. This change applies to any build of CopperSpice using version 1.8.2 or newer.

// original code
QScopedPointer<int, QScopedPointerPodDeleter> ptr1(reinterpret_cast<int *>(malloc(42)));
struct MyCustomDeleter
{
static inline void cleanup(MyClass *ptr) {
myDeleteFunction(ptr);
}
};
// using a custom deleter
// new syntax
QScopedPointer<int, QMallocDeleter> ptr3(reinterpret_cast<int *>(malloc(42)));
struct MyCustomDeleter
{
void operator()(MyClass *ptr) const {
myDeleteFunction(ptr);
}
};
// using a custom deleter

Forward Declared Pointers

Classes which are forward declared can be used within QScopedPointer. However the destructor of the forward declared class must be available when a QScopedPointer is constructed. All classes containing a QScopedPointer which point to a forward declared class must have non-inline constructors, destructors, and assignment operators.

class MyPrivateClass; // forward declaration
class MyClass
{
public:
MyClass();
inline ~MyClass() {} // will *not* compile, destructor can not be inline
MyClass(const MyClass &) = delete;
MyClass & operator=(const MyClass &) = delete;
private:
};
See also
QSharedPointer

Member Typedef Documentation

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::deleter_type

Typedef for std::unique_ptr<T, Deleter>::deleter_type.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::DeleterType

Typedef for deleter_type;

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::element_type

Typedef for std::unique_ptr<T, Deleter>::element_type.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::ElementType

Typedef for element_type.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::pointer

Typedef for std::unique_ptr::pointer.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::Pointer

Typedef for pointer.

Constructor & Destructor Documentation

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::QScopedPointer ( Pointer  p = nullptr)
explicitnoexcept

Constructs a new QScopedPointer object and sets the internal pointer to p.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::QScopedPointer ( Pointer  p,
Deleter  d 
)
explicitnoexcept

Constructs a new QScopedPointer object and sets the internal pointer to p and the deleter to d.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::QScopedPointer ( std::unique_ptr< T, Deleter > &&  p)
noexcept

Constructs a new QScopedPointer by moving ownership from p.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::~QScopedPointer ( )

Destroys this QScopedPointer object.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::QScopedPointer ( QScopedPointer< T, Deleter > &&  other)

Move constructs a new QScopedPointer from other.

Method Documentation

template<typename T , typename Deleter = std::default_delete<T>>
Pointer QScopedPointer< T, Deleter >::data ( ) const
noexcept

Returns the value of the pointer in the current QScopedPointer. This smart pointer class still owns the object it is pointing to. No other code should attempt to delete the object.

See also
get()
template<typename T , typename Deleter = std::default_delete<T>>
Pointer QScopedPointer< T, Deleter >::get ( ) const
noexcept

Equivalent to calling QScopedPointer::data().

template<typename T , typename Deleter = std::default_delete<T>>
const Deleter & QScopedPointer< T, Deleter >::get_deleter ( ) const
noexcept

Returns the deleter object which will be used for destruction of this smart pointer.

See also
Destroying Objects:
template<typename T , typename Deleter = std::default_delete<T>>
Deleter & QScopedPointer< T, Deleter >::get_deleter ( )
noexcept

Returns the deleter object which will be used for destruction of this smart pointer.

See also
Destroying Objects
template<typename T , typename Deleter = std::default_delete<T>>
bool QScopedPointer< T, Deleter >::isNull ( ) const
noexcept

Returns true if this QScopedPointer contains a nullptr, otherwise returns false.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer< T, Deleter >::operator bool ( ) const
explicitnoexcept

Returns true if this QScopedPointer does not contain a nullptr. This method is invoked automatically when a QScopedPointer is used as a boolean condition.

QScopedPointer<MyClass> scopedPointer = someFunction();
if (scopedPointer) {
...
}
template<typename T , typename Deleter = std::default_delete<T>>
bool QScopedPointer< T, Deleter >::operator! ( ) const
noexcept

Returns true if this QScopedPointer contains a nullptr, otherwise returns false.

See also
isNull()
template<typename T , typename Deleter = std::default_delete<T>>
ElementType & QScopedPointer< T, Deleter >::operator* ( ) const
noexcept

Dereferences this QScopedPointer and returns the object it points to. If the QScopedPointer is a nullptr the behavior is undefined.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer QScopedPointer< T, Deleter >::operator-> ( ) const
noexcept

Dereferences this QScopedPointer and returns a raw pointer to the object it points to. If the QScopedPointer is a nullptr the behavior is undefined.

template<typename T , typename Deleter = std::default_delete<T>>
QScopedPointer & QScopedPointer< T, Deleter >::operator= ( QScopedPointer< T, Deleter > &&  other)

Move assigns from other and returns a reference to this object.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer QScopedPointer< T, Deleter >::release ( )
noexcept

Equivalent to calling QScopedPointer::take().

template<typename T , typename Deleter = std::default_delete<T>>
void QScopedPointer< T, Deleter >::reset ( Pointer  other = nullptr)

Assigns other to this QScopedPointer.

If the current QScopedPointer is not a nullptr then the object it is pointing to will be destroyed.

template<typename T , typename Deleter = std::default_delete<T>>
void QScopedPointer< T, Deleter >::swap ( QScopedPointer< T, Deleter > &  other)
noexcept

Swap the current pointer with other.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer QScopedPointer< T, Deleter >::take ( )
noexcept

Releases the ownership of the managed object, if this scoped pointer is not a nullptr. This method sets the current scoped pointer to a nullptr and returns the old scoped pointer value.

The caller is responsible for deleting the object.

See also
release()

Friends And Related Function Documentation

bool operator!= ( const QScopedPointer< T, Deleter > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is not a nullptr, otherwise returns false.

bool operator!= ( const QScopedPointer< T1, Deleter1 > &  ptr1,
const QScopedPointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( const QScopedPointer< T1, Deleter1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( const T1 *  ptr1,
const QScopedPointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( std::nullptr_t  ,
const QScopedPointer< T, Deleter > &  ptr2 
)
related

Returns true if ptr2 is not a nullptr, otherwise returns false.

bool operator< ( const QScopedPointer< T1 > &  ptr1,
const QScopedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than ptr2.

bool operator<= ( const QScopedPointer< T1 > &  ptr1,
const QScopedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than or equal to ptr2.

bool operator== ( const QScopedPointer< T, Deleter > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is a nullptr, otherwise returns false.

bool operator== ( const QScopedPointer< T1, Deleter1 > &  ptr1,
const QScopedPointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( const QScopedPointer< T1, Deleter1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( const T1 *  ptr1,
const QScopedPointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( std::nullptr_t  ,
const QScopedPointer< T, Deleter > &  ptr2 
)
related

Returns true if ptr2 is a nullptr, otherwise returns false.

bool operator> ( const QScopedPointer< T1 > &  ptr1,
const QScopedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than ptr2.

bool operator>= ( const QScopedPointer< T1 > &  ptr1,
const QScopedPointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than or equal to ptr2.

QScopedPointer< T > QMakeScoped ( Args &&...  args)
related

Constructs a new object of type T by using the given args as the parameter list for the constructor of T.

QScopedPointer< T > QMakeUnique ( Args &&...  args)
related

Equivalent to calling QMakeScoped().