Commit 93224233 authored by Milian Wolff's avatar Milian Wolff
Browse files

Reuse itemRepositoryOp also for declaration.cpp implementation

Make the customization point more generic and reuse it here too.
I will be rolling this approach out in more places to ensure we
don't access the item repository without holding the lock properly.
parent 1522ae2b
......@@ -51,33 +51,22 @@ DeclarationData::DeclarationData()
{
}
namespace {
///@todo Use reference counting
class CommentRepository
{
public:
template <typename Op>
auto op(Op&& op)
struct DeclarationComment {
};
template <>
struct ItemRepositoryFor<DeclarationComment> {
static auto& repo()
{
QMutexLocker lock(&m_mutex);
return op(m_comments);
static QMutex mutex;
///@todo Use reference counting
static Repositories::StringRepository repo(QStringLiteral("Comment Repository"), &mutex);
return repo;
}
private:
QMutex m_mutex;
Repositories::StringRepository m_comments{QStringLiteral("Comment Repository"), &m_mutex};
};
CommentRepository& commentRepository()
{
static CommentRepository comments;
return comments;
}
} // unnamed namespace
void initDeclarationRepositories()
{
commentRepository();
ItemRepositoryFor<DeclarationComment>::repo();
}
Declaration::Kind Declaration::kind() const
......@@ -185,7 +174,7 @@ QByteArray Declaration::comment() const
if (!d->m_comment)
return QByteArray();
return commentRepository().op([d](const Repositories::StringRepository& repo) {
return itemRepositoryOp<DeclarationComment>([d](const Repositories::StringRepository& repo) {
return Repositories::arrayFromItem(repo.itemFromIndex(d->m_comment));
});
}
......@@ -201,7 +190,8 @@ void Declaration::setComment(const QByteArray& str)
const auto request = Repositories::StringRepositoryItemRequest(
str.constData(), IndexedString::hashString(str.constData(), str.length()), str.length());
d->m_comment = commentRepository().op([&](Repositories::StringRepository& repo) { return repo.index(request); });
d->m_comment = itemRepositoryOp<DeclarationComment>(
[&](Repositories::StringRepository& repo) { return repo.index(request); });
}
void Declaration::setComment(const QString& str)
......
......@@ -152,7 +152,7 @@ using IdentifierRepository = ItemRepository<ConstantIdentifierPrivate, Identifie
using IdentifierRepositoryManager = RepositoryManager<IdentifierRepository, false>;
template <>
struct ItemRepositoryForItemType<IndexedIdentifier> {
struct ItemRepositoryFor<IndexedIdentifier> {
static IdentifierRepository& repo()
{
static QRecursiveMutex mutex;
......@@ -330,12 +330,11 @@ using QualifiedIdentifierRepository
using QualifiedIdentifierRepositoryManager = RepositoryManager<QualifiedIdentifierRepository, false>;
template <>
struct ItemRepositoryForItemType<IndexedQualifiedIdentifier> {
struct ItemRepositoryFor<IndexedQualifiedIdentifier> {
static QualifiedIdentifierRepository& repo()
{
static QualifiedIdentifierRepositoryManager manager(
QStringLiteral("Qualified Identifier Repository"),
ItemRepositoryForItemType<IndexedIdentifier>::repo().mutex());
static QualifiedIdentifierRepositoryManager manager(QStringLiteral("Qualified Identifier Repository"),
ItemRepositoryFor<IndexedIdentifier>::repo().mutex());
return *manager.repository();
}
};
......
......@@ -124,7 +124,7 @@ using InstantiationInformationRepository
using InstantiationInformationRepositoryManager = RepositoryManager<InstantiationInformationRepository>;
template <>
struct ItemRepositoryForItemType<IndexedInstantiationInformation> {
struct ItemRepositoryFor<IndexedInstantiationInformation> {
static InstantiationInformationRepository& repo()
{
static InstantiationInformationRepositoryManager manager(QStringLiteral("Instantiation Information Repository"),
......
......@@ -2291,12 +2291,12 @@ private:
};
template <typename Item>
struct ItemRepositoryForItemType;
struct ItemRepositoryFor;
template <typename Item, typename Op>
static auto itemRepositoryOp(Op&& op)
{
auto& repo = ItemRepositoryForItemType<Item>::repo();
auto& repo = ItemRepositoryFor<Item>::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