Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 17 | #ifndef PERFETTO_PERFETTO_BASE_WEAK_PTR_H_ |
| 18 | #define PERFETTO_PERFETTO_BASE_WEAK_PTR_H_ |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 19 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 20 | #include "perfetto_base/thread_checker.h" |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | namespace perfetto { |
| 25 | namespace base { |
| 26 | |
| 27 | // A simple WeakPtr for single-threaded cases. |
| 28 | // Usage: |
| 29 | // class MyClass { |
| 30 | // MyClass() : weak_factory_(this) {} |
| 31 | // WeakPtr<MyClass> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 32 | // |
| 33 | // private: |
| 34 | // WeakPtrFactory<MyClass> weak_factory_; |
| 35 | // } |
| 36 | // |
| 37 | // int main() { |
| 38 | // std::unique_ptr<MyClass> foo(new MyClass); |
| 39 | // auto wptr = foo.GetWeakPtr(); |
| 40 | // ASSERT_TRUE(wptr); |
| 41 | // ASSERT_EQ(foo.get(), wptr->get()); |
| 42 | // foo.reset(); |
| 43 | // ASSERT_FALSE(wptr); |
| 44 | // ASSERT_EQ(nullptr, wptr->get()); |
| 45 | // } |
| 46 | |
| 47 | template <typename T> |
| 48 | class WeakPtrFactory; // Forward declaration, defined below. |
| 49 | |
| 50 | template <typename T> |
| 51 | class WeakPtr { |
| 52 | public: |
| 53 | WeakPtr() {} |
| 54 | WeakPtr(const WeakPtr&) = default; |
| 55 | WeakPtr& operator=(const WeakPtr&) = default; |
Primiano Tucci | 22c8417 | 2017-11-09 13:54:45 +0000 | [diff] [blame] | 56 | WeakPtr(WeakPtr&&) = default; |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 57 | WeakPtr& operator=(WeakPtr&&) = default; |
| 58 | |
| 59 | T* get() const { |
| 60 | PERFETTO_DCHECK_THREAD(thread_checker); |
| 61 | return handle_ ? *handle_.get() : nullptr; |
| 62 | } |
| 63 | T* operator->() const { return get(); } |
| 64 | T& operator*() const { return *get(); } |
| 65 | |
| 66 | explicit operator bool() const { return !!get(); } |
| 67 | |
| 68 | private: |
| 69 | friend class WeakPtrFactory<T>; |
| 70 | explicit WeakPtr(const std::shared_ptr<T*>& handle) : handle_(handle) {} |
| 71 | |
| 72 | std::shared_ptr<T*> handle_; |
Primiano Tucci | 49a2247 | 2017-11-08 23:43:52 +0000 | [diff] [blame] | 73 | PERFETTO_THREAD_CHECKER(thread_checker) |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | template <typename T> |
| 77 | class WeakPtrFactory { |
| 78 | public: |
| 79 | explicit WeakPtrFactory(T* owner) : handle_(new T* {owner}) { |
| 80 | PERFETTO_DCHECK_THREAD(thread_checker); |
| 81 | } |
| 82 | ~WeakPtrFactory() { |
| 83 | PERFETTO_DCHECK_THREAD(thread_checker); |
| 84 | *(handle_.get()) = nullptr; |
| 85 | } |
| 86 | |
| 87 | WeakPtr<T> GetWeakPtr() const { |
| 88 | PERFETTO_DCHECK_THREAD(thread_checker); |
| 89 | return WeakPtr<T>(handle_); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | WeakPtrFactory(const WeakPtrFactory&) = delete; |
| 94 | WeakPtrFactory& operator=(const WeakPtrFactory&) = delete; |
| 95 | |
| 96 | std::shared_ptr<T*> handle_; |
Primiano Tucci | 49a2247 | 2017-11-08 23:43:52 +0000 | [diff] [blame] | 97 | PERFETTO_THREAD_CHECKER(thread_checker) |
Primiano Tucci | 575af77 | 2017-11-08 18:14:17 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace base |
| 101 | } // namespace perfetto |
| 102 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 103 | #endif // PERFETTO_PERFETTO_BASE_WEAK_PTR_H_ |