lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
auto_restore.h
00001 /*****************************************************/
00002 /* lean Smart                   (c) Tobias Zirr 2011 */
00003 /*****************************************************/
00004 
00005 #ifndef LEAN_SMART_AUTO_RESTORE
00006 #define LEAN_SMART_AUTO_RESTORE
00007 
00008 #include "../lean.h"
00009 #include "../tags/noncopyable.h"
00010 
00011 namespace lean
00012 {
00013 namespace smart
00014 {
00015 
00017 template <class Value>
00018 class auto_restore : public noncopyable
00019 {
00020 private:
00021     Value &m_value;
00022     Value m_initialValue;
00023 
00024 public:
00026     typedef Value value_type;
00027 
00029     explicit auto_restore(value_type &value)
00030         : m_value(value),
00031         m_initialValue(value) { }
00033     auto_restore(value_type &value, const value_type &newValue)
00034         : m_value(value),
00035         m_initialValue(value)
00036     {
00037         m_value = newValue;
00038     }
00039 
00041     ~auto_restore()
00042     {
00043         m_value = m_initialValue;
00044     }
00045     
00047     LEAN_INLINE value_type& get(void) { return m_value; };
00049     LEAN_INLINE const value_type& get(void) const { return m_value; };
00050 
00052     LEAN_INLINE value_type& operator *() { return get(); };
00054     LEAN_INLINE const value_type& operator *() const { return get(); };
00056     LEAN_INLINE value_type* operator ->() { return &get(); };
00058     LEAN_INLINE const value_type* operator ->() const { return &get(); };
00059 
00061     LEAN_INLINE operator value_type&() { return get(); };
00063     LEAN_INLINE operator const value_type&() const { return get(); };
00064 };
00065 
00066 } // namespace
00067 
00068 using smart::auto_restore;
00069 
00070 } // namespace
00071 
00072 #endif