00001 //---------------------------------------------------------------------------- 00002 /** @file SgRestorer.h 00003 Change and restore stack-based variables. 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #ifndef SG_RESTORER_H 00008 #define SG_RESTORER_H 00009 00010 //---------------------------------------------------------------------------- 00011 00012 /** A SgRestorer<T> variable saves the current state of a variable of type 00013 T and restores it to that saved value upon leaving the scope in an 00014 exception-safe way. 00015 */ 00016 template <class T> 00017 class SgRestorer 00018 { 00019 public: 00020 explicit SgRestorer(T* oldState) 00021 : m_variable(oldState), 00022 m_oldState(*oldState) 00023 { } 00024 00025 ~SgRestorer() 00026 { 00027 *m_variable = m_oldState; 00028 } 00029 00030 private: 00031 T* m_variable; 00032 00033 T m_oldState; 00034 00035 /** Not implemented */ 00036 SgRestorer(const SgRestorer&); 00037 00038 /** Not implemented */ 00039 SgRestorer& operator=(const SgRestorer&); 00040 }; 00041 00042 //---------------------------------------------------------------------------- 00043 00044 /** Saves the current state of a variable of type T and asserts that the 00045 saved value equals the value upon leaving the scope. 00046 */ 00047 template <class T> 00048 class SgAssertRestored 00049 { 00050 public: 00051 explicit SgAssertRestored(T* oldState) 00052 : m_variable(oldState), 00053 m_oldState(*oldState) 00054 { } 00055 00056 ~SgAssertRestored() 00057 { 00058 SG_ASSERT(*m_variable == m_oldState); 00059 } 00060 00061 private: 00062 T* m_variable; 00063 00064 T m_oldState; 00065 00066 /** Not implemented. */ 00067 SgAssertRestored(const SgAssertRestored&); 00068 00069 /** Not implemented. */ 00070 SgAssertRestored& operator=(const SgAssertRestored&); 00071 }; 00072 00073 //---------------------------------------------------------------------------- 00074 00075 #endif // SG_RESTORER_H