summaryrefslogtreecommitdiffstats
path: root/xpcom/glue/nsTWeakRef.h
blob: 6c9a5e8ebffc0a4eeaacd377aecf65e099af2bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsTWeakRef_h__
#define nsTWeakRef_h__

#ifndef nsDebug_h___
#include "nsDebug.h"
#endif

/**
 * A weak reference class for use with generic C++ objects.  NOT THREADSAFE!
 *
 * Example usage:
 *
 *   class A {
 *   public:
 *     A() : mWeakSelf(this) {
 *     }
 *     ~A() {
 *       mWeakSelf.forget();
 *     }
 *     void Bar() { printf("Bar!\n"); }
 *     const nsTWeakRef<A> &AsWeakRef() const { return mWeakSelf; }
 *   private:
 *     nsTWeakRef<A> mWeakSelf;
 *   };
 *
 *   class B {
 *   public:
 *     void SetA(const nsTWeakRef<A> &a) {
 *       mA = a;
 *     }
 *     void Foo() {
 *       if (mA)
 *         mA->Bar();
 *     }
 *   private:
 *     nsTWeakRef<A> mA;
 *   };
 *
 *   void Test() {
 *     B b;
 *     {
 *       A a;
 *       b.SetA(a.AsWeakRef());
 *       b.Foo();  // prints "Bar!"
 *     }
 *     b.Foo();  // prints nothing because |a| has already been destroyed
 *   }
 *
 * One can imagine much more complex examples, especially when asynchronous
 * event processing is involved.
 *
 * Keep in mind that you should only ever need a class like this when you have
 * multiple instances of B, such that it is not possible for A and B to simply
 * have pointers to one another.
 */
template<class Type>
class nsTWeakRef
{
public:
  ~nsTWeakRef()
  {}

  /**
   * Construct from an object pointer (may be null).
   */
  explicit nsTWeakRef(Type* aObj = nullptr)
  {
    if (aObj) {
      mRef = new Inner(aObj);
    } else {
      mRef = nullptr;
    }
  }

  /**
   * Construct from another weak reference object.
   */
  explicit nsTWeakRef(const nsTWeakRef<Type>& aOther) : mRef(aOther.mRef)
  {}

  /**
   * Assign from an object pointer.
   */
  nsTWeakRef<Type>& operator=(Type* aObj)
  {
    if (aObj) {
      mRef = new Inner(aObj);
    } else {
      mRef = nullptr;
    }
    return *this;
  }

  /**
   * Assign from another weak reference object.
   */
  nsTWeakRef<Type>& operator=(const nsTWeakRef<Type>& aOther)
  {
    mRef = aOther.mRef;
    return *this;
  }

  /**
   * Get the referenced object.  This method may return null if the reference
   * has been cleared or if an out-of-memory error occurred at assignment.
   */
  Type* get() const { return mRef ? mRef->mObj : nullptr; }

  /**
   * Called to "null out" the weak reference.  Typically, the object referenced
   * by this weak reference calls this method when it is being destroyed.
   * @returns The former referenced object.
   */
  Type* forget()
  {
    Type* obj;
    if (mRef) {
      obj = mRef->mObj;
      mRef->mObj = nullptr;
      mRef = nullptr;
    } else {
      obj = nullptr;
    }
    return obj;
  }

  /**
   * Allow |*this| to be treated as a |Type*| for convenience.
   */
  operator Type*() const { return get(); }

  /**
   * Allow |*this| to be treated as a |Type*| for convenience.  Use with
   * caution since this method will crash if the referenced object is null.
   */
  Type* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
  {
    NS_ASSERTION(mRef && mRef->mObj,
                 "You can't dereference a null weak reference with operator->().");
    return get();
  }

private:

  struct Inner
  {
    int     mCnt;
    Type*   mObj;

    explicit Inner(Type* aObj)
      : mCnt(1)
      , mObj(aObj)
    {
    }
    void AddRef()
    {
      ++mCnt;
    }
    void Release()
    {
      if (--mCnt == 0) {
        delete this;
      }
    }
  };

  RefPtr<Inner> mRef;
};

#endif  // nsTWeakRef_h__