| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FML_MEMORY_WEAK_PTR_INTERNAL_H_ |
| 6 | #define FLUTTER_FML_MEMORY_WEAK_PTR_INTERNAL_H_ |
| 7 | |
| 8 | #include "flutter/fml/macros.h" |
| 9 | #include "flutter/fml/memory/ref_counted.h" |
| 10 | |
| 11 | namespace fml { |
| 12 | namespace internal { |
| 13 | |
| 14 | // |WeakPtr<T>|s have a reference to a |WeakPtrFlag| to determine whether they |
| 15 | // are valid (non-null) or not. We do not store a |T*| in this object since |
| 16 | // there may also be |WeakPtr<U>|s to the same object, where |U| is a superclass |
| 17 | // of |T|. |
| 18 | // |
| 19 | // This class in not thread-safe, though references may be released on any |
| 20 | // thread (allowing weak pointers to be destroyed/reset/reassigned on any |
| 21 | // thread). |
| 22 | class WeakPtrFlag : public fml::RefCountedThreadSafe<WeakPtrFlag> { |
| 23 | public: |
| 24 | WeakPtrFlag(); |
| 25 | |
| 26 | ~WeakPtrFlag(); |
| 27 | |
| 28 | bool is_valid() const { return is_valid_; } |
| 29 | |
| 30 | void Invalidate(); |
| 31 | |
| 32 | private: |
| 33 | bool is_valid_; |
| 34 | |
| 35 | FML_DISALLOW_COPY_AND_ASSIGN(WeakPtrFlag); |
| 36 | }; |
| 37 | |
| 38 | } // namespace internal |
| 39 | } // namespace fml |
| 40 | |
| 41 | #endif // FLUTTER_FML_MEMORY_WEAK_PTR_INTERNAL_H_ |
| 42 | |