Commit 4a32f1d8 authored by Debao Zhang's avatar Debao Zhang Committed by David Schulz

use QSharedPointer instead of QRefCountPointer

Change-Id: Ie670448bf924e02720eae6db33f2a346e8ac0001
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent 4435d55d
......@@ -46,6 +46,7 @@
#include <texteditor/texteditorsettings.h>
#include <QFileInfo>
#include <QSharedPointer>
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
......@@ -112,7 +113,7 @@ void CMakeEditor::build()
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factory, TextEditor::TextEditorActionHandler *ah)
: BaseTextEditorWidget(parent), m_factory(factory), m_ah(ah)
{
CMakeDocument *doc = new CMakeDocument();
QSharedPointer<CMakeDocument> doc(new CMakeDocument);
doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
setBaseTextDocument(doc);
......
......@@ -37,6 +37,7 @@
#include <texteditor/texteditorsettings.h>
#include <QCoreApplication>
#include <QSharedPointer>
using namespace TextEditor;
......@@ -124,7 +125,7 @@ ProjectFilesEditorWidget::ProjectFilesEditorWidget(QWidget *parent, ProjectFiles
m_factory(factory),
m_actionHandler(handler)
{
BaseTextDocument *doc = new BaseTextDocument();
QSharedPointer<BaseTextDocument> doc(new BaseTextDocument());
setBaseTextDocument(doc);
handler->setupActions(this);
......
......@@ -73,6 +73,7 @@
#include <QInputDialog>
#include <QToolBar>
#include <QTreeView>
#include <QSharedPointer>
using namespace GLSL;
using namespace GLSLEditor;
......@@ -159,7 +160,7 @@ GLSLTextEditorWidget::GLSLTextEditorWidget(QWidget *parent) :
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
new Highlighter(baseTextDocument());
new Highlighter(baseTextDocument().data());
// if (m_modelManager) {
// m_semanticHighlighter->setModelManager(m_modelManager);
......
......@@ -436,7 +436,7 @@ void SemanticHighlighter::applyResults(int from, int to)
if (m_startRevision != m_editor->editorRevision())
return;
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument();
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument().data();
QTC_ASSERT(baseTextDocument, return);
TextEditor::SyntaxHighlighter *highlighter = qobject_cast<TextEditor::SyntaxHighlighter *>(baseTextDocument->syntaxHighlighter());
QTC_ASSERT(highlighter, return);
......@@ -452,7 +452,7 @@ void SemanticHighlighter::finished()
if (m_startRevision != m_editor->editorRevision())
return;
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument();
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument().data();
QTC_ASSERT(baseTextDocument, return);
TextEditor::SyntaxHighlighter *highlighter = qobject_cast<TextEditor::SyntaxHighlighter *>(baseTextDocument->syntaxHighlighter());
QTC_ASSERT(highlighter, return);
......
......@@ -45,6 +45,7 @@
#include <QFileInfo>
#include <QDir>
#include <QSharedPointer>
namespace Qt4ProjectManager {
namespace Internal {
......@@ -81,7 +82,7 @@ Core::Id ProFileEditor::id() const
ProFileEditorWidget::ProFileEditorWidget(QWidget *parent, ProFileEditorFactory *factory, TextEditor::TextEditorActionHandler *ah)
: BaseTextEditorWidget(parent), m_factory(factory), m_ah(ah)
{
ProFileDocument *doc = new ProFileDocument();
QSharedPointer<ProFileDocument> doc(new ProFileDocument());
doc->setMimeType(QLatin1String(Constants::PROFILE_MIMETYPE));
setBaseTextDocument(doc);
......
......@@ -525,7 +525,7 @@ BaseTextEditor *BaseTextEditorWidget::editor() const
void BaseTextEditorWidget::selectEncoding()
{
BaseTextDocument *doc = d->m_document;
BaseTextDocument *doc = d->m_document.data();
CodecSelector codecSelector(this, doc);
switch (codecSelector.exec()) {
......@@ -655,7 +655,7 @@ void BaseTextEditorWidget::setChangeSet(const Utils::ChangeSet &changeSet)
Core::IDocument *BaseTextEditorWidget::editorDocument() const
{
return d->m_document;
return d->m_document.data();
}
void BaseTextEditorWidget::editorContentsChange(int position, int charsRemoved, int charsAdded)
......@@ -2136,15 +2136,15 @@ void BaseTextEditorWidget::setDisplayName(const QString &title)
emit changed();
}
BaseTextDocument *BaseTextEditorWidget::baseTextDocument() const
QSharedPointer<BaseTextDocument> BaseTextEditorWidget::baseTextDocument() const
{
return d->m_document;
}
void BaseTextEditorWidget::setBaseTextDocument(BaseTextDocument *doc)
void BaseTextEditorWidget::setBaseTextDocument(const QSharedPointer<BaseTextDocument> &doc)
{
if (doc) {
if (!doc.isNull()) {
d->setupDocumentSignals(doc);
d->m_document = doc;
}
......@@ -2504,12 +2504,12 @@ BaseTextEditorWidgetPrivate::~BaseTextEditorWidgetPrivate()
{
}
void BaseTextEditorWidgetPrivate::setupDocumentSignals(BaseTextDocument *document)
void BaseTextEditorWidgetPrivate::setupDocumentSignals(const QSharedPointer<BaseTextDocument> &document)
{
BaseTextDocument *oldDocument = q->baseTextDocument();
if (oldDocument) {
QSharedPointer<BaseTextDocument> oldDocument = q->baseTextDocument();
if (!oldDocument.isNull()) {
q->disconnect(oldDocument->document(), 0, q, 0);
q->disconnect(oldDocument, 0, q, 0);
q->disconnect(oldDocument.data(), 0, q, 0);
}
QTextDocument *doc = document->document();
......@@ -2534,10 +2534,10 @@ void BaseTextEditorWidgetPrivate::setupDocumentSignals(BaseTextDocument *documen
QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(changed()));
QObject::connect(doc, SIGNAL(contentsChange(int,int,int)), q,
SLOT(editorContentsChange(int,int,int)), Qt::DirectConnection);
QObject::connect(document, SIGNAL(changed()), q, SIGNAL(changed()));
QObject::connect(document, SIGNAL(titleChanged(QString)), q, SLOT(setDisplayName(QString)));
QObject::connect(document, SIGNAL(aboutToReload()), q, SLOT(documentAboutToBeReloaded()));
QObject::connect(document, SIGNAL(reloaded()), q, SLOT(documentReloaded()));
QObject::connect(document.data(), SIGNAL(changed()), q, SIGNAL(changed()));
QObject::connect(document.data(), SIGNAL(titleChanged(QString)), q, SLOT(setDisplayName(QString)));
QObject::connect(document.data(), SIGNAL(aboutToReload()), q, SLOT(documentAboutToBeReloaded()));
QObject::connect(document.data(), SIGNAL(reloaded()), q, SLOT(documentReloaded()));
q->slotUpdateExtraAreaWidth();
}
......@@ -6137,7 +6137,7 @@ void BaseTextEditorWidget::appendStandardContextMenuActions(QMenu *menu)
if (a && a->isEnabled())
menu->addAction(a);
BaseTextDocument *doc = baseTextDocument();
QSharedPointer<BaseTextDocument> doc = baseTextDocument();
if (doc->codec()->name() == QByteArray("UTF-8")) {
a = Core::ActionManager::command(Constants::SWITCH_UTF8BOM)->action();
if (a && a->isEnabled()) {
......@@ -6599,7 +6599,7 @@ IAssistInterface *BaseTextEditorWidget::createAssistInterface(AssistKind kind,
AssistReason reason) const
{
Q_UNUSED(kind);
return new DefaultAssistInterface(document(), position(), d->m_document, reason);
return new DefaultAssistInterface(document(), position(), d->m_document.data(), reason);
}
QString TextEditor::BaseTextEditorWidget::foldReplacementText(const QTextBlock &) const
......
......@@ -38,6 +38,7 @@
#include <find/ifindsupport.h>
#include <QPlainTextEdit>
#include <QSharedPointer>
QT_BEGIN_NAMESPACE
class QToolBar;
......@@ -357,8 +358,8 @@ public:
void duplicateFrom(BaseTextEditorWidget *editor);
protected:
BaseTextDocument *baseTextDocument() const;
void setBaseTextDocument(BaseTextDocument *doc);
QSharedPointer<BaseTextDocument> baseTextDocument() const;
void setBaseTextDocument(const QSharedPointer<BaseTextDocument> &doc);
void setDefaultPath(const QString &defaultPath);
......
......@@ -40,7 +40,7 @@
#include <utils/changeset.h>
#include <QBasicTimer>
#include <QSharedData>
#include <QSharedPointer>
#include <QPointer>
#include <QScopedPointer>
......@@ -75,73 +75,6 @@ public:
void fromSelection(const TabSettings &ts, const QTextCursor &selection);
};
//========== Pointers with reference count ==========
template <class T> class QRefCountData : public QSharedData
{
public:
QRefCountData(T *data) { m_data = data; }
~QRefCountData() { delete m_data; }
T *m_data;
};
/* MOSTLY COPIED FROM QSHAREDDATA(-POINTER) */
template <class T> class QRefCountPointer
{
public:
inline T &operator*() { return d ? *(d->m_data) : 0; }
inline const T &operator*() const { return d ? *(d->m_data) : 0; }
inline T *operator->() { return d ? d->m_data : 0; }
inline const T *operator->() const { return d ? d->m_data : 0; }
inline operator T *() { return d ? d->m_data : 0; }
inline operator const T *() const { return d ? d->m_data : 0; }
inline bool operator==(const QRefCountPointer<T> &other) const { return d == other.d; }
inline bool operator!=(const QRefCountPointer<T> &other) const { return d != other.d; }
inline QRefCountPointer() { d = 0; }
inline ~QRefCountPointer() { if (d && !d->ref.deref()) delete d; }
explicit QRefCountPointer(T *data) {
if (data) {
d = new QRefCountData<T>(data);
d->ref.ref();
}
else {
d = 0;
}
}
inline QRefCountPointer(const QRefCountPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); }
inline QRefCountPointer<T> & operator=(const QRefCountPointer<T> &o) {
if (o.d != d) {
if (d && !d->ref.deref())
delete d;
//todo: atomic assign of pointers
d = o.d;
if (d)
d->ref.ref();
}
return *this;
}
inline QRefCountPointer &operator=(T *o) {
if (d == 0 || d->m_data != o) {
if (d && !d->ref.deref())
delete d;
d = new QRefCountData<T>(o);
if (d)
d->ref.ref();
}
return *this;
}
inline bool operator!() const { return !d; }
private:
QRefCountData<T> *d;
};
//================BaseTextEditorPrivate==============
struct BaseTextEditorPrivateHighlightBlocks
......@@ -168,7 +101,7 @@ public:
~BaseTextEditorWidgetPrivate();
void setupBasicEditActions(TextEditorActionHandler *actionHandler);
void setupDocumentSignals(BaseTextDocument *document);
void setupDocumentSignals(const QSharedPointer<BaseTextDocument> &document);
void updateLineSelectionColor();
void print(QPrinter *printer);
......@@ -184,7 +117,7 @@ public:
QList<QTextEdit::ExtraSelection> m_syntaxHighlighterSelections;
QTextEdit::ExtraSelection m_lineSelection;
QRefCountPointer<BaseTextDocument> m_document;
QSharedPointer<BaseTextDocument> m_document;
QByteArray m_tempState;
QByteArray m_tempNavigationState;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment