home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / WILDASS / SOURCE / TASKS.CPP < prev    next >
C/C++ Source or Header  |  1993-08-12  |  7KB  |  376 lines

  1. // tasks.cpp
  2. //
  3. // implementation of the CTask 
  4. // and CTaskList classes
  5. //
  6. // update: 1.00 06-08-93 tw
  7.             
  8. #include <afxwin.h>
  9. #include <afxext.h> 
  10. #include <ctl3d.h>
  11.             
  12. #include "tasks.h"
  13.             
  14. #define new DEBUG_NEW 
  15.             
  16. // ***************** task class
  17.  
  18. CTask::CTask( TASKENTRY * pte )
  19. {
  20.    // set the size member to the appropriate size
  21.    // and copy the taskentry info
  22.    memcpy( &m_te, pte, sizeof( TASKENTRY ));
  23.    
  24.    // get the Name of the .exe file that
  25.    // is stored in the moduleentray for this
  26.    // task          
  27.    MODULEENTRY me;
  28.    me.dwSize = sizeof( MODULEENTRY );
  29.    VERIFY( ModuleFindHandle( &me, HModule() ));
  30.    m_strExePath = me.szExePath;
  31. }
  32.  
  33. HTASK
  34. CTask::HTask() const
  35. {     
  36.    // May return invalid task handles
  37. #ifdef _DEBUG   
  38.    if ( ! ::IsTask( m_te.hTask )) 
  39.    {
  40.       TRACE("Warning:CTask::HTask() returning invalid TASKENTRY\n"
  41.             "Dumping Task:\n");
  42.       Dump();
  43.    }
  44. #endif   
  45.    return m_te.hTask;
  46. }  
  47.             
  48. CTask::operator HTASK() const
  49. {
  50.    // May return invalid task handles
  51. #ifdef _DEBUG   
  52.    if ( ! ::IsTask( m_te.hTask ))
  53.    {
  54.       TRACE("Warning:CTask::operator HTASK() returning invalid TASKENTRY\n"
  55.             "Dumping Task:\n");
  56.       Dump();            
  57.    }      
  58. #endif   
  59.    return m_te.hTask;
  60. }    
  61.  
  62. HINSTANCE 
  63. CTask::HInstance() const
  64. {  
  65.    ASSERT_VALID( this );
  66.    return m_te.hInst;
  67. }  
  68.  
  69. CTask::operator HINSTANCE() const
  70. {
  71.    ASSERT_VALID( this );
  72.    return m_te.hInst;
  73. }
  74.  
  75. HMODULE 
  76. CTask::HModule() const
  77. {  
  78.    ASSERT_VALID( this );
  79.    return m_te.hModule;
  80. }  
  81.  
  82. // wont work as HINSTANCE==HMODULE   
  83. //CTask::operator HMODULE() const
  84. //{
  85. //   ASSERT_VALID( this );
  86. //   return m_te.hModule;
  87. //}   
  88.    
  89. const char*
  90. CTask::ExeName() const
  91. {
  92.    ASSERT_VALID( this );    
  93.    return m_strExePath;
  94. }
  95.  
  96. const char*
  97. CTask::ModuleName() const
  98. {
  99.    ASSERT_VALID( this );
  100.    return m_te.szModule;
  101. }     
  102.           
  103. // not so clean task termination
  104. void 
  105. CTask::DieHard() const
  106. {
  107.    ASSERT_VALID( this );
  108.    TerminateApp( HTask(), NO_UAE_BOX );
  109. }
  110.  
  111. // do a clean termination of this task
  112. void
  113. CTask::Die() const
  114. {
  115.    ASSERT_VALID( this );
  116.    VERIFY( EnumTaskWindows( HTask(), 
  117.                             EnumTaskWndProcCloseApp, 
  118.                             0 ));
  119. }
  120.    
  121. /* static */
  122. // this fxn is called for all non-owned top-level window
  123. // of this task
  124. BOOL CALLBACK __export 
  125. CTask::EnumTaskWndProcCloseApp( HWND hWnd, LPARAM /*lParam*/ )   
  126. {         
  127.    CWnd * pWnd = CWnd::FromHandle( hWnd );
  128.                     
  129.    if ( pWnd->IsWindowEnabled() )
  130.    {
  131.       pWnd->PostMessage( WM_CLOSE );
  132.    }
  133.    
  134.    return TRUE;
  135. }
  136.    
  137. /* static */
  138. BOOL
  139. CTask::IsTask( CTask * pTask ) 
  140. {
  141.    return ::IsTask( pTask->HTask() );
  142. }  
  143.  
  144. /* static */
  145. BOOL
  146. CTask::IsTask( HTASK task ) 
  147. {
  148.    return ::IsTask( task );
  149. }     
  150.  
  151. BOOL
  152. CTask::IsTask() const
  153. {
  154.    return ::IsTask( m_te.hTask );
  155. }
  156.  
  157. #ifdef _DEBUG
  158. void
  159. CTask::Dump() const
  160. {
  161.    afxDump << "[CTask] Module: " << ModuleName() << " .exe: " << ExeName() << "\n";
  162. }
  163.       
  164. void
  165. CTask::AssertValid() const
  166. {
  167.    ASSERT( ::IsTask( m_te.hTask ));
  168. }
  169.       
  170. #endif            
  171.  
  172.  
  173. HTASK 
  174. CTask::HParent() const
  175. {
  176.    ASSERT_VALID( this );
  177.    return m_te.hTaskParent;
  178. }
  179.  
  180. WORD  
  181. CTask::Events() const
  182. {
  183.    ASSERT_VALID( this );
  184.    return m_te.wcEvents;
  185. }
  186.  
  187. WORD  
  188. CTask::Usage() const
  189. {  
  190.    ASSERT_VALID( this );
  191.    MODULEENTRY me;
  192.    me.dwSize = sizeof( MODULEENTRY );
  193.    VERIFY( ModuleFindHandle( &me, HModule() ));
  194.    return me.wcUsage;   
  195. }
  196.  
  197.  
  198. // **********************[list of tasks]            
  199.             
  200. CTaskList::CTaskList() 
  201. {  
  202.    // contructing a tasklist means rebuilding the list          
  203.    Rebuild();
  204. }  
  205.  
  206. CTaskList::~CTaskList()
  207. {
  208.    // destucting the list involves clearing the list
  209.    Clear();
  210. }
  211.  
  212. void  
  213. CTaskList::Rebuild()
  214. {  
  215.    // cleat the list
  216.    Clear();
  217.  
  218.    // get a taskentry and initialize the size member
  219.    TASKENTRY te;
  220.    te.dwSize = sizeof( TASKENTRY );
  221.    
  222.    // get the first task from the task queue   
  223.    VERIFY( TaskFirst( &te ));
  224.    
  225.    // add it to the list
  226.    AddTail( new CTask( &te ));
  227.    
  228.    // get the other tasks from the task queue
  229.    // and add them to the list   
  230.    while ( TaskNext( &te ))
  231.    {
  232.       AddTail( new CTask( &te ));
  233.    }
  234.    
  235. #ifdef _DEBUG
  236.    Dump();
  237. #endif   
  238.  
  239. }
  240.  
  241. void 
  242. CTaskList::Clear()  
  243. {   
  244.    // release the list and all its members
  245.    for( POSITION pos = GetHeadPosition(); pos != NULL; )
  246.    {       
  247.       delete GetNext( pos );
  248.    } 
  249.    
  250.    RemoveAll();
  251. }
  252.  
  253. void 
  254. CTaskList::CloseDirty( CTask * pTask )
  255. {     
  256.    POSITION pos = Find( pTask );
  257.    
  258.    // must be in here... no one else
  259.    // allowed to remove tasks from us
  260.    ASSERT( pos );
  261.    
  262.    // make sure this task still has a valid taskhandle
  263.    // if not, only remove the CTask from list, but
  264.    // _dont_ kill the task
  265.    if ( pTask->IsTask() )
  266.    {                
  267. #ifdef _DEBUG   
  268.       TRACE("Dirty termination of Task - Dumping Task:\n");
  269.       pTask->Dump();
  270. #endif      
  271.       pTask->DieHard();
  272.    }
  273.    
  274.    // remove the CTask from the list
  275.    RemoveAt( pos );
  276.    
  277.    // free the associated memory
  278.    delete pTask;  
  279.  
  280. #ifdef _DEBUG   
  281.    Dump();   
  282. #endif   
  283.  
  284. }
  285.  
  286. void 
  287. CTaskList::CloseClean( CTask * pTask )
  288. {
  289.    POSITION pos = Find( pTask );
  290.    
  291.    // must be in here... no one else
  292.    // allowed to remove tasks from us
  293.    ASSERT( pos );
  294.  
  295.    // make sure this is still a valid task
  296.    if ( pTask->IsTask() )
  297.    {
  298. #ifdef _DEBUG   
  299.       TRACE("Clean termination of Task - Dumping Task:\n");
  300.       pTask->Dump();
  301. #endif      
  302.       pTask->Die();
  303.    } 
  304. #ifdef _DEBUG   
  305.    else 
  306.    {
  307.       TRACE("Warning: trying to close an invalid task in CTaskList::CloseClean\n");
  308.    }     
  309. #endif
  310.      
  311.    
  312.    // remove from list
  313.    RemoveAt( pos );
  314.    
  315.    // free the associated memory
  316.    delete pTask;
  317.  
  318. #ifdef _DEBUG   
  319.    Dump();   
  320. #endif   
  321.  
  322. }
  323.  
  324.  
  325. // avoid casts in usage  
  326. CTask*& 
  327. CTaskList::GetNext( POSITION& rPos )
  328. {
  329.    return (CTask*&) CObList::GetNext(rPos);
  330. }
  331.  
  332. CTask* 
  333. CTaskList::GetNext( POSITION& rPos ) const
  334. {
  335.    return (CTask*) CObList::GetNext(rPos);
  336. }
  337.  
  338. CTask*& 
  339. CTaskList::GetAt( POSITION pos )
  340. {
  341.    return (CTask*&) CObList::GetAt( pos );
  342. }
  343.  
  344. CTask* 
  345. CTaskList::GetAt( POSITION pos ) const
  346. {
  347.    return (CTask*) CObList::GetAt( pos );
  348. }
  349.  
  350. void
  351. CTaskList::Remove( CTask * pTask )
  352. {
  353.    POSITION pos = Find( pTask );
  354.    
  355.    // must be in here... no one else
  356.    // allowed to remove tasks from us
  357.    ASSERT( pos );
  358.  
  359.    RemoveAt( pos );
  360.    delete pTask;   
  361. }
  362.  
  363. #ifdef _DEBUG  
  364. void 
  365. CTaskList::Dump() const
  366. {     
  367.    afxDump << "Dumping a CTaskList with " << GetCount() << " items\n";
  368.    for( POSITION pos = GetHeadPosition(); pos != NULL; )
  369.    {       
  370.       CTask * pTask = GetNext( pos );
  371.       pTask->Dump(); 
  372.    }
  373. }
  374. #endif
  375.  
  376.