Skip to content

Commit 35039f2

Browse files
committed
Added insertInto to avoid crashes when inserting values into lists.
1 parent d712b52 commit 35039f2

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/atools.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ void convertVector(QVector<TYPE1>& dest, const QVector<TYPE2>& src)
9393
dest.append(type);
9494
}
9595

96+
/* Functions for safe insert that allow and index < 0 and > size + 1 */
97+
template<typename TYPE>
98+
void insertInto(QList<TYPE>& list, int index, const TYPE& type)
99+
{
100+
if(index < 0)
101+
list.prepend(type);
102+
else if(index > list.size())
103+
list.append(type);
104+
else
105+
list.insert(index, type);
106+
}
107+
108+
template<typename TYPE>
109+
void insertInto(QVector<TYPE>& list, int index, const TYPE& type)
110+
{
111+
if(index < 0)
112+
list.prepend(type);
113+
else if(index > list.size())
114+
list.append(type);
115+
else
116+
list.insert(index, type);
117+
}
118+
96119
/* Read whole file into a string */
97120
QString strFromFile(const QString& filename);
98121

0 commit comments

Comments
 (0)