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:
@@ -111,97 +111,149 @@ 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(); } \
|
|
||||||
void Alloc(size_t uiSize) { reserve(uiSize); } \
|
|
||||||
void Shrink() { name tmp(*this); swap(tmp); } \
|
|
||||||
\
|
|
||||||
size_t GetCount() const { return size(); } \
|
|
||||||
void SetCount(size_t n, T v = T()) { resize(n, v); } \
|
|
||||||
bool IsEmpty() const { return empty(); } \
|
|
||||||
size_t Count() const { return size(); } \
|
|
||||||
\
|
|
||||||
T& Item(size_t uiIndex) const \
|
|
||||||
{ wxASSERT( uiIndex < size() ); return (T&)operator[](uiIndex); } \
|
|
||||||
T& Last() const { return Item(size() - 1); } \
|
|
||||||
\
|
|
||||||
int Index(T item, bool bFromEnd = false) const \
|
|
||||||
{ \
|
|
||||||
if ( bFromEnd ) \
|
|
||||||
{ \
|
|
||||||
const const_reverse_iterator b = rbegin(), \
|
|
||||||
e = rend(); \
|
|
||||||
for ( const_reverse_iterator i = b; i != e; ++i ) \
|
|
||||||
if ( *i == item ) \
|
|
||||||
return (int)(e - i - 1); \
|
|
||||||
} \
|
|
||||||
else \
|
|
||||||
{ \
|
|
||||||
const const_iterator b = begin(), \
|
|
||||||
e = end(); \
|
|
||||||
for ( const_iterator i = b; i != e; ++i ) \
|
|
||||||
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(begin(), end(), lItem, p);\
|
|
||||||
return i != end() && !p(lItem, *i) ? (int)(i - begin()) \
|
|
||||||
: wxNOT_FOUND; \
|
|
||||||
} \
|
|
||||||
size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const \
|
|
||||||
{ \
|
|
||||||
Predicate p((SCMPFUNC)fnCompare); \
|
|
||||||
const_iterator i = std::lower_bound(begin(), end(), lItem, p);\
|
|
||||||
return i - begin(); \
|
|
||||||
} \
|
|
||||||
void Add(T lItem, size_t nInsert = 1) \
|
|
||||||
{ insert(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) \
|
|
||||||
{ insert(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) \
|
|
||||||
{ erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
|
|
||||||
\
|
|
||||||
void Sort(CMPFUNC fCmp) \
|
|
||||||
{ \
|
|
||||||
wxArray_SortFunction<T> p(fCmp); \
|
|
||||||
std::sort(begin(), end(), p); \
|
|
||||||
} \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, typename predicate>
|
||||||
|
class wxBaseArray : public std::vector<T>
|
||||||
|
{
|
||||||
|
typedef predicate Predicate;
|
||||||
|
typedef typename predicate::CMPFUNC SCMPFUNC;
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef typename wxArray_SortFunction<T>::CMPFUNC CMPFUNC;
|
||||||
|
|
||||||
|
typedef std::vector<T> base_vec;
|
||||||
|
|
||||||
|
typedef typename base_vec::value_type value_type;
|
||||||
|
typedef typename base_vec::reference reference;
|
||||||
|
typedef typename base_vec::const_reference const_reference;
|
||||||
|
typedef typename base_vec::iterator iterator;
|
||||||
|
typedef typename base_vec::const_iterator const_iterator;
|
||||||
|
typedef typename base_vec::const_reverse_iterator const_reverse_iterator;
|
||||||
|
typedef typename base_vec::difference_type difference_type;
|
||||||
|
typedef typename base_vec::size_type size_type;
|
||||||
|
|
||||||
|
public:
|
||||||
|
typedef T base_type;
|
||||||
|
|
||||||
|
wxBaseArray() : std::vector<T>() { }
|
||||||
|
explicit wxBaseArray(size_t n) : std::vector<T>(n) { }
|
||||||
|
wxBaseArray(size_t n, const_reference v) : std::vector<T>(n, v) { }
|
||||||
|
|
||||||
|
template <class InputIterator>
|
||||||
|
wxBaseArray(InputIterator first, InputIterator last)
|
||||||
|
: std::vector<T>(first, last)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
void Empty() { this->clear(); }
|
||||||
|
void Clear() { this->clear(); }
|
||||||
|
void Alloc(size_t uiSize) { this->reserve(uiSize); }
|
||||||
|
void Shrink()
|
||||||
|
{
|
||||||
|
std::vector<T> tmp(*this);
|
||||||
|
this->swap(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t GetCount() const { return this->size(); }
|
||||||
|
void SetCount(size_t n, T v = T()) { this->resize(n, v); }
|
||||||
|
bool IsEmpty() const { return this->empty(); }
|
||||||
|
size_t Count() const { return this->size(); }
|
||||||
|
|
||||||
|
T& Item(size_t uiIndex) const
|
||||||
|
{
|
||||||
|
wxASSERT( uiIndex < this->size() );
|
||||||
|
return const_cast<T&>((*this)[uiIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
T& Last() const { return Item(this->size() - 1); }
|
||||||
|
|
||||||
|
int Index(T item, bool bFromEnd = false) const
|
||||||
|
{
|
||||||
|
if ( bFromEnd )
|
||||||
|
{
|
||||||
|
const const_reverse_iterator b = this->rbegin(),
|
||||||
|
e = this->rend();
|
||||||
|
for ( const_reverse_iterator i = b; i != e; ++i )
|
||||||
|
if ( *i == item )
|
||||||
|
return (int)(e - i - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const const_iterator b = this->begin(),
|
||||||
|
e = this->end();
|
||||||
|
for ( const_iterator i = b; i != e; ++i )
|
||||||
|
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
|
||||||
|
|
||||||
#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
|
#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
|
||||||
|
Reference in New Issue
Block a user