-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtagged_ptr.hpp
More file actions
66 lines (52 loc) · 934 Bytes
/
Copy pathtagged_ptr.hpp
File metadata and controls
66 lines (52 loc) · 934 Bytes
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
#ifndef TAGGED_PTR_HPP
#define TAGGED_PTR_HPP
#include <cstddef>
#include <limits>
template <typename T>
class tagged_ptr
{
public:
using tag_t = std::size_t;
tagged_ptr() noexcept: ptr(nullptr), tag(0) {}
explicit tagged_ptr(T* p, tag_t t = 0) : ptr(p), tag(t) {}
tag_t next_tag() const
{
return (tag + 1) & (std::numeric_limits<tag_t>::max)();
}
void set_tag(tag_t t)
{
tag = t;
}
T* get_ptr() const
{
return ptr;
}
void set_ptr(T* p)
{
ptr = p;
}
T& operator*() const
{
return *ptr;
}
T* operator->() const
{
return ptr;
}
operator bool(void) const
{
return ptr != nullptr;
}
bool operator==(volatile tagged_ptr<T>& other)
{
return (ptr == other.ptr) && (tag == other.tag);
}
bool operator!= (volatile tagged_ptr<T>& other)
{
return !(operator==(other));
}
private:
T* ptr;
tag_t tag;
};
#endif /* TAGGED_PTR_HPP */