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 #include "core/fxcrt/unowned_ptr.h" 10 11 namespace fxcrt { 12 13 template <typename T> 14 class AutoRestorer { 15 public: 16 FX_STACK_ALLOCATED(); 17 AutoRestorer(T * location)18 explicit AutoRestorer(T* location) 19 : m_Location(location), m_OldValue(*location) {} ~AutoRestorer()20 ~AutoRestorer() { 21 if (m_Location) 22 *m_Location = m_OldValue; 23 } AbandonRestoration()24 void AbandonRestoration() { m_Location = nullptr; } 25 26 private: 27 UnownedPtr<T> m_Location; 28 const T m_OldValue; 29 }; 30 31 } // namespace fxcrt 32 33 using fxcrt::AutoRestorer; 34 35 #endif // CORE_FXCRT_AUTORESTORER_H_ 36