diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index 1b5f86e1f9..d3e1a2bb04 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -111,96 +111,148 @@ private: #define _WX_DECLARE_BASEARRAY(T, name, classexp) \ _WX_DECLARE_BASEARRAY_2(T, name, wxSortedArray_SortFunction, classexp) -#define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \ -class name : public std::vector \ -{ \ - typedef predicate Predicate; \ - typedef predicate::CMPFUNC SCMPFUNC; \ -public: \ - typedef wxArray_SortFunction::CMPFUNC CMPFUNC; \ - \ -public: \ - typedef T base_type; \ - \ - name() : std::vector() { } \ - name(size_type n) : std::vector(n) { } \ - name(size_type n, const_reference v) : std::vector(n, v) { } \ - template \ - name(InputIterator first, InputIterator last) : std::vector(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 p(fCmp); \ - std::sort(begin(), end(), p); \ - } \ -} +// 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) \ + class name : public wxBaseArray \ + { \ + typedef wxBaseArray base; \ + \ + public: \ + name() : base() { } \ + explicit name(size_t n) : base(n) { } \ + name(size_t n, \ + typename std::vector::const_reference v) : base(n, v) { } \ + template \ + name(InputIterator first, InputIterator last) : base(first, last) { } \ + } + +template +class wxBaseArray : public std::vector +{ + typedef predicate Predicate; + typedef typename predicate::CMPFUNC SCMPFUNC; + +public: + typedef typename wxArray_SortFunction::CMPFUNC CMPFUNC; + + typedef std::vector 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() { } + explicit wxBaseArray(size_t n) : std::vector(n) { } + wxBaseArray(size_t n, const_reference v) : std::vector(n, v) { } + + template + wxBaseArray(InputIterator first, InputIterator last) + : std::vector(first, last) + { } + + void Empty() { this->clear(); } + void Clear() { this->clear(); } + void Alloc(size_t uiSize) { this->reserve(uiSize); } + void Shrink() + { + std::vector 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((*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 p(fCmp); + std::sort(this->begin(), this->end(), p); + } +}; #else // if !wxUSE_STD_CONTAINERS