File indexing completed on 2024-05-12 04:38:03

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H
0008 #define KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H
0009 
0010 #include <QMetaType>
0011 #include <QPair>
0012 
0013 #include <language/languageexport.h>
0014 
0015 namespace KDevelop {
0016 class IndexedString;
0017 class IndexedTopDUContextEmbeddedTreeHandler;
0018 class TopDUContext;
0019 
0020 /**
0021  * Allows simple indirect access to top-contexts with on-demand loading
0022  */
0023 class KDEVPLATFORMLANGUAGE_EXPORT IndexedTopDUContext
0024 {
0025 public:
0026     inline IndexedTopDUContext(uint index) : m_index(index)
0027     {
0028         if (!index)
0029             setIsDummy(true);
0030     }
0031     IndexedTopDUContext(TopDUContext* context = nullptr);
0032 
0033     enum {
0034         DummyMask = 1u << 31u
0035     };
0036 
0037     /**
0038      * Returns the top-context represented by this indexed top-context. If it wasn't loaded yet, it is loaded.
0039      *
0040      * The duchain must be read-locked when this is called!
0041      * To prevent it from being unloaded, store it into a ReferencedTopDUContext before
0042      * releasing the duchain lock.
0043      */
0044     TopDUContext* data() const;
0045 
0046     /**
0047      * Returns whether the top-context is currently loaded.
0048      *
0049      * If not, it will be loaded when you call data().
0050      */
0051     bool isLoaded() const;
0052 
0053     inline bool operator==(const IndexedTopDUContext& rhs) const
0054     {
0055         return m_index == rhs.m_index;
0056     }
0057 
0058     inline bool operator!=(const IndexedTopDUContext& rhs) const
0059     {
0060         return m_index != rhs.m_index;
0061     }
0062 
0063     inline bool operator<(const IndexedTopDUContext& rhs) const
0064     {
0065         return m_index < rhs.m_index;
0066     }
0067 
0068     inline bool isValid() const
0069     {
0070         return m_index && !isDummy();
0071     }
0072 
0073     inline uint index() const
0074     {
0075         if (isDummy())
0076             return 0;
0077         else
0078             return m_index;
0079     }
0080 
0081     inline bool isDummy() const
0082     {
0083         return m_index & DummyMask;
0084     }
0085 
0086     void setIsDummy(bool isDummy)
0087     {
0088         if (isDummy)
0089             m_index |= DummyMask;
0090         else
0091             m_index &= ~(( uint )DummyMask);
0092     }
0093 
0094     /**
0095      * Allows giving this IndexedTopDUContext some data while logically keeping it invalid.
0096      *
0097      * It will still return zero on index(), data(), etc.
0098      *
0099      * @param first The highest of this value bit will be removed.
0100      */
0101     void setDummyData(ushort first, ushort second)
0102     {
0103         Q_ASSERT(isDummy());
0104         m_index = (((( uint )first) << 16) + second) | DummyMask;
0105     }
0106 
0107     /**
0108      * The data previously set through setDummyData(). Initially 0.
0109      */
0110     QPair<ushort, ushort> dummyData() const
0111     {
0112         uint withoutMask = m_index & (~(( uint )DummyMask));
0113         return qMakePair(( ushort )(withoutMask >> 16), ( ushort )withoutMask);
0114     }
0115 
0116     IndexedString url() const;
0117 
0118 private:
0119     uint m_index;
0120     friend class IndexedTopDUContextEmbeddedTreeHandler;
0121 };
0122 
0123 struct IndexedTopDUContextIndexConversion
0124 {
0125     inline static uint toIndex(const IndexedTopDUContext& top)
0126     {
0127         return top.index();
0128     }
0129 
0130     inline static IndexedTopDUContext toItem(uint index)
0131     {
0132         return IndexedTopDUContext(index);
0133     }
0134 };
0135 
0136 inline uint qHash(const IndexedTopDUContext& ctx)
0137 {
0138     return ctx.index();
0139 }
0140 }
0141 
0142 Q_DECLARE_METATYPE(KDevelop::IndexedTopDUContext)
0143 Q_DECLARE_TYPEINFO(KDevelop::IndexedTopDUContext, Q_MOVABLE_TYPE);
0144 
0145 #endif // KDEVPLATFORM_INDEXEDTOPDUCONTEXT_H