1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H
#define KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H
#include <QMetaType>
#include <QPair>
#include <language/languageexport.h>
namespace KDevelop {
class IndexedString;
class IndexedTopDUContextEmbeddedTreeHandler;
class TopDUContext;
/**
* Allows simple indirect access to top-contexts with on-demand loading
*/
class KDEVPLATFORMLANGUAGE_EXPORT IndexedTopDUContext
{
public:
inline IndexedTopDUContext(uint index) : m_index(index)
{
if (!index)
setIsDummy(true);
}
IndexedTopDUContext(TopDUContext* context = nullptr);
enum {
DummyMask = 1u << 31u
};
/**
* Returns the top-context represented by this indexed top-context. If it wasn't loaded yet, it is loaded.
*
* The duchain must be read-locked when this is called!
* To prevent it from being unloaded, store it into a ReferencedTopDUContext before
* releasing the duchain lock.
*/
TopDUContext* data() const;
/**
* Returns whether the top-context is currently loaded.
*
* If not, it will be loaded when you call data().
*/
bool isLoaded() const;
inline bool operator==(const IndexedTopDUContext& rhs) const
{
return m_index == rhs.m_index;
}
inline bool operator!=(const IndexedTopDUContext& rhs) const
{
return m_index != rhs.m_index;
}
inline bool operator<(const IndexedTopDUContext& rhs) const
{
return m_index < rhs.m_index;
}
inline bool isValid() const
{
return m_index && !isDummy();
}
inline uint index() const
{
if (isDummy())
return 0;
else
return m_index;
}
inline bool isDummy() const
{
return m_index & DummyMask;
}
void setIsDummy(bool isDummy)
{
if (isDummy)
m_index |= DummyMask;
else
m_index &= ~(( uint )DummyMask);
}
/**
* Allows giving this IndexedTopDUContext some data while logically keeping it invalid.
*
* It will still return zero on index(), data(), etc.
*
* @param first The highest of this value bit will be removed.
*/
void setDummyData(ushort first, ushort second)
{
Q_ASSERT(isDummy());
m_index = (((( uint )first) << 16) + second) | DummyMask;
}
/**
* The data previously set through setDummyData(). Initially 0.
*/
QPair<ushort, ushort> dummyData() const
{
uint withoutMask = m_index & (~(( uint )DummyMask));
return qMakePair(( ushort )(withoutMask >> 16), ( ushort )withoutMask);
}
IndexedString url() const;
private:
uint m_index;
friend class IndexedTopDUContextEmbeddedTreeHandler;
};
struct IndexedTopDUContextIndexConversion
{
inline static uint toIndex(const IndexedTopDUContext& top)
{
return top.index();
}
inline static IndexedTopDUContext toItem(uint index)
{
return IndexedTopDUContext(index);
}
};
inline uint qHash(const IndexedTopDUContext& ctx)
{
return ctx.index();
}
}
Q_DECLARE_METATYPE(KDevelop::IndexedTopDUContext)
Q_DECLARE_TYPEINFO(KDevelop::IndexedTopDUContext, Q_MOVABLE_TYPE);
#endif // KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H
|