forked from qt-creator/qt-creator
Utils: Promote CppEditor::StringTable
It's useful on a more general level, and it's only useful as real singleton. Change-Id: I1da38a8eb6f9332a36261288d1b088ca6204eb7a Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -156,6 +156,7 @@ add_qtc_library(Utils
|
||||
sortfiltermodel.h
|
||||
span.h
|
||||
statuslabel.cpp statuslabel.h
|
||||
stringtable.cpp stringtable.h
|
||||
stringutils.cpp stringutils.h
|
||||
styledbar.cpp styledbar.h
|
||||
stylehelper.cpp stylehelper.h
|
||||
|
@@ -3,28 +3,27 @@
|
||||
|
||||
#include "stringtable.h"
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/runextensions.h>
|
||||
#include "runextensions.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QElapsedTimer>
|
||||
#include <QMutex>
|
||||
#include <QSet>
|
||||
#include <QThreadPool>
|
||||
#include <QTimer>
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
#endif
|
||||
// FIXME: Re-create in better location?
|
||||
//#ifdef WITH_TESTS
|
||||
//#include <extensionsystem/pluginmanager.h>
|
||||
//#endif
|
||||
|
||||
namespace CppEditor::Internal {
|
||||
namespace Utils::StringTable {
|
||||
|
||||
enum {
|
||||
GCTimeOut = 10 * 1000 // 10 seconds
|
||||
};
|
||||
|
||||
enum {
|
||||
DebugStringTable = 0
|
||||
DebugStringTable = 1
|
||||
};
|
||||
|
||||
class StringTablePrivate : public QObject
|
||||
@@ -44,7 +43,11 @@ public:
|
||||
QTimer m_gcCountDown;
|
||||
};
|
||||
|
||||
static StringTablePrivate *m_instance = nullptr;
|
||||
static StringTablePrivate &stringTable()
|
||||
{
|
||||
static StringTablePrivate theStringTable;
|
||||
return theStringTable;
|
||||
}
|
||||
|
||||
StringTablePrivate::StringTablePrivate()
|
||||
{
|
||||
@@ -56,14 +59,9 @@ StringTablePrivate::StringTablePrivate()
|
||||
connect(&m_gcCountDown, &QTimer::timeout, this, &StringTablePrivate::startGC);
|
||||
}
|
||||
|
||||
QString StringTable::insert(const Utils::FilePath &path)
|
||||
QTCREATOR_UTILS_EXPORT QString insert(const QString &string)
|
||||
{
|
||||
return m_instance->insert(path.path());
|
||||
}
|
||||
|
||||
QString StringTable::insert(const QString &string)
|
||||
{
|
||||
return m_instance->insert(string);
|
||||
return stringTable().insert(string);
|
||||
}
|
||||
|
||||
void StringTablePrivate::cancelAndWait()
|
||||
@@ -96,39 +94,37 @@ void StringTablePrivate::startGC()
|
||||
m_future = Utils::runAsync(&StringTablePrivate::GC, this);
|
||||
}
|
||||
|
||||
void StringTable::scheduleGC()
|
||||
QTCREATOR_UTILS_EXPORT void scheduleGC()
|
||||
{
|
||||
QMetaObject::invokeMethod(&m_instance->m_gcCountDown, QOverload<>::of(&QTimer::start),
|
||||
QMetaObject::invokeMethod(&stringTable().m_gcCountDown, QOverload<>::of(&QTimer::start),
|
||||
Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
StringTable::StringTable()
|
||||
{
|
||||
m_instance = new StringTablePrivate;
|
||||
}
|
||||
|
||||
StringTable::~StringTable()
|
||||
{
|
||||
delete m_instance;
|
||||
m_instance = nullptr;
|
||||
}
|
||||
static int bytesSaved = 0;
|
||||
|
||||
static inline bool isQStringInUse(const QString &string)
|
||||
{
|
||||
auto data_ptr = const_cast<QString&>(string).data_ptr();
|
||||
QStringPrivate data_ptr = const_cast<QString&>(string).data_ptr();
|
||||
if (DebugStringTable) {
|
||||
const int ref = data_ptr->d_ptr()->ref_;
|
||||
bytesSaved += (ref - 1) * string.size();
|
||||
if (ref > 10)
|
||||
qDebug() << ref << string.size() << string.left(50);
|
||||
}
|
||||
return data_ptr->isShared() || !data_ptr->isMutable() /* QStringLiteral ? */;
|
||||
}
|
||||
|
||||
void StringTablePrivate::GC(QFutureInterface<void> &futureInterface)
|
||||
{
|
||||
#ifdef WITH_TESTS
|
||||
if (ExtensionSystem::PluginManager::isScenarioRunning("TestStringTable")) {
|
||||
if (ExtensionSystem::PluginManager::finishScenario())
|
||||
QThread::sleep(5);
|
||||
}
|
||||
#endif
|
||||
//#ifdef WITH_TESTS
|
||||
// if (ExtensionSystem::PluginManager::isScenarioRunning("TestStringTable")) {
|
||||
// if (ExtensionSystem::PluginManager::finishScenario())
|
||||
// QThread::sleep(5);
|
||||
// }
|
||||
//#endif
|
||||
|
||||
int initialSize = 0;
|
||||
bytesSaved = 0;
|
||||
QElapsedTimer timer;
|
||||
if (DebugStringTable) {
|
||||
initialSize = m_strings.size();
|
||||
@@ -149,8 +145,9 @@ void StringTablePrivate::GC(QFutureInterface<void> &futureInterface)
|
||||
if (DebugStringTable) {
|
||||
const int currentSize = m_strings.size();
|
||||
qDebug() << "StringTable::GC removed" << initialSize - currentSize
|
||||
<< "strings in" << timer.elapsed() << "ms, size is now" << currentSize;
|
||||
<< "strings in" << timer.elapsed() << "ms, size is now" << currentSize
|
||||
<< "saved: " << bytesSaved << "bytes";
|
||||
}
|
||||
}
|
||||
|
||||
} // CppEditor::Internal
|
||||
} // Utils::StringTable
|
13
src/libs/utils/stringtable.h
Normal file
13
src/libs/utils/stringtable.h
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/utils_global.h>
|
||||
|
||||
namespace Utils::StringTable {
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QString insert(const QString &string);
|
||||
QTCREATOR_UTILS_EXPORT void scheduleGC();
|
||||
|
||||
} // Utils::StringTable
|
@@ -287,6 +287,8 @@ Project {
|
||||
"../3rdparty/span/span.hpp",
|
||||
"statuslabel.cpp",
|
||||
"statuslabel.h",
|
||||
"stringtable.cpp",
|
||||
"stringtable.h",
|
||||
"stringutils.cpp",
|
||||
"stringutils.h",
|
||||
"styledbar.cpp",
|
||||
|
@@ -109,7 +109,6 @@ add_qtc_plugin(CppEditor
|
||||
searchsymbols.cpp searchsymbols.h
|
||||
semantichighlighter.cpp semantichighlighter.h
|
||||
senddocumenttracker.cpp senddocumenttracker.h
|
||||
stringtable.cpp stringtable.h
|
||||
symbolfinder.cpp symbolfinder.h
|
||||
symbolsfindfilter.cpp symbolsfindfilter.h
|
||||
typehierarchybuilder.cpp typehierarchybuilder.h
|
||||
|
@@ -229,8 +229,6 @@ QtcPlugin {
|
||||
"semantichighlighter.h",
|
||||
"senddocumenttracker.cpp",
|
||||
"senddocumenttracker.h",
|
||||
"stringtable.cpp",
|
||||
"stringtable.h",
|
||||
"symbolfinder.cpp",
|
||||
"symbolfinder.h",
|
||||
"symbolsfindfilter.cpp",
|
||||
|
@@ -27,7 +27,6 @@
|
||||
#include "cpptypehierarchy.h"
|
||||
#include "projectinfo.h"
|
||||
#include "resourcepreviewhoverhandler.h"
|
||||
#include "stringtable.h"
|
||||
|
||||
#ifdef WITH_TESTS
|
||||
#include "compileroptionsbuilder_test.h"
|
||||
@@ -87,6 +86,7 @@
|
||||
#include <utils/macroexpander.h>
|
||||
#include <utils/mimeutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/stringtable.h>
|
||||
#include <utils/theme/theme.h>
|
||||
|
||||
#include <QAction>
|
||||
@@ -186,7 +186,6 @@ public:
|
||||
CppIncludeHierarchyFactory m_cppIncludeHierarchyFactory;
|
||||
CppEditorFactory m_cppEditorFactory;
|
||||
|
||||
StringTable stringTable;
|
||||
CppModelManager modelManager;
|
||||
CppCodeModelSettings m_codeModelSettings;
|
||||
CppToolsSettings settings;
|
||||
|
@@ -3,7 +3,9 @@
|
||||
|
||||
#include "cpplocatordata.h"
|
||||
|
||||
#include "stringtable.h"
|
||||
#include <utils/stringtable.h>
|
||||
|
||||
using namespace Utils;
|
||||
|
||||
namespace CppEditor {
|
||||
|
||||
@@ -57,7 +59,7 @@ void CppLocatorData::onAboutToRemoveFiles(const QStringList &files)
|
||||
}
|
||||
}
|
||||
|
||||
Internal::StringTable::scheduleGC();
|
||||
StringTable::scheduleGC();
|
||||
flushPendingDocument(false);
|
||||
}
|
||||
|
||||
@@ -70,7 +72,7 @@ void CppLocatorData::flushPendingDocument(bool force) const
|
||||
return;
|
||||
|
||||
for (CPlusPlus::Document::Ptr doc : std::as_const(m_pendingDocuments))
|
||||
m_infosByFile.insert(Internal::StringTable::insert(doc->filePath()), m_search(doc));
|
||||
m_infosByFile.insert(StringTable::insert(doc->filePath().toString()), m_search(doc));
|
||||
|
||||
m_pendingDocuments.clear();
|
||||
m_pendingDocuments.reserve(MaxPendingDocuments);
|
||||
|
@@ -2,21 +2,23 @@
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "searchsymbols.h"
|
||||
#include "stringtable.h"
|
||||
|
||||
#include <cplusplus/Icons.h>
|
||||
#include <cplusplus/LookupContext.h>
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/scopedswap.h>
|
||||
#include <utils/stringtable.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
using namespace CPlusPlus;
|
||||
using namespace Utils;
|
||||
|
||||
namespace CppEditor {
|
||||
|
||||
using ScopedIndexItemPtr = Utils::ScopedSwap<IndexItem::Ptr>;
|
||||
using ScopedScope = Utils::ScopedSwap<QString>;
|
||||
using ScopedIndexItemPtr = ScopedSwap<IndexItem::Ptr>;
|
||||
using ScopedScope = ScopedSwap<QString>;
|
||||
|
||||
SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
|
||||
SymbolSearcher::Classes
|
||||
@@ -37,7 +39,7 @@ void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types)
|
||||
|
||||
IndexItem::Ptr SearchSymbols::operator()(Document::Ptr doc, const QString &scope)
|
||||
{
|
||||
IndexItem::Ptr root = IndexItem::create(Internal::StringTable::insert(doc->filePath()), 100);
|
||||
IndexItem::Ptr root = IndexItem::create(StringTable::insert(doc->filePath().toString()), 100);
|
||||
|
||||
{ // RAII scope
|
||||
ScopedIndexItemPtr parentRaii(_parent, root);
|
||||
@@ -46,13 +48,13 @@ IndexItem::Ptr SearchSymbols::operator()(Document::Ptr doc, const QString &scope
|
||||
|
||||
QTC_ASSERT(_parent, return IndexItem::Ptr());
|
||||
QTC_ASSERT(root, return IndexItem::Ptr());
|
||||
QTC_ASSERT(_parent->filePath().toString() == Internal::StringTable::insert(doc->filePath()),
|
||||
QTC_ASSERT(_parent->filePath().toString() == StringTable::insert(doc->filePath().toString()),
|
||||
return IndexItem::Ptr());
|
||||
|
||||
for (int i = 0, ei = doc->globalSymbolCount(); i != ei; ++i)
|
||||
accept(doc->globalSymbolAt(i));
|
||||
|
||||
Internal::StringTable::scheduleGC();
|
||||
StringTable::scheduleGC();
|
||||
m_paths.clear();
|
||||
}
|
||||
|
||||
@@ -274,13 +276,13 @@ IndexItem::Ptr SearchSymbols::addChildItem(const QString &symbolName, const QStr
|
||||
m_paths.insert(symbol->fileId(), path);
|
||||
}
|
||||
|
||||
const QIcon icon = Icons::iconForSymbol(symbol);
|
||||
const QIcon icon = CPlusPlus::Icons::iconForSymbol(symbol);
|
||||
|
||||
IndexItem::Ptr newItem = IndexItem::create(Internal::StringTable::insert(symbolName),
|
||||
Internal::StringTable::insert(symbolType),
|
||||
Internal::StringTable::insert(symbolScope),
|
||||
IndexItem::Ptr newItem = IndexItem::create(StringTable::insert(symbolName),
|
||||
StringTable::insert(symbolType),
|
||||
StringTable::insert(symbolScope),
|
||||
itemType,
|
||||
Internal::StringTable::insert(path),
|
||||
StringTable::insert(path),
|
||||
symbol->line(),
|
||||
symbol->column() - 1, // 1-based vs 0-based column
|
||||
icon);
|
||||
|
@@ -1,23 +0,0 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/filepath.h>
|
||||
|
||||
namespace CppEditor::Internal {
|
||||
|
||||
class StringTable
|
||||
{
|
||||
public:
|
||||
static QString insert(const Utils::FilePath &string);
|
||||
static QString insert(const QString &string);
|
||||
static void scheduleGC();
|
||||
|
||||
private:
|
||||
friend class CppEditorPluginPrivate;
|
||||
StringTable();
|
||||
~StringTable();
|
||||
};
|
||||
|
||||
} // CppEditor::Internal
|
Reference in New Issue
Block a user