-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhandle.h
40 lines (26 loc) · 1.08 KB
/
handle.h
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
#pragma once
// (c) Robert Muth - see LICENSE for more info
/*
* The Handle abstraction serves several purposes:
* * a common base class for the most common abstractions
* * a 32 bit replacement for pointers which will save storage on 64 bit systems
* * an enabler for the data structure striping mechanism
*/
#include <cstdint>
namespace cwerg {
const int kKindFree = 0xff;
// Reference mimic 32 bit tagged pointers where the lowest 8 bits are the tag
struct Handle {
constexpr Handle(uint32_t index, uint8_t kind)
: value(index << 8 | uint8_t(kind)) {}
explicit constexpr Handle(int32_t a_value) : value(a_value) {}
constexpr Handle() : Handle(0, 0) {}
uint32_t index() const { return value >> 8; }
uint8_t raw_kind() const { return value & 0xff; }
bool operator==(const Handle& other) const { return value == other.value; }
bool operator!=(const Handle& other) const { return value != other.value; }
bool operator<(const Handle& other) const { return value < other.value; }
bool isnull() const { return index() == 0; }
uint32_t value;
};
} // namespace cwerg