home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 October
/
Chip_1997-10_cd.bin
/
tema
/
sybase
/
powerj
/
hpp.z
/
WMVECTOR.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-12-23
|
10KB
|
357 lines
#ifndef _WMVECTOR_HPP_INCLUDED
#define _WMVECTOR_HPP_INCLUDED
#ifndef _WNO_PRAGMA_PUSH
#pragma pack(push,8);
#pragma enum int;
#endif
#ifndef _WVECTOR_HPP_INCLUDED
#include "wvector.hpp"
#endif
class WBaseSemaphore;
//-------------------------------------------------------------------
//
// WMTVectorBase
//
// The base class for our simple container class. It holds a list
// of pointers to WObject. This version is thread-safe (but slower).
//
// Note that for certain operations (like GetCount) you should first
// lock the list. You'll want to do this anyhow in case you need
// to manipulate the list.
//
//-------------------------------------------------------------------
class WCMCLASS WMTVectorBase : public WVectorBase {
public:
/**********************************************************
* Methods
**********************************************************/
// Lock
//
// Force a lock on the list. Note that you are responsible
// for unlocking it!.
WBool Lock() const;
// Unlock
//
// Unlock the list.
WBool Unlock() const;
/**********************************************************
* Overrides
**********************************************************/
virtual int Append( WObject * );
virtual void Clear( WBool force=FALSE );
virtual int Index( WObject * ) const;
virtual int Insert( int, WObject * );
virtual WObject *BaseRemove( WObject * );
virtual WObject *BaseRemoveAt( int index );
protected:
WMTVectorBase( WBool destroyObjects=FALSE, int growBy=10 );
~WMTVectorBase();
WBool operator==( const WVectorBase& ) const;
WBool operator==( const WMTVectorBase& ) const;
// WObject * operator[]( int index ) const;
WObject * AtIndex( int index ) const;
void Copy( const WVectorBase& );
void Copy( const WMTVectorBase& );
virtual WBool Equal( const WObject * l, const WObject * r ) const;
protected:
WBaseSemaphore *_mutex;
};
//-----------------------------------------------------------------
//
// WMTVector
//
// Templated class for holding pointers to WObject derived classes.
//
//-----------------------------------------------------------------
template<class T>
class WCMCLASS WMTVector : public WMTVectorBase {
public:
WMTVector( WBool destroyObjects=FALSE, int growBy=10 );
WMTVector( const WVector<T>& );
WMTVector( const WMTVector<T>& );
WMTVector<T>& operator=( const WVector<T>& );
WMTVector<T>& operator=( const WMTVector<T>& );
WBool operator==( const WVector<T>& ) const;
WBool operator==( const WMTVector<T>& ) const;
WBool operator!=( const WVector<T>& ) const;
WBool operator!=( const WMTVector<T>& ) const;
/**************************************************************
* Overrides
**************************************************************/
T * operator[]( int index ) const
{ return (T *)WMTVectorBase::AtIndex( index ); }
T * Remove( WObject * obj );
T * RemoveAt( int index );
protected:
virtual WBool Equal( const WObject * lhs, const WObject * rhs ) const;
};
template<class T>
WMTVector<T>::WMTVector( WBool destroyObjects, int growBy )
: WMTVectorBase( destroyObjects, growBy ) {
}
template<class T>
WMTVector<T>::WMTVector( const WVector<T>& rhs )
: WMTVectorBase( false ) {
Copy( (const WVectorBase&) rhs );
}
template<class T>
WMTVector<T>::WMTVector( const WMTVector<T>& rhs )
: WMTVectorBase( false ) {
Copy( (const WMTVectorBase&) rhs );
}
template<class T>
WMTVector<T>& WMTVector<T>::operator=( const WVector<T>& rhs ) {
Copy( (const WVectorBase&) rhs );
return *this;
}
template<class T>
WMTVector<T>& WMTVector<T>::operator=( const WMTVector<T>& rhs ) {
Copy( (const WMTVectorBase&) rhs );
return *this;
}
template<class T>
WBool WMTVector<T>::operator==( const WVector<T>& rhs ) const {
return WMTVectorBase::operator==( (const WVectorBase&) rhs );
}
template<class T>
WBool WMTVector<T>::operator==( const WMTVector<T>& rhs ) const {
return WMTVectorBase::operator==( (const WMTVectorBase&) rhs );
}
template<class T>
WBool WMTVector<T>::operator!=( const WVector<T>& rhs ) const {
return !operator==( rhs );
}
template<class T>
WBool WMTVector<T>::operator!=( const WMTVector<T>& rhs ) const {
return !operator==( rhs );
}
template<class T>
WBool WMTVector<T>::Equal( const WObject * lhs, const WObject * rhs ) const {
return (WBool)( *(T *)lhs == *(T *)rhs );
}
template<class T>
T * WMTVector<T>::Remove( WObject * obj ) {
return (T *)WMTVectorBase::BaseRemove( obj );
}
template<class T>
T * WMTVector<T>::RemoveAt( int index ) {
return (T *)WMTVectorBase::BaseRemoveAt( index );
}
//-----------------------------------------------------------------
//
// WMTSortableVector
//
// A sortable version of WMTVector.
//
//-----------------------------------------------------------------
template<class T>
class WCMCLASS WMTSortableVector : public WMTVector<T> {
public:
typedef WBool WCMDEF (*WMTVectorIterateRoutine )( const T *obj,
void *userData );
typedef int WCMDEF (*WMTVectorCompareRoutine)( const T *lhs,
const T *rhs );
typedef int WCMDEF (*WMTVectorBSearchRoutine)( const T *obj,
void *userData );
public:
WMTSortableVector( WBool destroyObjects=FALSE, int growBy=10 );
WMTSortableVector( const WVector<T>& );
WMTSortableVector( const WMTVector<T>& );
WMTSortableVector<T>& operator=( const WVector<T>& );
WMTSortableVector<T>& operator=( const WMTVector<T>& );
WBool operator==( const WVector<T>& ) const;
WBool operator==( const WMTVector<T>& ) const;
WBool operator!=( const WVector<T>& ) const;
WBool operator!=( const WMTVector<T>& ) const;
/**************************************************************
* Methods
**************************************************************/
// BSearch
//
// Does a binary search the list (must be sorted). Pass in
// a user-defined comparison function, which must return -1, 0
// or 1 (as with the bsearch function). No default behaviour
// is supplied.
T * BSearch( WMTVectorBSearchRoutine routine, void *userData );
// Iterate
//
// Iterate through the list, calling a user-defined function
// each time through. The user-defined function should return
// TRUE if the iteration should continue.
void Iterate( WMTVectorIterateRoutine routine, void *userData=NULL );
// Sort
//
// Sorts the vector. A user-supplied comparison routine can
// be used (should return -1, 0 or 1 as used with qsort) or
// if none is provided comparison is done using the
// CompareToSelf method of the objects.
void Sort( WMTVectorCompareRoutine routine=NULL );
protected:
static int DefaultCompare( const T *lhs, const T *rhs );
};
template<class T>
WMTSortableVector<T>::WMTSortableVector( WBool destroyObjects, int growBy )
: WMTVector( destroyObjects, growBy ) {
}
template<class T>
WMTSortableVector<T>::WMTSortableVector( const WVector<T>& rhs )
: WMTVector( rhs ) {
}
template<class T>
WMTSortableVector<T>::WMTSortableVector( const WMTVector<T>& rhs )
: WMTVector( rhs ) {
}
template<class T>
WMTSortableVector<T>& WMTSortableVector<T>::operator=( const WVector<T>& rhs ) {
Copy( (const WVectorBase&) rhs );
return *this;
}
template<class T>
WMTSortableVector<T>& WMTSortableVector<T>::operator=( const WMTVector<T>& rhs ) {
Copy( (const WMTVectorBase&) rhs );
return *this;
}
template<class T>
WBool WMTSortableVector<T>::operator==( const WVector<T>& rhs ) const {
return WMTVector<T>::operator==( rhs );
}
template<class T>
WBool WMTSortableVector<T>::operator==( const WMTVector<T>& rhs ) const {
return WMTVector<T>::operator==( rhs );
}
template<class T>
WBool WMTSortableVector<T>::operator!=( const WVector<T>& rhs ) const {
return !operator==( rhs );
}
template<class T>
WBool WMTSortableVector<T>::operator!=( const WMTVector<T>& rhs ) const {
return !operator==( rhs );
}
template<class T>
T * WMTSortableVector<T>::BSearch( WMTVectorBSearchRoutine routine,
void *userData ) {
if( !routine ) return NULL;
Lock();
T * o = (T *) DoBSearch( (WVectorBaseBSearchRoutine) routine,
userData );
Unlock();
return o;
}
template<class T>
void WMTSortableVector<T>::Iterate( WMTVectorIterateRoutine routine,
void *userData ) {
if( !routine ) return;
Lock();
DoIterate( (WVectorBaseIterateRoutine) routine, userData );
Unlock();
}
template<class T>
void WMTSortableVector<T>::Sort( WMTVectorCompareRoutine routine ) {
if( !routine ) routine = DefaultCompare;
Lock();
DoSort( (WVectorBaseCompareRoutine) routine );
Unlock();
}
template<class T>
static int WMTSortableVector<T>::DefaultCompare( const T *lhp, const T *rhp )
{
const T & lhs( *lhp );
const T & rhs( *rhp );
return lhs.CompareToSelf( rhs );
}
#ifndef _WNO_PRAGMA_PUSH
#pragma enum pop;
#pragma pack(pop);
#endif
#endif // _WMVECTOR_HPP_INCLUDED