CopperSpice API  2.0.0
QSharedDataPointer< T > Class Template Reference

Represents a pointer to an implicitly shared object. More...

Public Typedefs

using pointer = T *
 
using Type = T
 

Public Methods

 QSharedDataPointer ()
 
 QSharedDataPointer (const QSharedDataPointer< T > &other)
 
 QSharedDataPointer (QSharedDataPointer &&other)
 
 QSharedDataPointer (T *data)
 
 ~QSharedDataPointer ()
 
const T * constData () const
 
T * data ()
 
const T * data () const
 
void detach ()
 
 operator const T * () const
 
 operator T * ()
 
bool operator! () const
 
bool operator!= (const QSharedDataPointer< T > &other) const
 
T & operator* ()
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 
QSharedDataPointer< T > & operator= (const QSharedDataPointer< T > &other)
 
QSharedDataPointer< T > & operator= (QSharedDataPointer< T > &&other)
 
QSharedDataPointer & operator= (T *other)
 
bool operator== (const QSharedDataPointer< T > &other) const
 
void swap (QSharedDataPointer &other)
 

Protected Methods

T * clone ()
 

Detailed Description

template<typename T>
class QSharedDataPointer< T >

The QSharedDataPointer class represents a pointer to an implicitly shared object. QSharedDataPointer<T> simplifies the process of writing your own implicitly shared classes. QSharedDataPointer implements thread safe reference counting.

Example

This example shows how to implement a class which uses copy on write. Please be advised that implementing this type of class has significant drawbacks and is not recommended.

Declare class Employee
Contains a data member of type QSharedDataPointer<EmployeeData>
Declare class EmployeeData
Inherits from QSharedData
#include <QSharedData>
#include <QString>
class EmployeeData;
class Employee
{
public:
Employee() {
d = new EmployeeData;
}
Employee(int id, QString name) {
d = new EmployeeData;
setId(id);
setName(name);
}
Employee(const Employee &other)
: d (other.d)
{ }
void setId(int id) { d->id = id; }
void setName(QString name) { d->name = name; }
int id() const { return d->id; }
QString name() const { return d->name; }
private:
};
class EmployeeData : public QSharedData
{
public:
EmployeeData()
: id(-1)
{ }
int id;
QString name;
};
See also
QExplicitlySharedDataPointer, QScopedPointer, QSharedData, QSharedPointer

Member Typedef Documentation

template<typename T >
QSharedDataPointer< T >::pointer

Typedef for T*.

template<typename T >
QSharedDataPointer< T >::Type

Typedef for T.

Constructor & Destructor Documentation

template<typename T >
QSharedDataPointer< T >::QSharedDataPointer ( )
inline

Constructs a QSharedDataPointer initialized with a null data pointer.

template<class T >
QSharedDataPointer< T >::QSharedDataPointer ( T *  data)
inlineexplicit

Constructs a QSharedDataPointer with the data pointer set to data and increments the reference count.

template<typename T >
QSharedDataPointer< T >::QSharedDataPointer ( const QSharedDataPointer< T > &  other)
inline

Copy constructs a new QSharedDataPointer from other.

template<typename T >
QSharedDataPointer< T >::QSharedDataPointer ( QSharedDataPointer< T > &&  other)
inline

Move constructs a new QSharedDataPointer from other.

template<typename T >
QSharedDataPointer< T >::~QSharedDataPointer ( )
inline

Decrements the reference count of the shared data object. If the reference count becomes 0 the object is deleted.

Method Documentation

template<typename T >
T * QSharedDataPointer< T >::clone ( )
inlineprotected

Creates and returns a deep copy of the current data. This method is called by detach() when the reference count is greater than 1 in order to create the new copy. This method uses operator new and calls the copy constructor of the type T.

This method is provided so that you may support "virtual copy constructors" for your own types.

In order to do so declare a template specialization of this method for your own type. In this example, the template specialization for the clone() method calls the EmployeeData::clone() virtual method. A class derived from EmployeeData could override that method and return the proper polymorphic type.

template<>
return d->clone();
}
template<typename T >
const T * QSharedDataPointer< T >::constData ( ) const
inline

Returns a const pointer to the shared data object. This method does not call detach().

See also
data()
template<typename T >
T * QSharedDataPointer< T >::data ( )
inline

Returns a pointer to the shared data object. This method calls detach().

See also
constData()
template<typename T >
const T * QSharedDataPointer< T >::data ( ) const
inline

Returns a pointer to the shared data object. This method does not call detach().

template<typename T >
void QSharedDataPointer< T >::detach ( )
inline

If the shared data object reference count is greater than 1, this method creates a deep copy of the shared data object and sets the current QSharedDataPointer to the copy.

template<typename T >
QSharedDataPointer< T >::operator const T * ( ) const
inline

Returns a pointer to the shared data object. This method does not call detach().

template<typename T >
QSharedDataPointer< T >::operator T * ( )
inline

Returns a pointer to the shared data object. This method calls detach().

See also
data(), constData()
template<typename T >
bool QSharedDataPointer< T >::operator! ( ) const
inline

Returns true if the current QSharedDataPointer is null.

template<typename T >
bool QSharedDataPointer< T >::operator!= ( const QSharedDataPointer< T > &  other) const

Returns true if this QSharedDataPointer is not equal to other, otherwise returns false.

template<typename T >
T & QSharedDataPointer< T >::operator* ( )
inline

Returns a reference to the object. This method calls detach().

template<typename T >
const T & QSharedDataPointer< T >::operator* ( ) const
inline

Returns a const reference to the object. This method does not call detach().

template<typename T >
T * QSharedDataPointer< T >::operator-> ( )
inline

Returns a pointer to the object. This method calls detach().

template<typename T >
const T * QSharedDataPointer< T >::operator-> ( ) const
inline

Returns a const pointer to the object. This method does not call detach().

template<typename T >
QSharedDataPointer< T > & QSharedDataPointer< T >::operator= ( const QSharedDataPointer< T > &  other)
inline

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

The new reference count is incremented and the old reference count is decremented. If the reference count of the old shared data object becomes 0 then the old shared data object is deleted.

template<typename T >
QSharedDataPointer< T > & QSharedDataPointer< T >::operator= ( QSharedDataPointer< T > &&  other)
inline

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

template<typename T >
QSharedDataPointer & QSharedDataPointer< T >::operator= ( T *  other)
inline

Sets the current QSharedDataPointer to other and increments the reference count.

The reference count of the old shared data is decremented. If the reference count of the old shared data object becomes 0 then the old shared data object is deleted.

template<typename T >
bool QSharedDataPointer< T >::operator== ( const QSharedDataPointer< T > &  other) const
inline

Returns true if this QSharedDataPointer is equal to other, otherwise returns false.

template<typename T >
void QSharedDataPointer< T >::swap ( QSharedDataPointer< T > &  other)
inline

Swap this instance's shared data pointer with the shared data pointer in other.