File indexing completed on 2024-12-08 13:30:58

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2022 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #ifndef RECENTITEMS_H
0012 #define RECENTITEMS_H
0013 
0014 #include "TypeUtils.h"
0015 
0016 #include <QStringList>
0017 
0018 template <unsigned int N>
0019 class RecentItems: public QStringList
0020 {
0021   public:
0022     using QStringList::QStringList;
0023 
0024     inline void push_back(const QString &s) = delete;
0025     inline void append(const QString &) = delete;
0026 
0027     //since prepend is non-virual we must override push_front as well
0028     inline void push_front(const QString &s) { prepend(s); };
0029 
0030     inline void prepend(const QString &s)
0031     {
0032         // If an item exist, remove it from the list and reinsert it at the beginning.
0033         removeAll(s);
0034 
0035         if(!s.isEmpty()) QStringList::prepend(s);
0036         if(size() > maxNofRecent) removeLast();
0037         assert(size() <= maxNofRecent);
0038     }
0039 
0040   private:
0041     constexpr static qint32 maxNofRecent = N;
0042 };
0043 
0044 #endif /* RECENTITEMS_H */