Commit 9a28e06d authored by Milian Wolff's avatar Milian Wolff
Browse files

Unify template arguments for ItemRepositoryFor

The type we was is just a context, it doesn't have to be the item.
Unify on the non-item class for which the repository is used, or
introduce a separate context type tag where none exists. This allows
for shorter callee code.

Note that the separate type tag is already an established paradigm
which was used previously for the DeclarationComment e.g.

Fixes: #9
parent 56f3cc14
......@@ -149,8 +149,8 @@ private:
// Maps declaration-ids to definitions
using DefinitionsRepo = ItemRepository<DefinitionsItem, DefinitionsRequestItem>;
template <>
class ItemRepositoryFor<DefinitionsItem>
template<>
class ItemRepositoryFor<Definitions>
{
friend struct LockedItemRepository;
static DefinitionsRepo& repo()
......@@ -166,7 +166,7 @@ public:
Definitions::Definitions()
{
ItemRepositoryFor<DefinitionsItem>::init();
ItemRepositoryFor<Definitions>::init();
}
void Definitions::addDefinition(const DeclarationId& id, const IndexedDeclaration& definition)
......@@ -176,7 +176,7 @@ void Definitions::addDefinition(const DeclarationId& id, const IndexedDeclaratio
item.definitionsList().append(definition);
DefinitionsRequestItem request(item);
LockedItemRepository::write<DefinitionsItem>([&](DefinitionsRepo& repo) {
LockedItemRepository::write<Definitions>([&](DefinitionsRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -202,7 +202,7 @@ void Definitions::removeDefinition(const DeclarationId& id, const IndexedDeclara
item.declaration = id;
DefinitionsRequestItem request(item);
LockedItemRepository::write<DefinitionsItem>([&](DefinitionsRepo& repo) {
LockedItemRepository::write<Definitions>([&](DefinitionsRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -230,7 +230,7 @@ KDevVarLengthArray<IndexedDeclaration> Definitions::definitions(const Declaratio
item.declaration = id;
DefinitionsRequestItem request(item);
LockedItemRepository::read<DefinitionsItem>([&](const DefinitionsRepo& repo) {
LockedItemRepository::read<Definitions>([&](const DefinitionsRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -246,6 +246,8 @@ KDevVarLengthArray<IndexedDeclaration> Definitions::definitions(const Declaratio
void Definitions::dump(const QTextStream& out)
{
DefinitionsVisitor v(this, out);
LockedItemRepository::read<DefinitionsItem>([&](const DefinitionsRepo& repo) { repo.visitAllItems(v); });
LockedItemRepository::read<Definitions>([&](const DefinitionsRepo& repo) {
repo.visitAllItems(v);
});
}
}
......@@ -295,8 +295,10 @@ public:
using EnvironmentInformationListRepo
= ItemRepository<EnvironmentInformationListItem, EnvironmentInformationListRequest>;
template <>
class ItemRepositoryFor<EnvironmentInformationListItem>
struct EnvironmentInformationList {
};
template<>
class ItemRepositoryFor<EnvironmentInformationList>
{
friend struct LockedItemRepository;
static EnvironmentInformationListRepo& repo()
......@@ -313,8 +315,10 @@ public:
/// Maps top-context-indices to environment-information item.
using EnvironmentInformationRepo = ItemRepository<EnvironmentInformationItem, EnvironmentInformationRequest>;
template <>
class ItemRepositoryFor<EnvironmentInformationItem>
struct EnvironmentInformation {
};
template<>
class ItemRepositoryFor<EnvironmentInformation>
{
friend struct LockedItemRepository;
static EnvironmentInformationRepo& repo()
......@@ -522,7 +526,7 @@ public:
void addEnvironmentInformation(ParsingEnvironmentFilePointer info)
{
Q_ASSERT(!findInformation(info->indexedTopContext().index()));
Q_ASSERT(LockedItemRepository::read<EnvironmentInformationItem>([&](const EnvironmentInformationRepo& repo) {
Q_ASSERT(LockedItemRepository::read<EnvironmentInformation>([&](const EnvironmentInformationRepo& repo) {
return repo.findIndex(info->indexedTopContext().index()) == 0;
}));
......@@ -550,7 +554,7 @@ public:
removed2 = m_indexEnvironmentInformations.remove(infoIndex);
}
LockedItemRepository::write<EnvironmentInformationListItem>(
LockedItemRepository::write<EnvironmentInformationList>(
[infoIndex,
request = EnvironmentInformationListRequest(info->url())](EnvironmentInformationListRepo& repo) mutable {
// Remove it from the environment information lists if it was there
......@@ -568,7 +572,7 @@ public:
}
});
LockedItemRepository::write<EnvironmentInformationItem>(
LockedItemRepository::write<EnvironmentInformation>(
[infoIndex, removed, removed2](EnvironmentInformationRepo& repo) {
const uint index = repo.findIndex(infoIndex);
if (index) {
......@@ -592,7 +596,7 @@ public:
KDevVarLengthArray<uint> topContextIndices;
// First store all the possible indices into the KDevVarLengthArray, so we can process them without holding
// a mutex locked
LockedItemRepository::read<EnvironmentInformationListItem>(
LockedItemRepository::read<EnvironmentInformationList>(
[&topContextIndices,
request = EnvironmentInformationListRequest(url)](const EnvironmentInformationListRepo& repo) {
const EnvironmentInformationListItem* item = repo.findItem(request);
......@@ -716,7 +720,7 @@ public:
for (const ParsingEnvironmentFilePointer& file : qAsConst(check)) {
EnvironmentInformationRequest req(file.data());
LockedItemRepository::write<EnvironmentInformationItem>([&](EnvironmentInformationRepo& repo) {
LockedItemRepository::write<EnvironmentInformation>([&](EnvironmentInformationRepo& repo) {
uint index = repo.findIndex(req);
if (file->d_func()->isDynamic()) {
......@@ -758,7 +762,7 @@ public:
storeInformationList(url);
//Access the data in the repository, so the bucket isn't unloaded
const auto foundItem = LockedItemRepository::read<EnvironmentInformationListItem>(
const auto foundItem = LockedItemRepository::read<EnvironmentInformationList>(
[request = EnvironmentInformationListRequest(url)](const EnvironmentInformationListRepo& repo) {
return static_cast<bool>(repo.findItem(request));
});
......@@ -1044,7 +1048,7 @@ unloadContexts:
// Step two: Check if it is on disk, and if is, load it
// TODO: this looks pretty dubious, shouldn't we keep the repo locked while operating on the item?
const auto item = LockedItemRepository::read<EnvironmentInformationItem>(
const auto item = LockedItemRepository::read<EnvironmentInformation>(
[req = EnvironmentInformationRequest(topContextIndex)](const EnvironmentInformationRepo& repo) {
return repo.findItem(req);
});
......@@ -1094,8 +1098,9 @@ unloadContexts:
qCDebug(LANGUAGE) << "cleaning top-contexts";
CleanupListVisitor visitor;
uint startPos = 0;
LockedItemRepository::write<EnvironmentInformationItem>(
[&visitor](EnvironmentInformationRepo& repo) { repo.visitAllItems(visitor); });
LockedItemRepository::write<EnvironmentInformation>([&visitor](EnvironmentInformationRepo& repo) {
repo.visitAllItems(visitor);
});
int checkContextsCount = maxFinalCleanupCheckContexts;
int percentageOfContexts = (visitor.checkContexts.size() * 100) / minimumFinalCleanupCheckContextsPercentage;
......@@ -1192,7 +1197,7 @@ private:
}
}
LockedItemRepository::write<EnvironmentInformationListItem>(
LockedItemRepository::write<EnvironmentInformationList>(
[&, request = EnvironmentInformationListRequest(url)](EnvironmentInformationListRepo& repo) mutable {
const uint index = repo.findIndex(request);
......@@ -1292,8 +1297,8 @@ void DUChain::initialize()
// read. See https://bugs.kde.org/show_bug.cgi?id=250779
RecursiveImportRepository::repository();
ItemRepositoryFor<EnvironmentInformationListItem>::init();
ItemRepositoryFor<EnvironmentInformationItem>::init();
ItemRepositoryFor<EnvironmentInformationList>::init();
ItemRepositoryFor<EnvironmentInformation>::init();
// similar to above, see https://bugs.kde.org/show_bug.cgi?id=255323
initDeclarationRepositories();
......
......@@ -105,8 +105,8 @@ public:
// Maps declaration-ids to Importers
using ImportersRepo = ItemRepository<ImportersItem, ImportersRequestItem>;
template <>
class ItemRepositoryFor<ImportersItem>
template<>
class ItemRepositoryFor<Importers>
{
friend struct LockedItemRepository;
static ImportersRepo& repo()
......@@ -122,7 +122,7 @@ public:
Importers::Importers()
{
ItemRepositoryFor<ImportersItem>::init();
ItemRepositoryFor<Importers>::init();
}
void Importers::addImporter(const DeclarationId& id, const IndexedDUContext& use)
......@@ -132,7 +132,7 @@ void Importers::addImporter(const DeclarationId& id, const IndexedDUContext& use
item.importersList().append(use);
ImportersRequestItem request(item);
LockedItemRepository::write<ImportersItem>([&](ImportersRepo& repo) {
LockedItemRepository::write<Importers>([&](ImportersRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -158,7 +158,7 @@ void Importers::removeImporter(const DeclarationId& id, const IndexedDUContext&
item.declaration = id;
ImportersRequestItem request(item);
LockedItemRepository::write<ImportersItem>([&](ImportersRepo& repo) {
LockedItemRepository::write<Importers>([&](ImportersRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -186,7 +186,7 @@ KDevVarLengthArray<IndexedDUContext> Importers::importers(const DeclarationId& i
item.declaration = id;
ImportersRequestItem request(item);
LockedItemRepository::read<ImportersItem>([&](const ImportersRepo& repo) {
LockedItemRepository::read<Importers>([&](const ImportersRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......
......@@ -207,8 +207,8 @@ public:
QHash<TopDUContext::IndexedRecursiveImports, CachedIndexedRecursiveImports> importsCache;
};
template <>
class ItemRepositoryFor<PersistentSymbolTableItem>
template<>
class ItemRepositoryFor<PersistentSymbolTable>
{
friend struct LockedItemRepository;
static PersistentSymbolTableRepo& repo()
......@@ -224,7 +224,7 @@ public:
void PersistentSymbolTable::clearCache()
{
LockedItemRepository::write<PersistentSymbolTableItem>([](PersistentSymbolTableRepo& repo) {
LockedItemRepository::write<PersistentSymbolTable>([](PersistentSymbolTableRepo& repo) {
repo.importsCache.clear();
repo.declarationsCache.clear();
});
......@@ -232,7 +232,7 @@ void PersistentSymbolTable::clearCache()
PersistentSymbolTable::PersistentSymbolTable()
{
ItemRepositoryFor<PersistentSymbolTableItem>::init();
ItemRepositoryFor<PersistentSymbolTable>::init();
RecursiveImportCacheRepository::repository();
}
......@@ -245,7 +245,7 @@ void PersistentSymbolTable::addDeclaration(const IndexedQualifiedIdentifier& id,
PersistentSymbolTableItem item;
item.id = id;
LockedItemRepository::write<PersistentSymbolTableItem>([&item, &declaration](PersistentSymbolTableRepo& repo) {
LockedItemRepository::write<PersistentSymbolTable>([&item, &declaration](PersistentSymbolTableRepo& repo) {
repo.declarationsCache.remove(item.id);
uint index = repo.findIndex(item);
......@@ -295,7 +295,7 @@ void PersistentSymbolTable::removeDeclaration(const IndexedQualifiedIdentifier&
PersistentSymbolTableItem item;
item.id = id;
LockedItemRepository::write<PersistentSymbolTableItem>([&item, &declaration](PersistentSymbolTableRepo& repo) {
LockedItemRepository::write<PersistentSymbolTable>([&item, &declaration](PersistentSymbolTableRepo& repo) {
repo.declarationsCache.remove(item.id);
uint index = repo.findIndex(item);
......@@ -344,7 +344,7 @@ void PersistentSymbolTable::visitDeclarations(const IndexedQualifiedIdentifier&
PersistentSymbolTableItem item;
item.id = id;
LockedItemRepository::read<PersistentSymbolTableItem>([&item, &visitor](const PersistentSymbolTableRepo& repo) {
LockedItemRepository::read<PersistentSymbolTable>([&item, &visitor](const PersistentSymbolTableRepo& repo) {
uint index = repo.findIndex(item);
if (!index) {
......@@ -372,7 +372,7 @@ void PersistentSymbolTable::visitFilteredDeclarations(const IndexedQualifiedIden
PersistentSymbolTableItem item;
item.id = id;
LockedItemRepository::write<PersistentSymbolTableItem>([&](PersistentSymbolTableRepo& repo) {
LockedItemRepository::write<PersistentSymbolTable>([&](PersistentSymbolTableRepo& repo) {
uint index = repo.findIndex(item);
if (!index) {
return;
......@@ -493,7 +493,7 @@ void PersistentSymbolTable::dump(const QTextStream& out)
QDebug qout = fromTextStream(out);
DebugVisitor v(out);
LockedItemRepository::read<PersistentSymbolTableItem>([&](const PersistentSymbolTableRepo& repo) {
LockedItemRepository::read<PersistentSymbolTable>([&](const PersistentSymbolTableRepo& repo) {
repo.visitAllItems(v);
qout << "Statistics:" << endl;
......
......@@ -93,8 +93,8 @@ QRecursiveMutex* typeRepositoryMutex()
using TypeItemRepository = ItemRepository<AbstractTypeData, AbstractTypeDataRequest, true, QRecursiveMutex>;
template <>
class ItemRepositoryFor<AbstractTypeData>
template<>
class ItemRepositoryFor<AbstractType>
{
friend struct LockedItemRepository;
static TypeItemRepository& repo()
......@@ -110,7 +110,7 @@ public:
void initTypeRepository()
{
ItemRepositoryFor<AbstractTypeData>::init();
ItemRepositoryFor<AbstractType>::init();
}
uint TypeRepository::indexForType(const AbstractType::Ptr& input)
......@@ -118,8 +118,10 @@ uint TypeRepository::indexForType(const AbstractType::Ptr& input)
if (!input)
return 0;
uint i = LockedItemRepository::write<AbstractTypeData>(
[request = AbstractTypeDataRequest(*input)](TypeItemRepository& repo) { return repo.index(request); });
uint i = LockedItemRepository::write<AbstractType>(
[request = AbstractTypeDataRequest(*input)](TypeItemRepository& repo) {
return repo.index(request);
});
#ifdef DEBUG_TYPE_REPOSITORY
AbstractType::Ptr t = typeForIndex(i);
if (!t->equals(input.data())) {
......@@ -140,7 +142,7 @@ AbstractType::Ptr TypeRepository::typeForIndex(uint index)
if (index == 0)
return AbstractType::Ptr();
return LockedItemRepository::read<AbstractTypeData>([index](const TypeItemRepository& repo) {
return LockedItemRepository::read<AbstractType>([index](const TypeItemRepository& repo) {
auto item = repo.itemFromIndex(index);
return AbstractType::Ptr(TypeSystem::self().create(const_cast<AbstractTypeData*>(item)));
});
......@@ -152,7 +154,7 @@ static void changeReferenceCount(uint index, RefCountChanger changeRefCount)
if (!index)
return;
LockedItemRepository::write<AbstractTypeData>([index, changeRefCount](TypeItemRepository& repo) {
LockedItemRepository::write<AbstractType>([index, changeRefCount](TypeItemRepository& repo) {
AbstractTypeData* data = repo.dynamicItemFromIndexSimple(index);
Q_ASSERT(data);
changeRefCount(data->refCount);
......
......@@ -104,8 +104,8 @@ public:
// Maps declaration-ids to Uses
using UsesRepo = ItemRepository<UsesItem, UsesRequestItem>;
template <>
class ItemRepositoryFor<UsesItem>
template<>
class ItemRepositoryFor<Uses>
{
friend struct LockedItemRepository;
static UsesRepo& repo()
......@@ -121,7 +121,7 @@ public:
Uses::Uses()
{
ItemRepositoryFor<UsesItem>::init();
ItemRepositoryFor<Uses>::init();
}
void Uses::addUse(const DeclarationId& id, const IndexedTopDUContext& use)
......@@ -131,7 +131,7 @@ void Uses::addUse(const DeclarationId& id, const IndexedTopDUContext& use)
item.usesList().append(use);
UsesRequestItem request(item);
LockedItemRepository::write<UsesItem>([&](UsesRepo& repo) {
LockedItemRepository::write<Uses>([&](UsesRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -157,7 +157,7 @@ void Uses::removeUse(const DeclarationId& id, const IndexedTopDUContext& use)
item.declaration = id;
UsesRequestItem request(item);
LockedItemRepository::write<UsesItem>([&](UsesRepo& repo) {
LockedItemRepository::write<Uses>([&](UsesRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......@@ -182,8 +182,9 @@ bool Uses::hasUses(const DeclarationId& id) const
UsesItem item;
item.declaration = id;
return LockedItemRepository::read<UsesItem>(
[&item](const UsesRepo& repo) { return static_cast<bool>(repo.findIndex(item)); });
return LockedItemRepository::read<Uses>([&item](const UsesRepo& repo) {
return static_cast<bool>(repo.findIndex(item));
});
}
KDevVarLengthArray<IndexedTopDUContext> Uses::uses(const DeclarationId& id) const
......@@ -194,7 +195,7 @@ KDevVarLengthArray<IndexedTopDUContext> Uses::uses(const DeclarationId& id) cons
item.declaration = id;
UsesRequestItem request(item);
LockedItemRepository::read<UsesItem>([&](const UsesRepo& repo) {
LockedItemRepository::read<Uses>([&](const UsesRepo& repo) {
uint index = repo.findIndex(item);
if (index) {
......
......@@ -138,8 +138,8 @@ using IndexedStringRepository = ItemRepository<IndexedStringData, IndexedStringR
namespace KDevelop
{
template <>
class ItemRepositoryFor<IndexedStringData>
template<>
class ItemRepositoryFor<IndexedString>
{
friend struct LockedItemRepository;
static IndexedStringRepository& repo()
......@@ -172,7 +172,7 @@ public:
void editRepo() const
{
if (m_index && !isSingleCharIndex(m_index)) {
LockedItemRepository::write<IndexedStringData>(*this);
LockedItemRepository::write<IndexedString>(*this);
}
}
......@@ -213,7 +213,7 @@ IndexedString::IndexedString(const char* str, unsigned short length, uint hash)
} else {
const auto request = IndexedStringRepositoryItemRequest(str, hash ? hash : hashString(str, length), length);
bool refcount = shouldDoDUChainReferenceCounting(this);
m_index = LockedItemRepository::write<IndexedStringData>([request, refcount](IndexedStringRepository& repo) {
m_index = LockedItemRepository::write<IndexedString>([request, refcount](IndexedStringRepository& repo) {
auto index = repo.index(request);
if (refcount) {
ReferenceCountChanger::increase(index)(repo);
......@@ -334,8 +334,9 @@ QString IndexedString::str() const
return QString(QLatin1Char(indexToChar(m_index)));
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedStringData>(
[index](const IndexedStringRepository& repo) { return stringFromItem(repo.itemFromIndex(index)); });
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return stringFromItem(repo.itemFromIndex(index));
});
}
}
......@@ -351,8 +352,9 @@ int IndexedString::lengthFromIndex(uint index)
} else if (isSingleCharIndex(index)) {
return 1;
} else {
return LockedItemRepository::read<IndexedStringData>(
[index](const IndexedStringRepository& repo) { return repo.itemFromIndex(index)->length; });
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return repo.itemFromIndex(index)->length;
});
}
}
......@@ -369,8 +371,9 @@ const char* IndexedString::c_str() const
return reinterpret_cast<const char*>(&m_index) + offset;
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedStringData>(
[index](const IndexedStringRepository& repo) { return c_strFromItem(repo.itemFromIndex(index)); });
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return c_strFromItem(repo.itemFromIndex(index));
});
}
}
......@@ -382,8 +385,9 @@ QByteArray IndexedString::byteArray() const
return QByteArray(1, indexToChar(m_index));
} else {
const uint index = m_index;
return LockedItemRepository::read<IndexedStringData>(
[index](const IndexedStringRepository& repo) { return arrayFromItem(repo.itemFromIndex(index)); });
return LockedItemRepository::read<IndexedString>([index](const IndexedStringRepository& repo) {
return arrayFromItem(repo.itemFromIndex(index));
});
}
}
......@@ -406,8 +410,9 @@ uint IndexedString::indexForString(const char* str, short unsigned length, uint
return charToIndex(str[0]);
} else {
const auto request = IndexedStringRepositoryItemRequest(str, hash ? hash : hashString(str, length), length);
return LockedItemRepository::write<IndexedStringData>(
[request](IndexedStringRepository& repo) { return repo.index(request); });
return LockedItemRepository::write<IndexedString>([request](IndexedStringRepository& repo) {
return repo.index(request);
});
}
}
......
......@@ -2289,23 +2289,23 @@ private:
friend class ::BenchItemRepository;
};
template <typename Item>
template<typename Context>
class ItemRepositoryFor;
struct LockedItemRepository {
template <typename Item, typename Op>
template<typename Context, typename Op>
static auto read(Op&& op)
{
const auto& repo = ItemRepositoryFor<Item>::repo();
const auto& repo = ItemRepositoryFor<Context>::repo();
QMutexLocker lock(repo.mutex());
return op(repo);
}
template <typename Item, typename Op>
template<typename Context, typename Op>
static auto write(Op&& op)
{
auto& repo = ItemRepositoryFor<Item>::repo();
auto& repo = ItemRepositoryFor<Context>::repo();
QMutexLocker lock(repo.mutex());
return op(repo);
......
Supports Markdown
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