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

  1.  
  2. #ifndef _WMVECTOR_HPP_INCLUDED
  3. #define _WMVECTOR_HPP_INCLUDED
  4.  
  5. #ifndef _WNO_PRAGMA_PUSH
  6. #pragma pack(push,8);
  7. #pragma enum int;
  8. #endif
  9.  
  10. #ifndef _WVECTOR_HPP_INCLUDED
  11. #include "wvector.hpp"
  12. #endif
  13.  
  14. class WBaseSemaphore;
  15.  
  16. //-------------------------------------------------------------------
  17. //
  18. // WMTVectorBase
  19. //
  20. // The base class for our simple container class.  It holds a list
  21. // of pointers to WObject.  This version is thread-safe (but slower).
  22. //
  23. // Note that for certain operations (like GetCount) you should first
  24. // lock the list.  You'll want to do this anyhow in case you need
  25. // to manipulate the list.
  26. //
  27. //-------------------------------------------------------------------
  28.  
  29. class WCMCLASS WMTVectorBase : public WVectorBase {
  30.  
  31.     public:
  32.         /**********************************************************
  33.          * Methods
  34.          **********************************************************/
  35.  
  36.         // Lock
  37.         //
  38.         // Force a lock on the list.  Note that you are responsible
  39.         // for unlocking it!.
  40.  
  41.         WBool Lock() const;
  42.  
  43.         // Unlock
  44.         //
  45.         // Unlock the list.
  46.  
  47.         WBool Unlock() const;
  48.  
  49.         /**********************************************************
  50.          * Overrides
  51.          **********************************************************/
  52.  
  53.         virtual int      Append( WObject * );
  54.         virtual void     Clear( WBool force=FALSE );
  55.         virtual int      Index( WObject * ) const;
  56.         virtual int      Insert( int, WObject * );
  57.         virtual WObject *BaseRemove( WObject * );
  58.         virtual WObject *BaseRemoveAt( int index );
  59.  
  60.     protected:
  61.         WMTVectorBase( WBool destroyObjects=FALSE, int growBy=10 );
  62.  
  63.         ~WMTVectorBase();
  64.  
  65.         WBool operator==( const WVectorBase& ) const;
  66.  
  67.         WBool operator==( const WMTVectorBase& ) const;
  68.  
  69. //      WObject * operator[]( int index ) const;
  70.         WObject * AtIndex( int index ) const;
  71.  
  72.         void Copy( const WVectorBase& );
  73.  
  74.         void Copy( const WMTVectorBase& );
  75.  
  76.         virtual WBool Equal( const WObject * l, const WObject * r ) const;
  77.  
  78.     protected:
  79.  
  80.         WBaseSemaphore *_mutex;
  81. };
  82.  
  83. //-----------------------------------------------------------------
  84. //
  85. // WMTVector
  86. //
  87. // Templated class for holding pointers to WObject derived classes.
  88. //
  89. //-----------------------------------------------------------------
  90.  
  91. template<class T>
  92. class WCMCLASS WMTVector : public WMTVectorBase {
  93.      
  94.     public: 
  95.  
  96.         WMTVector( WBool destroyObjects=FALSE, int growBy=10 );
  97.       
  98.         WMTVector( const WVector<T>& );
  99.         
  100.         WMTVector( const WMTVector<T>& );
  101.         
  102.         WMTVector<T>& operator=( const WVector<T>& );
  103.       
  104.         WMTVector<T>& operator=( const WMTVector<T>& );
  105.       
  106.         WBool operator==( const WVector<T>& ) const;
  107.  
  108.         WBool operator==( const WMTVector<T>& ) const;
  109.         
  110.         WBool operator!=( const WVector<T>& ) const;
  111.  
  112.         WBool operator!=( const WMTVector<T>& ) const;
  113.         
  114.         /**************************************************************
  115.          * Overrides
  116.          **************************************************************/
  117.  
  118.         T * operator[]( int index ) const
  119.                 { return (T *)WMTVectorBase::AtIndex( index ); }
  120.  
  121.         T * Remove( WObject * obj );
  122.  
  123.         T * RemoveAt( int index );
  124.  
  125.     protected:
  126.         virtual WBool Equal( const WObject * lhs, const WObject * rhs ) const;
  127. };
  128.  
  129. template<class T>
  130. WMTVector<T>::WMTVector( WBool destroyObjects, int growBy )
  131.     : WMTVectorBase( destroyObjects, growBy ) {
  132. }
  133.  
  134. template<class T>
  135. WMTVector<T>::WMTVector( const WVector<T>& rhs )
  136.     : WMTVectorBase( false ) {
  137.     Copy( (const WVectorBase&) rhs );
  138. }
  139.  
  140. template<class T>
  141. WMTVector<T>::WMTVector( const WMTVector<T>& rhs )
  142.     : WMTVectorBase( false ) {
  143.     Copy( (const WMTVectorBase&) rhs );
  144. }
  145.  
  146. template<class T>
  147. WMTVector<T>& WMTVector<T>::operator=( const WVector<T>& rhs ) {
  148.     Copy( (const WVectorBase&) rhs );
  149.     return *this;
  150. }
  151.  
  152. template<class T>
  153. WMTVector<T>& WMTVector<T>::operator=( const WMTVector<T>& rhs ) {
  154.     Copy( (const WMTVectorBase&) rhs );
  155.     return *this;
  156. }
  157.  
  158. template<class T>
  159. WBool WMTVector<T>::operator==( const WVector<T>& rhs ) const {
  160.     return WMTVectorBase::operator==( (const WVectorBase&) rhs );
  161. }
  162.  
  163. template<class T>
  164. WBool WMTVector<T>::operator==( const WMTVector<T>& rhs ) const {
  165.     return WMTVectorBase::operator==( (const WMTVectorBase&) rhs );
  166. }
  167.  
  168. template<class T>
  169. WBool WMTVector<T>::operator!=( const WVector<T>& rhs ) const {
  170.     return !operator==( rhs );
  171. }
  172.  
  173. template<class T>
  174. WBool WMTVector<T>::operator!=( const WMTVector<T>& rhs ) const {
  175.     return !operator==( rhs );
  176. }
  177.  
  178. template<class T>
  179. WBool WMTVector<T>::Equal( const WObject * lhs, const WObject * rhs ) const {
  180.         return (WBool)( *(T *)lhs == *(T *)rhs );
  181. }
  182.  
  183. template<class T>
  184. T * WMTVector<T>::Remove( WObject * obj ) {
  185.     return (T *)WMTVectorBase::BaseRemove( obj );
  186. }
  187.  
  188. template<class T>
  189. T * WMTVector<T>::RemoveAt( int index ) {
  190.     return (T *)WMTVectorBase::BaseRemoveAt( index );
  191. }
  192.  
  193. //-----------------------------------------------------------------
  194. //
  195. // WMTSortableVector
  196. //
  197. // A sortable version of WMTVector.
  198. //
  199. //-----------------------------------------------------------------
  200.  
  201. template<class T>
  202. class WCMCLASS WMTSortableVector : public WMTVector<T> {
  203.      
  204.     public:
  205.         typedef WBool WCMDEF (*WMTVectorIterateRoutine )( const T *obj,
  206.                                                           void *userData );
  207.  
  208.         typedef int WCMDEF (*WMTVectorCompareRoutine)( const T *lhs,
  209.                                                        const T *rhs );
  210.  
  211.         typedef int WCMDEF (*WMTVectorBSearchRoutine)( const T *obj,
  212.                                                        void *userData );
  213.  
  214.     public: 
  215.         WMTSortableVector( WBool destroyObjects=FALSE, int growBy=10 );
  216.       
  217.         WMTSortableVector( const WVector<T>& );
  218.         
  219.         WMTSortableVector( const WMTVector<T>& );
  220.         
  221.         WMTSortableVector<T>& operator=( const WVector<T>& );
  222.       
  223.         WMTSortableVector<T>& operator=( const WMTVector<T>& );
  224.       
  225.         WBool operator==( const WVector<T>& ) const;
  226.  
  227.         WBool operator==( const WMTVector<T>& ) const;
  228.         
  229.         WBool operator!=( const WVector<T>& ) const;
  230.  
  231.         WBool operator!=( const WMTVector<T>& ) const;
  232.         
  233.         /**************************************************************
  234.          * Methods
  235.          **************************************************************/
  236.  
  237.         // BSearch
  238.         //
  239.         // Does a binary search the list (must be sorted).  Pass in
  240.         // a user-defined comparison function, which must return -1, 0
  241.         // or 1 (as with the bsearch function). No default behaviour
  242.         // is supplied.
  243.  
  244.         T * BSearch( WMTVectorBSearchRoutine routine, void *userData );
  245.  
  246.         // Iterate
  247.         //
  248.         // Iterate through the list, calling a user-defined function
  249.         // each time through.  The user-defined function should return
  250.         // TRUE if the iteration should continue.
  251.  
  252.         void Iterate( WMTVectorIterateRoutine routine, void *userData=NULL );
  253.  
  254.         // Sort
  255.         //
  256.         // Sorts the vector.  A user-supplied comparison routine can
  257.         // be used (should return -1, 0 or 1 as used with qsort) or
  258.         // if none is provided comparison is done using the
  259.         // CompareToSelf method of the objects.
  260.  
  261.         void Sort( WMTVectorCompareRoutine routine=NULL );
  262.  
  263.     protected:
  264.         static int DefaultCompare( const T *lhs, const T *rhs );
  265. };
  266.  
  267. template<class T>
  268. WMTSortableVector<T>::WMTSortableVector( WBool destroyObjects, int growBy )
  269.     : WMTVector( destroyObjects, growBy ) {
  270. }
  271.  
  272. template<class T>
  273. WMTSortableVector<T>::WMTSortableVector( const WVector<T>& rhs )
  274.     : WMTVector( rhs ) {
  275. }
  276.  
  277. template<class T>
  278. WMTSortableVector<T>::WMTSortableVector( const WMTVector<T>& rhs )
  279.     : WMTVector( rhs ) {
  280. }
  281.  
  282. template<class T>
  283. WMTSortableVector<T>& WMTSortableVector<T>::operator=( const WVector<T>& rhs ) {
  284.     Copy( (const WVectorBase&) rhs );
  285.     return *this;
  286. }
  287.  
  288. template<class T>
  289. WMTSortableVector<T>& WMTSortableVector<T>::operator=( const WMTVector<T>& rhs ) {
  290.     Copy( (const WMTVectorBase&) rhs );
  291.     return *this;
  292. }
  293.  
  294. template<class T>
  295. WBool WMTSortableVector<T>::operator==( const WVector<T>& rhs ) const {
  296.     return WMTVector<T>::operator==( rhs );
  297. }
  298.  
  299. template<class T>
  300. WBool WMTSortableVector<T>::operator==( const WMTVector<T>& rhs ) const {
  301.     return WMTVector<T>::operator==( rhs );
  302. }
  303.  
  304. template<class T>
  305. WBool WMTSortableVector<T>::operator!=( const WVector<T>& rhs ) const {
  306.     return !operator==( rhs );
  307. }
  308.  
  309. template<class T>
  310. WBool WMTSortableVector<T>::operator!=( const WMTVector<T>& rhs ) const {
  311.     return !operator==( rhs );
  312. }
  313.  
  314. template<class T>
  315. T * WMTSortableVector<T>::BSearch( WMTVectorBSearchRoutine routine,
  316.                                    void *userData ) {
  317.     if( !routine ) return NULL;
  318.     Lock();
  319.     T * o = (T *) DoBSearch( (WVectorBaseBSearchRoutine) routine,
  320.                                userData );
  321.     Unlock();
  322.     return o;
  323. }
  324.  
  325. template<class T>
  326. void WMTSortableVector<T>::Iterate( WMTVectorIterateRoutine routine,
  327.                                     void *userData ) {
  328.     if( !routine ) return;
  329.     Lock();
  330.     DoIterate( (WVectorBaseIterateRoutine) routine, userData );
  331.     Unlock();
  332. }
  333.  
  334. template<class T>
  335. void WMTSortableVector<T>::Sort( WMTVectorCompareRoutine routine ) {
  336.     if( !routine ) routine = DefaultCompare;
  337.     Lock();
  338.     DoSort( (WVectorBaseCompareRoutine) routine );
  339.     Unlock();
  340. }
  341.  
  342. template<class T>
  343. static int WMTSortableVector<T>::DefaultCompare( const T *lhp, const T *rhp )
  344. {
  345.     const T & lhs( *lhp );
  346.     const T & rhs( *rhp );
  347.  
  348.     return lhs.CompareToSelf( rhs );
  349. }
  350.  
  351. #ifndef _WNO_PRAGMA_PUSH
  352. #pragma enum pop;
  353. #pragma pack(pop);
  354. #endif
  355.  
  356. #endif // _WMVECTOR_HPP_INCLUDED
  357.