QT的隱式共享(Implicit Sharing)

        爲了最大化使用系統資源,更少的內存拷貝,QT中的很多類使用了隱式數據共享。當傳遞參數時,使用隱式共享類既安全又高效,因爲只傳遞對象指針。當對象中的數據被修改時,對象纔會被拷貝。也就是我們常說的“寫拷貝”(copy-on-write)。

 

概述:

        共享類由一個指向共享數據塊的指針,此共享數據塊包含對象的引用計數和對象的數據。

        當一個共享對象被創建時,它的引用計數設置爲1。當一個新的對象引用了此對象,那麼引用計數就會加1。反之,如果一個對象不再引用此對象,引用計數就會減1。而引用計數爲0時,共享對象會被刪除。

        有兩種方法拷貝共享對象,我們通常叫做深拷貝和淺拷貝。深拷貝會複製對象,而淺拷貝是引用拷貝,只複製共享數據塊的指針。深拷貝會佔用較多內存和CPU資源,因爲它僅僅設置指針的值,增加引用計數。

        隱式數據對象賦值(操作符=())操作使用淺拷貝。

        共享的優點,在不必要的情況下,程序不需要複製數據,從而減少內存使用和數據拷貝。對象能被簡單的賦值,作爲函數參數傳遞,並從函數返回。

        隱式數據的特性對於用戶是透明的。程序員不需要去擔心這些特性。甚至在多線的應用中,隱式共享的具體細節,在“ Threads and Implicitly Shared Classes”中有詳細解釋。

        如果想自定義隱式共享類,可以使用QSharedData和QSharedDataPointer。

 

隱式共享的具體細節:

        如果對象即將改變,並且引用計數大於1,隱式共享對象會自動從共享數據塊中分離。(通常叫做寫複製或值語義。)

        隱式共享類對內部數據操作敏感。任何成員函數修改了共享數據,在修改數據之前都會分離共享數據塊。

        QPen使用了隱式共享。任何成員函數修改內部數據,都會導致共享數據塊的分離。

        代碼片段:

void QPen::setStyle(Qt::PenStyle style)
{
    detach();           // detach from common data
    d->style = style;   // set the style member
}

void QPen::detach()
{
    if (d->ref != 1) {
        ...             // perform a deep copy
    }
}

 

類列表:

        下面列出的類在對象即將改變時,會自動從共享數據塊分離。程序員甚至都沒注意到對象的共享。因此,應該將這些單獨的實例作爲單獨的對象對待。它們看上去就像單獨的對象,但是卻有共享對象的優點。因此,你可以將這些類的實例作爲函數參數傳遞而不需要擔心拷貝的開銷。

例如:

QPixmap p1, p2;
p1.load("image.bmp");
p2 = p1;                        // p1 and p2 share data

QPainter paint;
paint.begin(&p2);               // cuts p2 loose from p1
paint.drawText(0,50, "Hi");
paint.end();

在這個例子中,p1和p2共享數據,直到QPainter::begin對p2調用,因爲繪製操作會修改共享對象。

警告:當你使用非常量的標準模板庫風格迭代器迭代隱式共享容器(QMap、QVector等等)時,不要拷貝這些容器。

QBitmap

The QBitmap class provides monochrome (1-bit depth) pixmaps.

QIcon

The QIcon class provides scalable icons in different modes and states.

QImage

The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

QPicture

The QPicture class is a paint device that records and replays QPainter commands.

QPixmap

The QPixmap class is an off-screen image representation that can be used as a paint device.

QCursor

The QCursor class provides a mouse cursor with an arbitrary shape.

QKeySequence

The QKeySequence class encapsulates a key sequence as used by shortcuts.

QPalette

The QPalette class contains color groups for each widget state.

QOpenGLDebugMessage

The QOpenGLDebugMessage class wraps an OpenGL debug message.

QBrush

The QBrush class defines the fill pattern of shapes drawn by QPainter.

QGradient

The QGradient class is used in combination with QBrush to specify gradient fills.

QPainterPath

The QPainterPath class provides a container for painting operations, enabling graphical shapes to be constructed and reused.

QPen

The QPen class defines how a QPainter should draw lines and outlines of shapes.

QPolygon

The QPolygon class provides a vector of points using integer precision.

QPolygonF

The QPolygonF class provides a vector of points using floating point precision.

QRegion

The QRegion class specifies a clip region for a painter.

QFont

The QFont class specifies a font used for drawing text.

QFontInfo

The QFontInfo class provides general information about fonts.

QFontMetrics

The QFontMetrics class provides font metrics information.

QFontMetricsF

The QFontMetricsF class provides font metrics information.

QGlyphRun

The QGlyphRun class provides direct access to the internal glyphs in a font.

QRawFont

The QRawFont class provides access to a single physical instance of a font.

QStaticText

The QStaticText class enables optimized drawing of text when the text and its layout is updated rarely.

QTextCursor

The QTextCursor class offers an API to access and modify QTextDocuments.

QTextDocumentFragment

The QTextDocumentFragment class represents a piece of formatted text from a QTextDocument.

QTextBlockFormat

The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument.

QTextCharFormat

The QTextCharFormat class provides formatting information for characters in a QTextDocument.

QTextFormat

The QTextFormat class provides formatting information for a QTextDocument.

QTextFrameFormat

The QTextFrameFormat class provides formatting information for frames in a QTextDocument.

QTextImageFormat

The QTextImageFormat class provides formatting information for images in a QTextDocument.

QTextListFormat

The QTextListFormat class provides formatting information for lists in a QTextDocument.

QTextTableCellFormat

The QTextTableCellFormat class provides formatting information for table cells in a QTextDocument.

QTextTableFormat

The QTextTableFormat class provides formatting information for tables in a QTextDocument.

QNetworkCacheMetaData

The QNetworkCacheMetaData class provides cache information.

QHttpPart

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.

QNetworkCookie

The QNetworkCookie class holds one network cookie.

QNetworkRequest

The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.

QNetworkConfiguration

The QNetworkConfiguration class provides an abstraction of one or more access point configurations.

QDnsDomainNameRecord

The QDnsDomainNameRecord class stores information about a domain name record.

QDnsHostAddressRecord

The QDnsHostAddressRecord class stores information about a host address record.

QDnsMailExchangeRecord

The QDnsMailExchangeRecord class stores information about a DNS MX record.

QDnsServiceRecord

The QDnsServiceRecord class stores information about a DNS SRV record.

QDnsTextRecord

The QDnsTextRecord class stores information about a DNS TXT record.

QNetworkAddressEntry

The QNetworkAddressEntry class stores one IP address supported by a network interface, along with its associated netmask and broadcast address.

QNetworkInterface

The QNetworkInterface class provides a listing of the host's IP addresses and network interfaces.

QNetworkProxy

The QNetworkProxy class provides a network layer proxy.

QNetworkProxyQuery

The QNetworkProxyQuery class is used to query the proxy settings for a socket.

QSslCertificate

The QSslCertificate class provides a convenient API for an X509 certificate.

QSslCertificateExtension

The QSslCertificateExtension class provides an API for accessing the extensions of an X509 certificate.

QSslCipher

The QSslCipher class represents an SSL cryptographic cipher.

QSslConfiguration

The QSslConfiguration class holds the configuration and state of an SSL connection

QSslError

The QSslError class provides an SSL error.

QSslKey

The QSslKey class provides an interface for private and public keys.

QDebug

Output stream for debugging information

QDir

Access to directory structures and their contents

QFileInfo

System-independent file information

QProcessEnvironment

Holds the environment variables that can be passed to a program

QUrl

Convenient interface for working with URLs

QUrlQuery

Way to manipulate a key-value pairs in a URL's query

QPersistentModelIndex

Used to locate data in a data model

QVariant

Acts like a union for the most common Qt data types

QMimeType

Describes types of file or data, represented by a MIME type string

QBitArray

Array of bits

QByteArray

Array of bytes

QCache

Template class that provides a cache

QContiguousCache

Template class that provides a contiguous cache

QDateTime

Date and time functions

QHash

Template class that provides a hash-table-based dictionary

QMultiHash

Convenience QHash subclass that provides multi-valued hashes

QLinkedList

Template class that provides linked lists

QList

Template class that provides lists

QLocale

Converts between numbers and their string representations in various languages

QMap

Template class that provides a red-black-tree-based dictionary

QMultiMap

Convenience QMap subclass that provides multi-valued maps

QQueue

Generic container that provides a queue

QRegExp

Pattern matching using regular expressions

QRegularExpression

Pattern matching using regular expressions

QRegularExpressionMatch

The results of a matching a QRegularExpression against a string

QRegularExpressionMatchIterator

Iterator on the results of a global match of a QRegularExpression object against a string

QSet

Template class that provides a hash-table-based set

QStack

Template class that provides a stack

QString

Unicode character string

QStringList

List of strings

QTextBoundaryFinder

Way of finding Unicode text boundaries in a string

QVector

Template class that provides a dynamic array

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章