xpcom/glue/AutoRestore.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/AutoRestore.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,46 @@
     1.4 +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* functions for restoring saved values at the end of a C++ scope */
    1.10 +
    1.11 +#ifndef mozilla_AutoRestore_h_
    1.12 +#define mozilla_AutoRestore_h_
    1.13 +
    1.14 +#include "mozilla/Attributes.h" // MOZ_STACK_CLASS
    1.15 +#include "mozilla/GuardObjects.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +
    1.19 +  /**
    1.20 +   * Save the current value of a variable and restore it when the object
    1.21 +   * goes out of scope.  For example:
    1.22 +   *   {
    1.23 +   *     AutoRestore<bool> savePainting(mIsPainting);
    1.24 +   *     mIsPainting = true;
    1.25 +   *     
    1.26 +   *     // ... your code here ...
    1.27 +   *
    1.28 +   *     // mIsPainting is reset to its old value at the end of this block
    1.29 +   *   }
    1.30 +   */
    1.31 +  template <class T>
    1.32 +  class MOZ_STACK_CLASS AutoRestore
    1.33 +  {
    1.34 +  private:
    1.35 +    T& mLocation;
    1.36 +    T mValue;
    1.37 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
    1.38 +  public:
    1.39 +    AutoRestore(T& aValue MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
    1.40 +      : mLocation(aValue), mValue(aValue)
    1.41 +    {
    1.42 +      MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    1.43 +    }
    1.44 +    ~AutoRestore() { mLocation = mValue; }
    1.45 +  };
    1.46 +
    1.47 +} // namespace mozilla
    1.48 +
    1.49 +#endif /* !defined(mozilla_AutoRestore_h_) */

mercurial