forked from klmr/cpp11-raw-ptr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptr.hpp
More file actions
177 lines (135 loc) · 4.36 KB
/
ptr.hpp
File metadata and controls
177 lines (135 loc) · 4.36 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef BASE_PTR_HPP
#define BASE_PTR_HPP
#include <memory>
namespace base {
template <typename T>
class ptr {
public:
using pointer = T*;
using reference = T&;
constexpr ptr() noexcept = default;
constexpr ptr(std::nullptr_t) noexcept : value() { }
template <typename U>
friend class ptr;
template <typename Other>
ptr(ptr<Other> const& other) noexcept : value(other.value) { }
pointer get() const noexcept { return value; }
reference operator *() const noexcept { return *get(); }
pointer operator ->() const noexcept { return get(); }
template <typename U>
friend ptr<U> raw_ptr(U*) noexcept;
private:
pointer value;
// We want to force users to use the builder function `raw_ptr`, rather than
// using a converting constructor to create a `ptr` instance from a raw T*.
// This makes it explicit that we are intentionally handling a raw pointer.
explicit ptr(pointer value) noexcept : value(value) { }
};
template <typename T>
inline ptr<T> raw_ptr(T* value) noexcept {
return static_cast<ptr<T>>(value);
}
template <typename T, typename U>
inline bool operator ==(ptr<T> const& lhs, ptr<U> const& rhs) noexcept {
return lhs.get() == rhs.get();
}
template <typename T>
inline bool operator ==(ptr<T> const& lhs, std::nullptr_t) noexcept {
return lhs.get() == nullptr;
}
template <typename T>
inline bool operator ==(std::nullptr_t, ptr<T> const& rhs) noexcept {
return rhs.get() == nullptr;
}
template <typename T, typename U>
inline bool operator !=(ptr<T> lhs, ptr<U> rhs) noexcept {
return not (lhs == rhs);
}
template <typename T>
inline bool operator !=(ptr<T> const& lhs, std::nullptr_t) noexcept {
return lhs.get() != nullptr;
}
template <typename T>
inline bool operator !=(std::nullptr_t, ptr<T> const& rhs) noexcept {
return rhs.get() != nullptr;
}
template <typename T, typename U>
inline ptr<T> static_pointer_cast(ptr<U> const& p) noexcept {
return raw_ptr(static_cast<T*>(p.get()));
}
template <typename T, typename U>
inline ptr<T> dynamic_pointer_cast(ptr<U> const& p) noexcept {
return raw_ptr(dynamic_cast<T*>(p.get()));
}
template <typename T, typename U>
inline ptr<T> const_pointer_cast(ptr<U> const& p) noexcept {
return raw_ptr(const_cast<T*>(p.get()));
}
template <typename T, typename U>
inline bool operator <(ptr<T> const& lhs, ptr<U> const& rhs) noexcept {
return lhs.get() < rhs.get();
}
template <typename T>
inline bool operator <(ptr<T> const&, std::nullptr_t) noexcept {
return false;
}
template <typename T>
inline bool operator <(std::nullptr_t, ptr<T> const& rhs) noexcept {
return rhs.get() != nullptr;
}
template <typename T, typename U>
inline bool operator <=(ptr<T> const& lhs, ptr<U> const& rhs) noexcept {
return lhs.get() <= rhs.get();
}
template <typename T>
inline bool operator <=(ptr<T> const& lhs, std::nullptr_t) noexcept {
return lhs.get() == nullptr;
}
template <typename T>
inline bool operator <=(std::nullptr_t, ptr<T> const&) noexcept {
return true;
}
template <typename T, typename U>
inline bool operator >(ptr<T> const& lhs, ptr<U> const& rhs) noexcept {
return lhs.get() > rhs.get();
}
template <typename T>
inline bool operator >(ptr<T> const& lhs, std::nullptr_t) noexcept {
return lhs != nullptr;
}
template <typename T>
inline bool operator >(std::nullptr_t, ptr<T> const&) noexcept {
return false;
}
template <typename T, typename U>
inline bool operator >=(ptr<T> const& lhs, ptr<U> const& rhs) noexcept {
return lhs.get() >= rhs.get();
}
template <typename T>
inline bool operator >=(ptr<T> const&, std::nullptr_t) noexcept {
return true;
}
template <typename T>
inline bool operator >=(std::nullptr_t, ptr<T> const& rhs) noexcept {
return nullptr == rhs.get();
}
} // namespace base
namespace std {
template <typename T>
struct pointer_traits<base::ptr<T>> {
using pointer = T*;
using element_type = T;
using difference_type = ptrdiff_t;
template <typename U>
using rebind = base::ptr<U>;
};
template <typename T>
struct hash<base::ptr<T>> {
using result_type = size_t;
using argument_type = base::ptr<T>;
result_type operator ()(argument_type const& p) const noexcept {
return std::hash<typename base::ptr<T>::pointer>()(p.get());
}
};
} // namespace std
#endif // ndef BASE_PTR_HPP