15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Weak pointers are pointers to an object that do not affect its lifetime,
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// and which may be invalidated (i.e. reset to NULL) by the object, or its
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// owner, at any time, most commonly when the object is about to be deleted.
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Weak pointers are useful when an object needs to be accessed safely by one
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// or more objects other than its owner, and those callers can cope with the
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// object vanishing and e.g. tasks posted to it being silently dropped.
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Reference-counting such an object would complicate the ownership graph and
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// make it harder to reason about the object's lifetime.
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// EXAMPLE:
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//  class Controller {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   public:
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//    void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    void WorkComplete(const Result& result) { ... }
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//   private:
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//    // Member variables should appear before the WeakPtrFactory, to ensure
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//    // that any WeakPtrs to Controller are invalidated before its members
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//    // variable's destructors are executed, rendering them invalid.
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//    WeakPtrFactory<Controller> weak_factory_;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  };
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  class Worker {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   public:
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    static void StartNew(const WeakPtr<Controller>& controller) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      Worker* worker = new Worker(controller);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      // Kick off asynchronous processing...
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    }
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   private:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    Worker(const WeakPtr<Controller>& controller)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//        : controller_(controller) {}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    void DidCompleteAsynchronousProcessing(const Result& result) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      if (controller_)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//        controller_->WorkComplete(result);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    WeakPtr<Controller> controller_;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  };
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// With this implementation a caller may use SpawnWorker() to dispatch multiple
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Workers and subsequently delete the Controller, without waiting for all
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Workers to have completed.
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// ------------------------- IMPORTANT: Thread-safety -------------------------
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Weak pointers may be passed safely between threads, but must always be
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// dereferenced and invalidated on the same thread otherwise checking the
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// pointer would be racey.
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is dereferenced, the factory and its WeakPtrs become bound to the calling
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// thread, and cannot be dereferenced or invalidated on any other thread. Bound
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// WeakPtrs can still be handed off to other threads, e.g. to use to post tasks
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// back to object on the bound thread.
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Invalidating the factory's WeakPtrs un-binds it from the thread, allowing it
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// to be passed for a different thread to use or delete it.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_MEMORY_WEAK_PTR_H_
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_MEMORY_WEAK_PTR_H_
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
70fb250657ef40d7500f20882d5c9909c1013367d3Ben Murdoch#include "base/sequence_checker.h"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/template_util.h"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T> class SupportsWeakPtr;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T> class WeakPtr;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These classes are part of the WeakPtr implementation.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT WeakReference {
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Although Flag is bound to a specific thread, it may be deleted from another
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // via base::WeakPtr::~WeakPtr().
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Flag();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Invalidate();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool IsValid() const;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    friend class base::RefCountedThreadSafe<Flag>;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~Flag();
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98fb250657ef40d7500f20882d5c9909c1013367d3Ben Murdoch    SequenceChecker sequence_checker_;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_valid_;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakReference();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit WeakReference(const Flag* flag);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~WeakReference();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_valid() const;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<const Flag> flag_;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT WeakReferenceOwner {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakReferenceOwner();
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~WeakReferenceOwner();
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakReference GetRef() const;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasRefs() const {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return flag_.get() && !flag_->HasOneRef();
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Invalidate();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  mutable scoped_refptr<WeakReference::Flag> flag_;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class simplifies the implementation of WeakPtr's type conversion
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// constructor by avoiding the need for a public accessor for ref_.  A
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// base class gives us a way to access ref_ in a protected fashion.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT WeakPtrBase {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakPtrBase();
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~WeakPtrBase();
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit WeakPtrBase(const WeakReference& ref);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakReference ref_;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class provides a common implementation of common functions that would
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// otherwise get instantiated separately for each distinct instantiation of
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SupportsWeakPtr<>.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SupportsWeakPtrBase {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // conversion will only compile if there is exists a Base which inherits
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function that makes calling this easier.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template<typename Derived>
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    typedef
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        is_convertible<Derived, internal::SupportsWeakPtrBase&> convertible;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    COMPILE_ASSERT(convertible::value,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   AsWeakPtr_argument_inherits_from_SupportsWeakPtr);
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return AsWeakPtrImpl<Derived>(t, *t);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This template function uses type inference to find a Base of Derived
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which is an instance of SupportsWeakPtr<Base>. We can then safely
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // static_cast the Base* to a Derived*.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template <typename Derived, typename Base>
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static WeakPtr<Derived> AsWeakPtrImpl(
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Derived* t, const SupportsWeakPtr<Base>&) {
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WeakPtr<Base> ptr = t->Base::AsWeakPtr();
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return WeakPtr<Derived>(ptr.ref_, static_cast<Derived*>(ptr.ptr_));
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace internal
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T> class WeakPtrFactory;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The WeakPtr class holds a weak reference to |T*|.
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is designed to be used like a normal pointer.  You should always
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// null-test an object of this class before using it or invoking a method that
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may result in the underlying object being destroyed.
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// EXAMPLE:
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   class Foo { ... };
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   WeakPtr<Foo> foo;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   if (foo)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     foo->method();
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T>
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WeakPtr : public internal::WeakPtrBase {
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakPtr() : ptr_(NULL) {
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Allow conversion from U to T provided U "is a" T. Note that this
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is separate from the (implicit) copy constructor.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template <typename U>
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.ptr_) {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T& operator*() const {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(get() != NULL);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *get();
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* operator->() const {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(get() != NULL);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return get();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Allow WeakPtr<element_type> to be used in boolean expressions, but not
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // implicitly convertible to a real bool (which is dangerous).
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Note that this trick is only safe when the == and != operators
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // are declared explicitly, as otherwise "weak_ptr1 == weak_ptr2"
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // will compile but do the wrong thing (i.e., convert to Testable
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // and then do the comparison).
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef T* WeakPtr::*Testable;
223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  operator Testable() const { return get() ? &WeakPtr::ptr_ : NULL; }
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void reset() {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ref_ = internal::WeakReference();
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ptr_ = NULL;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Explicitly declare comparison operators as required by the bool
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // trick, but keep them private.
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  template <class U> bool operator==(WeakPtr<U> const&) const;
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  template <class U> bool operator!=(WeakPtr<U> const&) const;
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class internal::SupportsWeakPtrBase;
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  template <typename U> friend class WeakPtr;
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class SupportsWeakPtr<T>;
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class WeakPtrFactory<T>;
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakPtr(const internal::WeakReference& ref, T* ptr)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : WeakPtrBase(ref),
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ptr_(ptr) {
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This pointer is only valid when ref_.is_valid() is true.  Otherwise, its
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value is undefined (as opposed to NULL).
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  T* ptr_;
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// A class may be composed of a WeakPtrFactory and thereby
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// control how it exposes weak pointers to itself.  This is helpful if you only
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// need weak pointers within the implementation of a class.  This class is also
256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// useful when working with primitive types.  For example, you could have a
257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// WeakPtrFactory<bool> that is used to pass around a weak reference to a bool.
258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)template <class T>
259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class WeakPtrFactory {
260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ~WeakPtrFactory() {
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ptr_ = NULL;
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  WeakPtr<T> GetWeakPtr() {
269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(ptr_);
270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_);
271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Call this method to invalidate all existing weak pointers.
274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void InvalidateWeakPtrs() {
275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(ptr_);
276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    weak_reference_owner_.Invalidate();
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
278c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
279c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Call this method to determine if any weak pointers exist.
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool HasWeakPtrs() const {
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(ptr_);
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return weak_reference_owner_.HasRefs();
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  internal::WeakReferenceOwner weak_reference_owner_;
287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  T* ptr_;
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// A class may extend from SupportsWeakPtr to let others take weak pointers to
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// it. This avoids the class itself implementing boilerplate to dispense weak
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// pointers.  However, since SupportsWeakPtr's destructor won't invalidate
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// weak pointers to the class until after the derived class' members have been
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// destroyed, its use can lead to subtle use-after-destroy issues.
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <class T>
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SupportsWeakPtr : public internal::SupportsWeakPtrBase {
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SupportsWeakPtr() {}
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WeakPtr<T> AsWeakPtr() {
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~SupportsWeakPtr() {}
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  internal::WeakReferenceOwner weak_reference_owner_;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper function that uses type deduction to safely return a WeakPtr<Derived>
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// extends a Base that extends SupportsWeakPtr<Base>.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// EXAMPLE:
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   class Base : public base::SupportsWeakPtr<Producer> {};
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   class Derived : public Base {};
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   Derived derived;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   base::WeakPtr<Derived> ptr = base::AsWeakPtr(&derived);
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that the following doesn't work (invalid type conversion) since
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Derived::AsWeakPtr() is WeakPtr<Base> SupportsWeakPtr<Base>::AsWeakPtr(),
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and there's no way to safely cast WeakPtr<Base> to WeakPtr<Derived> at
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the caller.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   base::WeakPtr<Derived> ptr = derived.AsWeakPtr();  // Fails.
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename Derived>
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WeakPtr<Derived> AsWeakPtr(Derived* t) {
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_MEMORY_WEAK_PTR_H_
339