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