-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrictedPtr.h
48 lines (40 loc) · 1.23 KB
/
restrictedPtr.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
41
42
43
44
45
46
47
48
#ifndef __restrictedPtr_H__
#define __restrictedPtr_H__
template<typename T, uint32_t mask = 0xFFFFFFF0>
class restrictedPtr_t
{
private:
typedef T *ptr_t;
ptr_t ptr;
static_assert(mask > 0, "restrictedPtr_t: mask cannot be 0");
constexpr static const uint32_t restrictMask = mask;
public:
constexpr restrictedPtr_t() noexcept : ptr(nullptr) { }
constexpr restrictedPtr_t(nullptr_t) noexcept : ptr(nullptr) { }
operator ptr_t() const noexcept { return ptr & restrictMask; }
T &operator *() noexcept { return *ptr_t(*this); }
restrictedPtr_t(ptr_t p) noexcept : restrictedPtr_t()
{
*this = p;
}
void operator =(ptr_t p) volatile noexcept
{
long value = reinterpret_cast<long>(p) & restrictMask;
ptr = reinterpret_cast<ptr_t>(value);
}
bool operator ==(const restrictedPtr_t<T> &p) volatile noexcept
{
return ptr_t(p) == ptr_t(*this);
}
bool operator ==(ptr_t p) volatile noexcept
{
return ptr_t(*this) == p;
}
explicit operator bool() volatile noexcept { return ptr != nullptr; }
explicit operator uint32_t() volatile noexcept
{
static_assert(sizeof(uint32_t) == sizeof(long), "operator uint32_t() on restrictedPtr_t is invalid");
return uint32_t(reinterpret_cast<long>(ptr));
}
};
#endif /*__restrictedPtr_H__*/