1998-05-20 10:01:55 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: dynarray.h
|
|
|
|
// Purpose: auto-resizable (i.e. dynamic) array support
|
|
|
|
// Author: Vadim Zeitlin
|
1998-07-16 13:29:46 -04:00
|
|
|
// Modified by:
|
1998-05-20 10:01:55 -04:00
|
|
|
// Created: 12.09.97
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _DYNARRAY_H
|
|
|
|
#define _DYNARRAY_H
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma interface "dynarray.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
|
1998-11-24 18:53:19 -05:00
|
|
|
/** @name Dynamic arrays and object arrays (array which own their elements)
|
1998-05-20 10:01:55 -04:00
|
|
|
@memo Arrays which grow on demand and do range checking (only in debug)
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
1998-11-24 18:53:19 -05:00
|
|
|
the initial size by which an array grows when an element is added
|
1998-07-16 13:29:46 -04:00
|
|
|
default value avoids allocate one or two bytes when the array is created
|
1998-05-20 10:01:55 -04:00
|
|
|
which is rather inefficient
|
|
|
|
*/
|
|
|
|
#define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// types
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
callback compare function for quick sort
|
1998-07-16 13:29:46 -04:00
|
|
|
must return negative value, 0 or positive value if pItem1 <, = or > pItem2
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
1999-10-01 14:27:51 -04:00
|
|
|
typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
|
1998-05-20 10:01:55 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
base class managing data having size of type 'long' (not used directly)
|
|
|
|
|
|
|
|
NB: for efficiency this often used class has no virtual functions (hence no
|
1998-07-16 13:29:46 -04:00
|
|
|
VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
|
|
|
|
create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
|
|
|
|
so it's not too important if it's not called (this happens when you cast
|
1998-05-20 10:01:55 -04:00
|
|
|
"SomeArray *" as "BaseArray *" and then delete it)
|
|
|
|
|
1998-11-24 18:53:19 -05:00
|
|
|
@memo Base class for template array classes
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-09-14 12:37:24 -04:00
|
|
|
class WXDLLEXPORT wxBaseArray
|
1998-05-20 10:01:55 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
/// EXCEPT for Gnu compiler to reduce warnings...
|
|
|
|
#ifdef __GNUG__
|
|
|
|
virtual
|
|
|
|
#endif
|
|
|
|
~wxBaseArray();
|
|
|
|
//@}
|
|
|
|
|
|
|
|
/** @name memory management */
|
|
|
|
//@{
|
1998-11-24 18:53:19 -05:00
|
|
|
/// empties the array, but doesn't release memory
|
1998-09-18 17:35:44 -04:00
|
|
|
void Empty() { m_nCount = 0; }
|
1998-11-24 18:53:19 -05:00
|
|
|
/// empties the array and releases memory
|
1998-05-20 10:01:55 -04:00
|
|
|
void Clear();
|
|
|
|
/// preallocates memory for given number of items
|
1998-08-18 11:36:12 -04:00
|
|
|
void Alloc(size_t uiSize);
|
1998-09-18 17:35:44 -04:00
|
|
|
/// minimizes the memory used by the array (frees unused memory)
|
|
|
|
void Shrink();
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/** @name simple accessors */
|
|
|
|
//@{
|
|
|
|
/// number of elements in the array
|
1998-09-18 17:35:44 -04:00
|
|
|
size_t Count() const { return m_nCount; }
|
|
|
|
size_t GetCount() const { return m_nCount; }
|
1998-05-20 10:01:55 -04:00
|
|
|
/// is it empty?
|
1998-09-18 17:35:44 -04:00
|
|
|
bool IsEmpty() const { return m_nCount == 0; }
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
|
|
|
protected:
|
1998-07-16 13:29:46 -04:00
|
|
|
// these methods are protected because if they were public one could
|
1998-11-24 18:53:19 -05:00
|
|
|
// mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
|
1998-05-20 10:01:55 -04:00
|
|
|
// type safe methods
|
|
|
|
|
|
|
|
/** @name items access */
|
|
|
|
//@{
|
|
|
|
/// get item at position uiIndex (range checking is done in debug version)
|
1998-08-18 11:36:12 -04:00
|
|
|
long& Item(size_t uiIndex) const
|
1998-09-18 17:35:44 -04:00
|
|
|
{ wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; }
|
1998-05-20 10:01:55 -04:00
|
|
|
/// same as Item()
|
1998-08-18 11:36:12 -04:00
|
|
|
long& operator[](size_t uiIndex) const { return Item(uiIndex); }
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/** @name item management */
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
Search the element in the array, starting from the either side
|
|
|
|
@param bFromEnd if TRUE, start from the end
|
1999-01-14 09:33:56 -05:00
|
|
|
@return index of the first item matched or wxNOT_FOUND
|
|
|
|
@see wxNOT_FOUND
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
1998-07-16 13:29:46 -04:00
|
|
|
int Index(long lItem, bool bFromEnd = FALSE) const;
|
|
|
|
/// search for an item using binary search in a sorted array
|
1998-07-17 04:18:57 -04:00
|
|
|
int Index(long lItem, CMPFUNC fnCompare) const;
|
1998-05-20 10:01:55 -04:00
|
|
|
/// add new element at the end
|
1998-07-16 13:29:46 -04:00
|
|
|
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])
|
1998-08-18 11:36:12 -04:00
|
|
|
void Insert(long lItem, size_t uiIndex);
|
1998-05-20 10:01:55 -04:00
|
|
|
/// remove first item matching this value
|
|
|
|
void Remove(long lItem);
|
|
|
|
/// remove item by index
|
1999-11-16 08:28:23 -05:00
|
|
|
void RemoveAt(size_t uiIndex);
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// sort array elements using given compare function
|
1998-07-16 13:29:46 -04:00
|
|
|
void Sort(CMPFUNC fnCompare);
|
1998-05-20 10:01:55 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Grow(); // makes array bigger if needed
|
|
|
|
|
1998-09-18 17:35:44 -04:00
|
|
|
size_t m_nSize, // current size of the array
|
|
|
|
m_nCount; // current number of elements
|
1998-05-20 10:01:55 -04:00
|
|
|
|
|
|
|
long *m_pItems; // pointer to data
|
|
|
|
};
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// template classes
|
|
|
|
// ============================================================================
|
|
|
|
|
1999-10-08 10:35:56 -04:00
|
|
|
// resolves the name conflict between the wxT() macor and T typedef: we can't
|
|
|
|
// use wxT() inside WX_DEFINE_ARRAY!
|
|
|
|
#define _WX_ERROR_SIZEOF wxT("illegal use of DEFINE_ARRAY")
|
|
|
|
#define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
|
1999-10-04 16:15:38 -04:00
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// This macro generates a new array class. It is intended for storage of simple
|
|
|
|
// types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
|
1998-07-16 13:29:46 -04:00
|
|
|
//
|
1998-05-20 10:01:55 -04:00
|
|
|
// NB: it has only inline functions => takes no space at all
|
1999-01-15 19:13:58 -05:00
|
|
|
// Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
|
|
|
|
// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
|
|
|
|
// so using a temporary variable instead.
|
1998-05-20 10:01:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-10-12 19:57:40 -04:00
|
|
|
#define _WX_DEFINE_ARRAY(T, name, classexp) \
|
1998-05-20 10:01:55 -04:00
|
|
|
typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
|
1999-10-12 19:57:40 -04:00
|
|
|
classexp name : public wxBaseArray \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ \
|
|
|
|
public: \
|
|
|
|
name() \
|
1999-08-05 16:38:51 -04:00
|
|
|
{ \
|
|
|
|
size_t type = sizeof(T); \
|
1999-08-02 00:44:01 -04:00
|
|
|
size_t sizelong = sizeof(long); \
|
1999-08-06 06:10:10 -04:00
|
|
|
if ( type > sizelong ) \
|
1999-10-04 16:15:38 -04:00
|
|
|
{ wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
|
1999-08-02 00:44:01 -04:00
|
|
|
} \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
|
|
|
name& operator=(const name& src) \
|
1999-01-15 19:13:58 -05:00
|
|
|
{ wxBaseArray* temp = (wxBaseArray*) this; \
|
|
|
|
(*temp) = ((const wxBaseArray&)src); \
|
1998-05-20 10:01:55 -04:00
|
|
|
return *this; } \
|
|
|
|
\
|
1998-09-18 17:35:44 -04:00
|
|
|
T& operator[](size_t uiIndex) const \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ return (T&)(wxBaseArray::Item(uiIndex)); } \
|
1998-09-18 17:35:44 -04:00
|
|
|
T& Item(size_t uiIndex) const \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ return (T&)(wxBaseArray::Item(uiIndex)); } \
|
1998-07-08 18:37:57 -04:00
|
|
|
T& Last() const \
|
|
|
|
{ return (T&)(wxBaseArray::Item(Count() - 1)); } \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
1998-06-05 10:46:17 -04:00
|
|
|
int Index(T Item, bool bFromEnd = FALSE) const \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ return wxBaseArray::Index((long)Item, bFromEnd); } \
|
|
|
|
\
|
|
|
|
void Add(T Item) \
|
|
|
|
{ wxBaseArray::Add((long)Item); } \
|
1998-09-18 17:35:44 -04:00
|
|
|
void Insert(T Item, size_t uiIndex) \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ wxBaseArray::Insert((long)Item, uiIndex) ; } \
|
|
|
|
\
|
1999-11-16 08:28:23 -05:00
|
|
|
void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
|
|
|
|
void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
|
1998-05-20 10:01:55 -04:00
|
|
|
void Remove(T Item) \
|
|
|
|
{ int iIndex = Index(Item); \
|
1999-04-12 18:20:19 -04:00
|
|
|
wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
|
1999-10-04 16:15:38 -04:00
|
|
|
_WX_ERROR_REMOVE); \
|
1999-11-18 09:54:02 -05:00
|
|
|
wxBaseArray::RemoveAt((size_t)iIndex); } \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
|
|
|
void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
|
|
|
|
}
|
|
|
|
|
1998-07-16 13:29:46 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// This is the same as the previous macro, but it defines a sorted array.
|
|
|
|
// Differences:
|
|
|
|
// 1) it must be given a COMPARE function in ctor which takes 2 items of type
|
|
|
|
// T* and should return -1, 0 or +1 if the first one is less/greater
|
|
|
|
// than/equal to the second one.
|
|
|
|
// 2) the Add() method inserts the item in such was that the array is always
|
|
|
|
// sorted (it uses the COMPARE function)
|
|
|
|
// 3) it has no Sort() method because it's always sorted
|
|
|
|
// 4) Index() method is much faster (the sorted arrays use binary search
|
|
|
|
// instead of linear one), but Add() is slower.
|
|
|
|
//
|
|
|
|
// Summary: use this class when the speed of Index() function is important, use
|
|
|
|
// the normal arrays otherwise.
|
|
|
|
//
|
|
|
|
// NB: it has only inline functions => takes no space at all
|
1999-01-15 19:13:58 -05:00
|
|
|
// Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
|
|
|
|
// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
|
|
|
|
// so using a temporary variable instead.
|
1998-07-16 13:29:46 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-10-12 19:57:40 -04:00
|
|
|
#define _WX_DEFINE_SORTED_ARRAY(T, name, classexp) \
|
1998-07-16 13:29:46 -04:00
|
|
|
typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
|
1999-10-12 19:57:40 -04:00
|
|
|
classexp name : public wxBaseArray \
|
1998-07-16 13:29:46 -04:00
|
|
|
{ \
|
|
|
|
public: \
|
|
|
|
name(SCMPFUNC##T fn) \
|
1999-08-02 00:44:01 -04:00
|
|
|
{ size_t type = sizeof(T); \
|
|
|
|
size_t sizelong = sizeof(long); \
|
1999-08-06 06:10:10 -04:00
|
|
|
if ( type > sizelong ) \
|
1999-10-04 16:15:38 -04:00
|
|
|
{ wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
|
1999-08-02 00:44:01 -04:00
|
|
|
m_fnCompare = fn; \
|
|
|
|
} \
|
1998-07-16 13:29:46 -04:00
|
|
|
\
|
|
|
|
name& operator=(const name& src) \
|
1999-01-15 19:13:58 -05:00
|
|
|
{ wxBaseArray* temp = (wxBaseArray*) this; \
|
|
|
|
(*temp) = ((const wxBaseArray&)src); \
|
1998-07-16 13:29:46 -04:00
|
|
|
m_fnCompare = src.m_fnCompare; \
|
|
|
|
return *this; } \
|
|
|
|
\
|
1998-09-18 17:35:44 -04:00
|
|
|
T& operator[](size_t uiIndex) const \
|
1998-07-16 13:29:46 -04:00
|
|
|
{ return (T&)(wxBaseArray::Item(uiIndex)); } \
|
1998-09-18 17:35:44 -04:00
|
|
|
T& Item(size_t uiIndex) const \
|
1998-07-16 13:29:46 -04:00
|
|
|
{ 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); }\
|
|
|
|
\
|
|
|
|
void Add(T Item) \
|
|
|
|
{ wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
|
|
|
|
\
|
1999-11-16 08:28:23 -05:00
|
|
|
void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
|
|
|
|
void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
|
1998-07-16 13:29:46 -04:00
|
|
|
void Remove(T Item) \
|
|
|
|
{ int iIndex = Index(Item); \
|
1999-04-12 18:20:19 -04:00
|
|
|
wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
|
1999-10-04 16:15:38 -04:00
|
|
|
_WX_ERROR_REMOVE ); \
|
1999-11-18 09:54:02 -05:00
|
|
|
wxBaseArray::RemoveAt((size_t)iIndex); } \
|
1998-07-16 13:29:46 -04:00
|
|
|
\
|
|
|
|
private: \
|
|
|
|
SCMPFUNC##T m_fnCompare; \
|
|
|
|
}
|
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-11-24 18:53:19 -05:00
|
|
|
// see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
|
1998-05-20 10:01:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-10-12 19:57:40 -04:00
|
|
|
#define _WX_DECLARE_OBJARRAY(T, name, classexp) \
|
1998-05-20 10:01:55 -04:00
|
|
|
typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
|
1999-10-12 19:57:40 -04:00
|
|
|
classexp name : public wxBaseArray \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ \
|
|
|
|
public: \
|
|
|
|
name() { } \
|
|
|
|
name(const name& src); \
|
|
|
|
name& operator=(const name& src); \
|
|
|
|
\
|
|
|
|
~name(); \
|
|
|
|
\
|
1998-09-18 17:35:44 -04:00
|
|
|
T& operator[](size_t uiIndex) const \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ return *(T*)wxBaseArray::Item(uiIndex); } \
|
1998-09-18 17:35:44 -04:00
|
|
|
T& Item(size_t uiIndex) const \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ return *(T*)wxBaseArray::Item(uiIndex); } \
|
1998-07-08 18:37:57 -04:00
|
|
|
T& Last() const \
|
|
|
|
{ return *(T*)(wxBaseArray::Item(Count() - 1)); } \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
1998-06-05 10:46:17 -04:00
|
|
|
int Index(const T& Item, bool bFromEnd = FALSE) const; \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
|
|
|
void Add(const T& Item); \
|
|
|
|
void Add(const T* pItem) \
|
|
|
|
{ wxBaseArray::Add((long)pItem); } \
|
|
|
|
\
|
1998-09-18 17:35:44 -04:00
|
|
|
void Insert(const T& Item, size_t uiIndex); \
|
|
|
|
void Insert(const T* pItem, size_t uiIndex) \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ wxBaseArray::Insert((long)pItem, uiIndex); } \
|
|
|
|
\
|
2000-07-15 15:51:35 -04:00
|
|
|
void Empty() { DoEmpty(); wxBaseArray::Empty(); } \
|
|
|
|
void Clear() { DoEmpty(); wxBaseArray::Clear(); } \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
1998-09-18 17:35:44 -04:00
|
|
|
T* Detach(size_t uiIndex) \
|
1998-05-20 10:01:55 -04:00
|
|
|
{ T* p = (T*)wxBaseArray::Item(uiIndex); \
|
1999-11-18 11:14:10 -05:00
|
|
|
wxBaseArray::RemoveAt(uiIndex); return p; } \
|
1999-11-16 08:28:23 -05:00
|
|
|
void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
|
|
|
|
void RemoveAt(size_t uiIndex); \
|
1998-05-20 10:01:55 -04:00
|
|
|
\
|
|
|
|
void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
|
|
|
|
\
|
|
|
|
private: \
|
2000-07-15 15:51:35 -04:00
|
|
|
void DoEmpty(); \
|
1998-05-20 10:01:55 -04:00
|
|
|
void DoCopy(const name& src); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-11-24 18:53:19 -05:00
|
|
|
/** @name Macros for definition of dynamic arrays and objarrays
|
1998-05-20 10:01:55 -04:00
|
|
|
|
|
|
|
These macros are ugly (especially if you look in the sources ;-), but they
|
|
|
|
allow us to define 'template' classes without actually using templates.
|
|
|
|
<BR>
|
|
|
|
<BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
Range checking is performed in debug build for both arrays and objarrays.
|
|
|
|
Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
|
|
|
|
they only grow, so loading 10 millions in an array only to delete them 2
|
|
|
|
lines below is <I>not</I> recommended. However, it does free memory when
|
|
|
|
it's destroyed, so if you destroy array also, it's ok.
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
This macro generates a new array class. It is intended for storage of simple
|
|
|
|
types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
|
|
|
|
<BR>
|
|
|
|
NB: it has only inline functions => takes no space at all
|
|
|
|
<BR>
|
|
|
|
|
|
|
|
@memo declare and define array class 'name' containing elements of type 'T'
|
|
|
|
*/
|
1999-10-12 19:57:40 -04:00
|
|
|
#define WX_DEFINE_ARRAY(T, name) \
|
|
|
|
typedef T _A##name; \
|
|
|
|
_WX_DEFINE_ARRAY(_A##name, name, class)
|
1998-07-16 13:29:46 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
This macro does the same as WX_DEFINE_ARRAY except that the array will be
|
|
|
|
sorted with the specified compare function.
|
|
|
|
*/
|
1999-10-12 19:57:40 -04:00
|
|
|
#define WX_DEFINE_SORTED_ARRAY(T, name) \
|
|
|
|
typedef T _A##name; \
|
|
|
|
_WX_DEFINE_SORTED_ARRAY(_A##name, name, class)
|
1998-07-16 13:29:46 -04:00
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
/**
|
1998-11-24 18:53:19 -05:00
|
|
|
This macro generates a new objarrays class which owns the objects it
|
|
|
|
contains, i.e. it will delete them when it is destroyed. An element is of
|
|
|
|
type T*, but arguments of type T& are taken (see below!) and T& is
|
|
|
|
returned. <BR>
|
1998-05-20 10:01:55 -04:00
|
|
|
Don't use this for simple types such as "int" or "long"!
|
|
|
|
You _may_ use it for "double" but it's awfully inefficient.
|
|
|
|
<BR>
|
|
|
|
<BR>
|
|
|
|
Note on Add/Insert functions:
|
|
|
|
<BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
1) function(T*) gives the object to the array, i.e. it will delete the
|
|
|
|
object when it's removed or in the array's dtor
|
1998-05-20 10:01:55 -04:00
|
|
|
<BR>
|
|
|
|
2) function(T&) will create a copy of the object and work with it
|
|
|
|
<BR>
|
|
|
|
<BR>
|
|
|
|
Also:
|
|
|
|
<BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
1) Remove() will delete the object after removing it from the array
|
1998-05-20 10:01:55 -04:00
|
|
|
<BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
2) Detach() just removes the object from the array (returning pointer to it)
|
1998-05-20 10:01:55 -04:00
|
|
|
<BR>
|
|
|
|
<BR>
|
|
|
|
NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
|
|
|
|
<BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
|
1998-05-20 10:01:55 -04:00
|
|
|
it will provoke memory leaks
|
|
|
|
<BR>
|
|
|
|
<BR>
|
1998-07-16 13:29:46 -04:00
|
|
|
some functions of this class are not inline, so it takes some space to
|
1998-05-20 10:01:55 -04:00
|
|
|
define new class from this template.
|
|
|
|
|
1998-11-24 18:53:19 -05:00
|
|
|
@memo declare objarray class 'name' containing elements of type 'T'
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
1999-10-12 19:57:40 -04:00
|
|
|
#define WX_DECLARE_OBJARRAY(T, name) \
|
|
|
|
typedef T _L##name; \
|
|
|
|
_WX_DECLARE_OBJARRAY(_L##name, name, class)
|
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
/**
|
1998-11-24 18:53:19 -05:00
|
|
|
To use an objarray class you must
|
1998-05-20 10:01:55 -04:00
|
|
|
<ll>
|
|
|
|
<li>#include "dynarray.h"
|
1998-11-24 18:53:19 -05:00
|
|
|
<li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
|
|
|
|
<li>#include "arrimpl.cpp"
|
|
|
|
<li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
|
1998-05-20 10:01:55 -04:00
|
|
|
</ll>
|
|
|
|
<BR><BR>
|
1998-11-24 18:53:19 -05:00
|
|
|
This is necessary because at the moment of DEFINE_OBJARRAY class
|
|
|
|
element_type must be fully defined (i.e. forward declaration is not
|
|
|
|
enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
|
|
|
|
two allows to break cicrcular dependencies with classes which have member
|
|
|
|
variables of objarray type.
|
1998-05-20 10:01:55 -04:00
|
|
|
|
1998-11-24 18:53:19 -05:00
|
|
|
@memo define (must include arrimpl.cpp!) objarray class 'name'
|
1998-05-20 10:01:55 -04:00
|
|
|
*/
|
1998-11-24 18:53:19 -05:00
|
|
|
#define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
1999-10-12 19:57:40 -04:00
|
|
|
// these macros do the same thing as the WX_XXX ones above, but should be used
|
|
|
|
// inside the library for user visible classes because otherwise they wouldn't
|
|
|
|
// be visible from outside (when using wxWindows as DLL under Windows)
|
|
|
|
#define WX_DEFINE_EXPORTED_ARRAY(T, name) \
|
|
|
|
typedef T _A##name; \
|
|
|
|
_WX_DEFINE_ARRAY(_A##name, name, class WXDLLEXPORT)
|
|
|
|
|
|
|
|
#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
|
|
|
|
typedef T _A##name; \
|
|
|
|
_WX_DEFINE_SORTED_ARRAY(_A##name, name, class WXDLLEXPORT)
|
|
|
|
|
|
|
|
#define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
|
|
|
|
typedef T _L##name; \
|
|
|
|
_WX_DECLARE_OBJARRAY(_L##name, name, class WXDLLEXPORT)
|
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** @name Some commonly used predefined arrays */
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//@{
|
|
|
|
/** @name ArrayInt */
|
1999-10-12 19:57:40 -04:00
|
|
|
WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt);
|
1998-05-20 10:01:55 -04:00
|
|
|
/** @name ArrayLong */
|
1999-10-12 19:57:40 -04:00
|
|
|
WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong);
|
1998-05-20 10:01:55 -04:00
|
|
|
/** @name ArrayPtrVoid */
|
1999-10-12 19:57:40 -04:00
|
|
|
WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid);
|
1998-05-20 10:01:55 -04:00
|
|
|
//@}
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
1998-12-29 11:54:48 -05:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// convinience macros
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2000-01-02 18:17:55 -05:00
|
|
|
// append all element of one array to another one
|
|
|
|
#define WX_APPEND_ARRAY(array, other) \
|
|
|
|
{ \
|
|
|
|
size_t count = other.Count(); \
|
|
|
|
for ( size_t n = 0; n < count; n++ ) \
|
|
|
|
{ \
|
|
|
|
array.Add(other[n]); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
1998-12-29 11:54:48 -05:00
|
|
|
// delete all array elements
|
|
|
|
//
|
|
|
|
// NB: the class declaration of the array elements must be visible from the
|
|
|
|
// place where you use this macro, otherwise the proper destructor may not
|
|
|
|
// be called (a decent compiler should give a warning about it, but don't
|
|
|
|
// count on it)!
|
|
|
|
#define WX_CLEAR_ARRAY(array) \
|
|
|
|
{ \
|
|
|
|
size_t count = array.Count(); \
|
|
|
|
for ( size_t n = 0; n < count; n++ ) \
|
|
|
|
{ \
|
|
|
|
delete array[n]; \
|
|
|
|
} \
|
1999-01-12 18:37:45 -05:00
|
|
|
\
|
|
|
|
array.Empty(); \
|
1998-12-29 11:54:48 -05:00
|
|
|
}
|
1999-10-12 19:57:40 -04:00
|
|
|
|
1998-05-20 10:01:55 -04:00
|
|
|
#endif // _DYNARRAY_H
|
|
|
|
|