1 /*
2     SPDX-FileCopyrightText: 2015-2017 Milian Wolff <mail@milianw.de>
3 
4     SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #ifndef INDICES_H
8 #define INDICES_H
9 
10 #include <functional>
11 
12 // sadly, C++ doesn't yet have opaque typedefs
13 template <typename Base>
14 struct Index
15 {
16     uint32_t index = 0;
17 
18     explicit operator bool() const
19     {
20         return index;
21     }
22 
23     bool operator<(Index o) const
24     {
25         return index < o.index;
26     }
27 
28     bool operator<=(Index o) const
29     {
30         return index <= o.index;
31     }
32 
33     bool operator>(Index o) const
34     {
35         return index > o.index;
36     }
37 
38     bool operator>=(Index o) const
39     {
40         return index >= o.index;
41     }
42 
43     bool operator!=(Index o) const
44     {
45         return index != o.index;
46     }
47 
48     bool operator==(Index o) const
49     {
50         return index == o.index;
51     }
52 
53     void operator++()
54     {
55         ++index;
56     }
57 };
58 
59 template <typename Base>
60 unsigned int qHash(const Index<Base> index, unsigned int seed = 0) noexcept
61 {
62     return qHash(index.index, seed);
63 }
64 
65 struct StringIndex : public Index<StringIndex>
66 {
67 };
68 struct ModuleIndex : public StringIndex
69 {
70 };
71 struct FunctionIndex : public StringIndex
72 {
73 };
74 struct FileIndex : public StringIndex
75 {
76 };
77 struct IpIndex : public Index<IpIndex>
78 {
79 };
80 struct TraceIndex : public Index<TraceIndex>
81 {
82 };
83 struct AllocationIndex : public Index<AllocationIndex>
84 {
85 };
86 struct AllocationInfoIndex : public Index<AllocationInfoIndex>
87 {
88 };
89 
90 struct IndexHasher
91 {
92     template <typename Base>
operatorIndexHasher93     std::size_t operator()(const Index<Base> index) const
94     {
95         return std::hash<uint32_t>()(index.index);
96     }
97 };
98 
99 namespace std {
100 template <>
101 struct hash<StringIndex> : IndexHasher
102 {
103 };
104 template <>
105 struct hash<TraceIndex> : IndexHasher
106 {
107 };
108 template <>
109 struct hash<IpIndex> : IndexHasher
110 {
111 };
112 }
113 
114 #endif // INDICES_H
115