File indexing completed on 2024-04-21 05:51:24

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef POPSTACKONEXIT_H
0010 #define POPSTACKONEXIT_H
0011 
0012 #include <QStack>
0013 
0014 namespace Konsole
0015 {
0016 /**
0017  * PopStackOnExit is a utility to remove all values from a QStack which are added during
0018  * the lifetime of a PopStackOnExit instance.
0019  *
0020  * When a PopStackOnExit instance is destroyed, elements are removed from the stack
0021  * until the stack count is reduced the value when the PopStackOnExit instance was created.
0022  */
0023 template<class T>
0024 class PopStackOnExit
0025 {
0026 public:
0027     explicit PopStackOnExit(QStack<T> &stack)
0028         : _stack(stack)
0029         , _count(stack.count())
0030     {
0031     }
0032 
0033     ~PopStackOnExit()
0034     {
0035         while (_stack.count() > _count) {
0036             _stack.pop();
0037         }
0038     }
0039 
0040 private:
0041     Q_DISABLE_COPY(PopStackOnExit)
0042 
0043     QStack<T> &_stack;
0044     int _count;
0045 };
0046 }
0047 
0048 #endif