diff --git a/docs/changes.txt b/docs/changes.txt index f59411397b..59579a9814 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -17,6 +17,11 @@ wxBase: by all derived classes, if you have user-defined event classes please add "wxEvent *Clone() const { return new MyEvent(*this); }" line to them +- wxArray macros have been changed to fix runtime problems under 64 bit + architectures. The base class is now implemented once for each needed + primitive type in order to avoid invalid reference casts. Macros are + provided to implement new arrays using these primitive base arrays. + All (GUI): - the event type constants are not constants any more but are dynamically diff --git a/include/wx/arrimpl.cpp b/include/wx/arrimpl.cpp index b16c18bded..8c975cdbb9 100644 --- a/include/wx/arrimpl.cpp +++ b/include/wx/arrimpl.cpp @@ -60,16 +60,16 @@ name::name(const name& src) \ void name::DoEmpty() \ { \ for ( size_t ui = 0; ui < Count(); ui++ ) \ - delete (T*)wxBaseArray::Item(ui); \ + delete (T*)wxBaseArrayPtrVoid::Item(ui); \ } \ \ void name::RemoveAt(size_t uiIndex) \ { \ wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \ \ - delete (T*)wxBaseArray::Item(uiIndex); \ + delete (T*)wxBaseArrayPtrVoid::Item(uiIndex); \ \ - wxBaseArray::RemoveAt(uiIndex); \ + wxBaseArrayPtrVoid::RemoveAt(uiIndex); \ } \ \ void name::Add(const T& item) \ @@ -92,7 +92,7 @@ int name::Index(const T& Item, bool bFromEnd) const \ if ( Count() > 0 ) { \ size_t ui = Count() - 1; \ do { \ - if ( (T*)wxBaseArray::Item(ui) == &Item ) \ + if ( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ return ui; \ ui--; \ } \ @@ -101,7 +101,7 @@ int name::Index(const T& Item, bool bFromEnd) const \ } \ else { \ for( size_t ui = 0; ui < Count(); ui++ ) { \ - if( (T*)wxBaseArray::Item(ui) == &Item ) \ + if( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ return ui; \ } \ } \ diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index f70cc6c2f3..649e4b9797 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -76,92 +76,49 @@ typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2); // you cast "SomeArray *" as "BaseArray *" and then delete it) // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxBaseArray -{ -public: - /** @name ctors and dtor */ - //@{ - /// default ctor - wxBaseArray(); - /// copy ctor - wxBaseArray(const wxBaseArray& array); - /// assignment operator - wxBaseArray& operator=(const wxBaseArray& src); - /// not virtual, see above - ~wxBaseArray(); - //@} - - /** @name memory management */ - //@{ - /// empties the array, but doesn't release memory - void Empty() { m_nCount = 0; } - /// empties the array and releases memory - void Clear(); - /// preallocates memory for given number of items - void Alloc(size_t uiSize); - /// minimizes the memory used by the array (frees unused memory) - void Shrink(); - //@} - - /** @name simple accessors */ - //@{ - /// number of elements in the array - size_t GetCount() const { return m_nCount; } - /// is it empty? - bool IsEmpty() const { return m_nCount == 0; } - /// this version is obsolete, use GetCount() - size_t Count() const { return m_nCount; } - //@} - -protected: - // these methods are protected because if they were public one could - // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's - // type safe methods - - /** @name items access */ - //@{ - /// get item at position uiIndex (range checking is done in debug version) - long& Item(size_t uiIndex) const - { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; } - /// same as Item() - long& operator[](size_t uiIndex) const { return Item(uiIndex); } - //@} - - /** @name item management */ - //@{ - /** - Search the element in the array, starting from the either side - @param bFromEnd if TRUE, start from the end - @return index of the first item matched or wxNOT_FOUND - @see wxNOT_FOUND - */ - int Index(long lItem, bool bFromEnd = FALSE) const; - /// search for an item using binary search in a sorted array - int Index(long lItem, CMPFUNC fnCompare) const; - /// search for a place to insert the element into a sorted array - size_t IndexForInsert(long lItem, CMPFUNC fnCompare) const; - /// add new element at the end - void Add(long lItem); - /// add item assuming the array is sorted with fnCompare function - void Add(long lItem, CMPFUNC fnCompare); - /// add new element at given position (it becomes Item[uiIndex]) - void Insert(long lItem, size_t uiIndex); - /// remove first item matching this value - void Remove(long lItem); - /// remove item by index - void RemoveAt(size_t uiIndex); - //@} - - /// sort array elements using given compare function - void Sort(CMPFUNC fnCompare); - -private: - void Grow(); // makes array bigger if needed - - size_t m_nSize, // current size of the array - m_nCount; // current number of elements - - long *m_pItems; // pointer to data +#define _WX_DECLARE_BASEARRAY(T, name, classexp) \ +classexp name \ +{ \ +public: \ + name(); \ + name(const name& array); \ + name& operator=(const name& src); \ + ~name(); \ + \ + void Empty() { m_nCount = 0; } \ + void Clear(); \ + void Alloc(size_t uiSize); \ + void Shrink(); \ + \ + size_t GetCount() const { return m_nCount; } \ + bool IsEmpty() const { return m_nCount == 0; } \ + size_t Count() const { return m_nCount; } \ + \ +protected: \ + T& Item(size_t uiIndex) const \ + { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; } \ + T& operator[](size_t uiIndex) const { return Item(uiIndex); } \ + \ + int Index(T lItem, bool bFromEnd = FALSE) const; \ + int Index(T lItem, CMPFUNC fnCompare) const; \ + size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const; \ + void Add(T lItem); \ + void Add(T lItem, CMPFUNC fnCompare); \ + void Insert(T lItem, size_t uiIndex); \ + void Remove(T lItem); \ + void RemoveAt(size_t uiIndex); \ + \ + void Sort(CMPFUNC fnCompare); \ + \ +private: \ + typedef T base_type; \ + \ + void Grow(); \ + \ + size_t m_nSize, \ + m_nCount; \ + \ + T *m_pItems; \ }; // ============================================================================ @@ -177,151 +134,152 @@ private: // The classes need a (even trivial) ~name() to link under Mac X // // _WX_ERROR_REMOVE is needed to resolve the name conflict between the wxT() -// macor and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY! +// macro and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY! #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove") // ---------------------------------------------------------------------------- -// _WX_DEFINE_ARRAY: array for simple types +// _WX_DEFINE_TYPEARRAY: array for simple types // ---------------------------------------------------------------------------- -#define _WX_DEFINE_ARRAY(T, name, classexp) \ -wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(long), \ - TypeIsTooBigToBeStoredInWxArray, \ - name); \ -typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \ -classexp name : public wxBaseArray \ -{ \ -public: \ - name() { } \ - ~name() { } \ - \ - name& operator=(const name& src) \ - { wxBaseArray* temp = (wxBaseArray*) this; \ - (*temp) = ((const wxBaseArray&)src); \ - return *this; } \ - \ - T& operator[](size_t uiIndex) const \ - { return (T&)(wxBaseArray::Item(uiIndex)); } \ - T& Item(size_t uiIndex) const \ - { return (T&)(wxBaseArray::Item(uiIndex)); } \ - T& Last() const \ - { return (T&)(wxBaseArray::Item(Count() - 1)); } \ - \ - int Index(T Item, bool bFromEnd = FALSE) const \ - { return wxBaseArray::Index((long)Item, bFromEnd); } \ - \ - void Add(T Item) \ - { wxBaseArray::Add((long)Item); } \ - void Insert(T Item, size_t uiIndex) \ - { wxBaseArray::Insert((long)Item, uiIndex) ; } \ - \ - void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \ - void Remove(T Item) \ - { int iIndex = Index(Item); \ - wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ - _WX_ERROR_REMOVE); \ - wxBaseArray::RemoveAt((size_t)iIndex); } \ - \ - void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \ +#define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \ +wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \ + TypeTooBigToBeStoredIn##base, \ + name); \ +typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \ +classexp name : public base \ +{ \ +public: \ + name() { } \ + ~name() { } \ + \ + name& operator=(const name& src) \ + { base* temp = (base*) this; \ + (*temp) = ((const base&)src); \ + return *this; } \ + \ + T& operator[](size_t uiIndex) const \ + { return (T&)(base::Item(uiIndex)); } \ + T& Item(size_t uiIndex) const \ + { return (T&)(base::Item(uiIndex)); } \ + T& Last() const \ + { return (T&)(base::Item(Count() - 1)); } \ + \ + int Index(T Item, bool bFromEnd = FALSE) const \ + { return base::Index(Item, bFromEnd); } \ + \ + void Add(T Item) \ + { base::Add(Item); } \ + void Insert(T Item, size_t uiIndex) \ + { base::Insert(Item, uiIndex) ; } \ + \ + void RemoveAt(size_t uiIndex) { base::RemoveAt(uiIndex); } \ + void Remove(T Item) \ + { int iIndex = Index(Item); \ + wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ + _WX_ERROR_REMOVE); \ + base::RemoveAt((size_t)iIndex); } \ + \ + void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \ } // ---------------------------------------------------------------------------- -// _WX_DEFINE_SORTED_ARRAY: sorted array for simple data types +// _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types +// cannot handle types with size greater than pointer because of sorting // ---------------------------------------------------------------------------- -#define _WX_DEFINE_SORTED_ARRAY(T, name, defcomp, classexp) \ -wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(long), \ - TypeIsTooBigToBeStoredInWxArray, \ - name); \ -typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \ -classexp name : public wxBaseArray \ -{ \ -public: \ - name(SCMPFUNC##T fn defcomp) { m_fnCompare = fn; } \ - \ - name& operator=(const name& src) \ - { wxBaseArray* temp = (wxBaseArray*) this; \ - (*temp) = ((const wxBaseArray&)src); \ - m_fnCompare = src.m_fnCompare; \ - return *this; } \ - \ - T& operator[](size_t uiIndex) const \ - { return (T&)(wxBaseArray::Item(uiIndex)); } \ - T& Item(size_t uiIndex) const \ - { return (T&)(wxBaseArray::Item(uiIndex)); } \ - T& Last() const \ - { return (T&)(wxBaseArray::Item(Count() - 1)); } \ - \ - int Index(T Item) const \ - { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\ - \ - size_t IndexForInsert(T Item) const \ - { return wxBaseArray::IndexForInsert((long)Item, \ - (CMPFUNC)m_fnCompare); } \ - \ - void AddAt(T item, size_t index) \ - { wxBaseArray::Insert((long)item, index); } \ - \ - void Add(T Item) \ - { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \ - \ - void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \ - void Remove(T Item) \ - { int iIndex = Index(Item); \ - wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ - _WX_ERROR_REMOVE ); \ - wxBaseArray::RemoveAt((size_t)iIndex); } \ - \ -private: \ - SCMPFUNC##T m_fnCompare; \ +#define _WX_DEFINE_SORTED_TYPEARRAY(T, name, base, defcomp, classexp) \ +wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(void *), \ + TypeTooBigToBeStoredInSorted##base, \ + name); \ +typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \ +classexp name : public base \ +{ \ +public: \ + name(SCMPFUNC##T fn defcomp) { m_fnCompare = fn; } \ + \ + name& operator=(const name& src) \ + { base* temp = (base*) this; \ + (*temp) = ((const base&)src); \ + m_fnCompare = src.m_fnCompare; \ + return *this; } \ + \ + T& operator[](size_t uiIndex) const \ + { return (T&)(base::Item(uiIndex)); } \ + T& Item(size_t uiIndex) const \ + { return (T&)(base::Item(uiIndex)); } \ + T& Last() const \ + { return (T&)(base::Item(Count() - 1)); } \ + \ + int Index(T Item) const \ + { return base::Index(Item, (CMPFUNC)m_fnCompare); } \ + \ + size_t IndexForInsert(T Item) const \ + { return base::IndexForInsert(Item, (CMPFUNC)m_fnCompare); } \ + \ + void AddAt(T item, size_t index) \ + { base::Insert(item, index); } \ + \ + void Add(T Item) \ + { base::Add(Item, (CMPFUNC)m_fnCompare); } \ + \ + void RemoveAt(size_t uiIndex) { base::RemoveAt(uiIndex); } \ + void Remove(T Item) \ + { int iIndex = Index(Item); \ + wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ + _WX_ERROR_REMOVE ); \ + base::RemoveAt((size_t)iIndex); } \ + \ +private: \ + SCMPFUNC##T m_fnCompare; \ } // ---------------------------------------------------------------------------- // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics // ---------------------------------------------------------------------------- -#define _WX_DECLARE_OBJARRAY(T, name, classexp) \ -typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \ -classexp name : public wxBaseArray \ -{ \ -public: \ - name() { } \ - name(const name& src); \ - name& operator=(const name& src); \ - \ - ~name(); \ - \ - T& operator[](size_t uiIndex) const \ - { return *(T*)wxBaseArray::Item(uiIndex); } \ - T& Item(size_t uiIndex) const \ - { return *(T*)wxBaseArray::Item(uiIndex); } \ - T& Last() const \ - { return *(T*)(wxBaseArray::Item(Count() - 1)); } \ - \ - int Index(const T& Item, bool bFromEnd = FALSE) const; \ - \ - void Add(const T& Item); \ - void Add(const T* pItem) \ - { wxBaseArray::Add((long)pItem); } \ - \ - void Insert(const T& Item, size_t uiIndex); \ - void Insert(const T* pItem, size_t uiIndex) \ - { wxBaseArray::Insert((long)pItem, uiIndex); } \ - \ - void Empty() { DoEmpty(); wxBaseArray::Empty(); } \ - void Clear() { DoEmpty(); wxBaseArray::Clear(); } \ - \ - T* Detach(size_t uiIndex) \ - { T* p = (T*)wxBaseArray::Item(uiIndex); \ - wxBaseArray::RemoveAt(uiIndex); return p; } \ - void RemoveAt(size_t uiIndex); \ - \ - void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \ - \ -private: \ - void DoEmpty(); \ - void DoCopy(const name& src); \ +#define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \ +typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \ +classexp name : public base \ +{ \ +typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \ +public: \ + name() { } \ + name(const name& src); \ + name& operator=(const name& src); \ + \ + ~name(); \ + \ + T& operator[](size_t uiIndex) const \ + { return *(T*)base::Item(uiIndex); } \ + T& Item(size_t uiIndex) const \ + { return *(T*)base::Item(uiIndex); } \ + T& Last() const \ + { return *(T*)(base::Item(Count() - 1)); } \ + \ + int Index(const T& Item, bool bFromEnd = FALSE) const; \ + \ + void Add(const T& Item); \ + void Add(const T* pItem) \ + { base::Add((T*)pItem); } \ + \ + void Insert(const T& Item, size_t uiIndex); \ + void Insert(const T* pItem, size_t uiIndex) \ + { base::Insert((T*)pItem, uiIndex); } \ + \ + void Empty() { DoEmpty(); base::Empty(); } \ + void Clear() { DoEmpty(); base::Clear(); } \ + \ + T* Detach(size_t uiIndex) \ + { T* p = (T*)base::Item(uiIndex); \ + base::RemoveAt(uiIndex); return p; } \ + void RemoveAt(size_t uiIndex); \ + \ + void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \ + \ +private: \ + void DoEmpty(); \ + void DoCopy(const name& src); \ } // ============================================================================ @@ -343,25 +301,40 @@ private: \ #define wxARRAY_DEFAULT_EXPORT // ---------------------------------------------------------------------------- -// WX_DEFINE_ARRAY(T, name) define an array class named "name" containing the -// elements of simple type T such that sizeof(T) <= sizeof(long) +// WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing +// the elements of type T +// ---------------------------------------------------------------------------- + +#define WX_DECLARE_BASEARRAY(T, name) \ + WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT) + +#define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \ + WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLEXPORT) + +#define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \ + typedef T _wxArray##name; \ + _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode) + +// ---------------------------------------------------------------------------- +// WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving +// from class "base" containing the elements of type T // // Note that the class defined has only inline function and doesn't take any // space at all so there is no size penalty for defining multiple array classes // ---------------------------------------------------------------------------- -#define WX_DEFINE_ARRAY(T, name) \ - WX_DEFINE_USER_EXPORTED_ARRAY(T, name, wxARRAY_DEFAULT_EXPORT) +#define WX_DEFINE_TYPEARRAY(T, name, base) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, wxARRAY_DEFAULT_EXPORT) -#define WX_DEFINE_EXPORTED_ARRAY(T, name) \ - WX_DEFINE_USER_EXPORTED_ARRAY(T, name, WXDLLEXPORT) +#define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT) -#define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \ - typedef T _wxArray##name; \ - _WX_DEFINE_ARRAY(_wxArray##name, name, class expmode) +#define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ + typedef T _wxArray##name; \ + _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, class expmode) // ---------------------------------------------------------------------------- -// WX_DEFINE_SORTED_ARRAY: this is the same as the previous macro, but it +// WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it // defines a sorted array. // // Differences: @@ -380,8 +353,8 @@ private: \ // // Note that you have to specify the comparison function when creating the // objects of this array type. If, as in 99% of cases, the comparison function -// is the same for all objects of a class, WX_DEFINE_SORTED_ARRAY_CMP below is -// more convenient. +// is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below +// is more convenient. // // Summary: use this class when the speed of Index() function is important, use // the normal arrays otherwise. @@ -389,38 +362,43 @@ private: \ #define wxARRAY_EMPTY_CMP -#define WX_DEFINE_SORTED_ARRAY(T, name) \ - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, wxARRAY_DEFAULT_EXPORT) +#define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \ + wxARRAY_DEFAULT_EXPORT) -#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \ - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, WXDLLEXPORT) +#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT) -#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \ - typedef T _wxArray##name; \ - _WX_DEFINE_SORTED_ARRAY(_wxArray##name, name, wxARRAY_EMPTY_CMP, class expmode) +#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ + typedef T _wxArray##name; \ + _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, \ + wxARRAY_EMPTY_CMP, class expmode) // ---------------------------------------------------------------------------- -// WX_DEFINE_SORTED_ARRAY_CMP: exactly the same as above but the comparison +// WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison // function is provided by this macro and the objects of this class have a // default constructor which just uses it. // // The arguments are: the element type, the comparison function and the array // name // -// NB: this is, of course, how WX_DEFINE_SORTED_ARRAY() should have worked from -// the very beginning - unfortunately I didn't think about this earlier :-( +// NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked +// from the very beginning - unfortunately I didn't think about this earlier // ---------------------------------------------------------------------------- -#define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \ - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, \ - wxARRAY_DEFAULT_EXPORT) +#define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ + wxARRAY_DEFAULT_EXPORT) -#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \ - WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, WXDLLEXPORT) +#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ + WXDLLEXPORT) -#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, name, expmode) \ +#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ + expmode) \ typedef T _wxArray##name; \ - _WX_DEFINE_SORTED_ARRAY(_wxArray##name, name, = cmpfunc, class expmode) + _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, = cmpfunc, \ + class expmode) // ---------------------------------------------------------------------------- // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class @@ -431,7 +409,6 @@ private: \ // and T& is returned. // // Don't use this for simple types such as "int" or "long"! -// You _may_ use it for "double" but it's awfully inefficient. // // Note on Add/Insert functions: // 1) function(T*) gives the object to the array, i.e. it will delete the @@ -450,7 +427,6 @@ private: \ // define new class from this template even if you don't use it - which is not // the case for the simple (non-object) array classes // -// // To use an objarray class you must // #include "dynarray.h" // WX_DECLARE_OBJARRAY(element_type, list_class_name) @@ -472,7 +448,7 @@ private: \ #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \ typedef T _wxObjArray##name; \ - _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, class expmode) + _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, class expmode) // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included, // try to provoke a human-understandable error if it used incorrectly. @@ -483,13 +459,135 @@ private: \ #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) +// ---------------------------------------------------------------------------- +// Some commonly used predefined base arrays +// ---------------------------------------------------------------------------- + +WX_DECLARE_EXPORTED_BASEARRAY(void *, wxBaseArrayPtrVoid); +WX_DECLARE_EXPORTED_BASEARRAY(short, wxBaseArrayShort); +WX_DECLARE_EXPORTED_BASEARRAY(int, wxBaseArrayInt); +WX_DECLARE_EXPORTED_BASEARRAY(long, wxBaseArrayLong); +WX_DECLARE_EXPORTED_BASEARRAY(double, wxBaseArrayDouble); + +// ---------------------------------------------------------------------------- +// Convenience macros to define arrays from base arrays +// ---------------------------------------------------------------------------- + +#define WX_DEFINE_ARRAY(T, name) \ + WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_EXPORTED_ARRAY(T, name) \ + WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode) + +#define WX_DEFINE_ARRAY_SHORT(T, name) \ + WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayShort) +#define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \ + WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) +#define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode) + +#define WX_DEFINE_ARRAY_INT(T, name) \ + WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayInt) +#define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \ + WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) +#define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) + +#define WX_DEFINE_ARRAY_LONG(T, name) \ + WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayLong) +#define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \ + WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) +#define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) + +#define WX_DEFINE_ARRAY_DOUBLE(T, name) \ + WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayDouble) +#define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \ + WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble) +#define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \ + WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble, expmode) + +// ---------------------------------------------------------------------------- +// Convenience macros to define sorted arrays from base arrays +// ---------------------------------------------------------------------------- + +#define WX_DEFINE_SORTED_ARRAY(T, name) \ + WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode) + +#define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \ + WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode) + +#define WX_DEFINE_SORTED_ARRAY_INT(T, name) \ + WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) + +#define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \ + WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) + +// ---------------------------------------------------------------------------- +// Convenience macros to define sorted arrays from base arrays +// ---------------------------------------------------------------------------- + +#define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \ + WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \ + name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ + wxBaseArrayPtrVoid, expmode) + +#define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ + WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \ + name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ + wxBaseArrayShort, expmode) + +#define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ + WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \ + name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ + wxBaseArrayInt, expmode) + +#define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ + WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) +#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ + WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) +#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \ + name, expmode) \ + WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ + wxBaseArrayLong, expmode) + // ---------------------------------------------------------------------------- // Some commonly used predefined arrays // ---------------------------------------------------------------------------- -WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt); -WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong); -WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid); +WX_DEFINE_EXPORTED_ARRAY_SHORT (short, wxArrayShort); +WX_DEFINE_EXPORTED_ARRAY_INT (int, wxArrayInt); +WX_DEFINE_EXPORTED_ARRAY_LONG (long, wxArrayLong); +WX_DEFINE_EXPORTED_ARRAY (void *, wxArrayPtrVoid); // ----------------------------------------------------------------------------- // convenience macros diff --git a/include/wx/encconv.h b/include/wx/encconv.h index 9e67c21141..84d2677175 100644 --- a/include/wx/encconv.h +++ b/include/wx/encconv.h @@ -47,7 +47,7 @@ enum // types // ---------------------------------------------------------------------------- -WX_DEFINE_ARRAY(wxFontEncoding, wxFontEncodingArray); +WX_DEFINE_ARRAY_INT(wxFontEncoding, wxFontEncodingArray); //-------------------------------------------------------------------------------- // wxEncodingConverter diff --git a/include/wx/textbuf.h b/include/wx/textbuf.h index f5a44afe68..ad6081eec3 100644 --- a/include/wx/textbuf.h +++ b/include/wx/textbuf.h @@ -42,7 +42,7 @@ enum wxTextFileType // wxTextBuffer // ---------------------------------------------------------------------------- -WX_DEFINE_EXPORTED_ARRAY(wxTextFileType, ArrayFileType); +WX_DEFINE_EXPORTED_ARRAY_INT(wxTextFileType, ArrayFileType); #endif // wxUSE_TEXTBUFFER diff --git a/include/wx/treebase.h b/include/wx/treebase.h index 7a78957235..13df7c5199 100644 --- a/include/wx/treebase.h +++ b/include/wx/treebase.h @@ -94,7 +94,7 @@ protected: wxTreeItemId m_pItem; }; -WX_DEFINE_EXPORTED_ARRAY(wxTreeItemId, wxArrayTreeItemIds); +WX_DEFINE_EXPORTED_ARRAY_LONG(wxTreeItemId, wxArrayTreeItemIds); // ---------------------------------------------------------------------------- // constants diff --git a/src/common/dynarray.cpp b/src/common/dynarray.cpp index 2d8e162378..e8efb32ecb 100644 --- a/src/common/dynarray.cpp +++ b/src/common/dynarray.cpp @@ -45,225 +45,256 @@ // ============================================================================ // ---------------------------------------------------------------------------- -// wxBaseArray - dynamice array of 'long's +// wxBaseArray - dynamic array of 'T's // ---------------------------------------------------------------------------- -// ctor -wxBaseArray::wxBaseArray() -{ - m_nSize = - m_nCount = 0; - m_pItems = (long *) NULL; +#define _DECLARE_BASEARRAY(T, name) \ +/* ctor */ \ +name::name() \ +{ \ + m_nSize = \ + m_nCount = 0; \ + m_pItems = (T *)NULL; \ +} \ + \ +/* copy ctor */ \ +name::name(const name& src) \ +{ \ + m_nSize = /* not src.m_nSize to save memory */ \ + m_nCount = src.m_nCount; \ + \ + if ( m_nSize != 0 ) { \ + m_pItems = new T[m_nSize]; \ + /* only copy if allocation succeeded */ \ + if ( m_pItems ) { \ + memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \ + } \ + else { \ + m_nSize = 0; \ + } \ + } \ + else \ + m_pItems = (T *) NULL; \ +} \ + \ +/* assignment operator */ \ +name& name::operator=(const name& src) \ +{ \ + wxDELETEA(m_pItems); \ + \ + m_nSize = /* not src.m_nSize to save memory */ \ + m_nCount = src.m_nCount; \ + \ + if ( m_nSize != 0 ){ \ + m_pItems = new T[m_nSize]; \ + /* only copy if allocation succeeded */ \ + if ( m_pItems ) { \ + memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \ + } \ + else { \ + m_nSize = 0; \ + } \ + } \ + else \ + m_pItems = (T *) NULL; \ + \ + return *this; \ +} \ + \ +/* grow the array */ \ +void name::Grow() \ +{ \ + /* only do it if no more place */ \ + if( m_nCount == m_nSize ) { \ + if( m_nSize == 0 ) { \ + /* was empty, alloc some memory */ \ + m_pItems = new T[WX_ARRAY_DEFAULT_INITIAL_SIZE]; \ + /* only grow if allocation succeeded */ \ + if ( m_pItems ) { \ + m_nSize = WX_ARRAY_DEFAULT_INITIAL_SIZE; \ + } \ + } \ + else \ + { \ + /* add 50% but not too much */ \ + size_t nIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \ + ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \ + if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) \ + nIncrement = ARRAY_MAXSIZE_INCREMENT; \ + T *pNew = new T[m_nSize + nIncrement]; \ + /* only grow if allocation succeeded */ \ + if ( pNew ) { \ + m_nSize += nIncrement; \ + /* copy data to new location */ \ + memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \ + delete [] m_pItems; \ + m_pItems = pNew; \ + } \ + } \ + } \ +} \ + \ +/* dtor */ \ +name::~name() \ +{ \ + wxDELETEA(m_pItems); \ +} \ + \ +/* clears the list */ \ +void name::Clear() \ +{ \ + m_nSize = \ + m_nCount = 0; \ + \ + wxDELETEA(m_pItems); \ +} \ + \ +/* pre-allocates memory (frees the previous data!) */ \ +void name::Alloc(size_t nSize) \ +{ \ + /* only if old buffer was not big enough */ \ + if ( nSize > m_nSize ) { \ + wxDELETEA(m_pItems); \ + m_nSize = 0; \ + m_pItems = new T[nSize]; \ + /* only alloc if allocation succeeded */ \ + if ( m_pItems ) { \ + m_nSize = nSize; \ + } \ + } \ + \ + m_nCount = 0; \ +} \ + \ +/* minimizes the memory usage by freeing unused memory */ \ +void name::Shrink() \ +{ \ + /* only do it if we have some memory to free */ \ + if( m_nCount < m_nSize ) { \ + /* allocates exactly as much memory as we need */ \ + T *pNew = new T[m_nCount]; \ + /* only shrink if allocation succeeded */ \ + if ( pNew ) { \ + /* copy data to new location */ \ + memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \ + delete [] m_pItems; \ + m_pItems = pNew; \ + } \ + } \ +} \ + \ +/* searches the array for an item (forward or backwards) */ \ +int name::Index(T lItem, bool bFromEnd) const \ +{ \ + if ( bFromEnd ) { \ + if ( m_nCount > 0 ) { \ + size_t n = m_nCount; \ + do { \ + if ( m_pItems[--n] == lItem ) \ + return n; \ + } \ + while ( n != 0 ); \ + } \ + } \ + else { \ + for( size_t n = 0; n < m_nCount; n++ ) { \ + if( m_pItems[n] == lItem ) \ + return n; \ + } \ + } \ + \ + return wxNOT_FOUND; \ +} \ + \ +/* search for a place to insert item into sorted array (binary search) */ \ +size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \ +{ \ + size_t i, \ + lo = 0, \ + hi = m_nCount; \ + int res; \ + \ + while ( lo < hi ) { \ + i = (lo + hi)/2; \ + \ + res = (*fnCompare)((const void *)&lItem, (const void *)&(m_pItems[i])); \ + if ( res < 0 ) \ + hi = i; \ + else if ( res > 0 ) \ + lo = i + 1; \ + else { \ + lo = i; \ + break; \ + } \ + } \ + \ + return lo; \ +} \ + \ +/* search for an item in a sorted array (binary search) */ \ +int name::Index(T lItem, CMPFUNC fnCompare) const \ +{ \ + size_t n = IndexForInsert(lItem, fnCompare); \ + \ + return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; \ +} \ + \ +/* add item at the end */ \ +void name::Add(T lItem) \ +{ \ + Grow(); \ + m_pItems[m_nCount++] = lItem; \ +} \ + \ +/* add item assuming the array is sorted with fnCompare function */ \ +void name::Add(T lItem, CMPFUNC fnCompare) \ +{ \ + Insert(lItem, IndexForInsert(lItem, fnCompare)); \ +} \ + \ +/* add item at the given position */ \ +void name::Insert(T lItem, size_t nIndex) \ +{ \ + wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \ + \ + Grow(); \ + \ + memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex], \ + (m_nCount - nIndex)*sizeof(T)); \ + m_pItems[nIndex] = lItem; \ + m_nCount++; \ +} \ + \ +/* removes item from array (by index) */ \ +void name::RemoveAt(size_t nIndex) \ +{ \ + wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::RemoveAt") ); \ + \ + memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], \ + (m_nCount - nIndex - 1)*sizeof(T)); \ + m_nCount--; \ +} \ + \ +/* removes item from array (by value) */ \ +void name::Remove(T lItem) \ +{ \ + int iIndex = Index(lItem); \ + \ + wxCHECK_RET( iIndex != wxNOT_FOUND, \ + wxT("removing inexistent item in wxArray::Remove") ); \ + \ + RemoveAt((size_t)iIndex); \ +} \ + \ +/* sort array elements using passed comparaison function */ \ +void name::Sort(CMPFUNC fCmp) \ +{ \ + qsort(m_pItems, m_nCount, sizeof(T), fCmp); \ } -// copy ctor -wxBaseArray::wxBaseArray(const wxBaseArray& src) -{ - m_nSize = // not src.m_nSize to save memory - m_nCount = src.m_nCount; +_DECLARE_BASEARRAY(void *, wxBaseArrayPtrVoid) +_DECLARE_BASEARRAY(short, wxBaseArrayShort) +_DECLARE_BASEARRAY(int, wxBaseArrayInt) +_DECLARE_BASEARRAY(long, wxBaseArrayLong) +_DECLARE_BASEARRAY(double, wxBaseArrayDouble) - if ( m_nSize != 0 ) { - m_pItems = new long[m_nSize]; - memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(long)); - } - else - m_pItems = (long *) NULL; -} - -// assignment operator -wxBaseArray& wxBaseArray::operator=(const wxBaseArray& src) -{ - wxDELETEA(m_pItems); - - m_nSize = // not src.m_nSize to save memory - m_nCount = src.m_nCount; - - if ( m_nSize != 0 ) { - m_pItems = new long[m_nSize]; - memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(long)); - } - else - m_pItems = (long *) NULL; - - return *this; -} - -// grow the array -void wxBaseArray::Grow() -{ - // only do it if no more place - if( m_nCount == m_nSize ) { - if( m_nSize == 0 ) { - // was empty, alloc some memory - m_nSize = WX_ARRAY_DEFAULT_INITIAL_SIZE; - m_pItems = new long[m_nSize]; - } - else - { - // add 50% but not too much - size_t nIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE - ? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; - if ( nIncrement > ARRAY_MAXSIZE_INCREMENT ) - nIncrement = ARRAY_MAXSIZE_INCREMENT; - m_nSize += nIncrement; - long *pNew = new long[m_nSize]; - - // copy data to new location - memcpy(pNew, m_pItems, m_nCount*sizeof(long)); - delete [] m_pItems; - m_pItems = pNew; - } - } -} - -// dtor -wxBaseArray::~wxBaseArray() -{ - wxDELETEA(m_pItems); -} - -// clears the list -void wxBaseArray::Clear() -{ - m_nSize = - m_nCount = 0; - - wxDELETEA(m_pItems); -} - -// pre-allocates memory (frees the previous data!) -void wxBaseArray::Alloc(size_t nSize) -{ - // only if old buffer was not big enough - if ( nSize > m_nSize ) { - wxDELETEA(m_pItems); - m_pItems = new long[nSize]; - m_nSize = nSize; - } - - m_nCount = 0; -} - -// minimizes the memory usage by freeing unused memory -void wxBaseArray::Shrink() -{ - // only do it if we have some memory to free - if( m_nCount < m_nSize ) { - // allocates exactly as much memory as we need - long *pNew = new long[m_nCount]; - - // copy data to new location - memcpy(pNew, m_pItems, m_nCount*sizeof(long)); - delete [] m_pItems; - m_pItems = pNew; - } -} - -// searches the array for an item (forward or backwards) -int wxBaseArray::Index(long lItem, bool bFromEnd) const -{ - if ( bFromEnd ) { - if ( m_nCount > 0 ) { - size_t n = m_nCount; - do { - if ( m_pItems[--n] == lItem ) - return n; - } - while ( n != 0 ); - } - } - else { - for( size_t n = 0; n < m_nCount; n++ ) { - if( m_pItems[n] == lItem ) - return n; - } - } - - return wxNOT_FOUND; -} - -// search for a place to insert an item into a sorted array (binary search) -size_t wxBaseArray::IndexForInsert(long lItem, CMPFUNC fnCompare) const -{ - size_t i, - lo = 0, - hi = m_nCount; - int res; - - while ( lo < hi ) { - i = (lo + hi)/2; - - res = (*fnCompare)((const void *)lItem, (const void *)m_pItems[i]); - if ( res < 0 ) - hi = i; - else if ( res > 0 ) - lo = i + 1; - else { - lo = i; - break; - } - } - - return lo; -} - -// search for an item in a sorted array (binary search) -int wxBaseArray::Index(long lItem, CMPFUNC fnCompare) const -{ - size_t n = IndexForInsert(lItem, fnCompare); - - return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; -} - -// add item at the end -void wxBaseArray::Add(long lItem) -{ - Grow(); - m_pItems[m_nCount++] = lItem; -} - -// add item assuming the array is sorted with fnCompare function -void wxBaseArray::Add(long lItem, CMPFUNC fnCompare) -{ - Insert(lItem, IndexForInsert(lItem, fnCompare)); -} - -// add item at the given position -void wxBaseArray::Insert(long lItem, size_t nIndex) -{ - wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); - - Grow(); - - memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex], - (m_nCount - nIndex)*sizeof(long)); - m_pItems[nIndex] = lItem; - m_nCount++; -} - -// removes item from array (by index) -void wxBaseArray::RemoveAt(size_t nIndex) -{ - wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::RemoveAt") ); - - memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], - (m_nCount - nIndex - 1)*sizeof(long)); - m_nCount--; -} - -// removes item from array (by value) -void wxBaseArray::Remove(long lItem) -{ - int iIndex = Index(lItem); - - wxCHECK_RET( iIndex != wxNOT_FOUND, - wxT("removing inexistent item in wxArray::Remove") ); - - RemoveAt((size_t)iIndex); -} - -// sort array elements using passed comparaison function -void wxBaseArray::Sort(CMPFUNC fCmp) -{ - qsort(m_pItems, m_nCount, sizeof(long), fCmp); -} diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 850879e1ce..79c094ccf7 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -124,7 +124,7 @@ static const int IMAGE_MARGIN_IN_REPORT_MODE = 5; int CMPFUNC_CONV wxSizeTCmpFn(size_t n1, size_t n2) { return n1 - n2; } -WX_DEFINE_SORTED_EXPORTED_ARRAY(size_t, wxIndexArray); +WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(size_t, wxIndexArray); // this class is used to store the selected items in the virtual list control // (but it is not tied to list control and so can be used with other controls @@ -518,7 +518,7 @@ public: void OnKillFocus( wxFocusEvent &event ); private: - DECLARE_DYNAMIC_CLASS(wxListTextCtrl); + DECLARE_DYNAMIC_CLASS(wxListTextCtrl) DECLARE_EVENT_TABLE() }; @@ -859,7 +859,7 @@ private: // if this is > 0, the control is frozen and doesn't redraw itself size_t m_freezeCount; - DECLARE_DYNAMIC_CLASS(wxListMainWindow); + DECLARE_DYNAMIC_CLASS(wxListMainWindow) DECLARE_EVENT_TABLE() }; @@ -1751,7 +1751,7 @@ void wxListLineData::ReverseHighlight( void ) // wxListHeaderWindow //----------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow); +IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow) BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow) EVT_PAINT (wxListHeaderWindow::OnPaint) @@ -2116,7 +2116,7 @@ void wxListRenameTimer::Notify() // wxListTextCtrl (internal) //----------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl,wxTextCtrl); +IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl,wxTextCtrl) BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl) EVT_CHAR (wxListTextCtrl::OnChar) @@ -2226,7 +2226,7 @@ void wxListTextCtrl::OnKillFocus( wxFocusEvent &event ) // wxListMainWindow //----------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow); +IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow) BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow) EVT_PAINT (wxListMainWindow::OnPaint)