File indexing completed on 2024-04-28 05:41:19

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef INDICES_H
0008 #define INDICES_H
0009 
0010 #include <functional>
0011 
0012 // sadly, C++ doesn't yet have opaque typedefs
0013 template <typename Base>
0014 struct Index
0015 {
0016     uint32_t index = 0;
0017 
0018     explicit operator bool() const
0019     {
0020         return index;
0021     }
0022 
0023     bool operator<(Index o) const
0024     {
0025         return index < o.index;
0026     }
0027 
0028     bool operator<=(Index o) const
0029     {
0030         return index <= o.index;
0031     }
0032 
0033     bool operator>(Index o) const
0034     {
0035         return index > o.index;
0036     }
0037 
0038     bool operator>=(Index o) const
0039     {
0040         return index >= o.index;
0041     }
0042 
0043     bool operator!=(Index o) const
0044     {
0045         return index != o.index;
0046     }
0047 
0048     bool operator==(Index o) const
0049     {
0050         return index == o.index;
0051     }
0052 
0053     void operator++()
0054     {
0055         ++index;
0056     }
0057 };
0058 
0059 template <typename Base>
0060 unsigned int qHash(const Index<Base> index, unsigned int seed = 0) noexcept
0061 {
0062     return qHash(index.index, seed);
0063 }
0064 
0065 struct StringIndex : public Index<StringIndex>
0066 {
0067 };
0068 struct ModuleIndex : public StringIndex
0069 {
0070 };
0071 struct FunctionIndex : public StringIndex
0072 {
0073 };
0074 struct FileIndex : public StringIndex
0075 {
0076 };
0077 struct IpIndex : public Index<IpIndex>
0078 {
0079 };
0080 struct TraceIndex : public Index<TraceIndex>
0081 {
0082 };
0083 struct AllocationIndex : public Index<AllocationIndex>
0084 {
0085 };
0086 struct AllocationInfoIndex : public Index<AllocationInfoIndex>
0087 {
0088 };
0089 
0090 struct IndexHasher
0091 {
0092     template <typename Base>
0093     std::size_t operator()(const Index<Base> index) const
0094     {
0095         return std::hash<uint32_t>()(index.index);
0096     }
0097 };
0098 
0099 namespace std {
0100 template <>
0101 struct hash<StringIndex> : IndexHasher
0102 {
0103 };
0104 template <>
0105 struct hash<TraceIndex> : IndexHasher
0106 {
0107 };
0108 template <>
0109 struct hash<IpIndex> : IndexHasher
0110 {
0111 };
0112 }
0113 
0114 #endif // INDICES_H