home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-14 | 2.0 KB | 120 lines | [TEXT/CWIE] |
- // TaskLife.cp
-
- #ifndef TaskLife_h
- #include "TaskLife.h"
- #endif
- #ifndef Assert_h
- #include "Assert.h"
- #endif
- #ifndef Task_h
- #include "Task.h"
- #endif
-
- TaskLife::TaskLife()
- : task( 0 ),
- complete( true ),
- runDeferredCompletor( this, &TaskLife::RunCompletor ),
- runApplicationCompletor( this, &TaskLife::RunCompletor ),
- applicationLink( runApplicationCompletor )
- {
- Assert( !DeferredTaskTime::IsNow() );
- ApplicationTaskQueue::The();
- }
-
- TaskLife::~TaskLife()
- {
- Assert( !DeferredTaskTime::IsNow() );
- KillAndWait();
- Assert( Complete() );
- }
-
- void TaskLife::Launch( Task *task, Completor completor )
- {
- Assert( Complete() );
- Assert( this->task == 0 );
-
- this->completor = completor;
- complete = false;
- this->task = task;
-
- if ( task == 0 )
- QueueCompletor();
- else
- {
- Assert( task->life == 0 );
- task->life = this;
- task->Launch();
- }
- }
-
- void TaskLife::QueueCompletor()
- {
- Assert( !Complete() );
-
- if ( completor.WantsApplicationTime() )
- ApplicationTaskQueue::The().Put( applicationLink );
- else
- deferer.Defer( runDeferredCompletor );
- }
-
- void TaskLife::FinishTask()
- {
- Assert( !Complete() );
-
- Task *task = this->task;
- this->task = 0;
-
- if ( task != 0 )
- {
- Assert( task->life == 0 );
- task->AtCompletion();
- }
-
- complete = true;
- }
-
- void TaskLife::RunCompletor( DeferredTaskTime time )
- {
- Completor completor = this->completor;
- this->completor = Completor();
-
- FinishTask();
-
- if ( completor.WantsDeferredTaskTime() )
- completor( time );
- }
-
- void TaskLife::RunCompletor( ApplicationTime time )
- {
- Completor completor = this->completor;
- this->completor = Completor();
-
- FinishTask();
-
- if ( completor.WantsApplicationTime() )
- completor( time );
- }
-
- void TaskLife::Kill()
- {
- Task *task = this->task;
- if ( task != 0 )
- task->Kill();
- }
-
- void TaskLife::WaitForCompletion() const
- {
- Assert( !DeferredTaskTime::IsNow() );
-
- static ApplicationTaskQueue& queue( ApplicationTaskQueue::The() );
-
- while ( !Complete() )
- queue.ExecuteOne();
- }
-
- void TaskLife::KillAndWait()
- {
- Kill();
- WaitForCompletion();
- }
-