Remove unnecessary owned<T>::GetRawPointer().
[zcpointer.git] / zcpointer.h
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <limits>
16 #include <memory>
17 #include <forward_list>
18
19 namespace zc {
20
21 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
22
23 template <typename T> class ref;
24
25 namespace internal {
26
27 template <typename T>
28 class OwnedPtrDeleter {
29 public:
30 OwnedPtrDeleter() {}
31 ~OwnedPtrDeleter() {}
32
33 OwnedPtrDeleter(OwnedPtrDeleter&& other) : refs_(std::move(other.refs_)) {
34 }
35
36 void operator()(T* t) const {
37 for (auto& ref : refs_) {
38 ref->MarkDeleted();
39 }
40 delete t;
41 }
42
43 protected:
44 friend class ref<T>;
45
46 void AddRef(ref<T>* ref) {
47 refs_.push_front(ref);
48 }
49
50 void RemoveRef(ref<T>* ref) {
51 refs_.remove(ref);
52 }
53
54 private:
55 std::forward_list<ref<T>*> refs_;
56 };
57
58 void RaiseUseAfterFree(const char* error) __attribute__((noreturn));
59
60 } // namespace internal
61
62 template <typename T>
63 class owned : public std::unique_ptr<T, internal::OwnedPtrDeleter<T>> {
64 private:
65 using Deleter = internal::OwnedPtrDeleter<T>;
66
67 public:
68 using std::unique_ptr<T, Deleter>::unique_ptr;
69
70 ref<T> get() {
71 return ref<T>(*this);
72 }
73
74 private:
75 T* get() const {
76 return this->std::unique_ptr<T, Deleter>::get();
77 }
78 };
79
80 template <typename T>
81 class ref {
82 public:
83 ref() : ptr_(DeletedSentinel()) {}
84
85 explicit ref(owned<T>& o) : ptr_(&o) {
86 ptr_->get_deleter().AddRef(this);
87 }
88
89 ref(const ref<T>& o) {
90 *this = o;
91 }
92
93 ref<T>& operator=(const ref<T>& o) {
94 ptr_ = o.ptr_;
95 if (!IsDeleted()) {
96 ptr_->get_deleter().AddRef(this);
97 }
98 return *this;
99 }
100
101 ~ref() {
102 ptr_->get_deleter().RemoveRef(this);
103 MarkDeleted();
104 }
105
106 T* operator->() const {
107 CheckDeleted();
108 return ptr_->operator->();
109 }
110
111 protected:
112 friend class internal::OwnedPtrDeleter<T>;
113
114 void MarkDeleted() {
115 ptr_ = DeletedSentinel();
116 }
117
118 private:
119 void CheckDeleted() const {
120 if (IsDeleted()) {
121 internal::RaiseUseAfterFree("attempt to access deleted pointer");
122 }
123 }
124
125 bool IsDeleted() const {
126 return ptr_ == DeletedSentinel();
127 }
128
129 inline static owned<T>* DeletedSentinel() {
130 return reinterpret_cast<owned<T>*>(std::numeric_limits<uintptr_t>::max());
131 }
132
133 owned<T>* ptr_;
134 };
135
136 #else
137
138 template <typename T>
139 using owned = std::unique_ptr<T>;
140
141 template <typename T>
142 using ref = T*;
143
144 #endif
145
146 } // namespace zc