// Copyright 2005 Google Inc. // All Rights Reserved // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the // distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. /** * @fileoverview Implements the disposable interface. The dispose method is used * to clean up references and resources. */ goog.provide('goog.Disposable'); goog.provide('goog.dispose'); /** * Class that provides the basic implementation for disposable objects. If your * class holds one or more references to COM objects, DOM nodes, or other * disposable objects, it should extend this class or implement the disposable * interface. * @constructor */ goog.Disposable = function() {}; /** * Whether the object has been disposed of. * @type {boolean} * @private */ goog.Disposable.prototype.disposed_ = false; /** * @return {boolean} Whether the object has been disposed of. */ goog.Disposable.prototype.isDisposed = function() { return this.disposed_; }; /** * @return {boolean} Whether the object has been disposed of. * @deprecated Use {@link #isDisposed} instead. */ goog.Disposable.prototype.getDisposed = goog.Disposable.prototype.isDisposed; /** * Disposes of the object. If the object hasn't already been disposed of, calls * {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should * override {@link #disposeInternal} in order to delete references to COM * objects, DOM nodes, and other disposable objects. */ goog.Disposable.prototype.dispose = function() { if (!this.disposed_) { // Set disposed_ to true first, in case during the chain of disposal this // gets disposed recursively. this.disposed_ = true; this.disposeInternal(); } }; /** * Deletes or nulls out any references to COM objects, DOM nodes, or other * disposable objects. Classes that extend {@code goog.Disposable} should * override this method. For example: *
 *   mypackage.MyClass = function() {
 *     goog.Disposable.call(this);
 *     // Constructor logic specific to MyClass.
 *     ...
 *   };
 *   goog.inherits(mypackage.MyClass, goog.Disposable);
 *
 *   mypackage.MyClass.prototype.disposeInternal = function() {
 *     mypackage.MyClass.superClass_.disposeInternal.call(this);
 *     // Dispose logic specific to MyClass.
 *     ...
 *   };
 * 
* @protected */ goog.Disposable.prototype.disposeInternal = function() { // No-op in the base class. }; /** * Calls {@code dispose} on the argument if it supports it. * @param {Object} obj The object to dispose of. */ goog.dispose = function(obj) { if (typeof obj.dispose == 'function') { obj.dispose(); } };