Skip to content

Commit 12ea361

Browse files
authored
Fixed clang compilation warnings about delete #2001 (#2061)
1 parent bb8b6c8 commit 12ea361

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/core/ptrvector.h

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2006 Milan Digital Audio LLC
3-
* Copyright 2009-2023 GrandOrgue contributors (see AUTHORS)
3+
* Copyright 2009-2024 GrandOrgue contributors (see AUTHORS)
44
* License GPL-2.0 or later
55
* (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
66
*/
@@ -16,16 +16,34 @@ template <class T> class ptr_vector : protected std::vector<T *> {
1616
ptr_vector(const ptr_vector &);
1717
const ptr_vector operator=(const ptr_vector &);
1818

19+
template <
20+
typename T1,
21+
std::enable_if_t<std::is_array<T1>::value, bool> = true>
22+
inline static void delete0(T1 *e) {
23+
delete[] e;
24+
}
25+
26+
template <
27+
typename T1,
28+
std::enable_if_t<!std::is_array<T1>::value, bool> = true>
29+
inline static void delete0(T1 *e) {
30+
delete e;
31+
}
32+
33+
inline static void delete1(T *e) {
34+
if (e)
35+
delete0(e);
36+
}
37+
1938
public:
2039
ptr_vector(unsigned new_size = 0) : std::vector<T *>(new_size) {
2140
for (unsigned i = 0; i < new_size; i++)
22-
at(i) = 0;
41+
at(i) = nullptr;
2342
}
2443

2544
~ptr_vector() {
26-
for (unsigned i = 0; i < size(); i++)
27-
if (at(i))
28-
delete at(i);
45+
for (auto &e : (*this))
46+
delete1(e);
2947
}
3048

3149
T *&operator[](unsigned pos) { return at(pos); }
@@ -43,11 +61,10 @@ template <class T> class ptr_vector : protected std::vector<T *> {
4361
void resize(unsigned new_size) {
4462
unsigned oldsize = size();
4563
for (unsigned i = new_size; i < oldsize; i++)
46-
if (at(i))
47-
delete at(i);
64+
delete1(at(i));
4865
std::vector<T *>::resize(new_size);
4966
for (unsigned i = oldsize; i < new_size; i++)
50-
at(i) = 0;
67+
at(i) = nullptr;
5168
}
5269

5370
void push_back(T *ptr) { std::vector<T *>::push_back(ptr); }

0 commit comments

Comments
 (0)