Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium 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 MOJO_PUBLIC_CPP_BINDINGS_LIB_DELETION_TRACKER_H_ | |
|
yzshen1
2017/03/03 00:03:50
Please make it consistent with the file name.
Ken Rockot(use gerrit already)
2017/03/03 00:37:05
N/A - delete this code and used WeakPtrs instead p
| |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_DELETION_TRACKER_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 | |
| 12 // A helper which can be used to detect object deletion from within the object's | |
| 13 // own methods. | |
| 14 // | |
| 15 // To use this, a class should declare a DestructionTracker as a data member | |
| 16 // and guard any operation which might delete the owning object with a | |
| 17 // stack-local instance of DestructionTracker::Flag, like so: | |
| 18 // | |
| 19 // DestructionTracker::Flag was_destroyed(&destruction_tracker_); | |
| 20 // DoSomethingWhichMightDestroyThis(); | |
| 21 // if (was_destroyed) | |
| 22 // return; // Escape safely | |
| 23 // // Continue doing stuff to |this|. | |
| 24 // | |
| 25 // This mechanism supports multiple DestructionTracker::Flag instances nested | |
| 26 // within the call stack so that methods which use it can be reentrant. | |
| 27 class DestructionTracker { | |
| 28 public: | |
| 29 class Flag { | |
| 30 public: | |
| 31 explicit Flag(DestructionTracker* tracker); | |
| 32 ~Flag(); | |
| 33 | |
| 34 explicit operator bool() const { return was_destroyed_; } | |
|
yzshen1
2017/03/03 00:03:50
can we use !tracker_ to indicate destructed?
Ken Rockot(use gerrit already)
2017/03/03 00:37:05
N/A
| |
| 35 | |
| 36 private: | |
| 37 friend class DestructionTracker; | |
| 38 | |
| 39 void NotifyDestroyed(); | |
| 40 | |
| 41 DestructionTracker* tracker_; | |
| 42 Flag* const outer_flag_; | |
| 43 bool was_destroyed_ = false; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(Flag); | |
| 46 }; | |
| 47 | |
| 48 DestructionTracker(); | |
| 49 ~DestructionTracker(); | |
| 50 | |
| 51 private: | |
| 52 void SetFlag(Flag* flag); | |
| 53 | |
| 54 Flag* flag_ = nullptr; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(DestructionTracker); | |
| 57 }; | |
| 58 | |
| 59 } // namespace mojo | |
| 60 | |
| 61 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_DELETION_TRACKER_H_ | |
| OLD | NEW |