Replace 90% of _WX_DECLARE_BASEARRAY_2 with a template class

Use template class instead of a macro to make it easier to read and
modify this code.

No real changes.
This commit is contained in:
Vadim Zeitlin
2018-05-31 22:25:57 +02:00
parent 724409a10c
commit f9721266ae

View File

@@ -111,96 +111,148 @@ private:
#define _WX_DECLARE_BASEARRAY(T, name, classexp) \ #define _WX_DECLARE_BASEARRAY(T, name, classexp) \
_WX_DECLARE_BASEARRAY_2(T, name, wxSortedArray_SortFunction<T>, classexp) _WX_DECLARE_BASEARRAY_2(T, name, wxSortedArray_SortFunction<T>, classexp)
// Note that "name" must be a class and not just a typedef because it can be
// (and is) forward declared in the existing code.
#define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \ #define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \
class name : public std::vector<T> \ class name : public wxBaseArray<T, predicate> \
{ \ { \
typedef predicate Predicate; \ typedef wxBaseArray<T, predicate> base; \
typedef predicate::CMPFUNC SCMPFUNC; \
public: \
typedef wxArray_SortFunction<T>::CMPFUNC CMPFUNC; \
\ \
public: \ public: \
typedef T base_type; \ name() : base() { } \
\ explicit name(size_t n) : base(n) { } \
name() : std::vector<T>() { } \ name(size_t n, \
name(size_type n) : std::vector<T>(n) { } \ typename std::vector<T>::const_reference v) : base(n, v) { } \
name(size_type n, const_reference v) : std::vector<T>(n, v) { } \
template <class InputIterator> \ template <class InputIterator> \
name(InputIterator first, InputIterator last) : std::vector<T>(first, last) { } \ name(InputIterator first, InputIterator last) : base(first, last) { } \
\ }
void Empty() { clear(); } \
void Clear() { clear(); } \ template <typename T, typename predicate>
void Alloc(size_t uiSize) { reserve(uiSize); } \ class wxBaseArray : public std::vector<T>
void Shrink() { name tmp(*this); swap(tmp); } \ {
\ typedef predicate Predicate;
size_t GetCount() const { return size(); } \ typedef typename predicate::CMPFUNC SCMPFUNC;
void SetCount(size_t n, T v = T()) { resize(n, v); } \
bool IsEmpty() const { return empty(); } \ public:
size_t Count() const { return size(); } \ typedef typename wxArray_SortFunction<T>::CMPFUNC CMPFUNC;
\
T& Item(size_t uiIndex) const \ typedef std::vector<T> base_vec;
{ wxASSERT( uiIndex < size() ); return (T&)operator[](uiIndex); } \
T& Last() const { return Item(size() - 1); } \ typedef typename base_vec::value_type value_type;
\ typedef typename base_vec::reference reference;
int Index(T item, bool bFromEnd = false) const \ typedef typename base_vec::const_reference const_reference;
{ \ typedef typename base_vec::iterator iterator;
if ( bFromEnd ) \ typedef typename base_vec::const_iterator const_iterator;
{ \ typedef typename base_vec::const_reverse_iterator const_reverse_iterator;
const const_reverse_iterator b = rbegin(), \ typedef typename base_vec::difference_type difference_type;
e = rend(); \ typedef typename base_vec::size_type size_type;
for ( const_reverse_iterator i = b; i != e; ++i ) \
if ( *i == item ) \ public:
return (int)(e - i - 1); \ typedef T base_type;
} \
else \ wxBaseArray() : std::vector<T>() { }
{ \ explicit wxBaseArray(size_t n) : std::vector<T>(n) { }
const const_iterator b = begin(), \ wxBaseArray(size_t n, const_reference v) : std::vector<T>(n, v) { }
e = end(); \
for ( const_iterator i = b; i != e; ++i ) \ template <class InputIterator>
if ( *i == item ) \ wxBaseArray(InputIterator first, InputIterator last)
return (int)(i - b); \ : std::vector<T>(first, last)
} \ { }
\
return wxNOT_FOUND; \ void Empty() { this->clear(); }
} \ void Clear() { this->clear(); }
int Index(T lItem, CMPFUNC fnCompare) const \ void Alloc(size_t uiSize) { this->reserve(uiSize); }
{ \ void Shrink()
Predicate p((SCMPFUNC)fnCompare); \ {
const_iterator i = std::lower_bound(begin(), end(), lItem, p);\ std::vector<T> tmp(*this);
return i != end() && !p(lItem, *i) ? (int)(i - begin()) \ this->swap(tmp);
: wxNOT_FOUND; \ }
} \
size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const \ size_t GetCount() const { return this->size(); }
{ \ void SetCount(size_t n, T v = T()) { this->resize(n, v); }
Predicate p((SCMPFUNC)fnCompare); \ bool IsEmpty() const { return this->empty(); }
const_iterator i = std::lower_bound(begin(), end(), lItem, p);\ size_t Count() const { return this->size(); }
return i - begin(); \
} \ T& Item(size_t uiIndex) const
void Add(T lItem, size_t nInsert = 1) \ {
{ insert(end(), nInsert, lItem); } \ wxASSERT( uiIndex < this->size() );
size_t Add(T lItem, CMPFUNC fnCompare) \ return const_cast<T&>((*this)[uiIndex]);
{ \ }
size_t n = IndexForInsert(lItem, fnCompare); \
Insert(lItem, n); \ T& Last() const { return Item(this->size() - 1); }
return n; \
} \ int Index(T item, bool bFromEnd = false) const
void Insert(T lItem, size_t uiIndex, size_t nInsert = 1) \ {
{ insert(begin() + uiIndex, nInsert, lItem); } \ if ( bFromEnd )
void Remove(T lItem) \ {
{ \ const const_reverse_iterator b = this->rbegin(),
int n = Index(lItem); \ e = this->rend();
wxCHECK_RET( n != wxNOT_FOUND, _WX_ERROR_REMOVE ); \ for ( const_reverse_iterator i = b; i != e; ++i )
RemoveAt((size_t)n); \ if ( *i == item )
} \ return (int)(e - i - 1);
void RemoveAt(size_t uiIndex, size_t nRemove = 1) \ }
{ erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \ else
\ {
void Sort(CMPFUNC fCmp) \ const const_iterator b = this->begin(),
{ \ e = this->end();
wxArray_SortFunction<T> p(fCmp); \ for ( const_iterator i = b; i != e; ++i )
std::sort(begin(), end(), p); \ if ( *i == item )
} \ return (int)(i - b);
} }
return wxNOT_FOUND;
}
int Index(T lItem, CMPFUNC fnCompare) const
{
Predicate p((SCMPFUNC)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
{
Predicate p((SCMPFUNC)fnCompare);
const_iterator i = std::lower_bound(this->begin(), this->end(), lItem, p);
return i - this->begin();
}
void Add(T lItem, size_t nInsert = 1)
{
this->insert(this->end(), nInsert, lItem);
}
size_t Add(T lItem, CMPFUNC fnCompare)
{
size_t n = IndexForInsert(lItem, fnCompare);
Insert(lItem, n);
return n;
}
void Insert(T lItem, size_t uiIndex, size_t nInsert = 1)
{
this->insert(this->begin() + uiIndex, nInsert, lItem);
}
void Remove(T lItem)
{
int n = Index(lItem);
wxCHECK_RET( n != wxNOT_FOUND, _WX_ERROR_REMOVE );
RemoveAt((size_t)n);
}
void RemoveAt(size_t uiIndex, size_t nRemove = 1)
{
this->erase(this->begin() + uiIndex, this->begin() + uiIndex + nRemove);
}
void Sort(CMPFUNC fCmp)
{
wxArray_SortFunction<T> p(fCmp);
std::sort(this->begin(), this->end(), p);
}
};
#else // if !wxUSE_STD_CONTAINERS #else // if !wxUSE_STD_CONTAINERS