blob: e18212d33d4bb9cc0260f5caef0206b4ac67e093 [file] [log] [blame]
Primiano Tucci575af772017-11-08 18:14:17 +00001/*
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 Eftevaagdd727e42017-12-05 08:49:55 -080017#ifndef PERFETTO_PERFETTO_BASE_WEAK_PTR_H_
18#define PERFETTO_PERFETTO_BASE_WEAK_PTR_H_
Primiano Tucci575af772017-11-08 18:14:17 +000019
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080020#include "perfetto_base/thread_checker.h"
Primiano Tucci575af772017-11-08 18:14:17 +000021
22#include <memory>
23
24namespace perfetto {
25namespace 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
47template <typename T>
48class WeakPtrFactory; // Forward declaration, defined below.
49
50template <typename T>
51class WeakPtr {
52 public:
53 WeakPtr() {}
54 WeakPtr(const WeakPtr&) = default;
55 WeakPtr& operator=(const WeakPtr&) = default;
Primiano Tucci22c84172017-11-09 13:54:45 +000056 WeakPtr(WeakPtr&&) = default;
Primiano Tucci575af772017-11-08 18:14:17 +000057 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 Tucci49a22472017-11-08 23:43:52 +000073 PERFETTO_THREAD_CHECKER(thread_checker)
Primiano Tucci575af772017-11-08 18:14:17 +000074};
75
76template <typename T>
77class 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 Tucci49a22472017-11-08 23:43:52 +000097 PERFETTO_THREAD_CHECKER(thread_checker)
Primiano Tucci575af772017-11-08 18:14:17 +000098};
99
100} // namespace base
101} // namespace perfetto
102
Oystein Eftevaagdd727e42017-12-05 08:49:55 -0800103#endif // PERFETTO_PERFETTO_BASE_WEAK_PTR_H_