![]() |
CopperSpice API
1.9.2
|
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 |
bool | operator!= (const QScopedPointer< T, Deleter > &other) const |
bool | operator!= (const T *ptr) const |
bool | operator!= (std::nullptr_t) const |
ElementType & | operator* () const noexcept |
Pointer | operator-> () const noexcept |
bool | operator< (const QScopedPointer< T, Deleter > &other) const |
bool | operator< (const T *ptr) const |
bool | operator< (std::nullptr_t) const |
bool | operator<= (const QScopedPointer< T, Deleter > &other) const |
bool | operator<= (const T *ptr) const |
bool | operator<= (std::nullptr_t) const |
QScopedPointer & | operator= (QScopedPointer< T, Deleter > &&other) |
bool | operator== (const QScopedPointer< T, Deleter > &other) const |
bool | operator== (const T *ptr) const |
bool | operator== (std::nullptr_t) const |
bool | operator> (const QScopedPointer< T, Deleter > &other) const |
bool | operator> (const T *ptr) const |
bool | operator> (std::nullptr_t) const |
bool | operator>= (const QScopedPointer< T, Deleter > &other) const |
bool | operator>= (const T *ptr) const |
bool | operator>= (std::nullptr_t) const |
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 | |
QScopedPointer< T > | QMakeScoped (Args &&...args) |
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.
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.
When a smart pointer is used instead of a raw pointer the source code can be simplified.
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.
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() |
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.
The syntax for using a custom deleter with QScopedPointer has been modified. This change applies to any build of CopperSpice using version 1.8.2 or newer.
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.
QScopedPointer< T, Deleter >::deleter_type |
Typedef for std::unique_ptr<T, Deleter>::deleter_type.
QScopedPointer< T, Deleter >::DeleterType |
Typedef for deleter_type.
QScopedPointer< T, Deleter >::element_type |
Typedef for std::unique_ptr<T, Deleter>::element_type.
QScopedPointer< T, Deleter >::ElementType |
Typedef for element_type.
QScopedPointer< T, Deleter >::pointer |
Typedef for std::unique_ptr::pointer.
QScopedPointer< T, Deleter >::Pointer |
Typedef for pointer.
|
explicitnoexcept |
Constructs a new QScopedPointer object and sets the internal pointer to p.
|
explicitnoexcept |
Constructs a new QScopedPointer object and sets the internal pointer to p and the deleter to d.
|
noexcept |
Constructs a new QScopedPointer by moving ownership from p.
QScopedPointer< T, Deleter >::~QScopedPointer | ( | ) |
Destroys this QScopedPointer object.
QScopedPointer< T, Deleter >::QScopedPointer | ( | QScopedPointer< T, Deleter > && | other | ) |
Move constructs a new QScopedPointer from other.
|
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.
|
noexcept |
Equivalent to calling QScopedPointer::data().
|
noexcept |
Returns the deleter object which will be used for destruction of this smart pointer.
|
noexcept |
Returns the deleter object which will be used for destruction of this smart pointer.
|
noexcept |
Returns true if this QScopedPointer contains a nullptr, otherwise returns false.
|
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.
|
noexcept |
Returns true if this QScopedPointer contains a nullptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator!= | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is not equal to other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator!= | ( | const T * | ptr | ) | const |
Returns true if this QScopedPointer is not equal to ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator!= | ( | std::nullptr_t | ) | const |
Returns true if this QScopedPointer is not equal to a nullptr, otherwise returns false.
|
noexcept |
Dereferences this QScopedPointer and returns the object it points to. If the QScopedPointer is a nullptr the behavior is undefined.
|
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.
bool QScopedPointer< T, Deleter >::operator< | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is less than other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator< | ( | const T * | ptr | ) | const |
Returns true if this QSharedPointer is less than ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator< | ( | std::nullptr_t | ) | const |
Returns true if this QSharedPointer is less than a nullptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator<= | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is less than or equal to other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator<= | ( | const T * | ptr | ) | const |
Returns true if this QSharedPointer is less than or equal to ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator<= | ( | std::nullptr_t | ) | const |
Returns true if this QSharedPointer is less than or equal to a nullptr, otherwise returns false.
QScopedPointer & QScopedPointer< T, Deleter >::operator= | ( | QScopedPointer< T, Deleter > && | other | ) |
Move assigns from other and returns a reference to this object.
bool QScopedPointer< T, Deleter >::operator== | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is equal to other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator== | ( | const T * | ptr | ) | const |
Returns true if this QScopedPointer is equal to ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator== | ( | std::nullptr_t | ) | const |
Returns true if this QScopedPointer is equal to a nullptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator> | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is greater than other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator> | ( | const T * | ptr | ) | const |
Returns true if this QSharedPointer is greater than ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator> | ( | std::nullptr_t | ) | const |
Returns true if this QSharedPointer is greater than a nullptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator>= | ( | const QScopedPointer< T, Deleter > & | other | ) | const |
Returns true if this QScopedPointer is equal to than or equal to other, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator>= | ( | const T * | ptr | ) | const |
Returns true if this QSharedPointer is greater than or equal to ptr, otherwise returns false.
bool QScopedPointer< T, Deleter >::operator>= | ( | std::nullptr_t | ) | const |
Returns true if this QSharedPointer is greater than or equal to a nullptr, otherwise returns false.
|
noexcept |
Equivalent to calling QScopedPointer::take().
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.
|
noexcept |
Swap the current pointer with other.
|
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.
|
related |
Constructs a new QScopedPointer of type T by using args as the parameter list for the constructor of T.