home *** CD-ROM | disk | FTP | other *** search
- #ifndef _WVECTOR_HPP_INCLUDED
- #define _WVECTOR_HPP_INCLUDED
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma pack(push,8);
- #pragma enum int;
- #endif
-
- #ifndef _WOBJECT_HPP_INCLUDED
- #include "wobject.hpp"
- #endif
-
- //-----------------------------------------------------------------
- //
- // WVectorBase
- //
- // The base class for our simple container class. It holds a list
- // of pointers to WObject.
- //
- //-----------------------------------------------------------------
-
- class WCMCLASS WVectorBase {
-
- public:
-
- WVectorBase( WBool destroyObjects=FALSE, int growBy=10 );
- virtual ~WVectorBase();
-
- // WObject * operator[]( int index ) const;
-
- WObject *AtIndex( int index ) const;
-
- /**********************************************************
- * Properties
- **********************************************************/
-
- // Count
- //
- // Number of pointers stored.
-
- int GetCount() const { return _count; }
-
- // Size
- //
- // Actual real number of pointers that can be stored.
-
- int GetSize() const { return _size; }
-
- /**********************************************************
- * Methods
- **********************************************************/
-
- // Append
- //
- // Add an object to the end. Returns the index of the new item.
-
- virtual int Append( WObject * );
-
- // Clear
- //
- // Clears out the list of pointers. Can optionally call
- // delete for each pointer.
-
- virtual void Clear( WBool forceDelete=FALSE );
-
- // Grow
- //
- // Grows the list by a given amount.
-
- WBool Grow( int growBy=10 );
-
- // Index
- //
- // Returns the index of a given pointer.
-
- virtual int Index( WObject * ) const;
-
- // Insert
- //
- // Inserts an object into a given position.
-
- virtual int Insert( int at, WObject * );
-
- // Remove/RemoveAt
- //
- // Removes an object from the vector without deleting it.
- //
- // Note: In the base class we are calling these "BaseRemove"
- // and "BaseRemoveAt". This is because the WVector classes
- // needs to define Remove/RemoveAt functions that return
- // different types and this causes problems with forward
- // declarations.
-
- virtual WObject * BaseRemove( WObject * );
- virtual WObject * BaseRemoveAt( int index );
-
- /*********************************************************
- * Other (internal)
- ********************************************************/
-
- int Count() const { return GetCount(); }
- int Size() const { return GetSize(); }
- void ClearAndDestroy();
- int InsertAt( int at, WObject *o );
-
- protected:
-
- WBool operator==( const WVectorBase& rhs ) const;
-
- typedef WBool WCMDEF (*WVectorBaseIterateRoutine)( const WObject *,
- void * );
-
- typedef int WCMDEF (*WVectorBaseCompareRoutine)( const void *,
- const void * );
-
- typedef int WCMDEF (*WVectorBaseBSearchRoutine)( const WObject *obj,
- void *userData );
-
- virtual void DoSort( WVectorBaseCompareRoutine routine );
- virtual void DoIterate( WVectorBaseIterateRoutine routine,
- void *userData );
- virtual WObject *DoBSearch( WVectorBaseBSearchRoutine routine,
- void *userData );
-
- virtual WBool Equal( const WObject * l, const WObject * r ) const;
-
- void Copy( const WVectorBase& rhs );
-
- private:
-
- WVectorBase( const WVectorBase& ); // no implementation
- WVectorBase& operator=( const WVectorBase& ); // no implementation
-
- protected:
-
- WObject ** _objs;
- int _count;
- int _size;
- WBool _destroyObjects;
- int _growBy;
- };
-
- //-----------------------------------------------------------------
- //
- // WVector
- //
- // Templated class for holding pointers to WObject derived classes.
- //
- //-----------------------------------------------------------------
-
- template<class T>
- class WCMCLASS WVector : public WVectorBase {
-
- public:
-
- // default constructor
- //
- // Constructs an empty vector. If the parameter is TRUE,
- // the destructor will call 'delete' on the pointers stored
- // in the vector.
-
- WVector( WBool destroyObjects=FALSE, int growBy=10 );
-
- // copy constructor
- //
- // Makes a copy of the pointers stored in the source vector.
- // The destructor will not call 'delete' on the objects.
-
- WVector( const WVector<T>& );
-
- // assignment operator
- //
- // Makes a copy of the pointers stored in the source vector.
- // The old pointers stored in this vector are discarded but
- // WILL NOT be deleted.
-
- WVector<T>& operator=( const WVector<T>& );
-
- // operator==
- //
- // Compares the elements of the vectors using the method Equal.
- // Returns true if the vectors have same number of elements
- // and each corresponding pair of elements compares equal.
-
- WBool operator==( const WVector<T>& ) const;
-
- WBool operator!=( const WVector<T>& ) const;
-
- /**************************************************************
- * Overrides
- **************************************************************/
-
- T * operator[]( int index ) const;
-
- T * Remove( WObject * obj );
-
- T * RemoveAt( int index );
-
- protected:
- virtual WBool Equal( const WObject * lhs, const WObject * rhs ) const;
- };
-
- template<class T>
- WVector<T>::WVector( WBool destroyObjects, int growBy )
- : WVectorBase( destroyObjects, growBy ) {
- }
-
- template<class T>
- WVector<T>::WVector( const WVector<T>& rhs )
- : WVectorBase( false ) {
- Copy( rhs );
- }
-
- template<class T>
- WVector<T>& WVector<T>::operator=( const WVector<T>& rhs ) {
- Copy( rhs );
- return *this;
- }
-
- template<class T>
- WBool WVector<T>::operator==( const WVector<T>& rhs ) const {
- return WVectorBase::operator==( rhs );
- }
-
- template<class T>
- WBool WVector<T>::operator!=( const WVector<T>& rhs ) const {
- return !operator==( rhs );
- }
-
- template<class T>
- T * WVector<T>::operator[]( int index ) const {
- return static_cast<T *>(WVectorBase::AtIndex( index ));
- }
-
- template<class T>
- WBool WVector<T>::Equal( const WObject * lhs, const WObject * rhs ) const {
- return (WBool)( *(static_cast<const T *>(lhs)) == *(static_cast<const T *>(rhs)) );
- }
-
- template<class T>
- T * WVector<T>::Remove( WObject * obj ) {
- return static_cast<T *>(WVectorBase::BaseRemove( obj ));
- }
-
- template<class T>
- T * WVector<T>::RemoveAt( int index ) {
- return static_cast<T *>(WVectorBase::BaseRemoveAt( index ));
- }
-
- //-----------------------------------------------------------------
- //
- // WSortableVector
- //
- // A sortable version of WVector.
- //
- //-----------------------------------------------------------------
-
- template<class T>
- class WCMCLASS WSortableVector : public WVector<T> {
-
- public:
- typedef WBool WCMDEF (*WVectorIterateRoutine )( const T *obj,
- void *userData );
-
- typedef int WCMDEF (*WVectorCompareRoutine)( const T *lhs,
- const T *rhs );
-
- typedef int WCMDEF (*WVectorBSearchRoutine)( const T *obj,
- void *userData );
-
- public:
- WSortableVector( WBool destroyObjects=FALSE, int growBy=10 );
-
- WSortableVector( const WVector<T>& );
-
- WSortableVector<T>& operator=( const WVector<T>& );
-
- WBool operator==( const WVector<T>& ) const;
-
- WBool operator!=( const WVector<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( WVectorBSearchRoutine 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( WVectorIterateRoutine 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( WVectorCompareRoutine routine=NULL );
-
- protected:
- static int DefaultCompare( const T *lhs, const T *rhs );
- };
-
- template<class T>
- WSortableVector<T>::WSortableVector( WBool destroyObjects, int growBy )
- : WVector( destroyObjects, growBy ) {
- }
-
- template<class T>
- WSortableVector<T>::WSortableVector( const WVector<T>& rhs )
- : WVector( rhs ) {
- }
-
- template<class T>
- WSortableVector<T>& WSortableVector<T>::operator=( const WVector<T>& rhs ) {
- Copy( rhs );
- return *this;
- }
-
- template<class T>
- WBool WSortableVector<T>::operator==( const WVector<T>& rhs ) const {
- return WVector<T>::operator==( rhs );
- }
-
- template<class T>
- WBool WSortableVector<T>::operator!=( const WVector<T>& rhs ) const {
- return !operator==( rhs );
- }
-
- template<class T>
- T * WSortableVector<T>::BSearch( WVectorBSearchRoutine routine,
- void *userData ) {
- if( !routine ) return NULL;
- return static_cast<T *>( DoBSearch( (WVectorBaseBSearchRoutine) routine, userData ) );
- }
-
- template<class T>
- void WSortableVector<T>::Iterate( WVectorIterateRoutine routine,
- void *userData ) {
- if( !routine ) return;
- DoIterate( (WVectorBaseIterateRoutine) routine, userData );
- }
-
- template<class T>
- void WSortableVector<T>::Sort( WVectorCompareRoutine routine ) {
- if( !routine ) routine = DefaultCompare;
- DoSort( (WVectorBaseCompareRoutine) routine );
- }
-
- template<class T>
- static int WSortableVector<T>::DefaultCompare( const T *lhp, const T *rhp )
- {
- return (*lhp).CompareToSelf( *rhp );
- }
-
- //
- // Declare a basic vector
- //
-
- extern template WVector<WObject>;
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma enum pop;
- #pragma pack(pop);
- #endif
-
- #endif // _WVECTOR_HPP_INCLUDED
-
-