CopperSpice API  1.9.1
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<class 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.

This example shows an implicitly shared implementation of the class Employee.

  • Define the class Employee to have a single data member of type QSharedDataPointer<EmployeeData>.
  • Define the EmployeeData class derived from QSharedData to contain all the data members which would typically have been declared in the Employee class.
#include <QSharedData>
#include <QString>
class EmployeeData : public QSharedData
{
public:
EmployeeData()
: id(-1)
{ }
int id;
QString name;
};
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:
};

The copy constructor for Employee is not strictly required in this example because a definition of the class EmployeeData is in scope when the class Employee is defined. However, it is common to only have a forward declaration for the data in scope so the copy constructor should be explicitly defined.

Implementing a class which uses copy on write has significant drawbacks and is not recommended.

See also
QSharedData, QExplicitlySharedDataPointer, QScopedPointer, QSharedPointer

Member Typedef Documentation

template<class T >
QSharedDataPointer< T >::pointer

Typedef for T*.

template<class T >
QSharedDataPointer< T >::Type

Typedef for T.

Constructor & Destructor Documentation

template<class 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<class T >
QSharedDataPointer< T >::QSharedDataPointer ( const QSharedDataPointer< T > &  other)
inline

Sets the data pointer of this to the data pointer in other and increments the reference count of the shared data object.

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

Move constructs a new QSharedDataPointer from other.

template<class 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<class 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<class 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<class T >
T * QSharedDataPointer< T >::data ( )
inline

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

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

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

template<class 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<class T >
QSharedDataPointer< T >::operator const T * ( ) const
inline

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

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

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

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

Returns true if the current QSharedDataPointer is null.

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

Returns true if other and the current QSharedDataPointer point to different objects. This method does not call detach().

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

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

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

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

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

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

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

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

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

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

The reference count is incremented and the 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<class T >
QSharedDataPointer< T > & QSharedDataPointer< T >::operator= ( QSharedDataPointer< T > &&  other)
inline

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

template<class 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<class T >
bool QSharedDataPointer< T >::operator== ( const QSharedDataPointer< T > &  other) const
inline

Returns true if other and the current QSharedDataPointer, point to the same object. This method does not call detach().

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

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