home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / Profiler / Container.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  24.4 KB  |  1,149 lines

  1. /****************************************************************************************
  2.  * Copyright (c) 1999 Microsoft Corporation.  All Rights Reserved.
  3.  *
  4.  * File:
  5.  *  table.hpp
  6.  *
  7.  * Description:
  8.  *    
  9.  *
  10.  *
  11.  ***************************************************************************************/
  12. #ifndef __CONTAINER_H__
  13. #define __CONTAINER_H__
  14.  
  15. #include "ProfilerBase.h"
  16.  
  17.  
  18. /***************************************************************************************
  19.  ********************                                               ********************
  20.  ********************                   SListNode                   ********************
  21.  ********************                                               ********************
  22.  ***************************************************************************************/
  23. template<class T, class K> class SList;
  24.  
  25.  
  26. template<class T, class K>
  27. class SListNode 
  28. {
  29.     friend class SList<T, K>;    
  30.             
  31.     public:
  32.  
  33.         SListNode( T entry, K Key ); 
  34.         virtual ~SListNode();
  35.  
  36.  
  37.     private:
  38.  
  39.         K m_key;
  40.         T m_pEntry;
  41.                 
  42.         SListNode<T, K> *m_pNext;
  43.             
  44. }; // SListNode
  45.  
  46.  
  47. /***************************************************************************************
  48.  *    Method:
  49.  *
  50.  *
  51.  *    Purpose:
  52.  *
  53.  *
  54.  *    Parameters: 
  55.  *
  56.  *
  57.  *    Return value:
  58.  *
  59.  *
  60.  *    Notes:
  61.  *
  62.  ***************************************************************************************/
  63.  /* public */
  64. template<class T, class K>
  65. SListNode<T, K>::SListNode( T entry, K key ) :
  66.     m_key( key ),
  67.     m_pNext( NULL ),
  68.     m_pEntry( entry )    
  69. {
  70.         
  71. } // ctor
  72.  
  73.  
  74. /***************************************************************************************
  75.  *    Method:
  76.  *
  77.  *
  78.  *    Purpose:
  79.  *
  80.  *
  81.  *    Parameters: 
  82.  *
  83.  *
  84.  *    Return value:
  85.  *
  86.  *
  87.  *    Notes:
  88.  *
  89.  ***************************************************************************************/
  90. /* public */
  91. template<class T, class K>
  92. SListNode<T, K>::~SListNode() 
  93. {
  94.     if ( m_pEntry != NULL )
  95.         delete m_pEntry;
  96.  
  97. } // dtor
  98.  
  99.  
  100. /***************************************************************************************
  101.  ********************                                               ********************
  102.  ********************               SList Declaration               ********************
  103.  ********************                                               ********************
  104.  ***************************************************************************************/
  105. template<class T, class K>
  106. class SList 
  107. {        
  108.     public:
  109.  
  110.         SList();
  111.         virtual ~SList(); 
  112.         
  113.         SList<T, K>( const SList<T, K> &source );
  114.         SList<T, K> &operator=( const SList<T, K> &source );
  115.  
  116.     
  117.     public:
  118.  
  119.         void Dump();                
  120.            
  121.            BOOL IsEmpty();     
  122.         T Lookup( K key );        
  123.         void DelEntry( K key );
  124.         void AddEntry( T entry, K key );            
  125.                        
  126.         //
  127.         // getters
  128.         //            
  129.         T Head();
  130.           T Tail();
  131.         T Entry();
  132.         ULONG Count(); 
  133.                         
  134.         //
  135.            // intrinsic iteration
  136.         //     
  137.         void Next();
  138.         void Reset();
  139.         BOOL AtEnd();
  140.                         
  141.                         
  142.     private:
  143.             
  144.           ULONG m_count;  
  145.         SListNode<T, K> *m_pHead;
  146.         SListNode<T, K> *m_pTail;
  147.         SListNode<T, K> *m_pCursor; 
  148.         CRITICAL_SECTION m_criticalSection;                   
  149.  
  150. }; // SList
  151.  
  152.  
  153. /***************************************************************************************
  154.  *    Method:
  155.  *
  156.  *
  157.  *    Purpose:
  158.  *
  159.  *
  160.  *    Parameters: 
  161.  *
  162.  *
  163.  *    Return value:
  164.  *
  165.  *
  166.  *    Notes:
  167.  *
  168.  ***************************************************************************************/
  169. /* public */
  170. template<class T, class K>
  171. SList<T, K>::SList() :
  172.     m_count( 0 ), 
  173.     m_pHead( NULL ),
  174.     m_pTail( NULL ),
  175.     m_pCursor( NULL )    
  176. {
  177.     InitializeCriticalSection( &m_criticalSection );
  178.     
  179. } // ctor
  180.  
  181.  
  182. /***************************************************************************************
  183.  *    Method:
  184.  *
  185.  *
  186.  *    Purpose:
  187.  *
  188.  *
  189.  *    Parameters: 
  190.  *
  191.  *
  192.  *    Return value:
  193.  *
  194.  *
  195.  *    Notes:
  196.  *
  197.  ***************************************************************************************/
  198. /* public */
  199. template<class T, class K>
  200. SList<T, K>::~SList()
  201. {
  202.        if ( m_pHead != NULL )
  203.     {      
  204.         ///////////////////////////////////////////////////////////////////////////
  205.         Synchronize guard( m_criticalSection );
  206.         ///////////////////////////////////////////////////////////////////////////  
  207.         SListNode<T, K> *cursor = m_pHead;
  208.     
  209.      
  210.           // delete all entries
  211.         m_count = 0;
  212.         for ( ; ((m_pHead = m_pHead->m_pNext) != NULL); cursor = m_pHead )
  213.             delete cursor;    
  214.    
  215.    
  216.            delete cursor;
  217.        }
  218.     
  219.        DeleteCriticalSection( &m_criticalSection );
  220.  
  221. } // dtor
  222.  
  223.  
  224. /***************************************************************************************
  225.  *    Method:
  226.  *
  227.  *
  228.  *    Purpose:
  229.  *
  230.  *
  231.  *    Parameters: 
  232.  *
  233.  *
  234.  *    Return value:
  235.  *
  236.  *
  237.  *    Notes:
  238.  *
  239.  ***************************************************************************************/
  240. /* public */
  241. template<class T, class K>
  242. SList<T, K> &SList<T, K>::operator=( const SList<T, K> &source )
  243. {
  244.     ///////////////////////////////////////////////////////////////////////////
  245.     Synchronize guard( m_criticalSection );
  246.        ///////////////////////////////////////////////////////////////////////////
  247.     
  248.     memcpy( this, &source, sizeof( SList<T, K> ) );
  249.     
  250.     
  251.     return *this;
  252.     
  253. } // assignment operator
  254.  
  255.  
  256. /***************************************************************************************
  257.  *    Method:
  258.  *
  259.  *
  260.  *    Purpose:
  261.  *
  262.  *
  263.  *    Parameters: 
  264.  *
  265.  *
  266.  *    Return value:
  267.  *
  268.  *
  269.  *    Notes:
  270.  *
  271.  ***************************************************************************************/
  272. /* public */
  273. template<class T, class K>
  274. SList<T, K>::SList( const SList<T, K> &source ) 
  275. {
  276.     ///////////////////////////////////////////////////////////////////////////
  277.     Synchronize guard( m_criticalSection );
  278.     ///////////////////////////////////////////////////////////////////////////
  279.     
  280.        m_pHead = source.m_pHead;
  281.     m_pTail = source.m_pTail;
  282.     
  283. } // copy ctor
  284.  
  285.  
  286. /***************************************************************************************
  287.  *    Method:
  288.  *
  289.  *
  290.  *    Purpose:
  291.  *
  292.  *
  293.  *    Parameters: 
  294.  *
  295.  *
  296.  *    Return value:
  297.  *
  298.  *
  299.  *    Notes:
  300.  *
  301.  ***************************************************************************************/
  302. /* public */
  303. template<class T, class K>
  304. void SList<T, K>::Dump() 
  305. {
  306.     ///////////////////////////////////////////////////////////////////////////
  307.     Synchronize guard( m_criticalSection );
  308.     ///////////////////////////////////////////////////////////////////////////
  309.  
  310.     SListNode<T, K> *cursor = m_pHead;
  311.     
  312.          
  313.       // dump all entries
  314.     for ( ; (cursor != NULL); cursor = cursor->m_pNext )
  315.            cursor->m_pEntry->Dump();
  316.       
  317.     
  318. } // SList<T, K>::Dump
  319.  
  320.  
  321. /***************************************************************************************
  322.  *    Method:
  323.  *
  324.  *
  325.  *    Purpose:
  326.  *
  327.  *
  328.  *    Parameters: 
  329.  *
  330.  *
  331.  *    Return value:
  332.  *
  333.  *
  334.  *    Notes:
  335.  *
  336.  ***************************************************************************************/
  337. /* public */
  338. template<class T, class K>
  339. ULONG SList<T, K>::Count()
  340. {
  341.     ///////////////////////////////////////////////////////////////////////////
  342.     Synchronize guard( m_criticalSection );
  343.     ///////////////////////////////////////////////////////////////////////////
  344.     
  345.     
  346.     return m_count;
  347.     
  348. } // SList<T, K>::Count
  349.  
  350.  
  351. /***************************************************************************************
  352.  *    Method:
  353.  *
  354.  *
  355.  *    Purpose:
  356.  *
  357.  *
  358.  *    Parameters: 
  359.  *
  360.  *
  361.  *    Return value:
  362.  *
  363.  *
  364.  *    Notes:
  365.  *
  366.  ***************************************************************************************/
  367. /* public */
  368. template<class T, class K>
  369. void SList<T, K>::AddEntry( T entry, K key )
  370. {
  371.     ///////////////////////////////////////////////////////////////////////////
  372.     Synchronize guard( m_criticalSection );
  373.     ///////////////////////////////////////////////////////////////////////////
  374.     
  375.     SListNode<T, K> *temp;
  376.     
  377.            
  378.       ++m_count;
  379.     temp = new SListNode<T, K>( entry, key );
  380.     
  381.     // since we are inserting at the head of
  382.     // the list, we only need to set the tail
  383.     // once (when adding the first entry).
  384.     if ( m_pHead == NULL )
  385.            m_pTail = temp;
  386.         
  387.     
  388.     // add new entry and adjust head    
  389.     temp->m_pNext = m_pHead;
  390.     m_pHead = temp;
  391.     
  392.  
  393. } // SList<T, K>::AddEntry
  394.  
  395.  
  396. /***************************************************************************************
  397.  *    Method:
  398.  *
  399.  *
  400.  *    Purpose:
  401.  *
  402.  *
  403.  *    Parameters: 
  404.  *
  405.  *
  406.  *    Return value:
  407.  *
  408.  *
  409.  *    Notes:
  410.  *
  411.  ***************************************************************************************/
  412. /* public */
  413. template<class T, class K>
  414. void SList<T, K>::DelEntry( K key )
  415. {
  416.     ///////////////////////////////////////////////////////////////////////////
  417.     Synchronize guard( m_criticalSection );
  418.     ///////////////////////////////////////////////////////////////////////////
  419.          
  420.     // Case 0: no entries---nothing to delete
  421.     if ( m_pHead != NULL )
  422.        {
  423.         SListNode<T, K> *cursor = m_pHead;
  424.         
  425.         
  426.         // Case 1: delete head entry
  427.            if ( m_pHead->m_pEntry->Compare( key ) == TRUE )
  428.         {                          
  429.             --m_count;                      
  430.             
  431.             // consider case where head == tail
  432.             if ( (m_pHead = m_pHead->m_pNext) == NULL )
  433.                 m_pCursor = m_pTail = m_pHead;
  434.                   
  435.                         
  436.             delete cursor;                    
  437.         }
  438.         // Case 2: delete inner entry
  439.         else
  440.         {
  441.             SListNode<T, K> *precursor = cursor;
  442.     
  443.     
  444.             // scan for match
  445.             for ( ; ((cursor = cursor->m_pNext) != NULL); precursor = cursor )
  446.             {
  447.                 if ( cursor->m_pEntry->Compare( key ) == TRUE )
  448.                 {
  449.                     --m_count;
  450.                     m_pCursor = precursor;
  451.                     precursor->m_pNext = cursor->m_pNext;
  452.                     
  453.                     // consider case where deleted entry is the tail 
  454.                     if ( m_pTail == cursor )
  455.                         m_pTail = precursor;
  456.                         
  457.                         
  458.                     delete cursor;                    
  459.                     break;
  460.                 }
  461.                 
  462.                } // for                                                                                
  463.          }
  464.        }   
  465.  
  466. } // SList<T, K>::DelEntry
  467.  
  468.  
  469. /***************************************************************************************
  470.  *    Method:
  471.  *
  472.  *
  473.  *    Purpose:
  474.  *
  475.  *
  476.  *    Parameters: 
  477.  *
  478.  *
  479.  *    Return value:
  480.  *
  481.  *
  482.  *    Notes:
  483.  *
  484.  ***************************************************************************************/
  485. /* public */
  486. template<class T, class K>
  487. T SList<T, K>::Lookup( K key )
  488. {
  489.     ///////////////////////////////////////////////////////////////////////////
  490.     Synchronize guard( m_criticalSection );
  491.     ///////////////////////////////////////////////////////////////////////////
  492.  
  493.     SListNode<T, K> *cursor = m_pHead;
  494.     
  495.     
  496.        // scan for match
  497.     for ( ; 
  498.           ((cursor != NULL) && (cursor->m_pEntry->Compare( key ) == FALSE)); 
  499.           cursor = cursor->m_pNext ) 
  500.          ; // continue
  501.    
  502.     
  503.     return ((cursor != NULL) ? cursor->m_pEntry : NULL);    
  504.  
  505. } // SList<T, K>::Lookup    
  506.     
  507.  
  508. /***************************************************************************************
  509.  *    Method:
  510.  *
  511.  *
  512.  *    Purpose:
  513.  *
  514.  *
  515.  *    Parameters: 
  516.  *
  517.  *
  518.  *    Return value:
  519.  *
  520.  *
  521.  *    Notes:
  522.  *
  523.  ***************************************************************************************/
  524. /* public */
  525. template<class T, class K>
  526. T SList<T, K>::Entry() 
  527. {
  528.     ///////////////////////////////////////////////////////////////////////////
  529.     Synchronize guard( m_criticalSection );
  530.     ///////////////////////////////////////////////////////////////////////////    
  531.  
  532.     
  533.     return ((m_pCursor != NULL) ? m_pCursor->m_pEntry : NULL);
  534.     
  535. } // SList<T, K>::Entry
  536.  
  537.  
  538. /***************************************************************************************
  539.  *    Method:
  540.  *
  541.  *
  542.  *    Purpose:
  543.  *
  544.  *
  545.  *    Parameters: 
  546.  *
  547.  *
  548.  *    Return value:
  549.  *
  550.  *
  551.  *    Notes:
  552.  *
  553.  ***************************************************************************************/
  554. /* public */
  555. template<class T, class K>
  556. T SList<T, K>::Head()
  557. {
  558.     ///////////////////////////////////////////////////////////////////////////
  559.     Synchronize guard( m_criticalSection );
  560.     ///////////////////////////////////////////////////////////////////////////
  561.           
  562.             
  563.     return ((m_pHead != NULL) ? m_pHead->m_pEntry : NULL);            
  564.     
  565. } // SList<T, K>::Head
  566.  
  567.  
  568. /***************************************************************************************
  569.  *    Method:
  570.  *
  571.  *
  572.  *    Purpose:
  573.  *
  574.  *
  575.  *    Parameters: 
  576.  *
  577.  *
  578.  *    Return value:
  579.  *
  580.  *
  581.  *    Notes:
  582.  *
  583.  ***************************************************************************************/
  584. /* public */
  585. template<class T, class K>
  586. T SList<T, K>::Tail() 
  587. {
  588.     ///////////////////////////////////////////////////////////////////////////
  589.     Synchronize guard( m_criticalSection );
  590.     ///////////////////////////////////////////////////////////////////////////
  591.             
  592.     
  593.     return ((m_pTail != NULL) ? m_pTail->m_pEntry : NULL);            
  594.                
  595. } // SList<T, K>::Tail
  596.  
  597.  
  598. /***************************************************************************************
  599.  *    Method:
  600.  *
  601.  *
  602.  *    Purpose:
  603.  *
  604.  *
  605.  *    Parameters: 
  606.  *
  607.  *
  608.  *    Return value:
  609.  *
  610.  *
  611.  *    Notes:
  612.  *
  613.  ***************************************************************************************/
  614. /* public */
  615. template<class T, class K>
  616. void SList<T, K>::Next()
  617. {
  618.     ///////////////////////////////////////////////////////////////////////////
  619.     Synchronize guard( m_criticalSection );
  620.     ///////////////////////////////////////////////////////////////////////////    
  621.     
  622.       if ( m_pCursor != NULL )
  623.         m_pCursor = m_pCursor->m_pNext; 
  624.  
  625.  
  626. } // SList<T, K>::Next()
  627.  
  628.  
  629. /***************************************************************************************
  630.  *    Method:
  631.  *
  632.  *
  633.  *    Purpose:
  634.  *
  635.  *
  636.  *    Parameters: 
  637.  *
  638.  *
  639.  *    Return value:
  640.  *
  641.  *
  642.  *    Notes:
  643.  *
  644.  ***************************************************************************************/
  645. /* public */
  646. template<class T, class K>
  647. void SList<T, K>::Reset() 
  648. {
  649.     ///////////////////////////////////////////////////////////////////////////
  650.     Synchronize guard( m_criticalSection );
  651.     ///////////////////////////////////////////////////////////////////////////
  652.   
  653.        m_pCursor = m_pHead;
  654.     
  655. } // SList<T, K>::Reset
  656.  
  657.  
  658. /***************************************************************************************
  659.  *    Method:
  660.  *
  661.  *
  662.  *    Purpose:
  663.  *
  664.  *
  665.  *    Parameters: 
  666.  *
  667.  *
  668.  *    Return value:
  669.  *
  670.  *
  671.  *    Notes:
  672.  *
  673.  ***************************************************************************************/
  674. /* public */
  675. template<class T, class K>
  676. BOOL SList<T, K>::AtEnd() 
  677. {
  678.     ///////////////////////////////////////////////////////////////////////////
  679.     Synchronize guard( m_criticalSection );
  680.     ///////////////////////////////////////////////////////////////////////////
  681.  
  682.  
  683.     return (BOOL)(m_pCursor == NULL);
  684.  
  685. } // SList<T, K>::AtEnd
  686.  
  687.  
  688. /***************************************************************************************
  689.  *    Method:
  690.  *
  691.  *
  692.  *    Purpose:
  693.  *
  694.  *
  695.  *    Parameters: 
  696.  *
  697.  *
  698.  *    Return value:
  699.  *
  700.  *
  701.  *    Notes:
  702.  *
  703.  ***************************************************************************************/
  704. /* public */
  705. template<class T, class K>
  706. BOOL SList<T, K>::IsEmpty() 
  707. {
  708.     ///////////////////////////////////////////////////////////////////////////
  709.     Synchronize guard( m_criticalSection );
  710.     ///////////////////////////////////////////////////////////////////////////
  711.  
  712.     
  713.     return (BOOL)(m_pHead == NULL);
  714.     
  715. } // SList<T, K>::IsEmpty
  716.  
  717.  
  718. /***************************************************************************************
  719.  ********************                                               ********************
  720.  ********************                   CNode                       ********************
  721.  ********************                                               ********************
  722.  ***************************************************************************************/
  723. template<class K> class CStack;
  724.  
  725.  
  726. template<class K>
  727. class CNode 
  728. {
  729.     friend class CStack<K>;    
  730.             
  731.     public:
  732.  
  733.         CNode( K item ); 
  734.         virtual ~CNode();
  735.  
  736.  
  737.     private:
  738.     
  739.         K m_pEntry;
  740.         CNode<K> *m_pNext;
  741.             
  742. }; // CNode
  743.  
  744.  
  745. /***************************************************************************************
  746.  *    Method:
  747.  *
  748.  *
  749.  *    Purpose:
  750.  *
  751.  *
  752.  *    Parameters: 
  753.  *
  754.  *
  755.  *    Return value:
  756.  *
  757.  *
  758.  *    Notes:
  759.  *
  760.  ***************************************************************************************/
  761.  /* public */
  762. template<class K>
  763. CNode<K>::CNode( K item ) :
  764.     m_pNext( NULL ),
  765.     m_pEntry( item )
  766. {
  767.         
  768. } // ctor
  769.  
  770.  
  771. /***************************************************************************************
  772.  *    Method:
  773.  *
  774.  *
  775.  *    Purpose:
  776.  *
  777.  *
  778.  *    Parameters: 
  779.  *
  780.  *
  781.  *    Return value:
  782.  *
  783.  *
  784.  *    Notes:
  785.  *
  786.  ***************************************************************************************/
  787. /* public */
  788. template<class K>
  789. CNode<K>::~CNode() 
  790. {
  791.  
  792.     if ( m_pEntry != NULL )
  793.         delete m_pEntry;
  794.                                               
  795.  
  796. } // dtor
  797.  
  798.  
  799. /***************************************************************************************
  800.  ********************                                               ********************
  801.  ********************                   CStack                        ********************
  802.  ********************                                               ********************
  803.  ***************************************************************************************/
  804. template< class K>
  805. class CStack
  806. {
  807.     public:
  808.     
  809.         CStack();
  810.         virtual ~CStack();
  811.  
  812.         CStack<K>( const CStack<K> &source );
  813.         CStack<K> &operator=( const CStack<K> &source );
  814.  
  815.  
  816.     public:
  817.  
  818.         void    Push( K item );
  819.         K        Pop();
  820.         BOOL    Empty();
  821.         void    Dump();
  822.         ULONG     Count();
  823.  
  824.  
  825.     private:
  826.     
  827.         CNode<K> *m_pTop;
  828.         int          m_Count;
  829.         
  830.         CRITICAL_SECTION m_criticalSection;
  831.  
  832. }; // CStack
  833.  
  834.  
  835. /***************************************************************************************
  836.  *    Method:
  837.  *
  838.  *
  839.  *    Purpose:
  840.  *
  841.  *
  842.  *    Parameters: 
  843.  *
  844.  *
  845.  *    Return value:
  846.  *
  847.  *
  848.  *    Notes:
  849.  *
  850.  ***************************************************************************************/
  851. /* public */
  852. template<class K>
  853. CStack<K>::CStack() :
  854.     m_Count( 0 ),
  855.     m_pTop( NULL )    
  856. {
  857.  
  858.     InitializeCriticalSection( &m_criticalSection );
  859.     
  860. } // ctor
  861.  
  862.  
  863. /***************************************************************************************
  864.  *    Method:
  865.  *
  866.  *
  867.  *    Purpose:
  868.  *
  869.  *
  870.  *    Parameters: 
  871.  *
  872.  *
  873.  *    Return value:
  874.  *
  875.  *
  876.  *    Notes:
  877.  *
  878.  ***************************************************************************************/
  879. /* public */
  880. template<class K>
  881. CStack<K>::~CStack()
  882. {
  883.     if ( m_Count != 0 )
  884.     {
  885.         ///////////////////////////////////////////////////////////////////////////
  886.         Synchronize guard( m_criticalSection );
  887.         ///////////////////////////////////////////////////////////////////////////  
  888.  
  889.         while ( m_Count != 0 )
  890.         {
  891.             CNode<K> *np = m_pTop;
  892.             CNode<K> *temp = np->m_pNext;
  893.  
  894.  
  895.             m_pTop  = temp;
  896.             m_Count--;
  897.  
  898.             delete np;
  899.         } 
  900.     }    
  901.     
  902.        DeleteCriticalSection( &m_criticalSection );
  903.  
  904. } // dtor
  905.  
  906.  
  907. /***************************************************************************************
  908.  *    Method:
  909.  *
  910.  *
  911.  *    Purpose:
  912.  *
  913.  *
  914.  *    Parameters: 
  915.  *
  916.  *
  917.  *    Return value:
  918.  *
  919.  *
  920.  *    Notes:
  921.  *
  922.  ***************************************************************************************/
  923. /* public */
  924. template<class K>
  925. CStack<K> &CStack<K>::operator=( const CStack<K> &source )
  926. {
  927.     ///////////////////////////////////////////////////////////////////////////
  928.     Synchronize guard( m_criticalSection );
  929.        ///////////////////////////////////////////////////////////////////////////
  930.     
  931.     memcpy( this, &source, sizeof( CStack<K> ) );
  932.     
  933.                                                 
  934.     return *this;
  935.     
  936. } // assignment operator
  937.  
  938.  
  939. /***************************************************************************************
  940.  *    Method:
  941.  *
  942.  *
  943.  *    Purpose:
  944.  *
  945.  *
  946.  *    Parameters: 
  947.  *
  948.  *
  949.  *    Return value:
  950.  *
  951.  *
  952.  *    Notes:
  953.  *
  954.  ***************************************************************************************/
  955. /* public */
  956. template<class K>
  957. CStack<K>::CStack( const CStack<K> &source ) 
  958. {
  959.     ///////////////////////////////////////////////////////////////////////////
  960.     Synchronize guard( m_criticalSection );
  961.     ///////////////////////////////////////////////////////////////////////////
  962.     
  963.     m_pTop = source.m_pTop;
  964.     m_Count = source.m_Count;
  965.     
  966. } // copy ctor
  967.  
  968.  
  969. /***************************************************************************************
  970.  *    Method:
  971.  *
  972.  *
  973.  *    Purpose:
  974.  *
  975.  *
  976.  *    Parameters: 
  977.  *
  978.  *
  979.  *    Return value:
  980.  *
  981.  *
  982.  *    Notes:
  983.  *
  984.  ***************************************************************************************/
  985. /* public */
  986. template<class K>
  987. ULONG CStack<K>::Count() 
  988. {
  989.     ///////////////////////////////////////////////////////////////////////////
  990.     Synchronize guard( m_criticalSection );
  991.     ///////////////////////////////////////////////////////////////////////////
  992.     
  993.     
  994.     return m_Count;
  995.     
  996. } // CStack<K>::Count
  997.  
  998.  
  999. /***************************************************************************************
  1000.  *    Method:
  1001.  *
  1002.  *
  1003.  *    Purpose:
  1004.  *
  1005.  *
  1006.  *    Parameters: 
  1007.  *
  1008.  *
  1009.  *    Return value:
  1010.  *
  1011.  *
  1012.  *    Notes:
  1013.  *
  1014.  ***************************************************************************************/
  1015. /* public */
  1016. template<class K>
  1017. void CStack<K>::Dump() 
  1018. {
  1019.     ///////////////////////////////////////////////////////////////////////////
  1020.     Synchronize guard( m_criticalSection );
  1021.     ///////////////////////////////////////////////////////////////////////////
  1022.     
  1023.     CNode<K> *cursor = m_pTop;
  1024.     
  1025.          
  1026.       // dump all entries
  1027.     for ( ; (cursor != NULL); cursor = cursor->m_pNext )
  1028.            cursor->m_pEntry->Dump();
  1029.       
  1030.  
  1031. } // CStack<K>::Dump
  1032.  
  1033.  
  1034. /***************************************************************************************
  1035.  *    Method:
  1036.  *
  1037.  *
  1038.  *    Purpose:
  1039.  *
  1040.  *
  1041.  *    Parameters: 
  1042.  *
  1043.  *
  1044.  *    Return value:
  1045.  *
  1046.  *
  1047.  *    Notes:
  1048.  *
  1049.  ***************************************************************************************/
  1050. /* public */
  1051. template<class K>
  1052. void CStack<K>::Push( K item )
  1053. {
  1054.     ///////////////////////////////////////////////////////////////////////////
  1055.     Synchronize guard( m_criticalSection );
  1056.     ///////////////////////////////////////////////////////////////////////////
  1057.  
  1058.     CNode<K> *np = new CNode<K>( item );
  1059.  
  1060.     if ( np != NULL )
  1061.     {
  1062.         m_Count++;
  1063.  
  1064.         np->m_pNext = m_pTop;
  1065.         m_pTop = np;
  1066.     }
  1067.  
  1068. } // CStack<K>::Push
  1069.  
  1070.  
  1071. /***************************************************************************************
  1072.  *    Method:
  1073.  *
  1074.  *
  1075.  *    Purpose:
  1076.  *
  1077.  *
  1078.  *    Parameters: 
  1079.  *
  1080.  *
  1081.  *    Return value:
  1082.  *
  1083.  *
  1084.  *    Notes:
  1085.  *
  1086.  ***************************************************************************************/
  1087. /* public */
  1088. template<class K>
  1089. K CStack<K>::Pop()
  1090. {        
  1091.     ///////////////////////////////////////////////////////////////////////////
  1092.     Synchronize guard( m_criticalSection );
  1093.     ///////////////////////////////////////////////////////////////////////////
  1094.  
  1095.     K item = NULL;
  1096.  
  1097.  
  1098.     if ( m_Count !=0 )
  1099.     {
  1100.         CNode<K> *np = m_pTop;
  1101.         
  1102.         
  1103.         item = np->m_pEntry->Clone();
  1104.         m_pTop = np->m_pNext;
  1105.  
  1106.         m_Count--;
  1107.         delete np;
  1108.     }
  1109.  
  1110.         
  1111.     return item;
  1112.     
  1113. } // CStack<K>::Pop
  1114.  
  1115.  
  1116. /***************************************************************************************
  1117.  *    Method:
  1118.  *
  1119.  *
  1120.  *    Purpose:
  1121.  *
  1122.  *
  1123.  *    Parameters: 
  1124.  *
  1125.  *
  1126.  *    Return value:
  1127.  *
  1128.  *
  1129.  *    Notes:
  1130.  *
  1131.  ***************************************************************************************/
  1132. /* public */
  1133. template<class K>
  1134. BOOL CStack<K>::Empty() 
  1135. {
  1136.     ///////////////////////////////////////////////////////////////////////////
  1137.     Synchronize guard( m_criticalSection );
  1138.     ///////////////////////////////////////////////////////////////////////////
  1139.     
  1140.     
  1141.     return (BOOL)(m_Count == NULL);
  1142.     
  1143. } // CStack<K>::Empty
  1144.  
  1145. #endif __CONTAINER_H__
  1146.  
  1147. // End of File
  1148.  
  1149.