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.

To fix this, overload wxBaseArray::Sort() to accept either the "legacy"
sort function compatible with qsort() or a function compatible with
std::sort(), as it seems both variants could be used before. Also make
the type of the latter function customizable via a new optional Sorter
template parameter in wxBaseArray in order to allow wxSortedArrayString
to specify its own variant of it, taking (const) references instead of
values.

This complicates things, but should preserve compatibility while being
type-safe and, also, allows to simplify _WX_DEFINE_SORTED_TYPEARRAY_2 by
not passing the sort function signature to it any more.
This commit is contained in:
Vadim Zeitlin
2018-06-03 23:27:38 +02:00
parent 6294511a4e
commit 93edcaef20
2 changed files with 39 additions and 24 deletions

View File

@@ -80,9 +80,25 @@ public:
}
};
_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
wxArrayStringBase, = wxStringSortAscending,
class WXDLLIMPEXP_BASE, wxArrayString::CompareFunction);
// Unlike all the other sorted arrays, this one uses a comparison function
// taking objects by reference rather than value, so define a special functor
// wrapping it.
class wxSortedArrayString_SortFunction
{
public:
typedef int (wxCMPFUNC_CONV *CMPFUNC)(const wxString&, const wxString&);
explicit wxSortedArrayString_SortFunction(CMPFUNC f) : m_f(f) { }
bool operator()(const wxString& s1, const wxString& s2)
{ return m_f(s1, s2) < 0; }
private:
CMPFUNC m_f;
};
typedef wxBaseSortedArray<wxString, wxSortedArrayString_SortFunction>
wxSortedArrayStringBase;
class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
{