File indexing completed on 2024-04-28 03:40:46

0001 /*
0002    Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #ifndef PUSHVALUE_H
0020 #define PUSHVALUE_H
0021 
0022 /**
0023  * A simple helper-class that does the following:
0024  * Backup the given reference-value given through @param ptr,
0025  * replace it with the value given through @param push,
0026  * restore the backed up value back on destruction.
0027  * */
0028 template<class Value>
0029 class PushValue {
0030   public:
0031     PushValue( Value& ptr, const Value& push = Value()  ) : m_ptr(ptr)  {
0032       m_oldPtr = m_ptr;
0033       m_ptr = push;
0034     }
0035     ~PushValue() {
0036       m_ptr = m_oldPtr;
0037     }
0038   private:
0039     Value& m_ptr;
0040     Value m_oldPtr;
0041 };
0042 
0043 ///Only difference to PushValue: The value is only replaced if the new value is positive
0044 template<class Value>
0045 class PushPositiveValue {
0046   public:
0047     PushPositiveValue( Value& ptr, const Value& push = Value()  ) : m_ptr(ptr)  {
0048       m_oldPtr = m_ptr;
0049       if( push ) {
0050         m_ptr = push;
0051       }
0052     }
0053     ~PushPositiveValue() {
0054       m_ptr = m_oldPtr;
0055     }
0056   private:
0057     Value& m_ptr;
0058     Value m_oldPtr;
0059 };
0060 
0061 #endif