PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/gecko_api/include/nsTWeakRef.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 206 lines | 77 code | 16 blank | 113 comment | 14 complexity | baa385aa4895827c08f6e84104ac394f MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2/* vim:set ts=2 sw=2 sts=2 et cindent: */
  3/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is Mozilla code.
 17 *
 18 * The Initial Developer of the Original Code is Google Inc.
 19 * Portions created by the Initial Developer are Copyright (C) 2006
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Darin Fisher <darin@meer.net>
 24 *
 25 * Alternatively, the contents of this file may be used under the terms of
 26 * either the GNU General Public License Version 2 or later (the "GPL"), or
 27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 28 * in which case the provisions of the GPL or the LGPL are applicable instead
 29 * of those above. If you wish to allow use of your version of this file only
 30 * under the terms of either the GPL or the LGPL, and not to allow others to
 31 * use your version of this file under the terms of the MPL, indicate your
 32 * decision by deleting the provisions above and replace them with the notice
 33 * and other provisions required by the GPL or the LGPL. If you do not delete
 34 * the provisions above, a recipient may use your version of this file under
 35 * the terms of any one of the MPL, the GPL or the LGPL.
 36 *
 37 * ***** END LICENSE BLOCK ***** */
 38
 39#ifndef nsTWeakRef_h__
 40#define nsTWeakRef_h__
 41
 42#ifndef nsDebug_h___
 43#include "nsDebug.h"
 44#endif
 45
 46/**
 47 * A weak reference class for use with generic C++ objects.  NOT THREADSAFE!
 48 *
 49 * Example usage:
 50 *
 51 *   class A {
 52 *   public:
 53 *     A() : mWeakSelf(this) {
 54 *     }
 55 *     ~A() {
 56 *       mWeakSelf.forget();
 57 *     }
 58 *     void Bar() { printf("Bar!\n"); }
 59 *     const nsTWeakRef<A> &AsWeakRef() const { return mWeakSelf; }
 60 *   private:
 61 *     nsTWeakRef<A> mWeakSelf;
 62 *   };
 63 *
 64 *   class B {
 65 *   public:
 66 *     void SetA(const nsTWeakRef<A> &a) {
 67 *       mA = a;
 68 *     }
 69 *     void Foo() {
 70 *       if (mA)
 71 *         mA->Bar();
 72 *     }
 73 *   private:
 74 *     nsTWeakRef<A> mA;
 75 *   };
 76 *
 77 *   void Test() {
 78 *     B b;
 79 *     {
 80 *       A a;
 81 *       b.SetA(a.AsWeakRef());
 82 *       b.Foo();  // prints "Bar!"
 83 *     }
 84 *     b.Foo();  // prints nothing because |a| has already been destroyed
 85 *   }
 86 *
 87 * One can imagine much more complex examples, especially when asynchronous
 88 * event processing is involved.
 89 *
 90 * Keep in mind that you should only ever need a class like this when you have
 91 * multiple instances of B, such that it is not possible for A and B to simply
 92 * have pointers to one another.
 93 */
 94template <class Type>
 95class nsTWeakRef {
 96public:
 97  ~nsTWeakRef() {
 98    if (mRef)
 99      mRef->Release();
100  }
101
102  /**
103   * Construct from an object pointer (may be null).
104   */
105  explicit
106  nsTWeakRef(Type *obj = nsnull) {
107    if (obj) {
108      mRef = new Inner(obj);
109    } else {
110      mRef = nsnull;
111    }
112  }
113
114  /**
115   * Construct from another weak reference object.
116   */
117  explicit
118  nsTWeakRef(const nsTWeakRef<Type> &other) : mRef(other.mRef) {
119    if (mRef)
120      mRef->AddRef();
121  }
122
123  /**
124   * Assign from an object pointer.
125   */
126  nsTWeakRef<Type> &operator=(Type *obj) {
127    if (mRef)  
128      mRef->Release();
129    if (obj) {
130      mRef = new Inner(obj);
131    } else {
132      mRef = nsnull;
133    }
134    return *this;
135  }
136
137  /**
138   * Assign from another weak reference object.
139   */ 
140  nsTWeakRef<Type> &operator=(const nsTWeakRef<Type> &other) {
141    if (mRef)  
142      mRef->Release();
143    mRef = other.mRef;
144    if (mRef)
145      mRef->AddRef();
146    return *this;
147  }
148
149  /**
150   * Get the referenced object.  This method may return null if the reference
151   * has been cleared or if an out-of-memory error occured at assignment.
152   */
153  Type *get() const {
154    return mRef ? mRef->mObj : nsnull;
155  }
156
157  /**
158   * Called to "null out" the weak reference.  Typically, the object referenced
159   * by this weak reference calls this method when it is being destroyed.
160   * @returns The former referenced object.
161   */
162  Type *forget() {
163    Type *obj;
164    if (mRef) {
165      obj = mRef->mObj;
166      mRef->mObj = nsnull;
167      mRef->Release();
168      mRef = nsnull;
169    } else {
170      obj = nsnull;
171    }
172    return obj;
173  }
174
175  /**
176   * Allow |*this| to be treated as a |Type*| for convenience.
177   */
178  operator Type *() const {
179    return get();
180  }
181
182  /**
183   * Allow |*this| to be treated as a |Type*| for convenience.  Use with
184   * caution since this method will crash if the referenced object is null.
185   */
186  Type *operator->() const {
187    NS_ASSERTION(mRef && mRef->mObj,
188        "You can't dereference a null weak reference with operator->().");
189    return get();
190  }
191
192private:
193
194  struct Inner {
195    int     mCnt;
196    Type   *mObj;
197
198    Inner(Type *obj) : mCnt(1), mObj(obj) {}
199    void AddRef() { ++mCnt; }
200    void Release() { if (--mCnt == 0) delete this; }
201  };
202
203  Inner *mRef;
204};
205
206#endif  // nsTWeakRef_h__