diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp index 0149f08dc48..61a9e057799 100644 --- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp @@ -46,6 +46,7 @@ #include #include +#include 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 doc(new CMakeDocument); doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)); setBaseTextDocument(doc); diff --git a/src/plugins/genericprojectmanager/genericprojectfileseditor.cpp b/src/plugins/genericprojectmanager/genericprojectfileseditor.cpp index b4cf19d02a0..a92593ef9cb 100644 --- a/src/plugins/genericprojectmanager/genericprojectfileseditor.cpp +++ b/src/plugins/genericprojectmanager/genericprojectfileseditor.cpp @@ -37,6 +37,7 @@ #include #include +#include using namespace TextEditor; @@ -124,7 +125,7 @@ ProjectFilesEditorWidget::ProjectFilesEditorWidget(QWidget *parent, ProjectFiles m_factory(factory), m_actionHandler(handler) { - BaseTextDocument *doc = new BaseTextDocument(); + QSharedPointer doc(new BaseTextDocument()); setBaseTextDocument(doc); handler->setupActions(this); diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp index 4f5bb5d663a..7a209e5c07b 100644 --- a/src/plugins/glsleditor/glsleditor.cpp +++ b/src/plugins/glsleditor/glsleditor.cpp @@ -73,6 +73,7 @@ #include #include #include +#include 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); diff --git a/src/plugins/qmljseditor/qmljssemantichighlighter.cpp b/src/plugins/qmljseditor/qmljssemantichighlighter.cpp index 15d6cfeceec..9db12efec83 100644 --- a/src/plugins/qmljseditor/qmljssemantichighlighter.cpp +++ b/src/plugins/qmljseditor/qmljssemantichighlighter.cpp @@ -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(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(baseTextDocument->syntaxHighlighter()); QTC_ASSERT(highlighter, return); diff --git a/src/plugins/qt4projectmanager/profileeditor.cpp b/src/plugins/qt4projectmanager/profileeditor.cpp index 5f511a14468..e49b4d16a5f 100644 --- a/src/plugins/qt4projectmanager/profileeditor.cpp +++ b/src/plugins/qt4projectmanager/profileeditor.cpp @@ -45,6 +45,7 @@ #include #include +#include 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 doc(new ProFileDocument()); doc->setMimeType(QLatin1String(Constants::PROFILE_MIMETYPE)); setBaseTextDocument(doc); diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index ac8f76eb930..cc22935149f 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -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 BaseTextEditorWidget::baseTextDocument() const { return d->m_document; } -void BaseTextEditorWidget::setBaseTextDocument(BaseTextDocument *doc) +void BaseTextEditorWidget::setBaseTextDocument(const QSharedPointer &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 &document) { - BaseTextDocument *oldDocument = q->baseTextDocument(); - if (oldDocument) { + QSharedPointer 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 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 diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index be0583b6c29..a61c30b6d1b 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -38,6 +38,7 @@ #include #include +#include QT_BEGIN_NAMESPACE class QToolBar; @@ -357,8 +358,8 @@ public: void duplicateFrom(BaseTextEditorWidget *editor); protected: - BaseTextDocument *baseTextDocument() const; - void setBaseTextDocument(BaseTextDocument *doc); + QSharedPointer baseTextDocument() const; + void setBaseTextDocument(const QSharedPointer &doc); void setDefaultPath(const QString &defaultPath); diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h index c8bcb8c5490..074ed6b0c3a 100644 --- a/src/plugins/texteditor/basetexteditor_p.h +++ b/src/plugins/texteditor/basetexteditor_p.h @@ -40,7 +40,7 @@ #include #include -#include +#include #include #include @@ -75,73 +75,6 @@ public: void fromSelection(const TabSettings &ts, const QTextCursor &selection); }; -//========== Pointers with reference count ========== - -template 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 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 &other) const { return d == other.d; } - inline bool operator!=(const QRefCountPointer &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(data); - d->ref.ref(); - } - else { - d = 0; - } - } - inline QRefCountPointer(const QRefCountPointer &o) : d(o.d) { if (d) d->ref.ref(); } - inline QRefCountPointer & operator=(const QRefCountPointer &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(o); - if (d) - d->ref.ref(); - } - return *this; - } - - inline bool operator!() const { return !d; } - -private: - QRefCountData *d; -}; - //================BaseTextEditorPrivate============== struct BaseTextEditorPrivateHighlightBlocks @@ -168,7 +101,7 @@ public: ~BaseTextEditorWidgetPrivate(); void setupBasicEditActions(TextEditorActionHandler *actionHandler); - void setupDocumentSignals(BaseTextDocument *document); + void setupDocumentSignals(const QSharedPointer &document); void updateLineSelectionColor(); void print(QPrinter *printer); @@ -184,7 +117,7 @@ public: QList m_syntaxHighlighterSelections; QTextEdit::ExtraSelection m_lineSelection; - QRefCountPointer m_document; + QSharedPointer m_document; QByteArray m_tempState; QByteArray m_tempNavigationState;