1 // Copyright 2016 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef ZCPOINTER_ZCPOINTER_H_
16 #define ZCPOINTER_ZCPOINTER_H_
20 #include <forward_list>
26 class UseAfterFreeError
: public std
::logic_error
{
28 using std
::logic_error
::logic_error
;
31 #if defined(ZCPOINTER_TRACK_REFS) && ZCPOINTER_TRACK_REFS
33 template <typename T
> class ref
;
38 class OwnedPtrDeleter
{
43 OwnedPtrDeleter(OwnedPtrDeleter
&& other
) : refs_(std
::move(other
.refs_
)) {
46 void operator=(const OwnedPtrDeleter
& o
) {
50 void operator()(T
* t
) const {
51 for (auto& ref
: refs_
) {
60 void AddRef(ref
<T
>* ref
) {
61 refs_
.push_front(ref
);
64 void RemoveRef(ref
<T
>* ref
) {
69 std
::forward_list
<ref
<T
>*> refs_
;
72 void RaiseUseAfterFree(const char* error
) __attribute__((noreturn
));
74 } // namespace internal
77 class owned
: public std
::unique_ptr
<T
, internal
::OwnedPtrDeleter
<T
>> {
79 using Deleter
= internal
::OwnedPtrDeleter
<T
>;
82 using std
::unique_ptr
<T
, Deleter
>::unique_ptr
;
90 return this->std
::unique_ptr
<T
, Deleter
>::get();
97 ref() : ptr_(nullptr) {}
99 ref(std
::nullptr_t
) : ref() {}
101 explicit ref(owned
<T
>& o
) : ptr_(nullptr) {
104 ptr_
->get_deleter().AddRef(this);
108 ref(const ref
<T
>& r
) {
112 ref
<T
>& operator=(const ref
<T
>& o
) {
114 if (ptr_
!= nullptr && !IsDeleted()) {
115 ptr_
->get_deleter().AddRef(this);
121 if (ptr_
!= nullptr && !IsDeleted()) {
122 ptr_
->get_deleter().RemoveRef(this);
127 T
* operator->() const {
129 return ptr_
->operator->();
132 bool operator==(const ref
<T
>& r
) const {
133 if (ptr_
== nullptr) {
134 return r
.ptr_
== nullptr;
136 return ptr_
== r
.ptr_
&& *ptr_
== *r
.ptr_
;
140 bool operator==(std
::nullptr_t
) const {
141 return ptr_
== nullptr;
144 bool operator!=(const ref
<T
>& r
) const {
145 return !(*this == r
);
149 friend class internal
::OwnedPtrDeleter
<T
>;
152 ptr_
= DeletedSentinel();
156 void CheckDeleted() const {
158 internal
::RaiseUseAfterFree("attempt to access deleted pointer");
162 bool IsDeleted() const {
163 return ptr_
== DeletedSentinel();
166 inline static owned
<T
>* DeletedSentinel() {
167 return reinterpret_cast<owned
<T
>*>(std
::numeric_limits
<uintptr_t>::max());
173 template <typename T
>
176 template <typename
... Args
>
177 explicit member(Args
&&... args
)
178 : t_(new T(std
::forward
<Args
>(args
)...)) {
186 return t_
.operator->();
189 const T
* operator->() const {
190 return t_
.operator->();
199 template <typename T
>
200 using owned
= std
::unique_ptr
<T
>;
202 template <typename T
>
205 template <typename T
>
206 class member
: public T
{
214 const T
* operator->() const {
223 #endif // ZCPOINTER_ZCPOINTER_H_