Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgRestorer.h

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


Sun Mar 13 2011 Doxygen 1.7.1