00001 #ifndef _SCOPEDSETTER_H_142167890764_ 00002 #define _SCOPEDSETTER_H_142167890764_ 00003 00004 /*! 00005 * ScopedSetter sets value to given variable and when goes 00006 * out of scope, reset its original state. 00007 * 00008 * \b Usage: 00009 * \code 00010 * // assume bool member variable m_isSet == true 00011 * ... 00012 * std::cout << (m_isSet ? "true" : "false") << std::endl; // print 'true' 00013 * { 00014 * SCOPED_SETTER(bool, m_isSet, false); // false is required state 00015 * std::cout << (m_isSet ? "true" : "false") << std::endl; // print 'false' 00016 * } // end of scope - return orig state 00017 * 00018 * std::cout << (m_isSet ? "true" : "false") << std::endl; // print 'true' 00019 * \endcode 00020 * 00021 * \author Milan Kriz (krizm7@fel.cvut.cz, milan.kriz@centrum.cz) 00022 */ 00023 template <typename T> 00024 class ScopedSetter 00025 { 00026 public: 00027 ScopedSetter(T &variable, const T &value) 00028 : m_variable(variable), 00029 m_origValue(variable) 00030 { 00031 m_variable = value; 00032 } 00033 00034 ~ScopedSetter() 00035 { 00036 m_variable = m_origValue; 00037 } 00038 00039 private: 00040 T &m_variable; 00041 T m_origValue; 00042 }; 00043 00044 #endif //_SCOPEDSETTER_H_142167890764_