DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5216dd412535)

VCS Links

Line Code
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is nsAutoRestore.h.
 *
 * The Initial Developer of the Original Code is the Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   L. David Baron <dbaron@dbaron.org>, Mozilla Corporation (original author)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/* functions for restoring saved values at the end of a C++ scope */

#ifndef mozilla_AutoRestore_h_
#define mozilla_AutoRestore_h_

#include "prtypes.h"
#include "nsDebug.h"

namespace mozilla {

  /**
   * The following classes are designed to cause assertions to detect
   * inadvertent use of guard objects as temporaries.  In other words,
   * when we have a guard object whose only purpose is its constructor and
   * destructor (and is never otherwise referenced), the intended use
   * might be:
   *     AutoRestore savePainting(mIsPainting);
   * but is is easy to accidentally write:
   *     AutoRestore(mIsPainting);
   * which compiles just fine, but runs the destructor well before the
   * intended time.
   *
   * They work by adding (#ifdef DEBUG) an additional parameter to the
   * guard object's constructor, with a default value, so that users of
   * the guard object's API do not need to do anything.  The default value
   * of this parameter is a temporary object.  C++ (ISO/IEC 14882:1998),
   * section 12.2 [class.temporary], clauses 4 and 5 seem to assume a
   * guarantee that temporaries are destroyed in the reverse of their
   * construction order, but I actually can't find a statement that that
   * is true in the general case (beyond the two specific cases mentioned
   * there).  However, it seems to be true.
   *
   * These classes are intended to be used only via the macros immediately
   * below them:
   *   MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER declares (ifdef DEBUG) a member
   *     variable, and should be put where a declaration of a private
   *     member variable would be placed.
   *   MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM should be placed at the end of the
   *     parameters to each constructor of the guard object; it declares
   *     (ifdef DEBUG) an additional parameter.  (But use the *_ONLY_PARAM
   *     variant for constructors that take no other parameters.)
   *   MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL should likewise be used in
   *     the implementation of such constructors when they are not inline.
   *   MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT should be used in
   *     the implementation of such constructors to pass the parameter to
   *     a base class that also uses these macros
   *   MOZILLA_GUARD_OBJECT_NOTIFIER_INIT is a statement that belongs in each
   *     constructor.  It uses the parameter declared by
   *     MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM.
   *
   * For more details, and examples of using these macros, see
   * https://developer.mozilla.org/en/Using_RAII_classes_in_Mozilla
   */
#ifdef DEBUG
  class GuardObjectNotifier
  {
  private:
    bool* mStatementDone;
  public:
    GuardObjectNotifier() : mStatementDone(NULL) {}

    ~GuardObjectNotifier() {
      *mStatementDone = true;
    }

    void SetStatementDone(bool *aStatementDone) {
      mStatementDone = aStatementDone;
    }
  };

  class GuardObjectNotificationReceiver
  {
  private:
    bool mStatementDone;
  public:
    GuardObjectNotificationReceiver() : mStatementDone(false) {}

    ~GuardObjectNotificationReceiver() {
      /*
       * Assert that the guard object was not used as a temporary.
       * (Note that this assert might also fire if Init is not called
       * because the guard object's implementation is not using the
       * above macros correctly.)
       */
      NS_ABORT_IF_FALSE(mStatementDone,
                        "guard object used as temporary");
    }

    void Init(const GuardObjectNotifier &aNotifier) {
      /*
       * aNotifier is passed as a const reference so that we can pass a
       * temporary, but we really intend it as non-const
       */
      const_cast<GuardObjectNotifier&>(aNotifier).
          SetStatementDone(&mStatementDone);
    }
  };

  #define MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER \
      mozilla::GuardObjectNotificationReceiver _mCheckNotUsedAsTemporary;
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM \
      , const mozilla::GuardObjectNotifier& _notifier = \
                mozilla::GuardObjectNotifier()
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_ONLY_PARAM \
      const mozilla::GuardObjectNotifier& _notifier = \
              mozilla::GuardObjectNotifier()
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL \
      , const mozilla::GuardObjectNotifier& _notifier
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT \
      , _notifier
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_INIT \
      PR_BEGIN_MACRO _mCheckNotUsedAsTemporary.Init(_notifier); PR_END_MACRO

#else /* defined(DEBUG) */

  #define MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_ONLY_PARAM
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT
  #define MOZILLA_GUARD_OBJECT_NOTIFIER_INIT PR_BEGIN_MACRO PR_END_MACRO

#endif /* !defined(DEBUG) */


  /**
   * Save the current value of a variable and restore it when the object
   * goes out of scope.  For example:
   *   {
   *     AutoRestore<PRBool> savePainting(mIsPainting);
   *     mIsPainting = PR_TRUE;
   *     
   *     // ... your code here ...
   *
   *     // mIsPainting is reset to its old value at the end of this block
   *   }
   */
  template <class T>
  class NS_STACK_CLASS AutoRestore
  {
  private:
    T& mLocation;
    T mValue;
    MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
  public:
    AutoRestore(T& aValue MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM)
      : mLocation(aValue), mValue(aValue)
    {
      MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
    }
    ~AutoRestore() { mLocation = mValue; }
  };

}

#endif /* !defined(mozilla_AutoRestore_h_) */