Commit 1522ae2b authored by Milian Wolff's avatar Milian Wolff
Browse files

Move item repository refcounting utility code to separate file

We only need this in two files so far, no need to pull it into
all other TUs that include the itemrepository without custom
refcounting logic.
parent 5026a1b8
......@@ -11,6 +11,7 @@
#include "stringhelpers.h"
#include "appendedlist_static.h"
#include "serialization/itemrepository.h"
#include "serialization/itemrepositoryreferencecounting.h"
#include "util/kdevhash.h"
#include <debug.h>
......@@ -1295,7 +1296,7 @@ IndexedTypeIdentifier::IndexedTypeIdentifier(const QString& identifier, bool isE
IndexedIdentifier::IndexedIdentifier(unsigned int index)
: m_index(index)
{
ItemRepositoryUtils::inc(this);
ItemRepositoryReferenceCounting::inc(this);
}
IndexedIdentifier::IndexedIdentifier()
......@@ -1321,12 +1322,12 @@ IndexedIdentifier::IndexedIdentifier(IndexedIdentifier&& rhs) Q_DECL_NOEXCEPT
IndexedIdentifier::~IndexedIdentifier()
{
ItemRepositoryUtils::dec(this);
ItemRepositoryReferenceCounting::dec(this);
}
IndexedIdentifier& IndexedIdentifier::operator=(unsigned int index)
{
ItemRepositoryUtils::setIndex(this, m_index, index);
ItemRepositoryReferenceCounting::setIndex(this, m_index, index);
return *this;
}
......@@ -1337,7 +1338,7 @@ IndexedIdentifier& IndexedIdentifier::operator=(const Identifier& id)
IndexedIdentifier& IndexedIdentifier::operator=(IndexedIdentifier&& rhs) Q_DECL_NOEXCEPT
{
ItemRepositoryUtils::moveIndex(this, m_index, &rhs, rhs.m_index, emptyConstantIdentifierPrivateIndex());
ItemRepositoryReferenceCounting::moveIndex(this, m_index, &rhs, rhs.m_index, emptyConstantIdentifierPrivateIndex());
return *this;
}
......@@ -1398,7 +1399,7 @@ IndexedQualifiedIdentifier::IndexedQualifiedIdentifier(unsigned int index)
{
ifDebug(qCDebug(LANGUAGE) << "(" << ++cnt << ")" << identifier().toString() << m_index);
ItemRepositoryUtils::inc(this);
ItemRepositoryReferenceCounting::inc(this);
}
IndexedQualifiedIdentifier::IndexedQualifiedIdentifier()
......@@ -1426,7 +1427,7 @@ IndexedQualifiedIdentifier& IndexedQualifiedIdentifier::operator=(unsigned int i
{
ifDebug(qCDebug(LANGUAGE) << "(" << ++cnt << ")" << identifier().toString() << m_index);
ItemRepositoryUtils::setIndex(this, m_index, index);
ItemRepositoryReferenceCounting::setIndex(this, m_index, index);
return *this;
}
......@@ -1442,14 +1443,14 @@ IndexedQualifiedIdentifier& IndexedQualifiedIdentifier::operator=(const IndexedQ
IndexedQualifiedIdentifier& IndexedQualifiedIdentifier::operator=(IndexedQualifiedIdentifier&& rhs) Q_DECL_NOEXCEPT
{
ItemRepositoryUtils::moveIndex(this, m_index, &rhs, rhs.m_index, emptyConstantQualifiedIdentifierPrivateIndex());
ItemRepositoryReferenceCounting::moveIndex(this, m_index, &rhs, rhs.m_index, emptyConstantQualifiedIdentifierPrivateIndex());
return *this;
}
IndexedQualifiedIdentifier::~IndexedQualifiedIdentifier()
{
ifDebug(qCDebug(LANGUAGE) << "(" << ++cnt << ")" << identifier().toString() << index);
ItemRepositoryUtils::dec(this);
ItemRepositoryReferenceCounting::dec(this);
}
bool IndexedQualifiedIdentifier::operator==(const IndexedQualifiedIdentifier& rhs) const
......
......@@ -7,6 +7,7 @@
#include "instantiationinformation.h"
#include "identifier.h"
#include "serialization/itemrepository.h"
#include "serialization/itemrepositoryreferencecounting.h"
#include "types/typeutils.h"
#include <typeinfo>
#include "types/typealiastype.h"
......@@ -151,13 +152,13 @@ IndexedInstantiationInformation::IndexedInstantiationInformation(uint index) : m
if (m_index == standardInstantiationInformationIndex())
m_index = 0;
ItemRepositoryUtils::inc(this);
ItemRepositoryReferenceCounting::inc(this);
}
IndexedInstantiationInformation::IndexedInstantiationInformation(const IndexedInstantiationInformation& rhs) : m_index(
rhs.m_index)
{
ItemRepositoryUtils::inc(this);
ItemRepositoryReferenceCounting::inc(this);
}
IndexedInstantiationInformation::IndexedInstantiationInformation(IndexedInstantiationInformation&& rhs) noexcept
......@@ -168,20 +169,20 @@ IndexedInstantiationInformation::IndexedInstantiationInformation(IndexedInstanti
IndexedInstantiationInformation& IndexedInstantiationInformation::operator=(const IndexedInstantiationInformation& rhs)
{
ItemRepositoryUtils::setIndex(this, m_index, rhs.m_index);
ItemRepositoryReferenceCounting::setIndex(this, m_index, rhs.m_index);
return *this;
}
IndexedInstantiationInformation&
IndexedInstantiationInformation::operator=(IndexedInstantiationInformation&& rhs) noexcept
{
ItemRepositoryUtils::moveIndex(this, m_index, &rhs, rhs.m_index, standardInstantiationInformationIndex());
ItemRepositoryReferenceCounting::moveIndex(this, m_index, &rhs, rhs.m_index, standardInstantiationInformationIndex());
return *this;
}
IndexedInstantiationInformation::~IndexedInstantiationInformation()
{
ItemRepositoryUtils::dec(this);
ItemRepositoryReferenceCounting::dec(this);
}
bool IndexedInstantiationInformation::isValid() const
......
......@@ -137,7 +137,7 @@ public:
private:
friend class IndexedInstantiationInformation;
friend struct ItemRepositoryUtils;
friend struct ItemRepositoryReferenceCounting;
uint m_refCount;
};
......
......@@ -28,6 +28,7 @@ install(FILES
itemrepositoryexampleitem.h
itemrepository.h
itemrepositoryregistry.h
itemrepositoryreferencecounting.h
repositorymanager.h
DESTINATION ${KDE_INSTALL_INCLUDEDIR}/kdevplatform/serialization COMPONENT Devel
)
......
......@@ -2301,93 +2301,6 @@ static auto itemRepositoryOp(Op&& op)
QMutexLocker lock(repo.mutex());
return op(repo);
}
struct ItemRepositoryUtils {
template <typename Item>
static inline bool inc(Item* item)
{
auto index = item->index();
if (!index || !shouldDoDUChainReferenceCounting(item)) {
return false;
}
itemRepositoryOp<Item>(
[&](auto& repo) { item->increase(repo.dynamicItemFromIndexSimple(index)->m_refCount, index); });
return true;
}
template <typename Item>
static inline bool dec(Item* item)
{
auto index = item->index();
if (!index || !shouldDoDUChainReferenceCounting(item)) {
return false;
}
itemRepositoryOp<Item>(
[&](auto& repo) { item->decrease(repo.dynamicItemFromIndexSimple(index)->m_refCount, index); });
return true;
}
template <typename Item>
static inline void setIndex(Item* item, unsigned int& m_index, unsigned int index)
{
if (m_index == index) {
return;
}
if (shouldDoDUChainReferenceCounting(item)) {
itemRepositoryOp<Item>([&](auto& repo) {
if (m_index) {
item->decrease(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
m_index = index;
if (m_index) {
item->increase(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
});
} else {
m_index = index;
}
}
template <typename Item>
static void moveIndex(Item* lhs, unsigned int& lhs_index, Item* rhs, unsigned int& rhs_index,
unsigned int emptyIndex)
{
if (lhs == rhs) {
return;
}
const auto lhsShouldDoDUChainReferenceCounting = shouldDoDUChainReferenceCounting(lhs);
const auto rhsShouldDoDUChainReferenceCounting = shouldDoDUChainReferenceCounting(rhs);
if (!lhsShouldDoDUChainReferenceCounting && !rhsShouldDoDUChainReferenceCounting) {
lhs_index = rhs_index;
rhs_index = emptyIndex;
return;
}
itemRepositoryOp<Item>([&](auto& repo) {
if (lhs_index && lhsShouldDoDUChainReferenceCounting) {
lhs->decrease(repo.dynamicItemFromIndexSimple(lhs_index)->m_refCount, lhs_index);
} else if (rhs_index && rhsShouldDoDUChainReferenceCounting && !lhsShouldDoDUChainReferenceCounting) {
rhs->decrease(repo.dynamicItemFromIndexSimple(rhs_index)->m_refCount, rhs_index);
}
lhs_index = rhs_index;
rhs_index = emptyIndex;
if (lhs_index && lhsShouldDoDUChainReferenceCounting && !rhsShouldDoDUChainReferenceCounting) {
lhs->increase(repo.dynamicItemFromIndexSimple(lhs_index)->m_refCount, lhs_index);
}
});
}
};
}
#endif
/*
SPDX-FileCopyrightText: 2022 Milian Wolff <mail@milianw.de>
SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef KDEVPLATFORM_ITEMREPOSITORYREFERENCECOUNTING_H
#define KDEVPLATFORM_ITEMREPOSITORYREFERENCECOUNTING_H
#include "itemrepository.h"
namespace KDevelop {
struct ItemRepositoryReferenceCounting {
template <typename Item>
static inline bool inc(Item* item)
{
auto index = item->index();
if (!index || !shouldDoDUChainReferenceCounting(item)) {
return false;
}
itemRepositoryOp<Item>(
[&](auto& repo) { item->increase(repo.dynamicItemFromIndexSimple(index)->m_refCount, index); });
return true;
}
template <typename Item>
static inline bool dec(Item* item)
{
auto index = item->index();
if (!index || !shouldDoDUChainReferenceCounting(item)) {
return false;
}
itemRepositoryOp<Item>(
[&](auto& repo) { item->decrease(repo.dynamicItemFromIndexSimple(index)->m_refCount, index); });
return true;
}
template <typename Item>
static inline void setIndex(Item* item, unsigned int& m_index, unsigned int index)
{
if (m_index == index) {
return;
}
if (shouldDoDUChainReferenceCounting(item)) {
itemRepositoryOp<Item>([&](auto& repo) {
if (m_index) {
item->decrease(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
m_index = index;
if (m_index) {
item->increase(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
});
} else {
m_index = index;
}
}
template <typename Item>
static void moveIndex(Item* lhs, unsigned int& lhs_index, Item* rhs, unsigned int& rhs_index,
unsigned int emptyIndex)
{
if (lhs == rhs) {
return;
}
const auto lhsShouldDoDUChainReferenceCounting = shouldDoDUChainReferenceCounting(lhs);
const auto rhsShouldDoDUChainReferenceCounting = shouldDoDUChainReferenceCounting(rhs);
if (!lhsShouldDoDUChainReferenceCounting && !rhsShouldDoDUChainReferenceCounting) {
lhs_index = rhs_index;
rhs_index = emptyIndex;
return;
}
itemRepositoryOp<Item>([&](auto& repo) {
if (lhs_index && lhsShouldDoDUChainReferenceCounting) {
lhs->decrease(repo.dynamicItemFromIndexSimple(lhs_index)->m_refCount, lhs_index);
} else if (rhs_index && rhsShouldDoDUChainReferenceCounting && !lhsShouldDoDUChainReferenceCounting) {
rhs->decrease(repo.dynamicItemFromIndexSimple(rhs_index)->m_refCount, rhs_index);
}
lhs_index = rhs_index;
rhs_index = emptyIndex;
if (lhs_index && lhsShouldDoDUChainReferenceCounting && !rhsShouldDoDUChainReferenceCounting) {
lhs->increase(repo.dynamicItemFromIndexSimple(lhs_index)->m_refCount, lhs_index);
}
});
}
};
}
#endif
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