From 546604ab559abc5581b8b4934eadbdadb607b3d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 3 Jun 2018 23:27:38 +0200 Subject: [PATCH] Fix wrong function pointer casts in dynamic arrays code Don't cast function pointers of incompatible types, this resulted in gcc 8 -Wcast-function-type warnings and could hide real errors. Use normal, taking elements by value, sort function for in wxBaseArray methods used by the sorted arrays only and provide Sort() overloads for both this sort function variant and the compatible with qsort() one taking pointers to the elements. --- include/wx/dynarray.h | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index fa52765dd3..d7adffbd35 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -44,6 +44,11 @@ typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2); // Array class providing legacy dynamic arrays API on top of wxVector<> // ---------------------------------------------------------------------------- +// For some reasons lost in the depths of time, sort functions with different +// signatures are used to sort normal arrays and to keep sorted arrays sorted. +// These two functors can be used as predicates with std::sort() adapting the +// sort function to it, whichever signature it uses. + template class wxArray_SortFunction { @@ -52,7 +57,7 @@ public: wxArray_SortFunction(CMPFUNC f) : m_f(f) { } bool operator()(const T& i1, const T& i2) - { return m_f((T*)&i1, (T*)&i2) < 0; } + { return m_f(const_cast(&i1), const_cast(&i2)) < 0; } private: CMPFUNC m_f; }; @@ -74,9 +79,9 @@ template class wxBaseArray : public wxVector { typedef wxSortedArray_SortFunction Predicate; - typedef int (wxCMPFUNC_CONV *SCMPFUNC)(T, T); public: + typedef typename Predicate::CMPFUNC SCMPFUNC; typedef typename wxArray_SortFunction::CMPFUNC CMPFUNC; typedef wxVector base_vec; @@ -151,17 +156,17 @@ public: return wxNOT_FOUND; } - int Index(T lItem, CMPFUNC fnCompare) const + int Index(T lItem, SCMPFUNC fnCompare) const { - Predicate p((SCMPFUNC)fnCompare); + Predicate p(fnCompare); const_iterator i = std::lower_bound(this->begin(), this->end(), lItem, p); return i != this->end() && !p(lItem, *i) ? (int)(i - this->begin()) : wxNOT_FOUND; } - size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const + size_t IndexForInsert(T lItem, SCMPFUNC fnCompare) const { - Predicate p((SCMPFUNC)fnCompare); + Predicate p(fnCompare); const_iterator i = std::lower_bound(this->begin(), this->end(), lItem, p); return i - this->begin(); } @@ -171,7 +176,7 @@ public: this->insert(this->end(), nInsert, lItem); } - size_t Add(T lItem, CMPFUNC fnCompare) + size_t Add(T lItem, SCMPFUNC fnCompare) { size_t n = IndexForInsert(lItem, fnCompare); Insert(lItem, n); @@ -200,6 +205,12 @@ public: wxArray_SortFunction p(fCmp); std::sort(this->begin(), this->end(), p); } + + void Sort(SCMPFUNC fCmp) + { + Predicate p(fCmp); + std::sort(this->begin(), this->end(), p); + } }; // ============================================================================ @@ -223,8 +234,6 @@ public: template class wxBaseSortedArray : public wxBaseArray { - typedef typename wxBaseArray::CMPFUNC CMPFUNC; - public: explicit wxBaseSortedArray(Cmp fn) : m_fnCompare(fn) { } @@ -237,7 +246,7 @@ public: size_t IndexForInsert(T item) const { - return this->wxBaseArray::IndexForInsert(item, (CMPFUNC)m_fnCompare); + return this->wxBaseArray::IndexForInsert(item, m_fnCompare); } void AddAt(T item, size_t index) @@ -247,7 +256,7 @@ public: size_t Add(T item) { - return this->wxBaseArray::Add(item, (CMPFUNC)m_fnCompare); + return this->wxBaseArray::Add(item, m_fnCompare); } void push_back(T item)