Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/destruction_tracker.h |
| diff --git a/mojo/public/cpp/bindings/lib/destruction_tracker.h b/mojo/public/cpp/bindings/lib/destruction_tracker.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7508487faf005dddfdb4e92d4eba2af58d9bb8ac |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/destruction_tracker.h |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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
|
| +#define MOJO_PUBLIC_CPP_BINDINGS_LIB_DELETION_TRACKER_H_ |
| + |
| +#include "base/macros.h" |
| + |
| +namespace mojo { |
| + |
| +// A helper which can be used to detect object deletion from within the object's |
| +// own methods. |
| +// |
| +// To use this, a class should declare a DestructionTracker as a data member |
| +// and guard any operation which might delete the owning object with a |
| +// stack-local instance of DestructionTracker::Flag, like so: |
| +// |
| +// DestructionTracker::Flag was_destroyed(&destruction_tracker_); |
| +// DoSomethingWhichMightDestroyThis(); |
| +// if (was_destroyed) |
| +// return; // Escape safely |
| +// // Continue doing stuff to |this|. |
| +// |
| +// This mechanism supports multiple DestructionTracker::Flag instances nested |
| +// within the call stack so that methods which use it can be reentrant. |
| +class DestructionTracker { |
| + public: |
| + class Flag { |
| + public: |
| + explicit Flag(DestructionTracker* tracker); |
| + ~Flag(); |
| + |
| + 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
|
| + |
| + private: |
| + friend class DestructionTracker; |
| + |
| + void NotifyDestroyed(); |
| + |
| + DestructionTracker* tracker_; |
| + Flag* const outer_flag_; |
| + bool was_destroyed_ = false; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Flag); |
| + }; |
| + |
| + DestructionTracker(); |
| + ~DestructionTracker(); |
| + |
| + private: |
| + void SetFlag(Flag* flag); |
| + |
| + Flag* flag_ = nullptr; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DestructionTracker); |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_DELETION_TRACKER_H_ |