1 /* 2 This source file is part of Konsole, a terminal emulator. 3 4 SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com> 5 6 SPDX-License-Identifier: GPL-2.0-or-later 7 */ 8 9 #ifndef POPSTACKONEXIT_H 10 #define POPSTACKONEXIT_H 11 12 #include <QStack> 13 14 namespace Konsole 15 { 16 /** 17 * PopStackOnExit is a utility to remove all values from a QStack which are added during 18 * the lifetime of a PopStackOnExit instance. 19 * 20 * When a PopStackOnExit instance is destroyed, elements are removed from the stack 21 * until the stack count is reduced the value when the PopStackOnExit instance was created. 22 */ 23 template<class T> 24 class PopStackOnExit 25 { 26 public: PopStackOnExit(QStack<T> & stack)27 explicit PopStackOnExit(QStack<T> &stack) 28 : _stack(stack) 29 , _count(stack.count()) 30 { 31 } 32 ~PopStackOnExit()33 ~PopStackOnExit() 34 { 35 while (_stack.count() > _count) { 36 _stack.pop(); 37 } 38 } 39 40 private: 41 Q_DISABLE_COPY(PopStackOnExit) 42 43 QStack<T> &_stack; 44 int _count; 45 }; 46 } 47 48 #endif 49