Qt 4.8
Public Types | Public Functions | Protected Functions | Private Functions | Properties | List of all members
QExplicitlySharedDataPointer< T > Class Template Reference

The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object. More...

#include <qshareddata.h>

Public Types

typedef T * pointer
 
typedef T Type
 This is the type of the shared data object. More...
 

Public Functions

const T * constData () const
 Returns a const pointer to the shared data object. More...
 
T * data () const
 Returns a pointer to the shared data object. More...
 
void detach ()
 If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the d pointer of this to the copy. More...
 
 operator bool () const
 Returns true if the d pointer of this is not null. More...
 
bool operator! () const
 Returns true if the d pointer of this is null. More...
 
bool operator!= (const QExplicitlySharedDataPointer< T > &other) const
 Returns true if other and this do not have the same d pointer. More...
 
bool operator!= (const T *ptr) const
 Returns true if the d pointer of this is not ptr. More...
 
T & operator* () const
 Provides access to the shared data object's members. More...
 
T * operator-> ()
 Provides access to the shared data object's members. More...
 
T * operator-> () const
 Provides const access to the shared data object's members. More...
 
QExplicitlySharedDataPointer< T > & operator= (const QExplicitlySharedDataPointer< T > &o)
 Sets the d pointer of this to the d pointer of other and increments the reference count of the shared data object. More...
 
QExplicitlySharedDataPointeroperator= (T *o)
 Sets the d pointer of this to sharedData and increments {sharedData}'s reference count. More...
 
bool operator== (const QExplicitlySharedDataPointer< T > &other) const
 Returns true if other and this have the same d pointer. More...
 
bool operator== (const T *ptr) const
 Returns true if the d pointer of this is ptr. More...
 
 QExplicitlySharedDataPointer ()
 Constructs a QExplicitlySharedDataPointer initialized with a null d pointer. More...
 
 QExplicitlySharedDataPointer (T *data)
 Constructs a QExplicitlySharedDataPointer with d pointer set to sharedData and increments {sharedData}'s reference count. More...
 
 QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer< T > &o)
 This standard copy constructor sets the {d pointer} of this to the {d pointer} in other and increments the reference count of the shared data object. More...
 
template<class X >
 QExplicitlySharedDataPointer (const QExplicitlySharedDataPointer< X > &o)
 This copy constructor is different in that it allows other to be a different type of explicitly shared data pointer but one that has a compatible shared data object. More...
 
void reset ()
 Resets this to be null. More...
 
void swap (QExplicitlySharedDataPointer &other)
 Swap this instance's explicitly shared data pointer with the explicitly shared data pointer in other. More...
 
 ~QExplicitlySharedDataPointer ()
 Decrements the reference count of the shared data object. More...
 

Protected Functions

T * clone ()
 

Private Functions

void detach_helper ()
 

Properties

T * d
 

Detailed Description

template<class T>
class QExplicitlySharedDataPointer< T >

The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.

Since
4.4
Note
This class or function is reentrant.

QExplicitlySharedDataPointer<T> makes writing your own explicitly shared classes easy. QExplicitlySharedDataPointer implements thread-safe reference counting, ensuring that adding QExplicitlySharedDataPointers to your reentrant classes won't make them non-reentrant.

Except for one big difference, QExplicitlySharedDataPointer is just like QSharedDataPointer. The big difference is that member functions of QExplicitlySharedDataPointer do not do the automatic copy on write operation (detach()) that non-const members of QSharedDataPointer do before allowing the shared data object to be modified. There is a detach() function available, but if you really want to detach(), you have to call it yourself. This means that QExplicitlySharedDataPointers behave like regular C++ pointers, except that by doing reference counting and not deleting the shared data object until the reference count is 0, they avoid the dangling pointer problem.

It is instructive to compare QExplicitlySharedDataPointer with QSharedDataPointer by way of an example. Consider the Employee example in QSharedDataPointer, modified to use explicit sharing as explained in the discussion Implicit vs Explicit Sharing.

Note that if you use this class but find you are calling detach() a lot, you probably should be using QSharedDataPointer instead.

In the member function documentation, d pointer always refers to the internal pointer to the shared data object.

See also
QSharedData, QSharedDataPointer

Definition at line 136 of file qshareddata.h.

Typedefs

◆ pointer

template<class T>
QExplicitlySharedDataPointer< T >::pointer
Warning
This function is not part of the public interface.

Definition at line 140 of file qshareddata.h.

◆ Type

template<class T>
QExplicitlySharedDataPointer< T >::Type

This is the type of the shared data object.

The d pointer points to an object of this type.

Definition at line 139 of file qshareddata.h.

Constructors and Destructors

◆ QExplicitlySharedDataPointer() [1/4]

template<class T>
QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( )
inline

Constructs a QExplicitlySharedDataPointer initialized with a null d pointer.

Definition at line 165 of file qshareddata.h.

165 { d = 0; }

◆ ~QExplicitlySharedDataPointer()

template<class T>
QExplicitlySharedDataPointer< T >::~QExplicitlySharedDataPointer ( )
inline

Decrements the reference count of the shared data object.

If the reference count becomes 0, the shared data object is deleted. This is then destroyed.

Definition at line 166 of file qshareddata.h.

166 { if (d && !d->ref.deref()) delete d; }

◆ QExplicitlySharedDataPointer() [2/4]

template<class T>
Q_INLINE_TEMPLATE QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( T *  data)
explicit

Constructs a QExplicitlySharedDataPointer with d pointer set to sharedData and increments {sharedData}'s reference count.

Definition at line 257 of file qshareddata.h.

257  : d(adata)
258 { if (d) d->ref.ref(); }

◆ QExplicitlySharedDataPointer() [3/4]

This standard copy constructor sets the {d pointer} of this to the {d pointer} in other and increments the reference count of the shared data object.

Definition at line 169 of file qshareddata.h.

169 : d(o.d) { if (d) d->ref.ref(); }

◆ QExplicitlySharedDataPointer() [4/4]

template<class T>
template<class X >
QExplicitlySharedDataPointer< T >::QExplicitlySharedDataPointer ( const QExplicitlySharedDataPointer< X > &  other)
inline

This copy constructor is different in that it allows other to be a different type of explicitly shared data pointer but one that has a compatible shared data object.

It performs a static cast of the d pointer in other and sets the {d pointer} of this to the converted d pointer. It increments the reference count of the shared data object.

Definition at line 172 of file qshareddata.h.

172  : d(static_cast<T *>(o.data()))
173  {
174  if(d)
175  d->ref.ref();
176  }

Functions

◆ clone()

template<class T >
Q_INLINE_TEMPLATE T * QExplicitlySharedDataPointer< T >::clone ( )
protected
Since
4.5

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

See QSharedDataPointer::clone() for an explanation of how to use it.

Definition at line 241 of file qshareddata.h.

242 {
243  return new T(*d);
244 }

◆ constData()

template<class T>
const T * QExplicitlySharedDataPointer< T >::constData ( ) const
inline

Returns a const pointer to the shared data object.

See also
data()

Definition at line 146 of file qshareddata.h.

146 { return d; }

◆ data()

template<class T>
T * QExplicitlySharedDataPointer< T >::data ( ) const
inline

Returns a pointer to the shared data object.

Definition at line 145 of file qshareddata.h.

Referenced by QPatternist::DistinctIterator::actualReflection(), QPatternist::UntypedAtomicConverter::actualReflection(), QPatternist::IndexOfIterator::actualReflection(), QPatternist::ContextItem::actualReflection(), QPatternist::OrderBy::OrderSpec::actualReflection(), QPatternist::QNameConstructor::actualReflection(), QPatternist::XsdElement::addSubstitutionGroup(), QPatternist::DocumentContentValidator::attribute(), QX11PaintEngine::begin(), QX11WindowSurface::beginPaint(), QGLContextPrivate::bindTextureFromNativePixmap(), QFontMetrics::boundingRect(), QFontMetricsF::boundingRect(), QFontMetrics::charWidth(), QPatternist::CallTargetDescription::checkCallsiteCircularity(), QPatternist::checkVariableCircularity(), QPixmap::clut(), QPixmap::colorCount(), QPatternist::SimpleContentConstructor::compress(), QPixmap::copy(), QPatternist::GenericPredicate::create(), QPatternist::create(), QPatternist::ExpressionFactory::createExpression(), QPatternist::XsdValidatingInstanceReader::createXQuery(), QPixmap::detach(), QFont::detach(), QX11PaintEngine::drawPixmap(), QX11PaintEngine::drawTiledPixmap(), QXmlQueryPrivate::dynamicContext(), QFontMetrics::elidedText(), QFontMetricsF::elidedText(), QPatternist::XsdValidatingInstanceReader::error(), QPatternist::ComparisonPlatform< IndexOfIterator, false >::fetchComparator(), QPatternist::findAxisStep(), QX11GLWindowSurface::flush(), QRawFont::fromFont(), QStaticTextPrivate::get(), QMeeGoGraphicsSystem::getLiveTextureHandle(), QMeeGoLivePixmapData::getSurfaceForBackingPixmap(), hasDuplicatedElementsInternal(), QMeeGoLivePixmapData::invalidateSurfaces(), QPatternist::Expression::invokeOptimizers(), QSslCertificate::issuerInfo(), QPatternist::Item::Item(), QPatternist::AtomicType::itemMatches(), QPatternist::CastingPlatform< UntypedAtomicConverter, true >::locateCaster(), QMeeGoGraphicsSystem::lockLiveTexture(), QFontInfo::operator=(), QFontMetrics::operator=(), QFontMetricsF::operator=(), QFont::operator=(), QPatternist::ItemType::operator|(), QPatternist::AxisStep::patternPriority(), QPixmap::pixmapData(), QPatternist::pushVariable(), QFont::QFont(), QFontMetrics::QFontMetrics(), QFontMetricsF::QFontMetricsF(), qstring_to_xtp(), qt_mac_cg_context(), qt_mac_create_imagemask(), qt_mac_pixmap_get_base(), qt_mac_pixmap_get_bytes_per_line(), QTextLayout::QTextLayout(), QPixmap::qwsBits(), QPixmap::qwsBytesPerLine(), QMeeGoGraphicsSystem::releaseLiveTexture(), QFont::resolve(), QPatternist::resolveVariable(), QPatternist::AccelTreeResourceLoader::retrieveDocument(), QPatternist::ConstructorFunctionsFactory::retrieveExpression(), QPatternist::Expression::rewrite(), QX11GLWindowSurface::scroll(), QSslCertificate::serialNumber(), QMacPixmapData::setAlphaChannel(), QPatternist::XsdSimpleType::setContext(), QPatternist::XsdComplexType::setContext(), QPatternist::XsdAttribute::Scope::setParent(), QPatternist::XsdElement::Scope::setParent(), QPatternist::UserFunctionCallsite::setSource(), QPatternist::XsdElement::setType(), QPatternist::XsdComplexType::setWxsSuperType(), QFontPrivate::smallCapsFontPrivate(), QSslCertificate::subjectInfo(), QFontMetrics::tightBoundingRect(), QFontMetricsF::tightBoundingRect(), toIcdConfig(), QPatternist::toItem(), QPatternist::AbstractFloat< isDouble >::toNegated(), QPatternist::Decimal::toNegated(), QPatternist::AtomicValue::toQt(), QPixmap::toWinHBITMAP(), QPatternist::DocumentFN::typeCheck(), QPatternist::CardinalityVerifier::verifyCardinality(), QPatternist::TypeChecker::verifyType(), QSslCertificate::version(), QFontMetrics::width(), QFontMetricsF::width(), QPatternist::NodeSortExpression::wrapAround(), QPatternist::StaticContext::wrapExpressionWith(), QPatternist::Literal::wrapLiteral(), QPatternist::AnyType::wxsTypeMatches(), QPatternist::BuiltinNodeType< kind >::xdtTypeMatches(), QPatternist::XSLTNodeTest::xdtTypeMatches(), QPatternist::XsdSchemaParser::XsdSchemaParser(), QPatternist::FunctionFactoryCollection::xslt20Factory(), and QPatternist::yyparse().

145 { return d; }

◆ detach()

template<class T>
void QExplicitlySharedDataPointer< T >::detach ( )
inline

If the shared data object's reference count is greater than 1, this function creates a deep copy of the shared data object and sets the d pointer of this to the copy.

Because QExplicitlySharedDataPointer does not do the automatic copy on write operations that members of QSharedDataPointer do, detach() is not called automatically anywhere in the member functions of this class. If you find that you are calling detach() everywhere in your code, consider using QSharedDataPointer instead.

Definition at line 148 of file qshareddata.h.

Referenced by QPicture::detach(), QStaticText::detach(), QGlyphRun::detach(), QDateTime::detach(), QFont::detach(), QDBusUnixFileDescriptor::giveFileDescriptor(), QRawFont::loadFromData(), QScriptString::operator=(), QScriptString::QScriptString(), and QRawFont::setPixelSize().

148 { if (d && d->ref != 1) detach_helper(); }

◆ detach_helper()

template<class T >
Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer< T >::detach_helper ( )
private

Definition at line 247 of file qshareddata.h.

248 {
249  T *x = clone();
250  x->ref.ref();
251  if (!d->ref.deref())
252  delete d;
253  d = x;
254 }

◆ operator bool()

template<class T>
QExplicitlySharedDataPointer< T >::operator bool ( ) const
inline

Returns true if the d pointer of this is not null.

Definition at line 158 of file qshareddata.h.

158 { return d != 0; }

◆ operator!()

template<class T>
bool QExplicitlySharedDataPointer< T >::operator! ( ) const
inline

Returns true if the d pointer of this is null.

Definition at line 206 of file qshareddata.h.

206 { return !d; }

◆ operator!=() [1/2]

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

Returns true if other and this do not have the same d pointer.

Definition at line 161 of file qshareddata.h.

161 { return d != other.d; }

◆ operator!=() [2/2]

template<class T>
bool QExplicitlySharedDataPointer< T >::operator!= ( const T *  ptr) const
inline

Returns true if the d pointer of this is not ptr.

Definition at line 163 of file qshareddata.h.

163 { return d != ptr; }
const T * ptr(const T &t)

◆ operator*()

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

Provides access to the shared data object's members.

Definition at line 142 of file qshareddata.h.

142 { return *d; }

◆ operator->() [1/2]

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

Provides access to the shared data object's members.

Definition at line 143 of file qshareddata.h.

143 { return d; }

◆ operator->() [2/2]

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

Provides const access to the shared data object's members.

Definition at line 144 of file qshareddata.h.

144 { return d; }

◆ operator=() [1/2]

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

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

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

Definition at line 178 of file qshareddata.h.

178  {
179  if (o.d != d) {
180  if (o.d)
181  o.d->ref.ref();
182  T *old = d;
183  d = o.d;
184  if (old && !old->ref.deref())
185  delete old;
186  }
187  return *this;
188  }

◆ operator=() [2/2]

template<class T>
QExplicitlySharedDataPointer & QExplicitlySharedDataPointer< T >::operator= ( T *  sharedData)
inline

Sets the d pointer of this to sharedData and increments {sharedData}'s reference count.

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

Definition at line 189 of file qshareddata.h.

189  {
190  if (o != d) {
191  if (o)
192  o->ref.ref();
193  T *old = d;
194  d = o;
195  if (old && !old->ref.deref())
196  delete old;
197  }
198  return *this;
199  }

◆ operator==() [1/2]

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

Returns true if other and this have the same d pointer.

Definition at line 160 of file qshareddata.h.

160 { return d == other.d; }

◆ operator==() [2/2]

template<class T>
bool QExplicitlySharedDataPointer< T >::operator== ( const T *  ptr) const
inline

Returns true if the d pointer of this is ptr.

Definition at line 162 of file qshareddata.h.

162 { return d == ptr; }
const T * ptr(const T &t)

◆ reset()

template<class T>
void QExplicitlySharedDataPointer< T >::reset ( )
inline

Resets this to be null.

i.e., this function sets the d pointer of this to 0, but first it decrements the reference count of the shared data object and deletes the shared data object if the reference count became 0.

Definition at line 150 of file qshareddata.h.

Referenced by QPatternist::ApplyTemplate::compress(), QXmlQueryPrivate::expression(), QXmlQueryPrivate::recompileRequired(), and QXmlQueryPrivate::setRequiredType().

151  {
152  if(d && !d->ref.deref())
153  delete d;
154 
155  d = 0;
156  }

◆ swap()

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

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

Definition at line 208 of file qshareddata.h.

Referenced by qSwap().

209  { qSwap(d, other.d); }
Q_INLINE_TEMPLATE void qSwap(QSharedDataPointer< T > &p1, QSharedDataPointer< T > &p2)
Definition: qshareddata.h:261

Properties

◆ d

template<class T>
T* QExplicitlySharedDataPointer< T >::d
private

The documentation for this class was generated from the following files: