home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / hpp.z / WVECTOR.HPP < prev    next >
C/C++ Source or Header  |  1997-01-23  |  11KB  |  381 lines

  1. #ifndef _WVECTOR_HPP_INCLUDED
  2. #define _WVECTOR_HPP_INCLUDED
  3.  
  4. #ifndef _WNO_PRAGMA_PUSH
  5. #pragma pack(push,8);
  6. #pragma enum int;
  7. #endif
  8.  
  9. #ifndef _WOBJECT_HPP_INCLUDED
  10. #include "wobject.hpp"
  11. #endif
  12.  
  13. //-----------------------------------------------------------------
  14. //
  15. // WVectorBase
  16. //
  17. // The base class for our simple container class.  It holds a list
  18. // of pointers to WObject.
  19. //
  20. //-----------------------------------------------------------------
  21.  
  22. class WCMCLASS WVectorBase {
  23.  
  24.     public:
  25.  
  26.         WVectorBase( WBool destroyObjects=FALSE, int growBy=10 );
  27.         virtual ~WVectorBase();
  28.  
  29.         // WObject * operator[]( int index ) const;
  30.  
  31.         WObject *AtIndex( int index ) const;
  32.  
  33.         /**********************************************************
  34.          * Properties
  35.          **********************************************************/
  36.  
  37.         // Count
  38.         //
  39.         // Number of pointers stored.
  40.  
  41.         int GetCount() const { return _count; }
  42.  
  43.         // Size
  44.         //
  45.         // Actual real number of pointers that can be stored.
  46.  
  47.         int GetSize() const { return _size; }
  48.  
  49.         /**********************************************************
  50.          * Methods
  51.          **********************************************************/
  52.  
  53.         // Append
  54.         //
  55.         // Add an object to the end.  Returns the index of the new item.
  56.  
  57.         virtual int Append( WObject * );
  58.  
  59.         // Clear
  60.         //
  61.         // Clears out the list of pointers.  Can optionally call
  62.         // delete for each pointer.
  63.  
  64.         virtual void Clear( WBool forceDelete=FALSE );
  65.  
  66.         // Grow
  67.         //
  68.         // Grows the list by a given amount.
  69.  
  70.         WBool Grow( int growBy=10 );
  71.  
  72.         // Index
  73.         //
  74.         // Returns the index of a given pointer.
  75.  
  76.         virtual int Index( WObject * ) const;
  77.  
  78.         // Insert
  79.         //
  80.         // Inserts an object into a given position.
  81.  
  82.         virtual int Insert( int at, WObject * );
  83.  
  84.         // Remove/RemoveAt
  85.         //
  86.         // Removes an object from the vector without deleting it.
  87.         //
  88.         // Note: In the base class we are calling these "BaseRemove"
  89.         // and "BaseRemoveAt".  This is because the WVector classes
  90.         // needs to define Remove/RemoveAt functions that return
  91.         // different types and this causes problems with forward
  92.         // declarations.
  93.  
  94.         virtual WObject * BaseRemove( WObject * );
  95.         virtual WObject * BaseRemoveAt( int index );
  96.  
  97.         /*********************************************************
  98.          * Other (internal)
  99.          ********************************************************/
  100.  
  101.         int  Count() const { return GetCount(); }
  102.         int  Size() const { return GetSize(); }
  103.         void ClearAndDestroy();
  104.         int  InsertAt( int at, WObject *o );
  105.  
  106.     protected:
  107.  
  108.         WBool operator==( const WVectorBase& rhs ) const;
  109.  
  110.         typedef WBool WCMDEF (*WVectorBaseIterateRoutine)( const WObject *,
  111.                                                            void * );
  112.  
  113.         typedef int WCMDEF (*WVectorBaseCompareRoutine)( const void *,
  114.                                                          const void * );
  115.  
  116.         typedef int WCMDEF (*WVectorBaseBSearchRoutine)( const WObject *obj,
  117.                                                          void *userData );
  118.  
  119.         virtual void DoSort( WVectorBaseCompareRoutine routine );
  120.         virtual void DoIterate( WVectorBaseIterateRoutine routine,
  121.                                 void *userData );
  122.         virtual WObject *DoBSearch( WVectorBaseBSearchRoutine routine,
  123.                                 void *userData );
  124.  
  125.         virtual WBool Equal( const WObject * l, const WObject * r ) const;
  126.  
  127.         void Copy( const WVectorBase& rhs );
  128.  
  129.     private:
  130.  
  131.         WVectorBase( const WVectorBase& );             // no implementation
  132.         WVectorBase& operator=( const WVectorBase& );  // no implementation
  133.  
  134.     protected:
  135.  
  136.         WObject **              _objs;
  137.         int                     _count;
  138.         int                     _size;
  139.         WBool                   _destroyObjects;
  140.         int                     _growBy;
  141. };
  142.  
  143. //-----------------------------------------------------------------
  144. //
  145. // WVector
  146. //
  147. // Templated class for holding pointers to WObject derived classes.
  148. //
  149. //-----------------------------------------------------------------
  150.  
  151. template<class T>
  152. class WCMCLASS WVector : public WVectorBase {
  153.  
  154.     public:
  155.  
  156.         // default constructor
  157.         //
  158.         // Constructs an empty vector. If the parameter is TRUE,
  159.         // the destructor will call 'delete' on the pointers stored
  160.         // in the vector.
  161.  
  162.         WVector( WBool destroyObjects=FALSE, int growBy=10 );
  163.  
  164.         // copy constructor
  165.         //
  166.         // Makes a copy of the pointers stored in the source vector.
  167.         // The destructor will not call 'delete' on the objects.
  168.  
  169.         WVector( const WVector<T>& );
  170.  
  171.         // assignment operator
  172.         //
  173.         // Makes a copy of the pointers stored in the source vector.
  174.         // The old pointers stored in this vector are discarded but
  175.         // WILL NOT be deleted.
  176.  
  177.         WVector<T>& operator=( const WVector<T>& );
  178.  
  179.         // operator==
  180.         //
  181.         // Compares the elements of the vectors using the method Equal.
  182.         // Returns true if the vectors have same number of elements
  183.         // and each corresponding pair of elements compares equal.
  184.  
  185.         WBool operator==( const WVector<T>& ) const;
  186.  
  187.         WBool operator!=( const WVector<T>& ) const;
  188.  
  189.         /**************************************************************
  190.          * Overrides
  191.          **************************************************************/
  192.  
  193.         T * operator[]( int index ) const;
  194.  
  195.         T * Remove( WObject * obj );
  196.  
  197.         T * RemoveAt( int index );
  198.  
  199.     protected:
  200.         virtual WBool Equal( const WObject * lhs, const WObject * rhs ) const;
  201. };
  202.  
  203. template<class T>
  204. WVector<T>::WVector( WBool destroyObjects, int growBy )
  205.     : WVectorBase( destroyObjects, growBy ) {
  206. }
  207.  
  208. template<class T>
  209. WVector<T>::WVector( const WVector<T>& rhs )
  210.     : WVectorBase( false ) {
  211.     Copy( rhs );
  212. }
  213.  
  214. template<class T>
  215. WVector<T>& WVector<T>::operator=( const WVector<T>& rhs ) {
  216.     Copy( rhs );
  217.     return *this;
  218. }
  219.  
  220. template<class T>
  221. WBool WVector<T>::operator==( const WVector<T>& rhs ) const {
  222.     return WVectorBase::operator==( rhs );
  223. }
  224.  
  225. template<class T>
  226. WBool WVector<T>::operator!=( const WVector<T>& rhs ) const {
  227.     return !operator==( rhs );
  228. }
  229.  
  230. template<class T>
  231. T * WVector<T>::operator[]( int index ) const {
  232.     return static_cast<T *>(WVectorBase::AtIndex( index ));
  233. }
  234.  
  235. template<class T>
  236. WBool WVector<T>::Equal( const WObject * lhs, const WObject * rhs ) const {
  237.     return (WBool)( *(static_cast<const T *>(lhs)) == *(static_cast<const T *>(rhs)) );
  238. }
  239.  
  240. template<class T>
  241. T * WVector<T>::Remove( WObject * obj ) {
  242.     return static_cast<T *>(WVectorBase::BaseRemove( obj ));
  243. }
  244.  
  245. template<class T>
  246. T * WVector<T>::RemoveAt( int index ) {
  247.     return static_cast<T *>(WVectorBase::BaseRemoveAt( index ));
  248. }
  249.  
  250. //-----------------------------------------------------------------
  251. //
  252. // WSortableVector
  253. //
  254. // A sortable version of WVector.
  255. //
  256. //-----------------------------------------------------------------
  257.  
  258. template<class T>
  259. class WCMCLASS WSortableVector : public WVector<T> {
  260.  
  261.     public:
  262.         typedef WBool WCMDEF (*WVectorIterateRoutine )( const T *obj,
  263.                                                         void *userData );
  264.  
  265.         typedef int WCMDEF (*WVectorCompareRoutine)( const T *lhs,
  266.                                                      const T *rhs );
  267.  
  268.         typedef int WCMDEF (*WVectorBSearchRoutine)( const T *obj,
  269.                                                      void *userData );
  270.  
  271.     public:
  272.         WSortableVector( WBool destroyObjects=FALSE, int growBy=10 );
  273.  
  274.         WSortableVector( const WVector<T>& );
  275.  
  276.         WSortableVector<T>& operator=( const WVector<T>& );
  277.  
  278.         WBool operator==( const WVector<T>& ) const;
  279.  
  280.         WBool operator!=( const WVector<T>& ) const;
  281.  
  282.         /**************************************************************
  283.          * Methods
  284.          **************************************************************/
  285.  
  286.         // BSearch
  287.         //
  288.         // Does a binary search the list (must be sorted).  Pass in
  289.         // a user-defined comparison function, which must return -1, 0
  290.         // or 1 (as with the bsearch function). No default behaviour
  291.         // is supplied.
  292.  
  293.         T * BSearch( WVectorBSearchRoutine routine, void *userData );
  294.  
  295.         // Iterate
  296.         //
  297.         // Iterate through the list, calling a user-defined function
  298.         // each time through.  The user-defined function should return
  299.         // TRUE if the iteration should continue.
  300.  
  301.         void Iterate( WVectorIterateRoutine routine, void *userData=NULL );
  302.  
  303.         // Sort
  304.         //
  305.         // Sorts the vector.  A user-supplied comparison routine can
  306.         // be used (should return -1, 0 or 1 as used with qsort) or
  307.         // if none is provided comparison is done using the
  308.         // CompareToSelf method of the objects.
  309.  
  310.         void Sort( WVectorCompareRoutine routine=NULL );
  311.  
  312.     protected:
  313.         static int DefaultCompare( const T *lhs, const T *rhs );
  314. };
  315.  
  316. template<class T>
  317. WSortableVector<T>::WSortableVector( WBool destroyObjects, int growBy )
  318.     : WVector( destroyObjects, growBy ) {
  319. }
  320.  
  321. template<class T>
  322. WSortableVector<T>::WSortableVector( const WVector<T>& rhs )
  323.     : WVector( rhs ) {
  324. }
  325.  
  326. template<class T>
  327. WSortableVector<T>& WSortableVector<T>::operator=( const WVector<T>& rhs ) {
  328.     Copy( rhs );
  329.     return *this;
  330. }
  331.  
  332. template<class T>
  333. WBool WSortableVector<T>::operator==( const WVector<T>& rhs ) const {
  334.     return WVector<T>::operator==( rhs );
  335. }
  336.  
  337. template<class T>
  338. WBool WSortableVector<T>::operator!=( const WVector<T>& rhs ) const {
  339.     return !operator==( rhs );
  340. }
  341.  
  342. template<class T>
  343. T * WSortableVector<T>::BSearch( WVectorBSearchRoutine routine,
  344.                                  void *userData ) {
  345.     if( !routine ) return NULL;
  346.     return static_cast<T *>( DoBSearch( (WVectorBaseBSearchRoutine) routine, userData ) );
  347. }
  348.  
  349. template<class T>
  350. void WSortableVector<T>::Iterate( WVectorIterateRoutine routine,
  351.                                   void *userData ) {
  352.     if( !routine ) return;
  353.     DoIterate( (WVectorBaseIterateRoutine) routine, userData );
  354. }
  355.  
  356. template<class T>
  357. void WSortableVector<T>::Sort( WVectorCompareRoutine routine ) {
  358.     if( !routine ) routine = DefaultCompare;
  359.     DoSort( (WVectorBaseCompareRoutine) routine );
  360. }
  361.  
  362. template<class T>
  363. static int WSortableVector<T>::DefaultCompare( const T *lhp, const T *rhp )
  364. {
  365.     return (*lhp).CompareToSelf( *rhp );
  366. }
  367.  
  368. //
  369. // Declare a basic vector
  370. //
  371.  
  372. extern template WVector<WObject>;
  373.  
  374. #ifndef _WNO_PRAGMA_PUSH
  375. #pragma enum pop;
  376. #pragma pack(pop);
  377. #endif
  378.  
  379. #endif // _WVECTOR_HPP_INCLUDED
  380.  
  381.