• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FXCRT_AUTORESTORER_H_
6 #define CORE_FXCRT_AUTORESTORER_H_
7 
8 #include "core/fxcrt/fx_memory.h"
9 
10 namespace fxcrt {
11 
12 template <typename T>
13 class AutoRestorer {
14  public:
15   FX_STACK_ALLOCATED();
16 
AutoRestorer(T * location)17   explicit AutoRestorer(T* location)
18       : m_Location(location), m_OldValue(*location) {}
~AutoRestorer()19   ~AutoRestorer() {
20     if (m_Location)
21       *m_Location = m_OldValue;
22   }
AbandonRestoration()23   void AbandonRestoration() { m_Location = nullptr; }
24 
25  private:
26   T* m_Location;
27   const T m_OldValue;
28 };
29 
30 }  // namespace fxcrt
31 
32 using fxcrt::AutoRestorer;
33 
34 #endif  // CORE_FXCRT_AUTORESTORER_H_
35